Skip to content

Commit

Permalink
all tests passing for singleton cache
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Aug 15, 2022
1 parent aa7cbbb commit 9e571c1
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ module('RecordData Compatibility', function (hooks) {
clientId: this.clientId,
};
}
isEmpty() {
return false;
}
// TODO missing from RFC but required to implement
unloadRecord() {
this.attributes = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ module('integration/deletedRecord - Deleting Records', function (hooks) {

record.deleteRecord();

assert.true(recordData.isEmpty(), 'new person state is empty');
assert.true(recordData.isEmpty(identifier), '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 @@ -251,7 +251,7 @@ module('integration/deletedRecord - Deleting Records', function (hooks) {

await record.destroyRecord();

assert.true(recordData.isEmpty(), 'new person state is empty');
assert.true(recordData.isEmpty(identifier), '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 @@ -307,7 +307,7 @@ module('integration/deletedRecord - Deleting Records', function (hooks) {

record.deleteRecord();

assert.true(recordData.isEmpty(), 'We reached the correct persisted saved state');
assert.true(recordData.isEmpty(identifier), '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.strictEqual(
store._instanceCache.peek({ identifier, bucket: 'recordData' }),
Expand Down Expand Up @@ -342,7 +342,7 @@ module('integration/deletedRecord - Deleting Records', function (hooks) {
record.deleteRecord();
await settled();

assert.true(recordData.isEmpty(), 'We reached the correct persisted saved state');
assert.true(recordData.isEmpty(identifier), '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.strictEqual(
store._instanceCache.peek({ identifier, bucket: 'recordData' }),
Expand Down
21 changes: 14 additions & 7 deletions packages/-ember-data/tests/integration/records/load-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { reject, resolve } from 'rsvp';
import { setupTest } from 'ember-qunit';

import JSONAPIAdapter from '@ember-data/adapter/json-api';
import { V2CACHE_SINGLETON_RECORD_DATA } from '@ember-data/canary-features';
import Model, { attr, belongsTo } from '@ember-data/model';
import JSONAPISerializer from '@ember-data/serializer/json-api';
import Store from '@ember-data/store';
Expand Down Expand Up @@ -172,7 +173,7 @@ module('integration/load - Loading Records', function (hooks) {

// test that after the initial load our state is correct
recordData = cache.peek({ identifier, bucket: 'recordData' });
assert.false(recordData.isEmpty(), 'after first fetch: We are no longer empty');
assert.false(recordData.isEmpty(identifier), 'after first fetch: We are no longer empty');
assert.false(_isLoading(cache, identifier), 'after first fetch: We have loaded');
assert.false(record.isReloading, 'after first fetch: We are not reloading');

Expand All @@ -190,29 +191,29 @@ module('integration/load - Loading Records', function (hooks) {
recordPromise = record.reload();

// test that during a reload our state is correct
assert.false(recordData.isEmpty(), 'awaiting reload: We remain non-empty');
assert.false(recordData.isEmpty(identifier), 'awaiting reload: We remain non-empty');
assert.false(_isLoading(cache, identifier), 'awaiting reload: We are not loading again');
assert.true(record.isReloading, 'awaiting reload: We are reloading');

await recordPromise;

// test that after a reload our state is correct
assert.false(recordData.isEmpty(), 'after reload: We remain non-empty');
assert.false(recordData.isEmpty(identifier), 'after reload: We remain non-empty');
assert.false(_isLoading(cache, identifier), 'after reload: We have loaded');
assert.false(record.isReloading, 'after reload:: We are not reloading');

run(() => record.unloadRecord());

// test that after an unload our state is correct
assert.true(recordData.isEmpty(), 'after unload: We are empty again');
assert.true(recordData.isEmpty(identifier), 'after unload: We are empty again');
assert.false(_isLoading(cache, identifier), 'after unload: We are not loading');
assert.false(record.isReloading, 'after unload:: We are not reloading');

recordPromise = store.findRecord('person', '1');

// test that during a reload-due-to-unload our state is correct
// This requires a retainer (the async bestFriend relationship)
assert.true(recordData.isEmpty(), 'awaiting second find: We remain empty');
assert.true(recordData.isEmpty(identifier), 'awaiting second find: We remain empty');
let newRecordData = cache.peek({ identifier, bucket: 'recordData' });
assert.strictEqual(newRecordData, undefined, 'We have no recordData during second find');
assert.true(_isLoading(cache, identifier), 'awaiting second find: We are loading again');
Expand All @@ -222,8 +223,14 @@ module('integration/load - Loading Records', function (hooks) {

// test that after the reload-due-to-unload our state is correct
newRecordData = cache.peek({ identifier, bucket: 'recordData' });
assert.true(recordData.isEmpty(), 'after second find: Our original recordData is still empty');
assert.false(newRecordData.isEmpty(), 'after second find: We are no longer empty');
if (V2CACHE_SINGLETON_RECORD_DATA) {
// this first assertion changes based on activation of singleton cache because its a simple mapping of identifier
// to state and we now have state
assert.false(recordData.isEmpty(identifier), 'after second find: Our original recordData is no longer empty');
} else {
assert.ftrue(recordData.isEmpty(identifier), 'after second find: Our original recordData is still empty');
}
assert.false(newRecordData.isEmpty(identifier), 'after second find: We are no longer empty');
assert.false(_isLoading(cache, identifier), 'after second find: We have loaded');
assert.false(record.isReloading, 'after second find: We are not reloading');
});
Expand Down
6 changes: 3 additions & 3 deletions packages/-ember-data/tests/integration/records/unload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ module('integration/unload - Unloading Records', function (hooks) {
assert.strictEqual(record.get('cars.firstObject.make'), 'jeep');
store.unloadRecord(record);
assert.true(record.isDestroying, 'the record is destroying');
assert.true(recordData.isEmpty(), 'Expected the previous data to be unloaded');
assert.true(recordData.isEmpty(identifier), 'Expected the previous data to be unloaded');

const recordAgain = await store.findRecord('person', '1');
assert.strictEqual(recordAgain.cars.length, 0, 'Expected relationship to be cleared by the new push');
Expand Down Expand Up @@ -809,7 +809,7 @@ module('integration/unload - Unloading Records', function (hooks) {
store.unloadRecord(record);
assert.true(record.isDestroying, 'the record is destroying');
assert.false(record.isDestroyed, 'the record is NOT YET destroyed');
assert.true(recordData.isEmpty(), 'We are unloaded after unloadRecord');
assert.true(recordData.isEmpty(identifier), 'We are unloaded after unloadRecord');

let wait = store.findRecord('person', '1').then((newRecord) => {
assert.false(record.isDestroyed, 'the record is NOT YET destroyed');
Expand Down Expand Up @@ -860,7 +860,7 @@ module('integration/unload - Unloading Records', function (hooks) {
run(function () {
store.unloadRecord(record);
assert.true(record.isDestroying, 'the record is destroying');
assert.true(recordData.isEmpty(), 'We are unloaded after unloadRecord');
assert.true(recordData.isEmpty(identifier), 'We are unloaded after unloadRecord');
});

run(function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ module('unit/model - Custom Class Model', function (hooks) {
let notificationCount = 0;
let identifier;
let recordData;
let storeWrapper;
class CreationStore extends CustomStore {
createRecordDataFor(identifier: StableRecordIdentifier, storeWrapper: RecordDataStoreWrapper) {
let rd = super.createRecordDataFor(identifier, storeWrapper);
createRecordDataFor(identifier: StableRecordIdentifier, sw: RecordDataStoreWrapper) {
let rd = super.createRecordDataFor(identifier, sw);
recordData = rd;
storeWrapper = sw;
return rd;
}
instantiateRecord(
Expand All @@ -108,10 +110,10 @@ module('unit/model - Custom Class Model', function (hooks) {
this.owner.register('service:store', CreationStore);
store = this.owner.lookup('service:store') as Store;
store.push({ data: { id: '1', type: 'person', attributes: { name: 'chris' } } });
recordData.storeWrapper.notifyChange(identifier, 'relationships', 'key');
recordData.storeWrapper.notifyChange(identifier, 'relationships', 'key');
recordData.storeWrapper.notifyChange(identifier, 'state');
recordData.storeWrapper.notifyChange(identifier, 'errors');
storeWrapper.notifyChange(identifier, 'relationships', 'key');
storeWrapper.notifyChange(identifier, 'relationships', 'key');
storeWrapper.notifyChange(identifier, 'state');
storeWrapper.notifyChange(identifier, 'errors');
await settled();

assert.strictEqual(notificationCount, 3, 'called notification callback');
Expand Down Expand Up @@ -144,7 +146,7 @@ module('unit/model - Custom Class Model', function (hooks) {
class CreationStore extends Store {
instantiateRecord(identifier, createRecordArgs, recordDataFor, notificationManager) {
rd = recordDataFor(identifier);
assert.strictEqual(rd.getAttr('name'), 'chris', 'Can look up record data from recordDataFor');
assert.strictEqual(rd.getAttr(identifier, 'name'), 'chris', 'Can look up record data from recordDataFor');
return {};
}
teardownRecord(record) {}
Expand Down Expand Up @@ -336,7 +338,7 @@ module('unit/model - Custom Class Model', function (hooks) {
assert.false(rd.isDeleted(identifier), 'we are not deleted when we start');
notificationManager.subscribe(identifier, (passedId, key) => {
assert.strictEqual(key, 'state', 'state change to deleted has been notified');
assert.true(recordDataFor(identifier).isDeleted(), 'we have been marked as deleted');
assert.true(recordDataFor(identifier).isDeleted(identifier), 'we have been marked as deleted');
});
return {};
},
Expand Down
22 changes: 11 additions & 11 deletions packages/-ember-data/tests/unit/model/rollback-attributes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ module('unit/model/rollbackAttributes - model.rollbackAttributes()', function (h
assert.true(person.isDeleted, 'must be deleted');
});

test(`invalid new record's attributes can be rollbacked`, function (assert) {
test(`invalid new record's attributes can be rollbacked`, async function (assert) {
let error = new DS.InvalidError([
{
detail: 'is invalid',
Expand All @@ -257,18 +257,18 @@ module('unit/model/rollbackAttributes - model.rollbackAttributes()', function (h
assert.true(person.isNew, 'must be new');
assert.true(person.hasDirtyAttributes, 'must be dirty');

return run(() => {
return person.save().catch((reason) => {
assert.strictEqual(error, reason);
assert.false(person.isValid);
try {
await person.save();
} catch (reason) {
assert.strictEqual(error, reason);
assert.false(person.isValid);

run(() => person.rollbackAttributes());
person.rollbackAttributes();

assert.false(person.isNew, 'must not be new');
assert.false(person.hasDirtyAttributes, 'must not be dirty');
assert.true(person.isDeleted, 'must be deleted');
});
});
assert.false(person.isNew, 'must not be new');
assert.false(person.hasDirtyAttributes, 'must not be dirty');
assert.true(person.isDeleted, 'must be deleted');
}
});

test(`invalid record's attributes can be rollbacked after multiple failed calls - #3677`, function (assert) {
Expand Down
2 changes: 1 addition & 1 deletion packages/canary-features/addon/default-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,5 @@
export default {
SAMPLE_FEATURE_FLAG: null,
V2CACHE_SINGLETON_RECORD_DATA: true,
V2CACHE_SINGLETON_MANAGER: false,
V2CACHE_SINGLETON_MANAGER: null,
};
Loading

0 comments on commit 9e571c1

Please sign in to comment.