Skip to content

Commit 8cf2be1

Browse files
test(units): improve test structure and expand coverage across all unit modules (#242)
* test(mirage): add POST route for new record save * test(init): add setPayloadIncludeReference coverage * test(request): add beforeEach reset and autoResolve default behavior test * test(retrieve): add beforeEach reset, peekRecord undefined and alias fallback tests * test(remove): add beforeEach reset and unloadRecord alias cleanup test * test(push): add beforeEach reset and pushPayload with included data test * test(create): add beforeEach reset and createRecord with attributes test * test(record): add beforeEach reset, save POST, string id PUT and rollback tests * test(utils): add sortBy asc, findBy/filterBy no-match and mergeObjects dedup tests * test(internals): fix _getBaseURL assertion and add default value checks
1 parent 81e929f commit 8cf2be1

10 files changed

Lines changed: 194 additions & 77 deletions

File tree

‎packages/tests/mirage/index.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export default function () {
3232
return addresses
3333
})
3434

35+
this.post('/addresses', (schema, request) => {
36+
return JSON.parse(request.requestBody)
37+
})
38+
3539
this.get('/addresses/:id', () => {
3640
return { data: addresses.data[1] }
3741
})

‎packages/tests/units/create.js‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
const execCreateTest = (ARM) => {
2-
ARM.setNamespace('api/v1')
3-
42
describe('Create collection record function', () => {
5-
test('Verify createRecord functionality', async () => {
3+
beforeEach(() => {
64
ARM.clearCollection('addresses')
7-
expect(ARM.getCollection('addresses')).toHaveLength(0)
5+
})
86

7+
test('Verify createRecord functionality', async () => {
98
ARM.createRecord('addresses', false)
109
expect(ARM.getCollection('addresses')).toHaveLength(1)
1110

@@ -17,6 +16,16 @@ const execCreateTest = (ARM) => {
1716
}
1817
expect(ARM.getCollection('addresses')).toHaveLength(5)
1918
})
19+
20+
test('Verify createRecord with custom attributes', () => {
21+
const record = ARM.createRecord('addresses', {
22+
attributes: { address1: 'Test Address', kind: 'office' },
23+
})
24+
25+
expect(record.get('attributes.address1')).toBe('Test Address')
26+
expect(record.get('attributes.kind')).toBe('office')
27+
expect(record.get('id')).toBeDefined()
28+
})
2029
})
2130
}
2231

‎packages/tests/units/init.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios'
2+
import ApiResourceManager from '../../src'
23

34
const execInitTest = (ARM) => {
45
describe('Instance initialization', () => {
@@ -23,6 +24,20 @@ const execInitTest = (ARM) => {
2324
'sailfish-os',
2425
)
2526
})
27+
28+
test('Verify setPayloadIncludeReference functionality', () => {
29+
// A fresh instance is needed because the shared ARM is frozen via setGlobal(),
30+
// which prevents plain (non-observable) properties from being mutated.
31+
// The constructor calls _initializeAxiosConfig() and resets axios.defaults.baseURL
32+
// to window.location.origin, so we save and restore it to avoid breaking other tests.
33+
const originalBaseURL = axios.defaults.baseURL
34+
const freshARM = new ApiResourceManager(['addresses'])
35+
freshARM.setPayloadIncludeReference('kind')
36+
expect(freshARM.payloadIncludedReference).toBe('kind')
37+
freshARM.setPayloadIncludeReference('type')
38+
expect(freshARM.payloadIncludedReference).toBe('type')
39+
axios.defaults.baseURL = originalBaseURL
40+
})
2641
})
2742
}
2843

‎packages/tests/units/internals.js‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const execInternalsTest = (ARM) => {
2-
ARM.setNamespace('api/v1')
3-
42
describe('Internal functions', () => {
53
test('Verify _initializeCollections functionality', () => {
64
ARM._initializeCollections(['shops'])
75
expect(ARM.collections.shops).toBeDefined()
86
})
97

108
test('Verify _getBaseURL functionality', () => {
9+
// ARM is frozen via setGlobal() after setHost/setNamespace('api/v2') was called.
10+
// namespace is a plain (non-observable) property, so it cannot change post-freeze.
1111
expect(ARM._getBaseURL()).toBe('https://api.arm-js-library.com/api/v2')
1212
})
1313

@@ -67,6 +67,18 @@ const execInternalsTest = (ARM) => {
6767
expect(user).toHaveProperty('isDirty')
6868
expect(user).toHaveProperty('originalRecord')
6969
})
70+
71+
test('Verify _injectCollectionReferenceKeys sets correct default values', () => {
72+
let record = { id: 42 }
73+
ARM._injectCollectionReferenceKeys('addresses', record)
74+
75+
expect(record.collectionName).toBe('addresses')
76+
expect(record.isLoading).toBe(false)
77+
expect(record.isError).toBe(false)
78+
expect(record.isPristine).toBe(true)
79+
expect(record.isDirty).toBe(false)
80+
expect(record.originalRecord).toBeDefined()
81+
})
7082
})
7183
}
7284

‎packages/tests/units/push.js‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
const execPushTest = (ARM) => {
2-
ARM.setNamespace('api/v1')
3-
42
describe('Push collection record function', () => {
5-
test('Verify pushPayload functionality', async () => {
3+
beforeEach(() => {
64
ARM.clearCollection('addresses')
7-
expect(ARM.getCollection('addresses')).toHaveLength(0)
5+
ARM.clearCollection('users')
6+
})
7+
8+
test('Verify pushPayload functionality', async () => {
9+
const results = await ARM.ajax({
10+
method: 'get',
11+
url: 'addresses',
12+
})
13+
14+
ARM.pushPayload('addresses', results.data.data)
15+
expect(ARM.getCollection('addresses')).toHaveLength(12)
16+
})
817

18+
test('Verify pushPayload with multiple collections', async () => {
919
const results = await ARM.ajax({
1020
method: 'get',
1121
url: 'addresses',
22+
params: { include: 'users' },
1223
})
1324

1425
ARM.pushPayload('addresses', results.data.data)
26+
ARM.pushPayload('users', results.data.included)
1527

1628
expect(ARM.getCollection('addresses')).toHaveLength(12)
29+
expect(ARM.getCollection('users')).toHaveLength(1)
1730
})
1831
})
1932
}

‎packages/tests/units/record.js‎

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { v1 as uuidv1 } from 'uuid'
22

33
const execRecordTest = (ARM) => {
4-
ARM.setNamespace('api/v1')
5-
64
describe('Collection Records: Properties and Functions', () => {
5+
beforeEach(() => {
6+
ARM.clearCollection('addresses')
7+
})
8+
79
describe('State Properties', () => {
810
test('Verify isLoading functionality', async () => {
9-
ARM.clearCollection('addresses')
10-
expect(ARM.getCollection('addresses')).toHaveLength(0)
11-
1211
await ARM.findRecord('addresses', 2518368, null, {
1312
autoResolve: false,
1413
skipId: uuidv1(),
@@ -22,9 +21,6 @@ const execRecordTest = (ARM) => {
2221
})
2322

2423
test('Verify isDirty functionality', async () => {
25-
ARM.clearCollection('addresses')
26-
expect(ARM.getCollection('addresses')).toHaveLength(0)
27-
2824
await ARM.findRecord('addresses', 2518368, null, {
2925
autoResolve: false,
3026
skipId: uuidv1(),
@@ -40,9 +36,6 @@ const execRecordTest = (ARM) => {
4036
})
4137

4238
test('Verify isError functionality', async () => {
43-
ARM.clearCollection('addresses')
44-
expect(ARM.getCollection('addresses')).toHaveLength(0)
45-
4639
await ARM.findAll('addresses', { autoResolve: false, skipId: uuidv1() })
4740
const record = ARM.peekRecord('addresses', 2519858)
4841

@@ -60,9 +53,6 @@ const execRecordTest = (ARM) => {
6053
})
6154

6255
test('Verify isPristine functionality', async () => {
63-
ARM.clearCollection('addresses')
64-
expect(ARM.getCollection('addresses')).toHaveLength(0)
65-
6656
await ARM.findRecord('addresses', 2518368, null, {
6757
autoResolve: false,
6858
skipId: uuidv1(),
@@ -80,9 +70,6 @@ const execRecordTest = (ARM) => {
8070

8171
describe('Getter and Setter Functions', () => {
8272
test('Verify get functionality', async () => {
83-
ARM.clearCollection('addresses')
84-
expect(ARM.getCollection('addresses')).toHaveLength(0)
85-
8673
await ARM.findRecord('addresses', 2518368, null, {
8774
autoResolve: false,
8875
skipId: uuidv1(),
@@ -92,9 +79,6 @@ const execRecordTest = (ARM) => {
9279
})
9380

9481
test('Verify set functionality', async () => {
95-
ARM.clearCollection('addresses')
96-
expect(ARM.getCollection('addresses')).toHaveLength(0)
97-
9882
await ARM.findRecord('addresses', 2518368, null, {
9983
autoResolve: false,
10084
skipId: uuidv1(),
@@ -105,9 +89,6 @@ const execRecordTest = (ARM) => {
10589
})
10690

10791
test('Verify setProperties functionality', async () => {
108-
ARM.clearCollection('addresses')
109-
expect(ARM.getCollection('addresses')).toHaveLength(0)
110-
11192
await ARM.findRecord('addresses', 2518368, null, {
11293
autoResolve: false,
11394
skipId: uuidv1(),
@@ -126,9 +107,6 @@ const execRecordTest = (ARM) => {
126107

127108
describe('Request Functions', () => {
128109
test('Verify save functionality', async () => {
129-
ARM.clearCollection('addresses')
130-
expect(ARM.getCollection('addresses')).toHaveLength(0)
131-
132110
await ARM.findRecord('addresses', 2518368, null, {
133111
autoResolve: false,
134112
skipId: uuidv1(),
@@ -141,10 +119,32 @@ const execRecordTest = (ARM) => {
141119
expect(record.get('attributes.address1')).toBe('Anabu Hills Modified')
142120
})
143121

144-
test('Verify reload functionality', async () => {
145-
ARM.clearCollection('addresses')
146-
expect(ARM.getCollection('addresses')).toHaveLength(0)
122+
test('Verify save functionality for new record uses POST', async () => {
123+
const record = ARM.createRecord('addresses', {
124+
attributes: { address1: 'New Address', kind: 'office', label: 'My New Office' },
125+
})
126+
const result = await record.save()
127+
expect(result).toBeDefined()
128+
})
129+
130+
test('Verify save with string collection record id uses PUT', async () => {
131+
ARM.pushPayload('addresses', [
132+
{
133+
id: 'string-record-id',
134+
type: 'addresses',
135+
attributes: { address1: 'String ID Test', kind: 'office' },
136+
},
137+
])
138+
const record = ARM.peekRecord('addresses', 'string-record-id')
139+
expect(record).toBeDefined()
140+
141+
record.set('attributes.address1', 'String ID Test Modified')
142+
const result = await record.save()
143+
expect(result).toBeDefined()
144+
expect(record.get('attributes.address1')).toBe('String ID Test Modified')
145+
})
147146

147+
test('Verify reload functionality', async () => {
148148
await ARM.findRecord('addresses', 2518368, null, {
149149
autoResolve: false,
150150
skipId: uuidv1(),
@@ -159,9 +159,6 @@ const execRecordTest = (ARM) => {
159159
})
160160

161161
test('Verify rollbackAttributes functionality', async () => {
162-
ARM.clearCollection('addresses')
163-
expect(ARM.getCollection('addresses')).toHaveLength(0)
164-
165162
await ARM.findRecord('addresses', 2518368, null, {
166163
autoResolve: false,
167164
skipId: uuidv1(),
@@ -175,10 +172,27 @@ const execRecordTest = (ARM) => {
175172
expect(record.get('attributes.address1')).toBe('Anabu Hills Test 4')
176173
})
177174

178-
test('Verify destroyRecord functionality', async () => {
179-
ARM.clearCollection('addresses')
180-
expect(ARM.getCollection('addresses')).toHaveLength(0)
175+
test('Verify rollbackAttributes after setProperties', async () => {
176+
await ARM.findRecord('addresses', 2518368, null, {
177+
autoResolve: false,
178+
skipId: uuidv1(),
179+
})
180+
const record = ARM.peekRecord('addresses', 2518368)
181181

182+
record.setProperties({
183+
attributes: {
184+
address1: 'New address1 changes',
185+
address2: 'New address2 changes',
186+
},
187+
})
188+
expect(record.get('isPristine')).toBe(false)
189+
190+
record.rollbackAttributes()
191+
expect(record.get('isPristine')).toBe(true)
192+
expect(record.get('attributes.address1')).toBe('Anabu Hills Test 4')
193+
})
194+
195+
test('Verify destroyRecord functionality', async () => {
182196
await ARM.findRecord('addresses', 2518368, null, {
183197
autoResolve: false,
184198
skipId: uuidv1(),
@@ -191,9 +205,6 @@ const execRecordTest = (ARM) => {
191205
})
192206

193207
test('Verify getCollection functionality', async () => {
194-
ARM.clearCollection('addresses')
195-
expect(ARM.getCollection('addresses')).toHaveLength(0)
196-
197208
await ARM.query(
198209
'addresses',
199210
{

‎packages/tests/units/remove.js‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import { v1 as uuidv1 } from 'uuid'
22

33
const execRemoveTest = (ARM) => {
4-
ARM.setNamespace('api/v1')
5-
64
describe('Remove collection record functions', () => {
5+
beforeEach(() => {
6+
ARM.clearCollection('addresses')
7+
})
8+
79
test('Verify clearCollection functionality', async () => {
10+
await ARM.query('addresses', {}, { autoResolve: false, skipId: uuidv1() })
11+
expect(ARM.getCollection('addresses')).toHaveLength(12)
12+
813
ARM.clearCollection('addresses')
914
expect(ARM.getCollection('addresses')).toHaveLength(0)
1015
})
@@ -14,6 +19,22 @@ const execRemoveTest = (ARM) => {
1419
ARM.unloadRecord(ARM.peekRecord('addresses', 2518368))
1520
expect(ARM.getCollection('addresses')).toHaveLength(11)
1621
})
22+
23+
test('Verify unloadRecord removes record from aliases', async () => {
24+
await ARM.query(
25+
'addresses',
26+
{},
27+
{
28+
autoResolve: false,
29+
alias: 'allAddressesAlias',
30+
skipId: uuidv1(),
31+
},
32+
)
33+
expect(ARM.getAlias('allAddressesAlias')).toHaveLength(12)
34+
35+
ARM.unloadRecord(ARM.peekRecord('addresses', 2518368))
36+
expect(ARM.getAlias('allAddressesAlias')).toHaveLength(11)
37+
})
1738
})
1839
}
1940

0 commit comments

Comments
 (0)