diff --git a/packages/tests/mirage/index.js b/packages/tests/mirage/index.js index cfaf63ee..4fc6af00 100644 --- a/packages/tests/mirage/index.js +++ b/packages/tests/mirage/index.js @@ -32,6 +32,10 @@ export default function () { return addresses }) + this.post('/addresses', (schema, request) => { + return JSON.parse(request.requestBody) + }) + this.get('/addresses/:id', () => { return { data: addresses.data[1] } }) diff --git a/packages/tests/units/create.js b/packages/tests/units/create.js index cc18b8a7..460e6d29 100644 --- a/packages/tests/units/create.js +++ b/packages/tests/units/create.js @@ -1,11 +1,10 @@ const execCreateTest = (ARM) => { - ARM.setNamespace('api/v1') - describe('Create collection record function', () => { - test('Verify createRecord functionality', async () => { + beforeEach(() => { ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) + }) + test('Verify createRecord functionality', async () => { ARM.createRecord('addresses', false) expect(ARM.getCollection('addresses')).toHaveLength(1) @@ -17,6 +16,16 @@ const execCreateTest = (ARM) => { } expect(ARM.getCollection('addresses')).toHaveLength(5) }) + + test('Verify createRecord with custom attributes', () => { + const record = ARM.createRecord('addresses', { + attributes: { address1: 'Test Address', kind: 'office' }, + }) + + expect(record.get('attributes.address1')).toBe('Test Address') + expect(record.get('attributes.kind')).toBe('office') + expect(record.get('id')).toBeDefined() + }) }) } diff --git a/packages/tests/units/init.js b/packages/tests/units/init.js index c8f9dfe8..d867ef14 100644 --- a/packages/tests/units/init.js +++ b/packages/tests/units/init.js @@ -1,4 +1,5 @@ import axios from 'axios' +import ApiResourceManager from '../../src' const execInitTest = (ARM) => { describe('Instance initialization', () => { @@ -23,6 +24,20 @@ const execInitTest = (ARM) => { 'sailfish-os', ) }) + + test('Verify setPayloadIncludeReference functionality', () => { + // A fresh instance is needed because the shared ARM is frozen via setGlobal(), + // which prevents plain (non-observable) properties from being mutated. + // The constructor calls _initializeAxiosConfig() and resets axios.defaults.baseURL + // to window.location.origin, so we save and restore it to avoid breaking other tests. + const originalBaseURL = axios.defaults.baseURL + const freshARM = new ApiResourceManager(['addresses']) + freshARM.setPayloadIncludeReference('kind') + expect(freshARM.payloadIncludedReference).toBe('kind') + freshARM.setPayloadIncludeReference('type') + expect(freshARM.payloadIncludedReference).toBe('type') + axios.defaults.baseURL = originalBaseURL + }) }) } diff --git a/packages/tests/units/internals.js b/packages/tests/units/internals.js index 870b8fd1..b2ee3716 100644 --- a/packages/tests/units/internals.js +++ b/packages/tests/units/internals.js @@ -1,6 +1,4 @@ const execInternalsTest = (ARM) => { - ARM.setNamespace('api/v1') - describe('Internal functions', () => { test('Verify _initializeCollections functionality', () => { ARM._initializeCollections(['shops']) @@ -8,6 +6,8 @@ const execInternalsTest = (ARM) => { }) test('Verify _getBaseURL functionality', () => { + // ARM is frozen via setGlobal() after setHost/setNamespace('api/v2') was called. + // namespace is a plain (non-observable) property, so it cannot change post-freeze. expect(ARM._getBaseURL()).toBe('https://api.arm-js-library.com/api/v2') }) @@ -67,6 +67,18 @@ const execInternalsTest = (ARM) => { expect(user).toHaveProperty('isDirty') expect(user).toHaveProperty('originalRecord') }) + + test('Verify _injectCollectionReferenceKeys sets correct default values', () => { + let record = { id: 42 } + ARM._injectCollectionReferenceKeys('addresses', record) + + expect(record.collectionName).toBe('addresses') + expect(record.isLoading).toBe(false) + expect(record.isError).toBe(false) + expect(record.isPristine).toBe(true) + expect(record.isDirty).toBe(false) + expect(record.originalRecord).toBeDefined() + }) }) } diff --git a/packages/tests/units/push.js b/packages/tests/units/push.js index 89d751b9..f73ecce9 100644 --- a/packages/tests/units/push.js +++ b/packages/tests/units/push.js @@ -1,19 +1,32 @@ const execPushTest = (ARM) => { - ARM.setNamespace('api/v1') - describe('Push collection record function', () => { - test('Verify pushPayload functionality', async () => { + beforeEach(() => { ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) + ARM.clearCollection('users') + }) + + test('Verify pushPayload functionality', async () => { + const results = await ARM.ajax({ + method: 'get', + url: 'addresses', + }) + + ARM.pushPayload('addresses', results.data.data) + expect(ARM.getCollection('addresses')).toHaveLength(12) + }) + test('Verify pushPayload with multiple collections', async () => { const results = await ARM.ajax({ method: 'get', url: 'addresses', + params: { include: 'users' }, }) ARM.pushPayload('addresses', results.data.data) + ARM.pushPayload('users', results.data.included) expect(ARM.getCollection('addresses')).toHaveLength(12) + expect(ARM.getCollection('users')).toHaveLength(1) }) }) } diff --git a/packages/tests/units/record.js b/packages/tests/units/record.js index 5bb7795c..56038256 100644 --- a/packages/tests/units/record.js +++ b/packages/tests/units/record.js @@ -1,14 +1,13 @@ import { v1 as uuidv1 } from 'uuid' const execRecordTest = (ARM) => { - ARM.setNamespace('api/v1') - describe('Collection Records: Properties and Functions', () => { + beforeEach(() => { + ARM.clearCollection('addresses') + }) + describe('State Properties', () => { test('Verify isLoading functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.findRecord('addresses', 2518368, null, { autoResolve: false, skipId: uuidv1(), @@ -22,9 +21,6 @@ const execRecordTest = (ARM) => { }) test('Verify isDirty functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.findRecord('addresses', 2518368, null, { autoResolve: false, skipId: uuidv1(), @@ -40,9 +36,6 @@ const execRecordTest = (ARM) => { }) test('Verify isError functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.findAll('addresses', { autoResolve: false, skipId: uuidv1() }) const record = ARM.peekRecord('addresses', 2519858) @@ -60,9 +53,6 @@ const execRecordTest = (ARM) => { }) test('Verify isPristine functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.findRecord('addresses', 2518368, null, { autoResolve: false, skipId: uuidv1(), @@ -80,9 +70,6 @@ const execRecordTest = (ARM) => { describe('Getter and Setter Functions', () => { test('Verify get functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.findRecord('addresses', 2518368, null, { autoResolve: false, skipId: uuidv1(), @@ -92,9 +79,6 @@ const execRecordTest = (ARM) => { }) test('Verify set functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.findRecord('addresses', 2518368, null, { autoResolve: false, skipId: uuidv1(), @@ -105,9 +89,6 @@ const execRecordTest = (ARM) => { }) test('Verify setProperties functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.findRecord('addresses', 2518368, null, { autoResolve: false, skipId: uuidv1(), @@ -126,9 +107,6 @@ const execRecordTest = (ARM) => { describe('Request Functions', () => { test('Verify save functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.findRecord('addresses', 2518368, null, { autoResolve: false, skipId: uuidv1(), @@ -141,10 +119,32 @@ const execRecordTest = (ARM) => { expect(record.get('attributes.address1')).toBe('Anabu Hills Modified') }) - test('Verify reload functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) + test('Verify save functionality for new record uses POST', async () => { + const record = ARM.createRecord('addresses', { + attributes: { address1: 'New Address', kind: 'office', label: 'My New Office' }, + }) + const result = await record.save() + expect(result).toBeDefined() + }) + + test('Verify save with string collection record id uses PUT', async () => { + ARM.pushPayload('addresses', [ + { + id: 'string-record-id', + type: 'addresses', + attributes: { address1: 'String ID Test', kind: 'office' }, + }, + ]) + const record = ARM.peekRecord('addresses', 'string-record-id') + expect(record).toBeDefined() + + record.set('attributes.address1', 'String ID Test Modified') + const result = await record.save() + expect(result).toBeDefined() + expect(record.get('attributes.address1')).toBe('String ID Test Modified') + }) + test('Verify reload functionality', async () => { await ARM.findRecord('addresses', 2518368, null, { autoResolve: false, skipId: uuidv1(), @@ -159,9 +159,6 @@ const execRecordTest = (ARM) => { }) test('Verify rollbackAttributes functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.findRecord('addresses', 2518368, null, { autoResolve: false, skipId: uuidv1(), @@ -175,10 +172,27 @@ const execRecordTest = (ARM) => { expect(record.get('attributes.address1')).toBe('Anabu Hills Test 4') }) - test('Verify destroyRecord functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) + test('Verify rollbackAttributes after setProperties', async () => { + await ARM.findRecord('addresses', 2518368, null, { + autoResolve: false, + skipId: uuidv1(), + }) + const record = ARM.peekRecord('addresses', 2518368) + record.setProperties({ + attributes: { + address1: 'New address1 changes', + address2: 'New address2 changes', + }, + }) + expect(record.get('isPristine')).toBe(false) + + record.rollbackAttributes() + expect(record.get('isPristine')).toBe(true) + expect(record.get('attributes.address1')).toBe('Anabu Hills Test 4') + }) + + test('Verify destroyRecord functionality', async () => { await ARM.findRecord('addresses', 2518368, null, { autoResolve: false, skipId: uuidv1(), @@ -191,9 +205,6 @@ const execRecordTest = (ARM) => { }) test('Verify getCollection functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.query( 'addresses', { diff --git a/packages/tests/units/remove.js b/packages/tests/units/remove.js index d7254cd6..b964bd4a 100644 --- a/packages/tests/units/remove.js +++ b/packages/tests/units/remove.js @@ -1,10 +1,15 @@ import { v1 as uuidv1 } from 'uuid' const execRemoveTest = (ARM) => { - ARM.setNamespace('api/v1') - describe('Remove collection record functions', () => { + beforeEach(() => { + ARM.clearCollection('addresses') + }) + test('Verify clearCollection functionality', async () => { + await ARM.query('addresses', {}, { autoResolve: false, skipId: uuidv1() }) + expect(ARM.getCollection('addresses')).toHaveLength(12) + ARM.clearCollection('addresses') expect(ARM.getCollection('addresses')).toHaveLength(0) }) @@ -14,6 +19,22 @@ const execRemoveTest = (ARM) => { ARM.unloadRecord(ARM.peekRecord('addresses', 2518368)) expect(ARM.getCollection('addresses')).toHaveLength(11) }) + + test('Verify unloadRecord removes record from aliases', async () => { + await ARM.query( + 'addresses', + {}, + { + autoResolve: false, + alias: 'allAddressesAlias', + skipId: uuidv1(), + }, + ) + expect(ARM.getAlias('allAddressesAlias')).toHaveLength(12) + + ARM.unloadRecord(ARM.peekRecord('addresses', 2518368)) + expect(ARM.getAlias('allAddressesAlias')).toHaveLength(11) + }) }) } diff --git a/packages/tests/units/request.js b/packages/tests/units/request.js index 35eee0f7..8cc41993 100644 --- a/packages/tests/units/request.js +++ b/packages/tests/units/request.js @@ -1,21 +1,17 @@ import { v1 as uuidv1 } from 'uuid' const execRequestTest = (ARM) => { - ARM.setNamespace('api/v1') - describe('Request functions from server', () => { - test('Verify query functionality', async () => { + beforeEach(() => { ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) + }) + test('Verify query functionality', async () => { await ARM.query('addresses', {}, { autoResolve: false, skipId: uuidv1() }) expect(ARM.getCollection('addresses')).toHaveLength(12) }) test('Verify queryRecord functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.queryRecord( 'addresses', { @@ -27,9 +23,6 @@ const execRequestTest = (ARM) => { }) test('Verify findRecord functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.findRecord('addresses', 2518368, null, { autoResolve: false, skipId: uuidv1(), @@ -38,17 +31,11 @@ const execRequestTest = (ARM) => { }) test('Verify findAll functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.findAll('addresses', { autoResolve: false, skipId: uuidv1() }) expect(ARM.getCollection('addresses')).toHaveLength(12) }) test('Verify reload functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - const result = await ARM.findRecord('addresses', 2518368, null, { autoResolve: false, skipId: uuidv1(), @@ -63,6 +50,21 @@ const execRequestTest = (ARM) => { expect(record.get('isPristine')).toBe(true) expect(record.get('attributes.address1')).toBe('Anabu Hills Test 4') }, 5000) + + test('Verify autoResolve default behavior returns reactive hash object', () => { + // query() returns the initial hash object synchronously. When XHR completes, + // _pushRequestHash replaces (not mutates) the entry in requestHashes, making + // the captured reference stale - so only the initial shape can be asserted here. + const result = ARM.query('addresses', {}, { skipId: uuidv1() }) + + expect(result).not.toBeInstanceOf(Promise) + expect(result).toHaveProperty('isLoading') + expect(result).toHaveProperty('isNew') + expect(result).toHaveProperty('data') + expect(result.isLoading).toBe(true) + expect(result.isNew).toBe(true) + expect(result.data).toEqual([]) + }) }) } diff --git a/packages/tests/units/retrieve.js b/packages/tests/units/retrieve.js index a7c3de6d..b3696215 100644 --- a/packages/tests/units/retrieve.js +++ b/packages/tests/units/retrieve.js @@ -1,9 +1,11 @@ import { v1 as uuidv1 } from 'uuid' const execRetrieveTest = (ARM) => { - ARM.setNamespace('api/v1') - describe('Retrieve functions from collections', () => { + beforeEach(() => { + ARM.clearCollection('addresses') + }) + test('Verify peekAll functionality', async () => { await ARM.query('addresses', {}, { autoResolve: false, skipId: uuidv1() }) expect(ARM.peekAll('addresses')).toHaveLength(12) @@ -14,10 +16,11 @@ const execRetrieveTest = (ARM) => { expect(ARM.peekRecord('addresses', 2518368).get('id')).toBe(2518368) }) - test('Verify getCollection functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) + test('Verify peekRecord returns undefined for non-existent id', () => { + expect(ARM.peekRecord('addresses', 9999999)).toBeUndefined() + }) + test('Verify getCollection functionality', async () => { await ARM.query( 'addresses', { page: { size: 5 } }, @@ -27,9 +30,6 @@ const execRetrieveTest = (ARM) => { }) test('Verify getAlias functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) - await ARM.query( 'addresses', { page: { size: 5 } }, @@ -38,10 +38,16 @@ const execRetrieveTest = (ARM) => { expect(ARM.getAlias('customerAddresses')).toHaveLength(5) }) - test('Verify getRequestAlias functionality', async () => { - ARM.clearCollection('addresses') - expect(ARM.getCollection('addresses')).toHaveLength(0) + test('Verify getAlias returns fallback when alias does not exist', () => { + const fallback = [ + { id: 999, attributes: { address1: 'Fallback Address' } }, + ] + const result = ARM.getAlias('nonExistentAlias', fallback) + expect(result).toBeDefined() + expect(result).toHaveLength(1) + }) + test('Verify getRequestAlias functionality', async () => { await ARM.query( 'addresses', { page: { size: 5 } }, diff --git a/packages/tests/units/utils.js b/packages/tests/units/utils.js index 63b7b7c7..48cd832b 100644 --- a/packages/tests/units/utils.js +++ b/packages/tests/units/utils.js @@ -99,6 +99,30 @@ const execUtilsTest = (ARM) => { expect(ARM.sortBy(addresses, ['id:desc'])).toEqual(addresses.reverse()) }) + test('Verify sortBy with asc direction', () => { + const items = [{ id: 3 }, { id: 1 }, { id: 2 }] + const sorted = ARM.sortBy(items, ['id:asc']) + expect(sorted[0].id).toBe(1) + expect(sorted[1].id).toBe(2) + expect(sorted[2].id).toBe(3) + }) + + test('Verify findBy returns undefined when no match', () => { + expect(ARM.findBy(addresses, { id: 999 })).toBeUndefined() + }) + + test('Verify filterBy returns empty array when no match', () => { + expect( + ARM.filterBy(addresses, { attributes: { kind: 'warehouse' } }), + ).toEqual([]) + }) + + test('Verify mergeObjects deduplicates overlapping records', () => { + const a = { id: 1, name: 'test' } + const merged = ARM.mergeObjects([a], [a, { id: 2, name: 'other' }]) + expect(merged).toHaveLength(2) + }) + test('Verify sum functionality', () => { expect(ARM.sum([100, 200, 300])).toBe(600) })