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
…shouldReloadAll (#6673)

* [TEST] reloading tests for adapter encapsulation side quest - shouldReloadAll

* refactor

* get test working

* fix deprecation warning

* add another failing test

* fix second test

* bugfixes

* last tests

* address comments

* Use a minimal serializer instead of JSONAPISerializer
  • Loading branch information
Gaurav0 authored and runspired committed Nov 9, 2019
1 parent 919fb21 commit c20c3da
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 4 deletions.
16 changes: 12 additions & 4 deletions packages/store/addon/-private/system/core-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2242,16 +2242,24 @@ abstract class CoreStore extends Service {

let snapshotArray = array._createSnapshot(options);

if (adapter.shouldReloadAll(this, snapshotArray)) {
set(array, 'isUpdating', true);
return promiseArray(_findAll(adapter, this, modelName, options));
if (options.reload !== false) {
if (
(adapter.shouldReloadAll && adapter.shouldReloadAll(this, snapshotArray)) ||
(!adapter.shouldReloadAll && snapshotArray.length === 0)
) {
set(array, 'isUpdating', true);
return promiseArray(_findAll(adapter, this, modelName, options));
}
}

if (options.backgroundReload === false) {
return promiseArray(Promise.resolve(array));
}

if (options.backgroundReload || adapter.shouldBackgroundReloadAll(this, snapshotArray)) {
if (
options.backgroundReload ||
(adapter.shouldBackgroundReloadAll && adapter.shouldBackgroundReloadAll(this, snapshotArray))
) {
set(array, 'isUpdating', true);
_findAll(adapter, this, modelName, options);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Store from '@ember-data/store';
import { RecordData } from '@ember-data/record-data/-private';
import { identifierCacheFor } from '@ember-data/store/-private';
import { IDENTIFIERS } from '@ember-data/canary-features';

export default class DefaultStore extends Store {
createRecordDataFor(modelName, id, clientId, storeWrapper) {
if (IDENTIFIERS) {
let identifier = identifierCacheFor(this).getOrCreateRecordIdentifier({
type: modelName,
id,
lid: clientId,
});
return new RecordData(identifier, storeWrapper);
} else {
return new RecordData(modelName, id, clientId, storeWrapper);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import { setupTest } from 'ember-qunit';
import { module, test } from 'qunit';
import EmberObject from '@ember/object';
import Store from 'adapter-encapsulation-test-app/services/store';
import Model, { attr } from '@ember-data/model';
import Transform from '@ember-data/serializer/transform';
import { resolve } from 'rsvp';

class MinimalSerializer extends EmberObject {
normalizeResponse(_, __, data) {
return data;
}
}

class Person extends Model {
@attr
firstName;

@attr
lastName;
}

function setupReloadTest(options) {
class TestMinimumAdapter extends EmberObject {
shouldReloadAllCalled = 0;
shouldReloadRecordCalled = 0;
shouldBackgroundReloadAllCalled = 0;
shouldBackgroundReloadRecordCalled = 0;

requestsMade = 0;

constructor() {
super(...arguments);

if (options.shouldReloadAll !== undefined) {
this.shouldReloadAll = function() {
this.shouldReloadAllCalled++;
return options.shouldReloadAll;
};
}

if (options.shouldReloadRecord !== undefined) {
this.shouldReloadRecord = function() {
this.shouldReloadRecordCalled++;
return options.shouldReloadRecord;
};
}
if (options.shouldBackgroundReloadAll !== undefined) {
this.shouldBackgroundReloadAll = function() {
this.shouldBackgroundReloadAllCalled++;
return options.shouldBackgroundReloadAll;
};
}

if (options.shouldBackgroundReloadRecord !== undefined) {
this.shouldBackgroundReloadRecord = function() {
this.shouldBackgroundReloadRecordCalled++;
return options.shouldBackgroundReloadRecord;
};
}
}

findAll() {
this.requestsMade++;
return resolve(options.resolveFindAllWith || { data: [] });
}

findRecord() {
this.requestsMade++;
return resolve(options.resolveFindRecordWith || { data: null });
}
}
this.owner.register('adapter:application', TestMinimumAdapter);

this.store = this.owner.lookup('service:store');
this.adapter = this.owner.lookup('adapter:application');
}

module('integration/reload - Reloading Tests', function(hooks) {
setupTest(hooks);

hooks.beforeEach(function() {
// Needed to avoid deprecation warning even though not using any transforms.
this.owner.register('transform:date', class DateTransform extends Transform {});
this.owner.register('transform:number', class NumberTransform extends Transform {});
this.owner.register('transform:boolean', class BooleanTransform extends Transform {});
this.owner.register('transform:string', class StringTransform extends Transform {});

this.owner.register('service:store', Store);
this.owner.register('serializer:application', MinimalSerializer);
this.owner.register('model:person', Person);
});

module('adapter.shouldReloadAll', function() {
test('adapter.shouldReloadAll is not called when store.findAll is called with a reload: false flag', async function(assert) {
setupReloadTest.call(this, {
shouldReloadAll: false,
shouldBackgroundReloadAll: false,
});

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

assert.equal(this.adapter.shouldReloadAllCalled, 0, 'shouldReloadAll is not called');
assert.equal(this.adapter.requestsMade, 0, 'no request is made');
});

test('adapter.shouldReloadAll is not called when store.findAll is called with a reload: true flag', async function(assert) {
setupReloadTest.call(this, {
shouldReloadAll: false,
shouldBackgroundReloadAll: false,
});

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

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

test('store.findAll does not error if adapter.shouldReloadAll is not defined (records are present)', async function(assert) {
setupReloadTest.call(this, {
shouldBackgroundReloadAll: false,
});

this.store.push({
data: {
id: 1,
type: 'person',
attributes: {
firstName: 'Gaurav',
lastName: 'Munjal',
},
},
});

await this.store.findAll('person');

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

test('store.findAll does not error if adapter.shouldReloadAll is not defined (records are absent)', async function(assert) {
setupReloadTest.call(this, {
shouldBackgroundReloadAll: false,
});

await this.store.findAll('person');

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

test('adapter.shouldReloadAll is called when store.findAll is called without a reload flag (shouldReloadAll is false)', async function(assert) {
setupReloadTest.call(this, {
shouldReloadAll: false,
shouldBackgroundReloadAll: false,
});

await this.store.findAll('person');

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

test('adapter.shouldReloadAll is called when store.findAll is called without a reload flag (shouldReloadAll is false)', async function(assert) {
setupReloadTest.call(this, {
shouldReloadAll: true,
shouldBackgroundReloadAll: false,
});

await this.store.findAll('person');

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

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

0 comments on commit c20c3da

Please sign in to comment.