Skip to content

Commit

Permalink
add test + fix for chained async has many (#7691)
Browse files Browse the repository at this point in the history
* [bugfix]: fix for chained async has many

* add fix and update tests

* remove console.logs

* make work with flags off

* fix test for lts

Co-authored-by: Chris Thoburn <[email protected]>
  • Loading branch information
sly7-7 and runspired authored Dec 15, 2021
1 parent 828742a commit 6ffdc85
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module('tracking state flags on a record', function (hooks) {
enumerable: true,
configurable: true,
get() {
tag.reg; // subscribe
tag.rev; // subscribe
if (_isDirty && !_isUpdating) {
_isUpdating = true;
resolve(desc.get.call(this)).then((v) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,14 +602,16 @@ module('integration/records/relationship-changes - Relationship changes', functi
});

let person = store.peekRecord('person', 'wat');
let siblings = person.get('siblings');
let siblingsPromise = person.siblings;

await siblingsPromise;

// flush initial state since
// nothing is consuming us.
// else the test will fail because we will
// (correctly) not notify the array observer
// as there is still a pending notification
siblings.length;
siblingsPromise.length;

let observer = {
arrayWillChange(array, start, removing, adding) {
Expand All @@ -627,7 +629,7 @@ module('integration/records/relationship-changes - Relationship changes', functi
},
};

siblings.addArrayObserver(observer);
siblingsPromise.addArrayObserver(observer);

store.push({
data: {
Expand All @@ -646,7 +648,7 @@ module('integration/records/relationship-changes - Relationship changes', functi
assert.strictEqual(willChangeCount, 1, 'willChange observer should be triggered once');
assert.strictEqual(didChangeCount, 1, 'didChange observer should be triggered once');

siblings.removeArrayObserver(observer);
siblingsPromise.removeArrayObserver(observer);
},
{ id: 'array-observers', count: 2, when: { ember: '>=3.26.0' } }
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ module('integration/relationships/has_many - Has-Many Relationships', function (
});
});

test('A hasMany relationship can be reloaded if it was fetched via a link', function (assert) {
test('A hasMany relationship can be reloaded if it was fetched via a link', async function (assert) {
let store = this.owner.lookup('service:store');
let adapter = store.adapterFor('application');

Expand Down Expand Up @@ -889,35 +889,28 @@ module('integration/relationships/has_many - Has-Many Relationships', function (
});
};

run(function () {
run(store, 'findRecord', 'post', 1)
.then(function (post) {
return post.get('comments');
})
.then(function (comments) {
assert.true(comments.get('isLoaded'), 'comments are loaded');
assert.strictEqual(comments.get('length'), 2, 'comments have 2 length');
let post = await store.findRecord('post', 1);
let comments = await post.comments;
assert.true(comments.get('isLoaded'), 'comments are loaded');
assert.strictEqual(comments.get('length'), 2, 'comments have 2 length');

adapter.findHasMany = function (store, snapshot, link, relationship) {
assert.strictEqual(relationship.type, 'comment', 'findHasMany relationship type was Comment');
assert.strictEqual(relationship.key, 'comments', 'findHasMany relationship key was comments');
assert.strictEqual(link, '/posts/1/comments', 'findHasMany link was /posts/1/comments');
adapter.findHasMany = function (store, snapshot, link, relationship) {
assert.strictEqual(relationship.type, 'comment', 'findHasMany relationship type was Comment');
assert.strictEqual(relationship.key, 'comments', 'findHasMany relationship key was comments');
assert.strictEqual(link, '/posts/1/comments', 'findHasMany link was /posts/1/comments');

return resolve({
data: [
{ id: 1, type: 'comment', attributes: { body: 'First' } },
{ id: 2, type: 'comment', attributes: { body: 'Second' } },
{ id: 3, type: 'comment', attributes: { body: 'Thirds' } },
],
});
};
return resolve({
data: [
{ id: 1, type: 'comment', attributes: { body: 'First' } },
{ id: 2, type: 'comment', attributes: { body: 'Second' } },
{ id: 3, type: 'comment', attributes: { body: 'Thirds' } },
],
});
};

return comments.reload();
})
.then(function (newComments) {
assert.strictEqual(newComments.get('length'), 3, 'reloaded comments have 3 length');
});
});
await comments.reload();

assert.strictEqual(comments.length, 3, 'reloaded comments have 3 length');
});

test('A sync hasMany relationship can be reloaded if it was fetched via ids', function (assert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,7 @@ module('integration/relationships/one_to_many_test - OneToMany relationships', f
assert.strictEqual(account.get('user'), null, 'Account does not have the user anymore');
});

test('createRecord updates inverse record array which has observers', function (assert) {
test('createRecord updates inverse record array which has observers', async function (assert) {
let store = this.owner.lookup('service:store');
let adapter = store.adapterFor('application');

Expand All @@ -1642,21 +1642,26 @@ module('integration/relationships/one_to_many_test - OneToMany relationships', f
};
};

return store.findAll('user').then((users) => {
assert.strictEqual(users.get('length'), 1, 'Exactly 1 user');
const users = await store.findAll('user');
assert.strictEqual(users.get('length'), 1, 'Exactly 1 user');

let user = users.get('firstObject');
assert.strictEqual(user.get('messages.length'), 0, 'Record array is initially empty');
let user = users.get('firstObject');
assert.strictEqual(user.get('messages.length'), 0, 'Record array is initially empty');

// set up an observer
user.addObserver('[email protected]', () => {});
user.get('messages.firstObject');
// set up an observer
user.addObserver('[email protected]', () => {});
user.get('messages.firstObject');

let message = store.createRecord('message', { user, title: 'EmberFest was great' });
assert.strictEqual(user.get('messages.length'), 1, 'The message is added to the record array');
const messages = await user.messages;

let messageFromArray = user.get('messages.firstObject');
assert.ok(message === messageFromArray, 'Only one message record instance should be created');
});
assert.strictEqual(messages.length, 0, 'we have no messages');
assert.strictEqual(user.messages.length, 0, 'we have no messages');

let message = store.createRecord('message', { user, title: 'EmberFest was great' });
assert.strictEqual(messages.length, 1, 'The message is added to the record array');
assert.strictEqual(user.messages.length, 1, 'The message is added to the record array');

let messageFromArray = user.messages.firstObject;
assert.true(message === messageFromArray, 'Only one message record instance should be created');
});
});
Loading

0 comments on commit 6ffdc85

Please sign in to comment.