chore: add support for scope_id in URL prefixes and method in URL suffixes

This commit is contained in:
Randall Morey committed 2020-07-15 16:23:38 -04:00
1 parent 89b736744f
commit 9f48f8c85e
3 files changed
+195 -31

No files matched your search

@@ -18,9 +18,64 @@ module('Unit | Adapter | application', function (hooks) {
});
test('it generates correct URL prefixes', function (assert) {
assert.expect(2);
const adapter = this.owner.lookup('adapter:application');
assert.ok(config.api.namespace);
assert.equal(adapter.urlPrefix(), config.api.namespace);
});
test('it generates URL prefixes with optional `scope_id` from adapterOptions', function (assert) {
assert.expect(2);
const scope_id = 'global';
const adapter = this.owner.lookup('adapter:application');
const prefix = adapter.urlPrefix(null, null, null, null, {
adapterOptions: {scope_id}
});
assert.ok(config.api.namespace);
assert.equal(prefix, `v1/${scope_id}`);
});
test('it generates correct default URL suffixes', function (assert) {
assert.expect(1);
const adapter = this.owner.lookup('adapter:application');
assert.equal(adapter.urlPrefix(), config.api.namespace);
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, {
adapterOptions: {method}
});
assert.ok(config.api.namespace);
assert.equal(suffix, ':set-something');
});
test('it generates correct complete URLs', function (assert) {
assert.expect(6);
const scope_id = 'o_1';
const method = 'my-custom-method';
const mockSnapshot = {
adapterOptions: {
scope_id,
method
}
};
const adapter = this.owner.lookup('adapter:application');
// test URL generation for each request type...
const findRecordURL = adapter.buildURL('project', '1', mockSnapshot, 'findRecord');
assert.equal(findRecordURL, '/v1/o_1/projects/1:my-custom-method');
const findAllURL = adapter.buildURL('project', null, mockSnapshot, 'findAll');
assert.equal(findAllURL, '/v1/o_1/projects:my-custom-method');
const findBelongsToURL = adapter.buildURL('project', '2', mockSnapshot, 'findBelongsTo');
assert.equal(findBelongsToURL, '/v1/o_1/projects/2:my-custom-method');
const createRecordURL = adapter.buildURL('project', null, mockSnapshot, 'createRecord');
assert.equal(createRecordURL, '/v1/o_1/projects:my-custom-method');
const updateRecordURL = adapter.buildURL('project', '3', mockSnapshot, 'updateRecord');
assert.equal(updateRecordURL, '/v1/o_1/projects/3:my-custom-method');
const deleteRecordURL = adapter.buildURL('project', '4', mockSnapshot, 'deleteRecord');
assert.equal(deleteRecordURL, '/v1/o_1/projects/4:my-custom-method');
});
test('it rewrites PUT to PATCH, but leaves others unchanged', function (assert) {