Files
threshold-ui/addons/api/tests/unit/adapters/application-test.js
T

206 lines
7.1 KiB
JavaScript
Raw Normal View History

2020-05-19 16:33:31 -04:00
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import RESTAdapter from '@ember-data/adapter/rest';
2020-06-03 17:58:24 -04:00
import { InvalidError } from '@ember-data/adapter/error';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
import { Response } from 'miragejs';
2020-05-19 16:33:31 -04:00
module('Unit | Adapter | application', function (hooks) {
setupTest(hooks);
setupMirage(hooks);
2020-05-19 16:33:31 -04:00
2021-11-05 12:33:09 -04:00
let config;
hooks.beforeEach(function () {
config = this.owner.resolveRegistration('config:environment');
});
2020-05-19 16:33:31 -04:00
test('its namespace is equal to the configured namespace', function (assert) {
assert.expect(2);
const adapter = this.owner.lookup('adapter:application');
assert.ok(adapter.namespace);
assert.equal(adapter.namespace, config.api.namespace);
});
test('it generates correct URL prefixes', function (assert) {
assert.expect(2);
2020-05-19 16:33:31 -04:00
const adapter = this.owner.lookup('adapter:application');
assert.ok(config.api.namespace);
2020-05-19 16:33:31 -04:00
assert.equal(adapter.urlPrefix(), config.api.namespace);
});
test('it generates correct default URL suffixes', function (assert) {
assert.expect(1);
const adapter = this.owner.lookup('adapter:application');
assert.equal(adapter.urlSuffix(), '');
});
test('it generates URL suffixes with optional `method` from adapterOptions', function (assert) {
assert.expect(2);
const method = 'set-something';
const adapter = this.owner.lookup('adapter:application');
const suffix = adapter.urlSuffix(null, null, {
2020-09-16 14:29:02 -04:00
adapterOptions: { method },
});
assert.ok(config.api.namespace);
assert.equal(suffix, ':set-something');
});
test('it generates correct complete URLs', function (assert) {
assert.expect(6);
const scopeID = 'o_1';
const method = 'my-custom-method';
const mockSnapshot = {
adapterOptions: {
scopeID,
2020-09-16 14:29:02 -04:00
method,
},
};
const adapter = this.owner.lookup('adapter:application');
// test URL generation for each request type...
2020-09-16 14:29:02 -04:00
const findRecordURL = adapter.buildURL(
'user',
'1',
mockSnapshot,
'findRecord'
);
2020-09-10 19:02:53 -04:00
assert.equal(findRecordURL, '/v1/users/1:my-custom-method');
2020-07-15 17:17:11 -04:00
const findAllURL = adapter.buildURL('user', null, mockSnapshot, 'findAll');
2020-09-10 19:02:53 -04:00
assert.equal(findAllURL, '/v1/users:my-custom-method');
2020-09-16 14:29:02 -04:00
const findBelongsToURL = adapter.buildURL(
'user',
'2',
mockSnapshot,
'findBelongsTo'
);
2020-09-10 19:02:53 -04:00
assert.equal(findBelongsToURL, '/v1/users/2:my-custom-method');
2020-09-16 14:29:02 -04:00
const createRecordURL = adapter.buildURL(
'user',
null,
mockSnapshot,
'createRecord'
);
2020-09-10 19:02:53 -04:00
assert.equal(createRecordURL, '/v1/users:my-custom-method');
2020-09-16 14:29:02 -04:00
const updateRecordURL = adapter.buildURL(
'user',
'3',
mockSnapshot,
'updateRecord'
);
2020-09-10 19:02:53 -04:00
assert.equal(updateRecordURL, '/v1/users/3:my-custom-method');
2020-09-16 14:29:02 -04:00
const deleteRecordURL = adapter.buildURL(
'user',
'4',
mockSnapshot,
'deleteRecord'
);
2020-09-10 19:02:53 -04:00
assert.equal(deleteRecordURL, '/v1/users/4:my-custom-method');
});
test('it can request records through the store from a specified scope', async function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
2020-09-16 14:29:02 -04:00
this.server.get('/v1/groups', (_, { queryParams: { scope_id } }) => {
2020-09-09 16:24:48 -04:00
assert.equal(scope_id, 'p_456', 'Scoped resource URL was requested.');
2020-09-16 14:29:02 -04:00
return { items: [] };
});
2020-09-09 16:24:48 -04:00
await store.query('group', { scope_id: 'p_456' });
});
test('it rewrites PUT to PATCH, but leaves others unchanged', function (assert) {
assert.expect(1);
const adapter = this.owner.lookup('adapter:application');
// TODO this is icky, should be changed to a spy or stub
const originalAjax = RESTAdapter.prototype.ajax;
RESTAdapter.prototype.ajax = (url, type) => {
assert.equal(type, 'PATCH');
RESTAdapter.prototype.ajax = originalAjax;
};
adapter.ajax('/', 'PUT');
});
2020-06-03 17:58:24 -04:00
test('it rewrites PUT/PATCH to POST when `adapterOptions.method` is passed to `updateRecord`', function (assert) {
assert.expect(1);
const adapter = this.owner.lookup('adapter:application');
const store = this.owner.lookup('service:store');
const snapshot = store.createRecord('role', {})._createSnapshot();
snapshot.adapterOptions = {
2020-09-16 14:29:02 -04:00
method: 'custom-method',
};
// TODO this is icky, should be changed to a spy or stub
const originalAjax = RESTAdapter.prototype.ajax;
RESTAdapter.prototype.ajax = (url, type) => {
assert.equal(type, 'POST');
RESTAdapter.prototype.ajax = originalAjax;
};
2020-09-16 14:29:02 -04:00
adapter.updateRecord(store, { modelName: 'user' }, snapshot);
});
test('it prenormalizes "empty" responses into a form that the fetch-manager will not reject', async function (assert) {
assert.expect(1);
2020-07-15 11:08:59 -04:00
const adapter = this.owner.lookup('adapter:application');
const store = this.owner.lookup('service:store');
2020-07-15 17:17:11 -04:00
this.server.get('/v1/users', () => new Response({}));
2020-09-16 14:29:02 -04:00
const prenormalized = await adapter.findAll(
store,
{ modelName: 'user' },
null,
[]
);
assert.deepEqual(prenormalized, { items: [] });
});
2020-06-03 17:58:24 -04:00
test('it correctly identifies 400 responses as invalid', function (assert) {
assert.expect(2);
const adapter = this.owner.lookup('adapter:application');
assert.ok(adapter.isInvalid(400));
assert.notOk(adapter.isInvalid(401));
});
test('it returns an proper InvalidError from handleResponse', function (assert) {
assert.expect(4);
2020-06-03 17:58:24 -04:00
const adapter = this.owner.lookup('adapter:application');
const payload = {
2020-06-03 17:58:24 -04:00
status: 400,
code: 'invalid_argument',
message: 'The request was invalid.',
};
const handledResponse = adapter.handleResponse(400, {}, payload);
2020-06-03 17:58:24 -04:00
assert.ok(handledResponse instanceof InvalidError);
assert.equal(handledResponse.errors.length, 1);
assert.equal(handledResponse.message, 'The request was invalid.');
assert.ok(handledResponse.errors[0].isInvalid);
});
test('it assigns convenience booleans for error types', function (assert) {
assert.expect(6);
const adapter = this.owner.lookup('adapter:application');
const getResponse = (status) =>
adapter.handleResponse(status, {}, { status });
assert.ok(getResponse(401).errors[0].isUnauthenticated);
assert.ok(getResponse(403).errors[0].isForbidden);
assert.ok(getResponse(404).errors[0].isNotFound);
assert.ok(getResponse(500).errors[0].isServer);
assert.ok(getResponse(0).errors[0].isUnknown);
assert.ok(getResponse(false).errors[0].isUnknown);
2020-06-03 17:58:24 -04:00
});
test('it returns field-level errors in InvalidError from handleResponse', function (assert) {
assert.expect(1);
const adapter = this.owner.lookup('adapter:application');
const payload = {
2020-06-03 17:58:24 -04:00
status: 400,
code: 'invalid_argument',
message: 'The request was invalid.',
details: {
2020-09-11 12:01:56 -04:00
request_fields: [{ name: 'name', description: 'Name is wrong.' }],
2020-06-03 17:58:24 -04:00
},
};
const handledResponse = adapter.handleResponse(400, {}, payload);
2020-06-03 17:58:24 -04:00
assert.equal(
handledResponse.errors.length,
2,
'A base error plus one field error'
);
});
2020-05-19 16:33:31 -04:00
});