Skip to content

Commit

Permalink
Additional test coverage for async belongsTo mutation (#5584)
Browse files Browse the repository at this point in the history
* Added failing test for #5575
* add integration and acceptance tests for editing an async belongsTo
  • Loading branch information
jlami authored and runspired committed Aug 29, 2018
1 parent bee48f0 commit baf2c3a
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 0 deletions.
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

0 comments on commit baf2c3a

Please sign in to comment.