@@ -6,10 +6,21 @@ import { recordIdentifierFor } from '@ember-data/store';
6
6
import type { StableRecordIdentifier } from '@warp-drive/core-types' ;
7
7
import { Type } from '@warp-drive/core-types/symbols' ;
8
8
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' ;
10
10
11
11
import type Store from 'warp-drive__schema-record/services/store' ;
12
12
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
+
13
24
interface User {
14
25
id : string | null ;
15
26
$type : 'user' ;
@@ -18,7 +29,9 @@ interface User {
18
29
netWorth : number ;
19
30
coolometer : number ;
20
31
rank : number ;
21
- bestFriend ?: User ;
32
+ bestFriend ?: User | null ;
33
+ [ Type ] : 'user' ;
34
+ [ Checkout ] ( ) : Promise < EditableUser > ;
22
35
}
23
36
24
37
module ( 'Reads | basic fields' , function ( hooks ) {
@@ -195,7 +208,7 @@ module('Reads | basic fields', function (hooks) {
195
208
assert . strictEqual ( resource . attributes ?. rank , '0' , 'resource cache value for rank is correct' ) ;
196
209
} ) ;
197
210
198
- test ( 'user record is immutable without calling checkout' , function ( assert ) {
211
+ test ( 'Record is immutable without calling checkout' , async function ( assert ) {
199
212
const store = this . owner . lookup ( 'service:store' ) as Store ;
200
213
const { schema } = store ;
201
214
registerDerivations ( schema ) ;
@@ -207,11 +220,46 @@ module('Reads | basic fields', function (hooks) {
207
220
} )
208
221
) ;
209
222
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' ) ;
211
235
212
- assert . ok ( record , 'record is created' ) ;
213
236
assert . throws ( ( ) => {
214
- record . name = 'Rey Skywalker ' ;
237
+ immutableRecord . name = 'Gilfoyle ' ;
215
238
} , / E r r o r : C a n n o t s e t n a m e o n u s e r b e c a u s e t h e r e c o r d i s n o t e d i t a b l e / ) ;
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
+ ) ;
216
264
} ) ;
217
265
} ) ;
0 commit comments