Skip to content

Commit

Permalink
Merge pull request #3925 from seanpdoyle/sd-cleanup-adapter-tests
Browse files Browse the repository at this point in the history
[CLEANUP] Clean up adapter/find-all-test.js
  • Loading branch information
fivetanley committed Nov 14, 2015
2 parents a7f1155 + df81e19 commit 80f582d
Showing 1 changed file with 33 additions and 34 deletions.
67 changes: 33 additions & 34 deletions tests/integration/adapter/find-all-test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import {createStore} from 'dummy/tests/helpers/store';
import setupStore from 'dummy/tests/helpers/store';
import Ember from 'ember';

import {module, test} from 'qunit';

import DS from 'ember-data';

var get = Ember.get;
var Person, store, allRecords;
var run = Ember.run;
var env;
const { attr } = DS;
const { get, run } = Ember;
const { resolve, reject } = Ember.RSVP;

let Person, store, allRecords, env;

module("integration/adapter/find_all - Finding All Records of a Type", {
beforeEach: function() {
beforeEach() {
Person = DS.Model.extend({
updatedAt: DS.attr('string'),
name: DS.attr('string'),
firstName: DS.attr('string'),
lastName: DS.attr('string')
updatedAt: attr('string'),
name: attr('string'),
firstName: attr('string'),
lastName: attr('string')
});

allRecords = null;
Expand All @@ -28,83 +27,83 @@ module("integration/adapter/find_all - Finding All Records of a Type", {
store = env.store;
},

afterEach: function() {
run(function() {
afterEach() {
run(() => {
if (allRecords) { allRecords.destroy(); }
store.destroy();
});
}
});

test("When all records for a type are requested, the store should call the adapter's `findAll` method.", function(assert) {
test("When all records for a type are requested, the store should call the adapter's `findAll` method.", (assert) => {
assert.expect(5);

env.registry.register('adapter:person', DS.Adapter.extend({
findAll: function(store, type, since) {
findAll() {
// this will get called twice
assert.ok(true, "the adapter's findAll method should be invoked");

return Ember.RSVP.resolve([{ id: 1, name: "Braaaahm Dale" }]);
return resolve([{ id: 1, name: "Braaaahm Dale" }]);
}
}));

var allRecords;
let allRecords;

run(function() {
store.findAll('person').then(function(all) {
run(() => {
store.findAll('person').then((all) => {
allRecords = all;
assert.equal(get(all, 'length'), 1, "the record array's length is 1 after a record is loaded into it");
assert.equal(all.objectAt(0).get('name'), "Braaaahm Dale", "the first item in the record array is Braaaahm Dale");
});
});

run(function() {
store.findAll('person').then(function(all) {
run(() => {
store.findAll('person').then((all) => {
// Only one record array per type should ever be created (identity map)
assert.strictEqual(allRecords, all, "the same record array is returned every time all records of a type are requested");
});
});
});

test("When all records for a type are requested, a rejection should reject the promise", function(assert) {
test("When all records for a type are requested, a rejection should reject the promise", (assert) => {
assert.expect(5);

var count = 0;
let count = 0;
env.registry.register('adapter:person', DS.Adapter.extend({
findAll: function(store, type, since) {
findAll() {
// this will get called twice
assert.ok(true, "the adapter's findAll method should be invoked");

if (count++ === 0) {
return Ember.RSVP.reject();
return reject();
} else {
return Ember.RSVP.resolve([{ id: 1, name: "Braaaahm Dale" }]);
return resolve([{ id: 1, name: "Braaaahm Dale" }]);
}
}
}));

var allRecords;
let allRecords;

run(function() {
store.findAll('person').then(null, function() {
run(() => {
store.findAll('person').then(null, () => {
assert.ok(true, "The rejection should get here");
return store.findAll('person');
}).then(function(all) {
}).then((all) => {
allRecords = all;
assert.equal(get(all, 'length'), 1, "the record array's length is 1 after a record is loaded into it");
assert.equal(all.objectAt(0).get('name'), "Braaaahm Dale", "the first item in the record array is Braaaahm Dale");
});
});
});

test("When all records for a type are requested, records that are already loaded should be returned immediately.", function(assert) {
test("When all records for a type are requested, records that are already loaded should be returned immediately.", (assert) => {
assert.expect(3);
store = createStore({
adapter: DS.Adapter.extend(),
person: Person
});

run(function() {
run(() => {
// Load a record from the server
store.push({
data: {
Expand All @@ -126,7 +125,7 @@ test("When all records for a type are requested, records that are already loaded
assert.equal(allRecords.objectAt(1).get('name'), "Alex MacCaw", "the second item in the record array is Alex MacCaw");
});

test("When all records for a type are requested, records that are created on the client should be added to the record array.", function(assert) {
test("When all records for a type are requested, records that are created on the client should be added to the record array.", (assert) => {
assert.expect(3);

store = createStore({
Expand All @@ -138,7 +137,7 @@ test("When all records for a type are requested, records that are created on the

assert.equal(get(allRecords, 'length'), 0, "precond - the record array's length is zero before any records are loaded");

run(function() {
run(() => {
store.createRecord('person', { name: "Carsten Nielsen" });
});

Expand Down

0 comments on commit 80f582d

Please sign in to comment.