Skip to content

Add: async: true relationship tests for legacy mode #9910

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

Merged
Merged
Changes from 1 commit
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
Next Next commit
Add: async relationship tests for legacy mode
  • Loading branch information
MehulKChaudhari committed Apr 4, 2025
commit 023b3e6f3d9357d0ba2cb97dcc20ae7580ab614c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { module, test } from 'qunit';

import { setupTest } from 'ember-qunit';

import { PromiseBelongsTo, PromiseManyArray } from '@ember-data/model/-private';
import {
registerDerivations as registerLegacyDerivations,
withDefaults as withLegacy,
Expand Down Expand Up @@ -129,4 +130,132 @@ module('Legacy | Create | relationships', function (hooks) {
assert.strictEqual(Rey.friends[0], Matt, 'Rey has Matt as bestFriend');
assert.strictEqual(Matt.friends[0], Rey, 'Matt has Rey as bestFriend');
});

test('we can create with an async belongsTo', async function (assert) {
type User = {
id: string | null;
$type: 'user';
name: string;
bestFriend: User | null;
friends: User[];
[Type]: 'user';
};
const store = this.owner.lookup('service:store') as Store;
const { schema } = store;
registerLegacyDerivations(schema);

schema.registerResource(
withLegacy({
type: 'user',
fields: [
{
name: 'name',
type: null,
kind: 'attribute',
},
{
name: 'bestFriend',
type: 'user',
kind: 'belongsTo',
options: { async: true, inverse: 'bestFriend' },
},
],
})
);

const Matt = store.push<User>({
data: {
type: 'user',
id: '2',
attributes: {
name: 'Matt Seidel',
},
relationships: {
bestFriend: {
data: null,
},
},
},
});

const Rey = store.createRecord<User>('user', {
name: 'Rey Skybarker',
bestFriend: Matt,
});

assert.strictEqual(Rey.id, null, 'id is accessible');
assert.strictEqual(Rey.name, 'Rey Skybarker', 'name is accessible');
assert.true(Rey.bestFriend instanceof PromiseBelongsTo, 'Rey has an async bestFriend');

const ReyBestFriend = await Rey.bestFriend;
assert.strictEqual(ReyBestFriend, Matt, 'Rey has Matt as bestFriend');

const MattBestFriend = await Matt.bestFriend;
assert.strictEqual(MattBestFriend, Rey, 'Matt has Rey as bestFriend');
});

test('we can create with an async hasMany', async function (assert) {
type User = {
id: string | null;
$type: 'user';
name: string;
bestFriend: User | null;
friends: User[];
[Type]: 'user';
};
const store = this.owner.lookup('service:store') as Store;
const { schema } = store;
registerLegacyDerivations(schema);

schema.registerResource(
withLegacy({
type: 'user',
fields: [
{
name: 'name',
type: null,
kind: 'attribute',
},
{
name: 'friends',
type: 'user',
kind: 'hasMany',
options: { async: true, inverse: 'friends' },
},
],
})
);

const Matt = store.push<User>({
data: {
type: 'user',
id: '2',
attributes: {
name: 'Matt Seidel',
},
relationships: {
friends: {
data: [],
},
},
},
});

const Rey = store.createRecord<User>('user', {
name: 'Rey Skybarker',
friends: [Matt],
});

assert.strictEqual(Rey.id, null, 'id is accessible');
assert.strictEqual(Rey.name, 'Rey Skybarker', 'name is accessible');
assert.true(Rey.friends instanceof PromiseManyArray, 'Rey has async friends');

const ReyFriends = await Rey.friends;
assert.strictEqual(ReyFriends.length, 1, 'Rey has only one friend :(');
assert.strictEqual(ReyFriends[0], Matt, 'Rey has Matt as friend');

const MattFriends = await Matt.friends;
assert.strictEqual(MattFriends.length, 1, 'Matt has only one friend :(');
assert.strictEqual(MattFriends[0], Rey, 'Matt has Rey as friend');
});
});
Loading