Skip to content

Commit

Permalink
Add test that legacy schema records cannot access derived properties
Browse files Browse the repository at this point in the history
  • Loading branch information
gitKrystan committed Nov 10, 2023
1 parent 484cf41 commit c5b63d2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 0 additions & 1 deletion packages/schema-record/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ export class SchemaRecord {
return computeResource(store, cache, target, identifier, field, prop as string);

case 'derived':
// FIXME: Needs test for assertion
assert(
`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`,
!target[Legacy]
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-record/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FieldSchema } from '@ember-data/store/-types/q/schema-service';
import { createCache, getValue } from '@ember-data/tracking';
import { createSignal, type Signal, Signals } from '@ember-data/tracking/-private';
import { type Signal, Signals } from '@ember-data/tracking/-private';
import type { StableRecordIdentifier } from '@warp-drive/core-types';
import type { Value } from '@warp-drive/core-types/json/raw';
import type { AttributeSchema, RelationshipSchema } from '@warp-drive/core-types/schema';
Expand Down
53 changes: 53 additions & 0 deletions tests/warp-drive__schema-record/tests/legacy/mode-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface User {
netWorth: number;
coolometer: number;
rank: number;
fullName: string;
}

module('Legacy Mode', function (hooks) {
Expand Down Expand Up @@ -195,4 +196,56 @@ module('Legacy Mode', function (hooks) {
);
}
});

test('records in legacy mode cannot access derivations', function (assert) {
const store = this.owner.lookup('service:store') as Store;
const schema = new SchemaService();
store.registerSchema(schema);

schema.defineSchema('user', {
legacy: true,
fields: [
{
name: 'firstName',
type: null,
kind: 'attribute',
},
{
name: 'lastName',
type: null,
kind: 'attribute',
},
{
name: 'fullName',
type: 'concat',
options: { fields: ['firstName', 'lastName'], separator: ' ' },
kind: 'derived',
},
],
});

const record = store.push({
data: {
type: 'user',
id: '1',
attributes: {
firstName: 'Rey',
lastName: 'Pupatine',
},
},
}) as User;

assert.true(record[Legacy], 'record is in legacy mode');

try {
record.fullName;
assert.ok(false, 'record.fullName should throw');
} catch (e) {
assert.strictEqual(
(e as Error).message,
"Assertion Failed: SchemaRecord.fullName is not available in legacy mode because it has type 'derived'",
'record.fullName throws'
);
}
});
});

0 comments on commit c5b63d2

Please sign in to comment.