Skip to content

Commit

Permalink
test: Update tests to use hasMany relationship and add deprecatedTests
Browse files Browse the repository at this point in the history
  • Loading branch information
rossketron committed Dec 1, 2022
1 parent 3e59136 commit 2973188
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 25 deletions.
89 changes: 64 additions & 25 deletions tests/main/tests/integration/record-array-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,12 @@ module('unit/record-array - RecordArray', function (hooks) {
assert.true(people.isLoaded, 'The array is now loaded');
});

test('a record array should only remove object(s) if found in collection', async function (assert) {
test('a hasMany record array should only remove object(s) if found in collection', async function (assert) {
class AsyncTag extends Model {
@hasMany('person', { async: true, inverse: 'tag' })
people;
}

store.push({
data: [
{
Expand All @@ -415,52 +420,86 @@ module('unit/record-array - RecordArray', function (hooks) {
type: 'person',
id: '2',
attributes: {
name: 'Scumbag Katz',
name: 'Scumbag Tom',
},
},
{
type: 'person',
id: '3',
attributes: {
name: 'Scumbag Bryn',
name: 'Scumbag Ross',
},
},
{
type: 'tag',
id: '1',
relationships: {
people: {
data: [
{ type: 'person', id: '1' },
{ type: 'person', id: '2' },
],
},
},
},
],
});

const personNotInData = {
type: 'person',
id: '5',
attributes: {
name: 'Scumbag Ross',
},
};

let recordArray = store.peekAll('person');
let tag = await store.findRecord('tag', '1');
let recordArray = tag.people;
let scumbagInRecordArray = await store.findRecord('person', '1');
let scumbagNotInRecordArray = await store.findRecord('person', '3');

assert.strictEqual(recordArray.at(2).id, '3', 'should retrieve correct record at index 2');
assert.strictEqual(recordArray.at(1).id, '2', 'should retrieve correct record at index 1');
assert.strictEqual(recordArray.at(0).id, '1', 'should retrieve correct record at index 0');

recordArray.removeObject(personNotInData);
recordArray.removeObject(scumbagNotInRecordArray);

assert.strictEqual(
recordArray.length,
2,
'sync record array unchanged after attempting to remove object not found in collection'
);

recordArray.removeObject(scumbagInRecordArray);

let didRemoveObject = recordArray.length === 1 && !recordArray.includes(scumbagInRecordArray);
assert.true(didRemoveObject, 'sync record array successfully removed expected object from collection');

recordArray.push(scumbagInRecordArray);

let scumbagsToRemove = [scumbagInRecordArray, scumbagNotInRecordArray];
recordArray.removeObjects(scumbagsToRemove);

didRemoveObject = recordArray.length === 1 && !recordArray.includes(scumbagInRecordArray);
assert.true(didRemoveObject, 'sync record array only removes objects in list that are found in collection');

recordArray.push(scumbagInRecordArray);
this.owner.unregister('model:tag');
this.owner.register('model:tag', AsyncTag);

assert.strictEqual(recordArray.at(2).id, '3', 'should retrieve correct record at index 2');
assert.strictEqual(recordArray.at(1).id, '2', 'should retrieve correct record at index 1');
assert.strictEqual(recordArray.at(0).id, '1', 'should retrieve correct record at index 0');

let personInData = recordArray.at(0);
recordArray.removeObject(personInData);
recordArray.removeObject(scumbagNotInRecordArray);

assert.strictEqual(
recordArray.length,
2,
'async record array unchanged after attempting to remove object not found in collection'
);

recordArray.removeObject(scumbagInRecordArray);

assert.notOk(recordArray.includes(personInData), 'expected record was removed from record array');
assert.strictEqual(recordArray.at(1).id, '3', 'should retrieve correct record at index 1');
assert.strictEqual(recordArray.at(0).id, '2', 'should retrieve correct record at index 0');
didRemoveObject = recordArray.length === 1 && !recordArray.includes(scumbagInRecordArray);
assert.true(didRemoveObject, 'async record array successfully removed expected object from collection');

personInData = recordArray.at(1);
const peopleToRemove = [personInData, personNotInData];
recordArray.push(scumbagInRecordArray);

recordArray.removeObjects(peopleToRemove);
scumbagsToRemove = [scumbagInRecordArray, scumbagNotInRecordArray];
recordArray.removeObjects(scumbagsToRemove);

assert.notOk(recordArray.includes(personInData), 'expected record was removed from record array');
assert.strictEqual(recordArray.at(0).id, '2', 'should retrieve correct record at index 0');
didRemoveObject = recordArray.length === 1 && !recordArray.includes(scumbagInRecordArray);
assert.true(didRemoveObject, 'async record array only removes objects in list that are found in collection');
});
});
80 changes: 80 additions & 0 deletions tests/main/tests/integration/references/has-many-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1054,4 +1054,84 @@ module('integration/references/has-many', function (hooks) {
assert.strictEqual(pets.length, 2);
});
});

deprecatedTest(
'removeObject(promise)',
{ id: 'ember-data:deprecate-promise-proxies', until: '5.0', count: 1 },
async function (assert) {
const store = this.owner.lookup('service:store');
const deferred = defer();

const family = store.push({
data: {
type: 'family',
id: '1',
relationships: {
persons: {
data: [
{ type: 'person', id: '1' },
{ type: 'person', id: '2' },
],
},
},
},
});
const personsReference = family.hasMany('persons');
let removeObjectResult = personsReference.removeObject(deferred.promise);

assert.ok(removeObjectResult.then, 'HasManyReference.removeObject returns a promise');

const payload = {
data: [{ data: { type: 'person', id: '1', attributes: { name: 'Vito' } } }],
};

deferred.resolve(payload);

const records = await removeObjectResult;
assert.strictEqual(get(records, 'length'), 1);
assert.strictEqual(records.at(0).name, 'Michael');
}
);

deprecatedTest(
'removeObjects(promise)',
{ id: 'ember-data:deprecate-promise-proxies', until: '5.0', count: 1 },
async function (assert) {
const store = this.owner.lookup('service:store');
const deferred = defer();

const family = store.push({
data: {
type: 'family',
id: '1',
relationships: {
persons: {
data: [
{ type: 'person', id: '1' },
{ type: 'person', id: '2' },
],
},
},
},
});
const personsReference = family.hasMany('persons');
let removeObjectsResult = personsReference.removeObjects(deferred.promise);

assert.ok(removeObjectsResult.then, 'HasManyReference.removeObjects returns a promise');

const payload = {
data: [
{
data: [{ type: 'person', id: '2', attributes: { name: 'Michael' } }],
},
],
};

deferred.resolve(payload);

const records = await removeObjectsResult;
assert.strictEqual(get(records, 'length'), 1);
assert.strictEqual(records.at(0).name, 'Michael');
}
);
});

0 comments on commit 2973188

Please sign in to comment.