open-consul/ui-v2/tests/unit/adapters/kv-test.js
John Cowen 7903512b26
UI - Refactor Adapter.handleResponse (#4398)
* Add some tests to check the correct GET API endpoints are called

* Refactor adapters

1. Add integration tests for `urlFor...` and majority `handleResponse` methods
2. Refactor out `handleResponse` a little more into single/batch/boolean
methods
3. Move setting of the `Datacenter` property into the `handleResponse`
method, basically the same place that the uid is being set using the dc
parsed form the URL
4. Add some Errors for if you don't pass ids to certain `urlFor` methods
2018-07-30 17:55:44 +01:00

117 lines
3.9 KiB
JavaScript

import { module, skip } from 'qunit';
import { setupTest } from 'ember-qunit';
import test from 'ember-sinon-qunit/test-support/test';
import stubSuper from 'consul-ui/tests/helpers/stub-super';
module('Unit | Adapter | kv', function(hooks) {
setupTest(hooks);
skip('what should handleResponse return when createRecord is called with a `false` payload');
// Replace this with your real tests.
test('it exists', function(assert) {
const adapter = this.owner.lookup('adapter:kv');
assert.ok(adapter);
});
test('handleResponse with single type requests', function(assert) {
const adapter = this.owner.lookup('adapter:kv');
const expected = 'key/name';
const dc = 'dc1';
const url = `/v1/kv/${expected}?dc=${dc}`;
// handleResponse calls `urlForCreateRecord`, so stub that out
// so we are testing a single unit of code
const urlForCreateRecord = this.stub(adapter, 'urlForCreateRecord');
urlForCreateRecord.returns(url);
// handleResponse calls `this._super`, so stub that out also
// so we only test a single unit
// calling `it() will now create a 'sandbox' with a stubbed `_super`
const it = stubSuper(adapter, function(status, headers, response, requestData) {
return response;
});
// right now, the message here is more for documentation purposes
// and to replicate the `test`/`it` API
// it does not currently get printed to the QUnit test runner output
// the following tests use our stubbed `_super` sandbox
it('returns a KV uid pojo when createRecord is called with a `true` payload', function() {
const uid = {
uid: JSON.stringify([dc, expected]),
};
const headers = {};
const actual = adapter.handleResponse(200, headers, true, { url: url });
assert.deepEqual(actual, uid);
});
it("returns the original payload plus the uid if it's not a Boolean", function() {
const uid = {
Datacenter: dc,
uid: JSON.stringify([dc, expected]),
};
const actual = adapter.handleResponse(200, {}, [uid], { url: url });
assert.deepEqual(actual, uid);
});
});
skip('handleRequest for multiple type requests');
test('dataForRequest returns', function(assert) {
const adapter = this.owner.lookup('adapter:kv');
// dataForRequest goes through window.atob
adapter.decoder = {
execute: this.stub().returnsArg(0),
};
//
const expected = 'value';
const deep = {
kv: {
Value: expected,
},
};
const it = stubSuper(adapter, this.stub().returns(deep));
it('returns string KV value when calling update/create record', function() {
const requests = [
{
request: 'updateRecord',
expected: expected,
},
{
request: 'createRecord',
expected: expected,
},
];
requests.forEach(function(item, i, arr) {
const actual = adapter.dataForRequest({
requestType: item.request,
});
assert.equal(actual, expected);
});
});
// not included in the above forEach as it's a slightly different concept
it('returns string KV object when calling queryRecord (or anything else) record', function() {
const actual = adapter.dataForRequest({
requestType: 'queryRecord',
});
assert.equal(actual, null);
});
});
test('methodForRequest returns the correct method', function(assert) {
const adapter = this.owner.lookup('adapter:kv');
const requests = [
{
request: 'deleteRecord',
expected: 'DELETE',
},
{
request: 'createRecord',
expected: 'PUT',
},
{
request: 'anythingElse',
expected: 'GET',
},
];
requests.forEach(function(item) {
const actual = adapter.methodForRequest({ requestType: item.request });
assert.equal(actual, item.expected);
});
});
});