-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
300 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
tests/warp-drive__schema-record/tests/reactivity/schema-array-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { rerender } from '@ember/test-helpers'; | ||
|
||
import { module, test } from 'qunit'; | ||
|
||
import { setupRenderingTest } from 'ember-qunit'; | ||
|
||
import type Store from '@ember-data/store'; | ||
import type { FieldSchema } from '@ember-data/store/-types/q/schema-service'; | ||
import { registerDerivations, SchemaService, withFields } from '@warp-drive/schema-record/schema'; | ||
|
||
import { reactiveContext } from '../-utils/reactive-context'; | ||
|
||
interface address { | ||
street: string; | ||
city: string; | ||
state: string; | ||
zip: string | number; | ||
} | ||
|
||
interface User { | ||
id: string | null; | ||
$type: 'user'; | ||
name: string; | ||
addresses: Array<address | null> | null; | ||
favoriteNumbers: string[]; | ||
age: number; | ||
netWorth: number; | ||
coolometer: number; | ||
rank: number; | ||
} | ||
|
||
module('Reactivity | schema-array fields can receive remote updates', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('we can use simple fields with no `type`', async function (assert) { | ||
const store = this.owner.lookup('service:store') as Store; | ||
const schema = new SchemaService(); | ||
store.registerSchema(schema); | ||
registerDerivations(schema); | ||
|
||
schema.defineSchema('address', { | ||
fields: [ | ||
{ | ||
name: 'street', | ||
type: null, | ||
kind: 'field', | ||
}, | ||
{ | ||
name: 'city', | ||
type: null, | ||
kind: 'field', | ||
}, | ||
{ | ||
name: 'state', | ||
type: null, | ||
kind: 'field', | ||
}, | ||
{ | ||
name: 'zip', | ||
type: null, | ||
kind: 'field', | ||
}, | ||
], | ||
}); | ||
|
||
schema.defineSchema('user', { | ||
fields: withFields([ | ||
{ | ||
name: 'favoriteNumbers', | ||
type: null, | ||
kind: 'array', | ||
}, | ||
{ | ||
name: 'addresses', | ||
type: 'address', | ||
kind: 'schema-array', | ||
}, | ||
]), | ||
}); | ||
const fieldsMap = schema.schemas.get('user')!.fields; | ||
const fields: FieldSchema[] = [...fieldsMap.values()]; | ||
|
||
const record = store.push({ | ||
data: { | ||
type: 'user', | ||
id: '1', | ||
attributes: { | ||
favoriteNumbers: ['1', '2'], | ||
addresses: [ | ||
{ street: '123 Main St', city: 'Anytown', state: 'NY', zip: '12345' }, | ||
{ street: '456 Elm St', city: 'Anytown', state: 'NY', zip: '12345' }, | ||
], | ||
}, | ||
}, | ||
}) as User; | ||
|
||
assert.strictEqual(record.id, '1', 'id is accessible'); | ||
assert.strictEqual(record.$type, 'user', '$type is accessible'); | ||
assert.deepEqual(record.favoriteNumbers, ['1', '2'], 'favoriteNumbers is accessible'); | ||
|
||
const { counters, fieldOrder } = await reactiveContext.call(this, record, fields); | ||
const favoriteNumbersIndex = fieldOrder.indexOf('favoriteNumbers'); | ||
|
||
assert.strictEqual(counters.id, 1, 'idCount is 1'); | ||
assert.strictEqual(counters.$type, 1, '$typeCount is 1'); | ||
assert.strictEqual(counters.favoriteNumbers, 1, 'favoriteNumbersCount is 1'); | ||
assert | ||
.dom(`li:nth-child(${favoriteNumbersIndex + 1})`) | ||
.hasText('favoriteNumbers: 1,2', 'favoriteNumbers is rendered'); | ||
|
||
// remote update | ||
store.push({ | ||
data: { | ||
type: 'user', | ||
id: '1', | ||
attributes: { favoriteNumbers: ['3', '4'] }, | ||
}, | ||
}); | ||
|
||
assert.strictEqual(record.id, '1', 'id is accessible'); | ||
assert.strictEqual(record.$type, 'user', '$type is accessible'); | ||
assert.deepEqual(record.favoriteNumbers, ['3', '4'], 'favoriteNumbers is accessible'); | ||
|
||
await rerender(); | ||
|
||
assert.strictEqual(counters.id, 1, 'idCount is 1'); | ||
assert.strictEqual(counters.$type, 1, '$typeCount is 1'); | ||
assert.strictEqual(counters.favoriteNumbers, 2, 'favoriteNumbersCount is 2'); | ||
|
||
assert | ||
.dom(`li:nth-child(${favoriteNumbersIndex + 1})`) | ||
.hasText('favoriteNumbers: 3,4', 'favoriteNumbers is rendered'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters