github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/ui/tests/unit/services/token-test.js (about)

     1  import { getOwner } from '@ember/application';
     2  import Service from '@ember/service';
     3  import { moduleFor, test } from 'ember-qunit';
     4  import Pretender from 'pretender';
     5  
     6  moduleFor('service:token', 'Unit | Service | Token', {
     7    beforeEach() {
     8      const mockSystem = Service.extend({
     9        activeRegion: 'region-1',
    10        shouldIncludeRegion: true,
    11      });
    12  
    13      this.register('service:system', mockSystem);
    14      this.system = getOwner(this).lookup('service:system');
    15  
    16      this.server = new Pretender(function() {
    17        this.get('/path', () => [200, {}, null]);
    18      });
    19    },
    20    afterEach() {
    21      this.server.shutdown();
    22    },
    23    subject() {
    24      return getOwner(this)
    25        .factoryFor('service:token')
    26        .create();
    27    },
    28  });
    29  
    30  test('authorizedRequest includes the region param when the system service says to', function(assert) {
    31    const token = this.subject();
    32  
    33    token.authorizedRequest('/path');
    34    assert.equal(
    35      this.server.handledRequests.pop().url,
    36      `/path?region=${this.system.get('activeRegion')}`,
    37      'The region param is included when the system service shouldIncludeRegion property is true'
    38    );
    39  
    40    this.system.set('shouldIncludeRegion', false);
    41  
    42    token.authorizedRequest('/path');
    43    assert.equal(
    44      this.server.handledRequests.pop().url,
    45      '/path',
    46      'The region param is not included when the system service shouldIncludeRegion property is false'
    47    );
    48  });
    49  
    50  test('authorizedRawRequest bypasses adding the region param', function(assert) {
    51    const token = this.subject();
    52  
    53    token.authorizedRawRequest('/path');
    54    assert.equal(
    55      this.server.handledRequests.pop().url,
    56      '/path',
    57      'The region param is ommitted when making a raw request'
    58    );
    59  });