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

Additional test coverage for async belongsTo mutation #5584

Merged
merged 5 commits into from
Aug 29, 2018
Merged
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
103 changes: 103 additions & 0 deletions tests/acceptance/relationships/belongs-to-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class Person extends Model {
children;
@belongsTo('person', { async: true, inverse: 'children' })
parent;
@belongsTo('pet', { inverse: 'bestHuman', async: true })
bestDog;
}

class Pet extends Model {
@belongsTo('person', { inverse: 'bestDog', async: false })
bestHuman;
@attr
name;
}

class TestAdapter extends JSONAPIAdapter {
Expand Down Expand Up @@ -181,6 +190,100 @@ module('async belongs-to rendering tests', function(hooks) {
adapter = store.adapterFor('application');
});

module('for local changes', function(hooks) {
hooks.beforeEach(function() {
let { owner } = this;
owner.register('model:person', Person);
owner.register('model:pet', Pet);
});

test('async belongsTo returns correct new value after a local change', async function(assert) {
let chris = store.push({
data: {
type: 'person',
id: '1',
attributes: { name: 'Chris' },
relationships: {
bestDog: {
data: null,
},
},
},
included: [
{
type: 'pet',
id: '1',
attributes: { name: 'Shen' },
relationships: {
bestHuman: {
data: null,
},
},
},
{
type: 'pet',
id: '2',
attributes: { name: 'Pirate' },
relationships: {
bestHuman: {
data: null,
},
},
},
],
});

let shen = store.peekRecord('pet', '1');
let pirate = store.peekRecord('pet', '2');
let bestDog = await chris.get('bestDog');

this.set('chris', chris);

await render(hbs`
<p>{{chris.bestDog.name}}</p>
`);
await settled();

assert.equal(this.element.textContent.trim(), '');
assert.ok(shen.get('bestHuman') === null, 'precond - Shen has no best human');
assert.ok(pirate.get('bestHuman') === null, 'precond - pirate has no best human');
assert.ok(bestDog === null, 'precond - Chris has no best dog');

chris.set('bestDog', shen);
bestDog = await chris.get('bestDog');
await settled();

assert.equal(this.element.textContent.trim(), 'Shen');
assert.ok(shen.get('bestHuman') === chris, "scene 1 - Chris is Shen's best human");
assert.ok(pirate.get('bestHuman') === null, 'scene 1 - pirate has no best human');
assert.ok(bestDog === shen, "scene 1 - Shen is Chris's best dog");

chris.set('bestDog', pirate);
bestDog = await chris.get('bestDog');
await settled();

assert.equal(this.element.textContent.trim(), 'Pirate');
assert.ok(shen.get('bestHuman') === null, "scene 2 - Chris is no longer Shen's best human");
assert.ok(pirate.get('bestHuman') === chris, 'scene 2 - pirate now has Chris as best human');
assert.ok(bestDog === pirate, "scene 2 - Pirate is now Chris's best dog");

chris.set('bestDog', null);
bestDog = await chris.get('bestDog');
await settled();

assert.equal(this.element.textContent.trim(), '');
assert.ok(
shen.get('bestHuman') === null,
"scene 3 - Chris remains no longer Shen's best human"
);
assert.ok(
pirate.get('bestHuman') === null,
'scene 3 - pirate no longer has Chris as best human'
);
assert.ok(bestDog === null, 'scene 3 - Chris has no best dog');
});
});

module('for data-no-link scenarios', function() {
test('We can render an async belongs-to', async function(assert) {
let people = makePeopleWithRelationshipData();
Expand Down
72 changes: 72 additions & 0 deletions tests/integration/relationships/belongs-to-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,78 @@ module('integration/relationship/belongs-to BelongsTo Relationships (new-style)'

assert.ok(personPet === pet, 'We ended up in the same state');
});

test('async belongsTo returns correct new value after a local change', async function(assert) {
let chris = store.push({
data: {
type: 'person',
id: '1',
attributes: { name: 'Chris' },
relationships: {
bestDog: {
data: null,
},
},
},
included: [
{
type: 'pet',
id: '1',
attributes: { name: 'Shen' },
relationships: {
bestHuman: {
data: null,
},
},
},
{
type: 'pet',
id: '2',
attributes: { name: 'Pirate' },
relationships: {
bestHuman: {
data: null,
},
},
},
],
});

let shen = store.peekRecord('pet', '1');
let pirate = store.peekRecord('pet', '2');
let bestDog = await chris.get('bestDog');

assert.ok(shen.get('bestHuman') === null, 'precond - Shen has no best human');
assert.ok(pirate.get('bestHuman') === null, 'precond - pirate has no best human');
assert.ok(bestDog === null, 'precond - Chris has no best dog');

chris.set('bestDog', shen);
bestDog = await chris.get('bestDog');

assert.ok(shen.get('bestHuman') === chris, "scene 1 - Chris is Shen's best human");
assert.ok(pirate.get('bestHuman') === null, 'scene 1 - pirate has no best human');
assert.ok(bestDog === shen, "scene 1 - Shen is Chris's best dog");

chris.set('bestDog', pirate);
bestDog = await chris.get('bestDog');

assert.ok(shen.get('bestHuman') === null, "scene 2 - Chris is no longer Shen's best human");
assert.ok(pirate.get('bestHuman') === chris, 'scene 2 - pirate now has Chris as best human');
assert.ok(bestDog === pirate, "scene 2 - Pirate is now Chris's best dog");

chris.set('bestDog', null);
bestDog = await chris.get('bestDog');

assert.ok(
shen.get('bestHuman') === null,
"scene 3 - Chris remains no longer Shen's best human"
);
assert.ok(
pirate.get('bestHuman') === null,
'scene 3 - pirate no longer has Chris as best human'
);
assert.ok(bestDog === null, 'scene 3 - Chris has no best dog');
});
});

module('integration/relationship/belongs_to Belongs-To Relationships', {
Expand Down