Skip to content

Commit

Permalink
fix most tests, 44 to go
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Aug 5, 2022
1 parent c3f3068 commit e99428a
Show file tree
Hide file tree
Showing 27 changed files with 281 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import JSONAPIAdapter from '@ember-data/adapter/json-api';
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import { LEGACY_SUPPORT } from '@ember-data/model/-private';
import JSONAPISerializer from '@ember-data/serializer/json-api';
import Store from '@ember-data/store';
import Store, { recordIdentifierFor } from '@ember-data/store';

import { implicitRelationshipsFor } from '../../helpers/accessors';

Expand Down Expand Up @@ -263,7 +263,7 @@ module('async belongs-to rendering tests', function (hooks) {
});

const storeWrapper = store._instanceCache._storeWrapper;
const identifier = pete._internalModel.identifier;
const identifier = recordIdentifierFor(pete);
const implicitRelationships = implicitRelationshipsFor(storeWrapper, identifier);
const implicitKeys = Object.keys(implicitRelationships);
const petOwnerImplicit = implicitRelationships[implicitKeys[0]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,7 @@ module('integration/adapter/find-all - Finding All Records of a Type', function
);

store.createRecord('person', { name: 'Carsten Nielsen' });
await settled();

await settled();
// await settled();

assert.strictEqual(allRecords.length, 1, "the record array's length is 1");
assert.strictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { setupTest } from 'ember-qunit';
import RESTAdapter from '@ember-data/adapter/rest';
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import RESTSerializer from '@ember-data/serializer/rest';
import { recordIdentifierFor } from '@ember-data/store';
import deepCopy from '@ember-data/unpublished-test-infra/test-support/deep-copy';
import testInDebug from '@ember-data/unpublished-test-infra/test-support/test-in-debug';

Expand Down Expand Up @@ -695,7 +696,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function (hooks) {
this.owner.register('model:comment', Comment);

let post = store.createRecord('post');
let internalModel = post._internalModel;
let identifier = recordIdentifierFor(post);

post.deleteRecord();
await post.save();
Expand All @@ -706,7 +707,8 @@ module('integration/adapter/rest_adapter - REST Adapter', function (hooks) {
assert.strictEqual(passedVerb, null, 'There is no ajax call to delete a record that has never been saved.');
assert.strictEqual(passedHash, null, 'There is no ajax call to delete a record that has never been saved.');

assert.true(internalModel.isEmpty, 'the post is now deleted');
const isLoaded = store._instanceCache.recordIsLoaded(identifier);
assert.false(isLoaded, 'the post is now deleted');
});

test('findAll - returning an array populates the array', async function (assert) {
Expand Down Expand Up @@ -2475,6 +2477,9 @@ module('integration/adapter/rest_adapter - REST Adapter', function (hooks) {
} catch (err) {
assert.strictEqual(err, errorThrown);
assert.ok(err, 'promise rejected');
if (err !== errorThrown) {
throw err;
}
}
});

Expand Down Expand Up @@ -2626,7 +2631,7 @@ module('integration/adapter/rest_adapter - REST Adapter', function (hooks) {
}
});

test('findAll resolves with a collection of Models, not DS.InternalModels', async function (assert) {
test('findAll resolves with a collection of Models, not Identifiers', async function (assert) {
class Post extends Model {
@attr name;
@hasMany('comment', { async: true, inverse: 'post' }) comments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ module('integration/adapter/rest_adapter - REST Adapter - createRecord', functio
],
});

store.push({
const post = store.push({
data: {
type: 'post',
id: '1',
Expand All @@ -458,7 +458,6 @@ module('integration/adapter/rest_adapter - REST Adapter - createRecord', functio
},
},
});

store.push({
data: {
type: 'comment',
Expand All @@ -474,19 +473,14 @@ module('integration/adapter/rest_adapter - REST Adapter - createRecord', functio
},
});

const post = store.peekRecord('post', 1);
const commentCount = post.get('comments.length');

assert.strictEqual(commentCount, 1, 'the post starts life with a comment');

let comment = store.createRecord('comment', { name: 'Another Comment', post: post });

await comment.save();

assert.strictEqual(comment.get('post'), post, 'the comment is related to the post');

await post.reload();

assert.strictEqual(post.get('comments.length'), 2, 'Post comment count has been updated');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ import RESTAdapter from '@ember-data/adapter/rest';
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import JSONAPISerializer from '@ember-data/serializer/json-api';
import RESTSerializer from '@ember-data/serializer/rest';
import { recordIdentifierFor } from '@ember-data/store';
import { Snapshot } from '@ember-data/store/-private';
import testInDebug from '@ember-data/unpublished-test-infra/test-support/test-in-debug';

function moveRecordOutOfInFlight(record) {
// move record out of the inflight state so the tests can clean up
// correctly
let { store, _internalModel } = record;
let { store } = record;
let identifier = recordIdentifierFor(record);

// TODO this would be made nicer by a cancellation API
let pending = store.getRequestStateService().getPendingRequestsForRecord(_internalModel.identifier);
let pending = store.getRequestStateService().getPendingRequestsForRecord(identifier);
pending.splice(0, pending.length);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ module('integration/record_array_manager', function (hooks) {
let adapterPopulated = manager.createAdapterPopulatedRecordArray('person', query);
let allSummary = tap(all, 'willDestroy');
let adapterPopulatedSummary = tap(adapterPopulated, 'willDestroy');
let internalPersonModel = person._internalModel;
let identifier = recordIdentifierFor(person);

assert.strictEqual(allSummary.called.length, 0, 'initial: no calls to all.willDestroy');
assert.strictEqual(adapterPopulatedSummary.called.length, 0, 'initial: no calls to adapterPopulated.willDestroy');
assert.strictEqual(
manager.getRecordArraysForIdentifier(internalPersonModel.identifier).size,
manager.getRecordArraysForIdentifier(identifier).size,
1,
'initial: expected the person to be a member of 1 recordArrays'
);
Expand All @@ -112,7 +112,7 @@ module('integration/record_array_manager', function (hooks) {
await settled();

assert.strictEqual(
manager.getRecordArraysForIdentifier(internalPersonModel.identifier).size,
manager.getRecordArraysForIdentifier(identifier).size,
0,
'expected the person to be a member of no recordArrays'
);
Expand All @@ -123,7 +123,7 @@ module('integration/record_array_manager', function (hooks) {
await settled();

assert.strictEqual(
manager.getRecordArraysForIdentifier(internalPersonModel.identifier).size,
manager.getRecordArraysForIdentifier(identifier).size,
0,
'expected the person to be a member of no recordArrays'
);
Expand Down Expand Up @@ -285,11 +285,11 @@ module('integration/record_array_manager', function (hooks) {
let createRecordArrayCalled = 0;
let superCreateRecordArray = manager.createRecordArray;

manager.createRecordArray = function (modelName, internalModels) {
manager.createRecordArray = function (modelName, identifiers) {
createRecordArrayCalled++;
assert.strictEqual(modelName, 'car');
assert.strictEqual(internalModels.length, 1);
assert.strictEqual(internalModels[0].id, '1');
assert.strictEqual(identifiers.length, 1);
assert.strictEqual(identifiers[0].id, '1');
return superCreateRecordArray.apply(this, arguments);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ module('integration/record-arrays/adapter_populated_record_array - AdapterPopula
});

test('pass record array to adapter.query regardless of arity', async function (assert) {
assert.expect(10);
let store = this.owner.lookup('service:store');
let adapter = store.adapterFor('application');

Expand All @@ -224,13 +225,13 @@ module('integration/record-arrays/adapter_populated_record_array - AdapterPopula

let superCreateAdapterPopulatedRecordArray = store.recordArrayManager.createAdapterPopulatedRecordArray;

store.recordArrayManager.createStore = function (modelName, query, internalModels, _payload) {
store.recordArrayManager.createAdapterPopulatedRecordArray = function (modelName, query, identifiers, _payload) {
assert.strictEqual(arguments.length, 4);

assert.strictEqual(modelName, 'person');
assert.strictEqual(query, actualQuery);
assert.strictEqual(_payload, payload);
assert.strictEqual(internalModels.length, 2);
assert.strictEqual(identifiers.length, 2);
return superCreateAdapterPopulatedRecordArray.apply(this, arguments);
};

Expand All @@ -247,7 +248,7 @@ module('integration/record-arrays/adapter_populated_record_array - AdapterPopula
return payload;
};

store.recordArrayManager.createStore = function (modelName, query) {
store.recordArrayManager.createAdapterPopulatedRecordArray = function (modelName, query) {
assert.strictEqual(arguments.length, 2);

assert.strictEqual(modelName, 'person');
Expand All @@ -258,7 +259,7 @@ module('integration/record-arrays/adapter_populated_record_array - AdapterPopula
store.query('person', actualQuery);
});

test('loadRecord re-syncs internalModels recordArrays', async function (assert) {
test('loadRecord re-syncs identifiers recordArrays', async function (assert) {
let store = this.owner.lookup('service:store');
let adapter = store.adapterFor('application');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ module('integration/record-data - Record Data State', function (hooks) {
return isDeletionCommitted;
}

setIsDeleted(identifier, isDeleted): void {}
setIsDeleted(): void {
isDeleted = true;
}
}

let TestStore = Store.extend({
Expand Down Expand Up @@ -235,6 +237,10 @@ module('integration/record-data - Record Data State', function (hooks) {
storeWrapper = sw;
}

isEmpty(): boolean {
return !isNew && isDeleted;
}

isNew(): boolean {
return isNew;
}
Expand All @@ -247,7 +253,8 @@ module('integration/record-data - Record Data State', function (hooks) {
return isDeletionCommitted;
}

setIsDeleted(identifier, isDeleted: boolean): void {
setIsDeleted(identifier, value: boolean): void {
isDeleted = true;
calledSetIsDeleted = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ module('integration/store-wrapper - RecordData StoreWrapper tests', function (ho
assert.expect(3);
let { owner } = this;
let count = 0;
let internalModel;
let recordData;
let newRecordData;

class RecordDataForTest extends TestRecordData {
id: string;
Expand All @@ -298,7 +298,7 @@ module('integration/store-wrapper - RecordData StoreWrapper tests', function (ho
if (count === 1) {
recordData = storeWrapper.recordDataFor('house');
} else if (count === 2) {
internalModel = store._instanceCache._internalModelForResource({ type: 'house', lid });
newRecordData = this;
}
}

Expand Down Expand Up @@ -336,15 +336,15 @@ module('integration/store-wrapper - RecordData StoreWrapper tests', function (ho

assert.ok(recordData._isNew, 'Our RecordData is new');
assert.ok(
internalModel.isNew(),
'The internalModel for a RecordData created via Wrapper.recordDataFor(type) is in the "new" state'
newRecordData.isNew(),
'The recordData for a RecordData created via Wrapper.recordDataFor(type) is in the "new" state'
);

assert.strictEqual(count, 2, 'two TestRecordDatas have been created');
});

test('setRecordId', async function (assert) {
assert.expect(1);
assert.expect(2);
let { owner } = this;

class RecordDataForTest extends TestRecordData {
Expand All @@ -371,8 +371,7 @@ module('integration/store-wrapper - RecordData StoreWrapper tests', function (ho
store = owner.lookup('service:store');

let house = store.createRecord('house');
// TODO there is a bug when setting id while creating the Record instance, preventing the id property lookup to work
// assert.strictEqual(house.get('id'), '17', 'setRecordId correctly set the id');
assert.strictEqual(house.get('id'), '17', 'setRecordId correctly set the id');
assert.strictEqual(
store.peekRecord('house', 17),
house,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Adapter from '@ember-data/adapter';
import { InvalidError } from '@ember-data/adapter/error';
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import JSONAPISerializer from '@ember-data/serializer/json-api';
import { recordIdentifierFor } from '@ember-data/store';

module('integration/deletedRecord - Deleting Records', function (hooks) {
setupTest(hooks);
Expand Down Expand Up @@ -202,11 +203,12 @@ module('integration/deletedRecord - Deleting Records', function (hooks) {
);
assert.strictEqual(get(store.peekAll('person'), 'length'), 1, 'The new person should be in the store');

let internalModel = record._internalModel;
let identifier = recordIdentifierFor(record);
let recordData = store._instanceCache.getRecordData(identifier);

record.deleteRecord();

assert.true(internalModel.isEmpty, 'new person state is empty');
assert.true(recordData.isEmpty(), 'new person state is empty');
assert.strictEqual(get(store.peekAll('person'), 'length'), 0, 'The new person should be removed from the store');
});

Expand Down Expand Up @@ -244,11 +246,12 @@ module('integration/deletedRecord - Deleting Records', function (hooks) {
);
assert.strictEqual(get(store.peekAll('person'), 'length'), 1, 'The new person should be in the store');

let internalModel = record._internalModel;
let identifier = recordIdentifierFor(record);
let recordData = store._instanceCache.getRecordData(identifier);

await record.destroyRecord();

assert.true(internalModel.isEmpty, 'new person state is empty');
assert.true(recordData.isEmpty(), 'new person state is empty');
assert.strictEqual(get(store.peekAll('person'), 'length'), 0, 'The new person should be removed from the store');
});

Expand Down Expand Up @@ -299,17 +302,14 @@ module('integration/deletedRecord - Deleting Records', function (hooks) {

assert.strictEqual(get(store.peekAll('person'), 'length'), 1, 'The new person should be in the store');

let internalModel = record._internalModel;
let identifier = recordIdentifierFor(record);
let recordData = store._instanceCache.getRecordData(identifier);

record.deleteRecord();

// it is uncertain that `root.empty` vs `root.deleted.saved` afterwards is correct
// but this is the expected result of `unloadRecord`. We may want a `root.deleted.saved.unloaded` state?
assert.true(internalModel.isEmpty, 'We reached the correct persisted saved state');
assert.true(recordData.isEmpty(), 'We reached the correct persisted saved state');
assert.strictEqual(get(store.peekAll('person'), 'length'), 0, 'The new person should be removed from the store');

// assert.ok(cache.indexOf(internalModel) === -1, 'The internal model is removed from the cache');
assert.true(internalModel.isDestroyed, 'The internal model is destroyed');
assert.true(recordData.isDestroyed, 'The recordData is destroyed');

await record.save();
});
Expand All @@ -332,18 +332,15 @@ module('integration/deletedRecord - Deleting Records', function (hooks) {

assert.strictEqual(get(store.peekAll('person'), 'length'), 1, 'The new person should be in the store');

let internalModel = record._internalModel;
let identifier = recordIdentifierFor(record);
let recordData = store._instanceCache.getRecordData(identifier);

record.deleteRecord();
await settled();

// it is uncertain that `root.empty` vs `root.deleted.saved` afterwards is correct
// but this is the expected result of `unloadRecord`. We may want a `root.deleted.saved.unloaded` state?
assert.true(internalModel.isEmpty, 'We reached the correct persisted saved state');
assert.true(recordData.isEmpty(), 'We reached the correct persisted saved state');
assert.strictEqual(get(store.peekAll('person'), 'length'), 0, 'The new person should be removed from the store');

// assert.ok(cache.indexOf(internalModel) === -1, 'The internal model is removed from the cache');
assert.true(internalModel.isDestroyed, 'The internal model is destroyed');
assert.true(recordData.isDestroyed, 'The internal model is destroyed');

record.unloadRecord();
await settled();
Expand Down
Loading

0 comments on commit e99428a

Please sign in to comment.