github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/ui-v2/tests/unit/adapters/kv-test.js (about)

     1  import { module, skip } from 'qunit';
     2  import { setupTest } from 'ember-qunit';
     3  import test from 'ember-sinon-qunit/test-support/test';
     4  import stubSuper from 'consul-ui/tests/helpers/stub-super';
     5  
     6  module('Unit | Adapter | kv', function(hooks) {
     7    setupTest(hooks);
     8  
     9    skip('what should handleResponse return when createRecord is called with a `false` payload');
    10  
    11    // Replace this with your real tests.
    12    test('it exists', function(assert) {
    13      const adapter = this.owner.lookup('adapter:kv');
    14      assert.ok(adapter);
    15    });
    16    test('handleResponse with single type requests', function(assert) {
    17      const adapter = this.owner.lookup('adapter:kv');
    18      const expected = 'key/name';
    19      const dc = 'dc1';
    20      const url = `/v1/kv/${expected}?dc=${dc}`;
    21      // handleResponse calls `urlForCreateRecord`, so stub that out
    22      // so we are testing a single unit of code
    23      const urlForCreateRecord = this.stub(adapter, 'urlForCreateRecord');
    24      urlForCreateRecord.returns(url);
    25      // handleResponse calls `this._super`, so stub that out also
    26      // so we only test a single unit
    27      // calling `it() will now create a 'sandbox' with a stubbed `_super`
    28      const it = stubSuper(adapter, function(status, headers, response, requestData) {
    29        return response;
    30      });
    31  
    32      // right now, the message here is more for documentation purposes
    33      // and to replicate the `test`/`it` API
    34      // it does not currently get printed to the QUnit test runner output
    35  
    36      // the following tests use our stubbed `_super` sandbox
    37      it('returns a KV uid pojo when createRecord is called with a `true` payload', function() {
    38        const uid = {
    39          uid: JSON.stringify([dc, expected]),
    40        };
    41        const headers = {};
    42        const actual = adapter.handleResponse(200, headers, true, { url: url });
    43        assert.deepEqual(actual, uid);
    44      });
    45      it("returns the original payload plus the uid if it's not a Boolean", function() {
    46        const uid = {
    47          Datacenter: dc,
    48          uid: JSON.stringify([dc, expected]),
    49        };
    50        const actual = adapter.handleResponse(200, {}, [uid], { url: url });
    51        assert.deepEqual(actual, uid);
    52      });
    53    });
    54    skip('handleRequest for multiple type requests');
    55    test('dataForRequest returns', function(assert) {
    56      const adapter = this.owner.lookup('adapter:kv');
    57      // dataForRequest goes through window.atob
    58      adapter.decoder = {
    59        execute: this.stub().returnsArg(0),
    60      };
    61      //
    62      const expected = 'value';
    63      const deep = {
    64        kv: {
    65          Value: expected,
    66        },
    67      };
    68      const it = stubSuper(adapter, this.stub().returns(deep));
    69      it('returns string KV value when calling update/create record', function() {
    70        const requests = [
    71          {
    72            request: 'updateRecord',
    73            expected: expected,
    74          },
    75          {
    76            request: 'createRecord',
    77            expected: expected,
    78          },
    79        ];
    80        requests.forEach(function(item, i, arr) {
    81          const actual = adapter.dataForRequest({
    82            requestType: item.request,
    83          });
    84          assert.equal(actual, expected);
    85        });
    86      });
    87      // not included in the above forEach as it's a slightly different concept
    88      it('returns string KV object when calling queryRecord (or anything else) record', function() {
    89        const actual = adapter.dataForRequest({
    90          requestType: 'queryRecord',
    91        });
    92        assert.equal(actual, null);
    93      });
    94    });
    95    test('methodForRequest returns the correct method', function(assert) {
    96      const adapter = this.owner.lookup('adapter:kv');
    97      const requests = [
    98        {
    99          request: 'deleteRecord',
   100          expected: 'DELETE',
   101        },
   102        {
   103          request: 'createRecord',
   104          expected: 'PUT',
   105        },
   106        {
   107          request: 'anythingElse',
   108          expected: 'GET',
   109        },
   110      ];
   111      requests.forEach(function(item) {
   112        const actual = adapter.methodForRequest({ requestType: item.request });
   113        assert.equal(actual, item.expected);
   114      });
   115    });
   116  });