Files
threshold-ui/addons/api/tests/unit/models/base-test.js
T

167 lines
5.3 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 setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
2020-05-19 16:33:31 -04:00
module('Unit | Model | base', function (hooks) {
setupTest(hooks);
setupMirage(hooks);
2020-05-19 16:33:31 -04:00
// Replace this with your real tests.
test('it exists', function (assert) {
let store = this.owner.lookup('service:store');
let model = store.createRecord('base', {});
assert.ok(model);
});
2021-12-15 09:43:20 -05:00
test('it may have a scope fragment (and thus a scopeModel)', function (assert) {
assert.expect(2);
const store = this.owner.lookup('service:store');
store.push({
data: {
id: 'o_1',
2020-09-16 14:29:02 -04:00
type: 'scope',
},
});
store.push({
data: {
id: '123abc',
2020-07-15 17:17:11 -04:00
type: 'user',
attributes: {
scope: {
scope_id: 'o_1',
2020-09-16 14:29:02 -04:00
type: 'scope',
},
},
},
});
const scope = store.peekRecord('scope', 'o_1');
2020-07-15 17:17:11 -04:00
const model = store.peekRecord('user', '123abc');
assert.strictEqual(model.scope.scope_id, scope.id);
assert.strictEqual(model.scopeModel.id, scope.id);
});
2020-07-22 12:08:10 -04:00
test('it may accept a `scopeModel` for convenience, instead of a fragment', function (assert) {
assert.expect(3);
const store = this.owner.lookup('service:store');
2020-09-16 14:29:02 -04:00
const scope = store.createRecord('scope', { id: 'o_1', type: 'org' });
2020-07-22 12:08:10 -04:00
const model = store.createRecord('user', '123abc');
assert.notEqual(model.scopeID, scope.id);
model.scopeModel = scope;
assert.strictEqual(model.scopeID, scope.id);
assert.true(model.scope.isOrg);
2020-07-22 12:08:10 -04:00
});
test('it has a displayName attribute', function (assert) {
assert.expect(3);
const store = this.owner.lookup('service:store');
store.push({
data: {
id: '123abc',
2020-07-15 17:17:11 -04:00
type: 'user',
attributes: {},
},
});
2020-07-15 17:17:11 -04:00
const model = store.peekRecord('user', '123abc');
assert.strictEqual(model.name, undefined);
assert.strictEqual(model.displayName, '123abc');
model.name = 'Test';
assert.strictEqual(model.displayName, 'Test');
});
2022-12-06 14:29:58 -05:00
test('it has canSave and cannotSave attributes', async function (assert) {
assert.expect(11);
const store = this.owner.lookup('service:store');
store.push({
data: {
id: '1',
2020-07-15 17:17:11 -04:00
type: 'user',
attributes: {},
},
});
2022-12-06 14:29:58 -05:00
this.server.patch('/v1/users/1', () => {
assert.ok(true, 'Correctly scoped update record URL was requested.');
return {};
});
2020-07-15 17:17:11 -04:00
const model = store.peekRecord('user', '1');
assert.false(model.canSave);
assert.true(model.cannotSave);
// Should be able to save if dirty
2020-07-15 17:17:11 -04:00
model.name = 'User';
2022-12-06 14:29:58 -05:00
assert.true(model.hasDirtyAttributes);
assert.true(model.canSave);
assert.false(model.cannotSave);
2022-12-06 14:29:58 -05:00
// Should not be able to save while currently saving
2022-12-06 14:29:58 -05:00
const savePromise = model.save();
// Verify conditions before save completes
assert.true(model.isSaving);
assert.false(model.canSave);
assert.true(model.cannotSave);
2022-12-06 14:29:58 -05:00
// Verify conditions after save completes
await savePromise;
assert.false(model.isSaving);
assert.false(model.canSave);
});
test('it saves records to a scoped URL', async function (assert) {
assert.expect(3);
const store = this.owner.lookup('service:store');
const model = store.createRecord('user', { scopeID: 'o_123' });
2020-09-09 16:53:41 -04:00
this.server.post('/v1/users', () => {
2020-09-16 14:29:02 -04:00
assert.ok(true, 'Correctly scoped create record URL was requested.');
return {};
});
await model.save();
2020-09-09 16:53:41 -04:00
this.server.patch('/v1/users', () => {
2020-09-16 14:29:02 -04:00
assert.ok(true, 'Correctly scoped update record URL was requested.');
return {};
});
await model.save();
2023-01-09 08:48:49 -08:00
this.server.delete('/v1/users/u_123', () => {
2020-09-16 14:29:02 -04:00
assert.ok(true, 'Correctly scoped delete record URL was requested.');
return {};
});
2023-01-09 08:48:49 -08:00
model.id = 'u_123';
await model.destroyRecord();
});
test('it saves to a custom scoped URL if requested', async function (assert) {
assert.expect(3);
const store = this.owner.lookup('service:store');
const customScopeID = 'global';
2020-09-16 14:29:02 -04:00
const scope = { scope_id: 'o_123' };
const model = store.createRecord('user', { scope });
2020-09-09 16:53:41 -04:00
this.server.post('/v1/users', () => {
2020-09-16 14:29:02 -04:00
assert.ok(true, 'Correctly scoped create record URL was requested.');
return {};
});
2020-09-16 14:29:02 -04:00
await model.save({ adapterOptions: { scopeID: customScopeID } });
2020-09-09 16:53:41 -04:00
this.server.patch('/v1/users', () => {
2020-09-16 14:29:02 -04:00
assert.ok(true, 'Correctly scoped update record URL was requested.');
return {};
});
2020-09-16 14:29:02 -04:00
await model.save({ adapterOptions: { scopeID: customScopeID } });
2023-01-09 08:48:49 -08:00
this.server.delete('/v1/users/u_123', () => {
2020-09-16 14:29:02 -04:00
assert.ok(true, 'Correctly scoped delete record URL was requested.');
return {};
});
2023-01-09 08:48:49 -08:00
model.id = 'u_123';
2020-09-16 14:29:02 -04:00
await model.destroyRecord({ adapterOptions: { scopeID: customScopeID } });
});
test('it saves records to a URL with a custom method if requested', async function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const method = 'my-custom-method';
2020-09-16 14:29:02 -04:00
const scope = { scope_id: 'o_123' };
const model = store.createRecord('user', { scope });
2020-09-09 16:53:41 -04:00
this.server.post(`/v1/users:${method}`, () => {
2020-09-16 14:29:02 -04:00
assert.ok(true, 'Correctly scoped create record URL was requested.');
return {};
});
2020-09-16 14:29:02 -04:00
await model.save({ adapterOptions: { method } });
});
2020-05-19 16:33:31 -04:00
});