Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: tests verify behavior mutating a record #9754

Merged
merged 4 commits into from
Mar 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ import { recordIdentifierFor } from '@ember-data/store';
import type { StableRecordIdentifier } from '@warp-drive/core-types';
import { Type } from '@warp-drive/core-types/symbols';
import type { SchemaRecord, Transformation } from '@warp-drive/schema-record';
import { registerDerivations, withDefaults } from '@warp-drive/schema-record';
import { Checkout, registerDerivations, withDefaults } from '@warp-drive/schema-record';

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

type EditableUser = {
readonly id: string;
readonly $type: 'user';
name: string;
age: number;
netWorth: number;
coolometer: number;
rank: number;
readonly [Type]: 'user';
};

interface User {
id: string | null;
$type: 'user';
Expand All @@ -18,6 +29,9 @@ interface User {
netWorth: number;
coolometer: number;
rank: number;
bestFriend?: User | null;
[Type]: 'user';
[Checkout](): Promise<EditableUser>;
}

module('Reads | basic fields', function (hooks) {
Expand Down Expand Up @@ -193,4 +207,47 @@ module('Reads | basic fields', function (hooks) {
assert.strictEqual(resource.attributes?.coolometer, '100.000', 'resource cache value for coolometer is correct');
assert.strictEqual(resource.attributes?.rank, '0', 'resource cache value for rank is correct');
});

test('Record is immutable without calling checkout', async function (assert) {
const store = this.owner.lookup('service:store') as Store;
const { schema } = store;
registerDerivations(schema);

schema.registerResource(
withDefaults({
type: 'user',
fields: [{ name: 'name', kind: 'field' }],
})
);

const immutableRecord = store.push<User>({
data: {
id: '1',
type: 'user',
attributes: {
name: 'Rey Skybarker',
},
},
});

assert.strictEqual(immutableRecord.id, '1', 'id is accessible');
assert.strictEqual(immutableRecord.name, 'Rey Skybarker', 'name is accessible');

assert.throws(() => {
immutableRecord.name = 'Gilfoyle';
}, /Error: Cannot set name on user because the record is not editable/);

// Verify address remains unchanged
assert.strictEqual(immutableRecord.name, 'Rey Skybarker', 'name remains unchanged after failed mutation attempt');

const editableRecord = await immutableRecord[Checkout]();
editableRecord.name = 'Gilfoyle';

assert.strictEqual(editableRecord.name, 'Gilfoyle', 'name can be mutated after checkout');

// Verify cache updates
const identifier = recordIdentifierFor(editableRecord);
const cachedResourceData = store.cache.peek(identifier);
assert.strictEqual(cachedResourceData?.attributes?.name, 'Gilfoyle', 'Cache reflects updated name after checkout');
});
});
Loading