Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX TEST] reloading tests for adapter encapsulation side quest - shouldReloadRecord #6714

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {});
});