Skip to content

Commit

Permalink
Add test that legacy schema records cannot access resource fields
Browse files Browse the repository at this point in the history
  • Loading branch information
gitKrystan committed Nov 10, 2023
1 parent c5b63d2 commit 8ec3207
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
1 change: 0 additions & 1 deletion packages/schema-record/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ export class SchemaRecord {
entangleSignal(signals, receiver, field.name);
return computeAttribute(cache, identifier, prop as string);
case 'resource':
// 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
65 changes: 65 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 @@ -248,4 +248,69 @@ module('Legacy Mode', function (hooks) {
);
}
});

test('records in legacy mode cannot access resources', 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: 'name',
type: null,
kind: 'attribute',
},
{
name: 'bestFriend',
type: 'user',
kind: 'resource',
options: { inverse: 'bestFriend', async: true },
},
],
});

const record = store.push({
data: {
type: 'user',
id: '1',
attributes: {
name: 'Chris',
},
relationships: {
bestFriend: {
data: { type: 'user', id: '2' },
},
},
},
included: [
{
type: 'user',
id: '2',
attributes: {
name: 'Rey',
},
relationships: {
bestFriend: {
data: { type: 'user', id: '1' },
},
},
},
],
}) as User;

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

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

0 comments on commit 8ec3207

Please sign in to comment.