Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/tests/mirage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] }
})
Expand Down
17 changes: 13 additions & 4 deletions packages/tests/units/create.js
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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()
})
})
}

Expand Down
15 changes: 15 additions & 0 deletions packages/tests/units/init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from 'axios'
import ApiResourceManager from '../../src'

const execInitTest = (ARM) => {
describe('Instance initialization', () => {
Expand All @@ -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
})
})
}

Expand Down
16 changes: 14 additions & 2 deletions packages/tests/units/internals.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const execInternalsTest = (ARM) => {
ARM.setNamespace('api/v1')

describe('Internal functions', () => {
test('Verify _initializeCollections functionality', () => {
ARM._initializeCollections(['shops'])
expect(ARM.collections.shops).toBeDefined()
})

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')
})

Expand Down Expand Up @@ -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()
})
})
}

Expand Down
21 changes: 17 additions & 4 deletions packages/tests/units/push.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
}
Expand Down
87 changes: 49 additions & 38 deletions packages/tests/units/record.js
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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(),
Expand All @@ -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)

Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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',
{
Expand Down
25 changes: 23 additions & 2 deletions packages/tests/units/remove.js
Original file line number Diff line number Diff line change
@@ -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)
})
Expand All @@ -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)
})
})
}

Expand Down
Loading
Loading