Skip to content

Commit

Permalink
add test from #6600
Browse files Browse the repository at this point in the history
  • Loading branch information
snewcomer committed Apr 24, 2021
1 parent fbf3778 commit 73583ec
Showing 1 changed file with 102 additions and 1 deletion.
103 changes: 102 additions & 1 deletion packages/-ember-data/tests/integration/records/delete-record-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { setupTest } from 'ember-qunit';

import Adapter from '@ember-data/adapter';
import { InvalidError } from '@ember-data/adapter/error';
import Model, { attr, hasMany } from '@ember-data/model';
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import JSONAPISerializer from '@ember-data/serializer/json-api';

module('integration/deletedRecord - Deleting Records', function (hooks) {
Expand Down Expand Up @@ -352,4 +352,105 @@ module('integration/deletedRecord - Deleting Records', function (hooks) {
record.unloadRecord();
await settled();
});

test('Records with an async hasMany can be pushed again after they were destroyed on client side', async function (assert) {
let group;
let employee;

class Company extends Model {
@attr('string') name;
toString() {
return 'Company';
}
}
class Group extends Model {
@belongsTo('company', { async: true }) company;
@hasMany('employee', { inverse: 'groups', async: true }) employees;
toString() {
return 'Group';
}
}
class Employee extends Model {
@hasMany('group', { inverse: 'employees', async: true }) groups;
@attr('string') name;
}

this.owner.register('model:company', Company);
this.owner.register('model:group', Group);
this.owner.register('model:employee', Employee);

let store = this.owner.lookup('service:store');
let adapter = store.adapterFor('application');

adapter.deleteRecord = function () {
return EmberPromise.resolve();
};

// Push the company as a long-lived record that will be referenced by the group
store.push({
data: {
type: 'company',
id: '1',
attributes: {
name: 'Inc.',
},
},
});

const jsonEmployee = {
data: {
type: 'employee',
id: '1',
attributes: {
name: 'Adam Sunderland',
},
relationships: {
groups: {
data: [{ type: 'group', id: '1' }],
},
},
},
};

const jsonGroup = {
data: {
type: 'group',
id: '1',
relationships: {
company: {
data: {
id: '1',
type: 'company',
},
},
},
},
};

// Server push with the group and employee
store.push(jsonEmployee);
store.push(jsonGroup);

group = store.peekRecord('group', '1');

// Sanity Check
assert.ok(group, 'expected group to be found');
assert.equal(group.get('company.name'), 'Inc.', 'group belongs to our company');
assert.equal(group.get('employees.length'), 1, 'expected 1 related record before delete');
employee = group.get('employees').objectAt(0);
assert.equal(employee.get('name'), 'Adam Sunderland', 'expected related records to be loaded');

await group.destroyRecord();
await employee.destroyRecord();

assert.equal(store.peekAll('employee').get('length'), 0, 'no employee record loaded');
assert.equal(store.peekAll('group').get('length'), 0, 'no group record loaded');

// Server pushes the same group and employee once more after they have been destroyed client-side. (The company is a long-lived record)
store.push(jsonEmployee);
store.push(jsonGroup);

group = store.peekRecord('group', '1');
assert.equal(group.get('employees.length'), 1, 'expected 1 related record after delete and restore');
});
});

0 comments on commit 73583ec

Please sign in to comment.