@@ -178,6 +178,145 @@ module('Legacy Mode', function (hooks) {
178
178
assert . strictEqual ( record . constructor . name , 'Record<user>' , 'it has a useful constructor name' ) ;
179
179
} ) ;
180
180
181
+ test ( 'records not in legacy mode can access values with type "field"' , function ( assert ) {
182
+ const store = this . owner . lookup ( 'service:store' ) as Store ;
183
+ const { schema } = store ;
184
+ registerDerivations ( schema ) ;
185
+
186
+ schema . registerResource (
187
+ withDefaults ( {
188
+ type : 'user' ,
189
+ fields : [
190
+ {
191
+ name : 'name' ,
192
+ kind : 'field' ,
193
+ } ,
194
+ ] ,
195
+ } )
196
+ ) ;
197
+
198
+ const record = store . push < User > ( {
199
+ data : {
200
+ type : 'user' ,
201
+ id : '1' ,
202
+ attributes : { name : 'Rey Pupatine' } ,
203
+ } ,
204
+ } ) ;
205
+
206
+ assert . false ( record [ Legacy ] , 'record is NOT in legacy mode' ) ;
207
+ assert . strictEqual ( record . $type , 'user' , '$type is accessible' ) ;
208
+
209
+ assert . strictEqual ( record . name , 'Rey Pupatine' , 'can access name field' ) ;
210
+ } ) ;
211
+
212
+ test ( 'records in legacy mode cannot access values with type "field"' , function ( assert ) {
213
+ const store = this . owner . lookup ( 'service:store' ) as Store ;
214
+ const { schema } = store ;
215
+ registerLegacyDerivations ( schema ) ;
216
+
217
+ schema . registerResource (
218
+ withLegacyFields ( {
219
+ type : 'user' ,
220
+ fields : [
221
+ {
222
+ name : 'name' ,
223
+ kind : 'field' ,
224
+ } ,
225
+ ] ,
226
+ } )
227
+ ) ;
228
+
229
+ const record = store . push < User > ( {
230
+ data : {
231
+ type : 'user' ,
232
+ id : '1' ,
233
+ attributes : { name : 'Rey Pupatine' } ,
234
+ } ,
235
+ } ) ;
236
+
237
+ assert . true ( record [ Legacy ] , 'record is in legacy mode' ) ;
238
+
239
+ try {
240
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
241
+ record . name ;
242
+ assert . ok ( false , 'record.name should throw' ) ;
243
+ } catch ( e ) {
244
+ assert . strictEqual (
245
+ ( e as Error ) . message ,
246
+ "SchemaRecord.name is not available in legacy mode because it has type 'field'" ,
247
+ 'record.name throws'
248
+ ) ;
249
+ }
250
+ } ) ;
251
+
252
+ test ( 'records in legacy mode cannot access resources' , function ( assert ) {
253
+ const store = this . owner . lookup ( 'service:store' ) as Store ;
254
+ const { schema } = store ;
255
+ registerLegacyDerivations ( schema ) ;
256
+
257
+ schema . registerResource (
258
+ withLegacyFields ( {
259
+ type : 'user' ,
260
+ fields : [
261
+ {
262
+ name : 'name' ,
263
+ type : null ,
264
+ kind : 'attribute' ,
265
+ } ,
266
+ {
267
+ name : 'bestFriend' ,
268
+ type : 'user' ,
269
+ kind : 'resource' ,
270
+ options : { inverse : 'bestFriend' , async : true } ,
271
+ } ,
272
+ ] ,
273
+ } )
274
+ ) ;
275
+
276
+ const record = store . push < User > ( {
277
+ data : {
278
+ type : 'user' ,
279
+ id : '1' ,
280
+ attributes : {
281
+ name : 'Chris' ,
282
+ } ,
283
+ relationships : {
284
+ bestFriend : {
285
+ data : { type : 'user' , id : '2' } ,
286
+ } ,
287
+ } ,
288
+ } ,
289
+ included : [
290
+ {
291
+ type : 'user' ,
292
+ id : '2' ,
293
+ attributes : {
294
+ name : 'Rey' ,
295
+ } ,
296
+ relationships : {
297
+ bestFriend : {
298
+ data : { type : 'user' , id : '1' } ,
299
+ } ,
300
+ } ,
301
+ } ,
302
+ ] ,
303
+ } ) ;
304
+
305
+ assert . true ( record [ Legacy ] , 'record is in legacy mode' ) ;
306
+
307
+ try {
308
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
309
+ record . bestFriend ;
310
+ assert . ok ( false , 'record.bestFriend should throw' ) ;
311
+ } catch ( e ) {
312
+ assert . strictEqual (
313
+ ( e as Error ) . message ,
314
+ "SchemaRecord.bestFriend is not available in legacy mode because it has type 'resource'" ,
315
+ 'record.bestFriend throws'
316
+ ) ;
317
+ }
318
+ } ) ;
319
+
181
320
test ( 'we can access errors' , function ( assert ) {
182
321
const store = this . owner . lookup ( 'service:store' ) as Store ;
183
322
const { schema } = store ;
0 commit comments