Skip to content

Commit 488b6b5

Browse files
fix: test
1 parent 43428bd commit 488b6b5

File tree

1 file changed

+54
-6
lines changed

1 file changed

+54
-6
lines changed

tests/warp-drive__schema-record/tests/polaris/basic-fields-field-read-test.ts

+54-6
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,21 @@ import { recordIdentifierFor } from '@ember-data/store';
66
import type { StableRecordIdentifier } from '@warp-drive/core-types';
77
import { Type } from '@warp-drive/core-types/symbols';
88
import type { SchemaRecord, Transformation } from '@warp-drive/schema-record';
9-
import { registerDerivations, withDefaults } from '@warp-drive/schema-record';
9+
import { Checkout, registerDerivations, withDefaults } from '@warp-drive/schema-record';
1010

1111
import type Store from 'warp-drive__schema-record/services/store';
1212

13+
type EditableUser = {
14+
readonly id: string;
15+
readonly $type: 'user';
16+
name: string;
17+
age: number;
18+
netWorth: number;
19+
coolometer: number;
20+
rank: number;
21+
readonly [Type]: 'user';
22+
};
23+
1324
interface User {
1425
id: string | null;
1526
$type: 'user';
@@ -18,7 +29,9 @@ interface User {
1829
netWorth: number;
1930
coolometer: number;
2031
rank: number;
21-
bestFriend?: User;
32+
bestFriend?: User | null;
33+
[Type]: 'user';
34+
[Checkout](): Promise<EditableUser>;
2235
}
2336

2437
module('Reads | basic fields', function (hooks) {
@@ -195,7 +208,7 @@ module('Reads | basic fields', function (hooks) {
195208
assert.strictEqual(resource.attributes?.rank, '0', 'resource cache value for rank is correct');
196209
});
197210

198-
test('user record is immutable without calling checkout', function (assert) {
211+
test('Record is immutable without calling checkout', async function (assert) {
199212
const store = this.owner.lookup('service:store') as Store;
200213
const { schema } = store;
201214
registerDerivations(schema);
@@ -207,11 +220,46 @@ module('Reads | basic fields', function (hooks) {
207220
})
208221
);
209222

210-
const record = store.createRecord('user', { name: 'Rey Skybarker' }) as User;
223+
const immutableRecord = store.push<User>({
224+
data: {
225+
id: '1',
226+
type: 'user',
227+
attributes: {
228+
name: 'Rey Skybarker'
229+
}
230+
}
231+
});
232+
233+
assert.strictEqual(immutableRecord.id, '1', 'id is accessible');
234+
assert.strictEqual(immutableRecord.name, 'Rey Skybarker', 'name is accessible');
211235

212-
assert.ok(record, 'record is created');
213236
assert.throws(() => {
214-
record.name = 'Rey Skywalker';
237+
immutableRecord.name = 'Gilfoyle';
215238
}, /Error: Cannot set name on user because the record is not editable/);
239+
240+
// Verify address remains unchanged
241+
assert.strictEqual(
242+
immutableRecord.name,
243+
"Rey Skybarker",
244+
'name remains unchanged after failed mutation attempt'
245+
);
246+
247+
const editableRecord = await immutableRecord[Checkout]();
248+
editableRecord.name = 'Gilfoyle';
249+
250+
assert.strictEqual(
251+
editableRecord.name,
252+
"Gilfoyle",
253+
'name can be mutated after checkout'
254+
);
255+
256+
// Verify cache updates
257+
const identifier = recordIdentifierFor(editableRecord);
258+
const cachedResourceData = store.cache.peek(identifier);
259+
assert.strictEqual(
260+
cachedResourceData?.attributes?.name,
261+
"Gilfoyle",
262+
'Cache reflects updated name after checkout'
263+
);
216264
});
217265
});

0 commit comments

Comments
 (0)