test: 💍 add missing adapter test
This commit is contained in:
2 files changed
+57
-26
No files matched your search
@@ -1,28 +1,23 @@
|
||||
import ApplicationAdapter from './application';
|
||||
import { serializeIntoHash } from '@ember-data/adapter/-private';
|
||||
|
||||
export default class HostCatalogAdapter extends ApplicationAdapter {
|
||||
/**
|
||||
Called by the store when a newly created record is
|
||||
saved via the `save` method on a model record instance.
|
||||
|
||||
The `createRecord` method serializes the record and makes an Ajax (HTTP POST) request
|
||||
to a URL computed by `buildURL`.
|
||||
|
||||
See `serialize` for information on how to customize the serialized form
|
||||
of a record.
|
||||
|
||||
@method createRecord
|
||||
@param {Store} store
|
||||
@param {Model} type
|
||||
@param {Snapshot} snapshot
|
||||
@return {Promise} promise
|
||||
*/
|
||||
createRecord(store, type, snapshot) {
|
||||
let url = this.buildURL(type.modelName, null, snapshot, 'createRecord');
|
||||
const pluginName = snapshot.attr('plugin')?.name;
|
||||
if (pluginName) url = `${url}?plugin_name=${pluginName}`;
|
||||
const data = serializeIntoHash(store, type, snapshot);
|
||||
return this.ajax(url, 'POST', { data });
|
||||
* Overrides the default `buildURL` such that on _create_, URLs for records of
|
||||
* type `plugin` get an extra query parameter `?plugin_name`. All other
|
||||
* request types behave as normal.
|
||||
*
|
||||
* @override
|
||||
* @param {string} modelName
|
||||
* @param {string} id
|
||||
* @return {string} url
|
||||
* @return {string} requestType
|
||||
*/
|
||||
buildURL(modelName, id, snapshot, requestType) {
|
||||
let url = super.buildURL(...arguments);
|
||||
if (requestType === 'createRecord') {
|
||||
const pluginName = snapshot.attr('plugin')?.name;
|
||||
if (pluginName) url = `${url}?plugin_name=${pluginName}`;
|
||||
}
|
||||
return url;
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,45 @@ import { setupTest } from 'ember-qunit';
|
||||
module('Unit | Adapter | host catalog', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
// Replace this with your real tests.
|
||||
test('it exists', function (assert) {
|
||||
let adapter = this.owner.lookup('adapter:host-catalog');
|
||||
assert.ok(adapter);
|
||||
test('it generates correct createRecord URLs for static host catalogs', function (assert) {
|
||||
assert.expect(1);
|
||||
const scopeID = 'o_1';
|
||||
const mockSnapshot = {
|
||||
adapterOptions: {
|
||||
scopeID,
|
||||
},
|
||||
attr() {
|
||||
return null;
|
||||
},
|
||||
};
|
||||
const adapter = this.owner.lookup('adapter:host-catalog');
|
||||
const createRecordURL = adapter.buildURL(
|
||||
'host-catalog',
|
||||
null,
|
||||
mockSnapshot,
|
||||
'createRecord'
|
||||
);
|
||||
assert.equal(createRecordURL, '/v1/host-catalogs');
|
||||
});
|
||||
|
||||
test('it generates correct createRecord URLs for dynamic host catalogs with extra query parameter ?plugin_name', function (assert) {
|
||||
assert.expect(1);
|
||||
const scopeID = 'o_1';
|
||||
const mockSnapshot = {
|
||||
adapterOptions: {
|
||||
scopeID,
|
||||
},
|
||||
attr() {
|
||||
return { name: 'aws' };
|
||||
},
|
||||
};
|
||||
const adapter = this.owner.lookup('adapter:host-catalog');
|
||||
const createRecordURL = adapter.buildURL(
|
||||
'host-catalog',
|
||||
null,
|
||||
mockSnapshot,
|
||||
'createRecord'
|
||||
);
|
||||
assert.equal(createRecordURL, '/v1/host-catalogs?plugin_name=aws');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user