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 test for hasMany + addObject + unloadRecord #5052

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
81 changes: 81 additions & 0 deletions tests/integration/relationships/has-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,87 @@ test("When a hasMany relationship is accessed, the adapter's findMany method sho
});
});

test("hasMany + canonical vs currentState + unloadRecord", function(assert) {
assert.expect(6);

let postData = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

userData seems better ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

type: 'user',
id: '1',
attributes: {
name: 'omg'
},
relationships: {
contacts: {
data: [
{
type: 'user',
id: 2
},
{
type: 'user',
id: 3
},
{
type: 'user',
id: 4
}
]
}
}
};

run(() => {
env.store.push({
data: postData,
included: [
{
type: 'user',
id: 2
},
{
type: 'user',
id: 3
},
{
type: 'user',
id: 4
}
]
});
});

let user = env.store.peekRecord('user', 1);
let contacts = user.get('contacts');

env.store.adapterFor('user').deleteRecord = function() {
return { data: { type: 'user', id: 2 } };
};

assert.deepEqual(contacts.map(c => c.get('id')), ['2','3','4'], 'user should have expected contacts');

run(() => {
contacts.addObject(env.store.createRecord('user', { id: 5 }));
contacts.addObject(env.store.createRecord('user', { id: 6 }));
contacts.addObject(env.store.createRecord('user', { id: 7 }));
});

assert.deepEqual(contacts.map(c => c.get('id')), ['2','3','4','5','6','7'], 'user should have expected contacts');

run(() => {
env.store.peekRecord('user', 2).unloadRecord();
env.store.peekRecord('user', 6).unloadRecord();
});

assert.deepEqual(contacts.map(c => c.get('id')), ['2','3','4','5','6','7'], `user's contacts should have expected contacts`);
assert.equal(contacts, user.get('contacts'));

run(() => {
contacts.addObject(env.store.createRecord('user', { id: 8 }));
});

assert.deepEqual(contacts.map(c => c.get('id')), ['2','3','4','5','6','7','8'], `user's contacts should have expected contacts`);
assert.equal(contacts, user.get('contacts'));
});
test("adapter.findMany only gets unique IDs even if duplicate IDs are present in the hasMany relationship", function(assert) {
assert.expect(2);

Expand Down
Loading