Skip to content

Commit

Permalink
[BUGFIX TEST] reloading tests for adapter encapsulation side quest - …
Browse files Browse the repository at this point in the history
…shouldReloadRecord (#6714)
  • Loading branch information
Gaurav0 authored and runspired committed Nov 11, 2019
1 parent 6784ede commit 3641f29
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/store/addon/-private/system/core-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,11 @@ abstract class CoreStore extends Service {
let adapter = this.adapterFor(internalModel.modelName);

// Refetch the record if the adapter thinks the record is stale
if (adapter.shouldReloadRecord(this, snapshot)) {
if (
typeof options.reload === 'undefined' &&
adapter.shouldReloadRecord &&
adapter.shouldReloadRecord(this, snapshot)
) {
return this._scheduleFetch(internalModel, options);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,184 @@ module('integration/reload - Reloading Tests', function(hooks) {
});
});

module('adapter.shouldReloadRecord', function() {});
module('adapter.shouldReloadRecord', function() {
test('adapter.shouldReloadRecord is not called when store.findRecord is called for an unloaded record (but we do make request)', async function(assert) {
let payload = {
data: {
id: '1',
type: 'person',
attributes: {
firstName: 'Gaurav',
lastName: 'Munjal',
},
},
};

setupReloadTest.call(this, {
shouldBackgroundReloadRecord: false,
resolveFindRecordWith: payload,
});

let record = this.store.push(payload);

this.store.unloadRecord(record);

await this.store.findRecord('person', '1');

assert.equal(this.adapter.shouldReloadRecordCalled, 0, 'shouldReloadRecord is not called');
assert.equal(this.adapter.requestsMade, 1, 'an ajax request is made');
});

test('adapter.shouldReloadRecord is not called when store.findRecord is called for a never loaded record (but we do make request)', async function(assert) {
let payload = {
data: {
id: '1',
type: 'person',
attributes: {
firstName: 'Gaurav',
lastName: 'Munjal',
},
},
};

setupReloadTest.call(this, {
shouldBackgroundReloadRecord: false,
resolveFindRecordWith: payload,
});

await this.store.findRecord('person', '1');

assert.equal(this.adapter.shouldReloadRecordCalled, 0, 'shouldReloadRecord is not called');
assert.equal(this.adapter.requestsMade, 1, 'an ajax request is made');
});

test('adapter.shouldReloadRecord is not called when store.findRecord is called with a reload flag (but we do make request if reload is true)', async function(assert) {
let payload = {
data: {
id: '1',
type: 'person',
attributes: {
firstName: 'Gaurav',
lastName: 'Munjal',
},
},
};

setupReloadTest.call(this, {
shouldBackgroundReloadRecord: false,
resolveFindRecordWith: payload,
});

this.store.push(payload);

await this.store.findRecord('person', '1', { reload: true });

assert.equal(this.adapter.shouldReloadRecordCalled, 0, 'shouldReloadRecord is not called');
assert.equal(this.adapter.requestsMade, 1, 'an ajax request is made');
});

test('adapter.shouldReloadRecord is not called when store.findRecord is called with a reload flag (and we do not make request if reload is false)', async function(assert) {
let payload = {
data: {
id: '1',
type: 'person',
attributes: {
firstName: 'Gaurav',
lastName: 'Munjal',
},
},
};

setupReloadTest.call(this, {
shouldBackgroundReloadRecord: false,
resolveFindRecordWith: payload,
});

this.store.push(payload);

await this.store.findRecord('person', '1', { reload: false });

assert.equal(this.adapter.shouldReloadRecordCalled, 0, 'shouldReloadRecord is not called');
assert.equal(this.adapter.requestsMade, 0, 'no ajax request is made');
});

test('if adapter.shouldReloadRecord is undefined, we default to false and do not make a request', async function(assert) {
let payload = {
data: {
id: '1',
type: 'person',
attributes: {
firstName: 'Gaurav',
lastName: 'Munjal',
},
},
};

setupReloadTest.call(this, {
shouldBackgroundReloadRecord: false,
resolveFindRecordWith: payload,
});

this.store.push(payload);

await this.store.findRecord('person', '1');

assert.equal(this.adapter.shouldReloadRecordCalled, 0, 'shouldReloadRecord is not called');
assert.equal(this.adapter.requestsMade, 0, 'no ajax request is made');
});

test('adapter.shouldReloadRecord is called when store.findRecord is called without a reload flag (shouldReloadRecord returns true)', async function(assert) {
let payload = {
data: {
id: '1',
type: 'person',
attributes: {
firstName: 'Gaurav',
lastName: 'Munjal',
},
},
};

setupReloadTest.call(this, {
shouldReloadRecord: true,
shouldBackgroundReloadRecord: false,
resolveFindRecordWith: payload,
});

this.store.push(payload);

await this.store.findRecord('person', '1');

assert.equal(this.adapter.shouldReloadRecordCalled, 1, 'shouldReloadRecord is called');
assert.equal(this.adapter.requestsMade, 1, 'an ajax request is made');
});

test('adapter.shouldReloadRecord is called when store.findRecord is called without a reload flag (shouldReloadRecord returns false)', async function(assert) {
let payload = {
data: {
id: '1',
type: 'person',
attributes: {
firstName: 'Gaurav',
lastName: 'Munjal',
},
},
};

setupReloadTest.call(this, {
shouldReloadRecord: false,
shouldBackgroundReloadRecord: false,
resolveFindRecordWith: payload,
});

this.store.push(payload);

await this.store.findRecord('person', '1');

assert.equal(this.adapter.shouldReloadRecordCalled, 1, 'shouldReloadRecord is called');
assert.equal(this.adapter.requestsMade, 0, 'no ajax request is made');
});
});

module('adapter.shouldBackgroundReloadRecord', function() {});
});

0 comments on commit 3641f29

Please sign in to comment.