Files
threshold-ui/addons/api/tests/unit/models/base-test.js
T
hashiccandZhihe Li 63b810d53f Migrate from ember-cli-mirage to miragejs (#3088)
* chore: 🤖 import explicit mirage references

* chore: 🤖 add mirage setupMirage helper

* chore: 🤖 working example with application adapter test

* chore: 🤖 use internal setupMirage test helper from api addon

* chore: 🤖 conditionally load mirage

* chore: 🤖 filter mirage folder inclusion in production builds

* chore: 🤖 drop ember-mirage and use our own test helpers

* chore: 🤖 collapse reference to import declaration

* docs: ✏️ add comment better explaining the mirage test env

* chore: 🤖 update configuration and manage mirage-related deps

* fix: 🐛 restore ipc mock and add missing checkOS handler

* chore: 🤖 add comment explaining omitting mirage deps in build

* chore: 🤖 allow mirage.enabled to include deps and handlers

* chore: 🤖 re-enable mirage for core dummy app

* Add missing copyright headers

* chore: 🤖 use embroider macros to conditionally start mirage

* docs: ✏️ update api readme for mirage changes

* chore: 🤖 replace remaining ember-cli-mirage references

* chore: 🤖 add mirage to test builds

* fix: 🐛 ensure that auto mirage start doesn't happen in test env

* chore: 🤖 use const instead of var

Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>

* refactor: 💡 use cleaner logic for excluded api build modules

* fix: 🐛 handle issue where importSync was too eager

This was fixed by bumping the @embroider/macros dependency that includes
a fix to this issue:
https://github.com/embroider-build/embroider/pull/2511

* refactor: 💡 move mirage from addon to app directory

* refactor: 💡 update mirage test helper to be a factory

* chore: 🤖 exclude mirage directory when necessary at build time

* chore: 🤖 add miragejs as admin dev dependency

* refactor: 💡 update reference to startMirage

* chore: 🤖 update local mirage test helper

* chore: 🤖 change import path for mirage on desktop

* chore: 🤖 update imports in tests for startMirage

* chore: 🤖 update startMirage imports for api addon tests

* chore: 🤖 add miragejs dev dep to desktop

* docs: ✏️ update README

* chore: 🤖 set up setupMirage for core tests

* chore: 🤖 fix copyright header

* Add missing copyright headers

---------

Co-authored-by: Zhihe Li <zhihe.li@hashicorp.com>
2026-02-03 13:54:32 -05:00

168 lines
5.3 KiB
JavaScript

/**
* Copyright IBM Corp. 2021, 2026
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { setupMirage } from 'dummy/tests/helpers/mirage';
module('Unit | Model | base', function (hooks) {
setupTest(hooks);
setupMirage(hooks);
// 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);
});
test('it may have a scope fragment (and thus a scopeModel)', function (assert) {
const store = this.owner.lookup('service:store');
store.push({
data: {
id: 'o_1',
type: 'scope',
},
});
store.push({
data: {
id: '123abc',
type: 'user',
attributes: {
scope: {
scope_id: 'o_1',
type: 'scope',
},
},
},
});
const scope = store.peekRecord('scope', 'o_1');
const model = store.peekRecord('user', '123abc');
assert.strictEqual(model.scope.scope_id, scope.id);
assert.strictEqual(model.scopeModel.id, scope.id);
});
test('it may accept a `scopeModel` for convenience, instead of a fragment', function (assert) {
const store = this.owner.lookup('service:store');
const scope = store.createRecord('scope', { id: 'o_1', type: 'org' });
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);
});
test('it has a displayName attribute', function (assert) {
const store = this.owner.lookup('service:store');
store.push({
data: {
id: '123abc',
type: 'user',
attributes: {},
},
});
const model = store.peekRecord('user', '123abc');
assert.strictEqual(model.name, undefined);
assert.strictEqual(model.displayName, '123abc');
model.name = 'Test';
assert.strictEqual(model.displayName, 'Test');
});
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',
type: 'user',
attributes: {},
},
});
this.server.patch('users/1', () => {
assert.ok(true, 'Correctly scoped update record URL was requested.');
return { id: 1 };
});
const model = store.peekRecord('user', '1');
assert.false(model.canSave);
assert.true(model.cannotSave);
// Should be able to save if dirty
model.name = 'User';
assert.true(model.hasDirtyAttributes);
assert.true(model.canSave);
assert.false(model.cannotSave);
// Should not be able to save while currently saving
const savePromise = model.save();
// Verify conditions before save completes
assert.true(model.isSaving);
assert.false(model.canSave);
assert.true(model.cannotSave);
// 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' });
this.server.post('users', () => {
assert.ok(true, 'Correctly scoped create record URL was requested.');
return { id: 'u_123' };
});
await model.save();
this.server.patch('users/u_123', () => {
assert.ok(true, 'Correctly scoped update record URL was requested.');
return { id: 'u_123' };
});
await model.save();
this.server.delete('users/u_123', () => {
assert.ok(true, 'Correctly scoped delete record URL was requested.');
return {};
});
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';
const scope = { scope_id: 'o_123' };
const model = store.createRecord('user', { scope });
this.server.post('users', () => {
assert.ok(true, 'Correctly scoped create record URL was requested.');
return { id: 'u_123' };
});
await model.save({ adapterOptions: { scopeID: customScopeID } });
this.server.patch('users/u_123', () => {
assert.ok(true, 'Correctly scoped update record URL was requested.');
return { id: 'u_123' };
});
await model.save({ adapterOptions: { scopeID: customScopeID } });
this.server.delete('users/u_123', () => {
assert.ok(true, 'Correctly scoped delete record URL was requested.');
return {};
});
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';
const scope = { scope_id: 'o_123' };
const model = store.createRecord('user', { scope });
this.server.post(`users:${method}`, () => {
assert.ok(true, 'Correctly scoped create record URL was requested.');
return { id: 'u_123' };
});
await model.save({ adapterOptions: { method } });
});
});