Skip to content

Commit

Permalink
Merge pull request #104 from rsutphin/change-watcher-tests
Browse files Browse the repository at this point in the history
Change watcher tests
  • Loading branch information
nolanlawson committed Jan 16, 2016
2 parents 838774a + 1efa7c8 commit 408ada8
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 65 deletions.
39 changes: 34 additions & 5 deletions tests/helpers/module-for-acceptance.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
import { module } from 'qunit';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
import config from 'dummy/config/environment';

import Ember from 'ember';
/* globals PouchDB */

export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
beforeEach(assert) {
var done = assert.async();

if (options.beforeEach) {
options.beforeEach.apply(this, arguments);
}
Ember.RSVP.Promise.resolve().then(() => {
return (new PouchDB(config.emberpouch.localDb)).destroy();
}).then(() => {
this.application = startApp();

this.lookup = function (item) {
return this.application.__container__.lookup(item);
};

this.store = function store() {
return this.lookup('service:store');
};

// At the container level, adapters are not singletons (ember-data
// manages them). To get the instance that the app is using, we have to
// go through the store.
this.adapter = function adapter() {
return this.store().adapterFor('taco-soup');
};

this.db = function db() {
return this.adapter().get('db');
};

if (options.beforeEach) {
options.beforeEach.apply(this, arguments);
}
}).finally(done);
},

afterEach() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,26 @@
import { module, test } from 'qunit';
import startApp from '../../helpers/start-app';
import config from 'dummy/config/environment';
import { test } from 'qunit';
import moduleForIntegration from '../../helpers/module-for-acceptance';

import Ember from 'ember';
/* globals PouchDB */

var App;

/*
* Tests basic CRUD behavior for an app using the ember-pouch adapter.
*/

module('adapter:pouch [integration]', {
beforeEach: function (assert) {
var done = assert.async();

(new PouchDB(config.emberpouch.localDb)).destroy().then(() => {
App = startApp();
var bootPromise;
Ember.run(() => {
if (App.boot) {
App.advanceReadiness();
bootPromise = App.boot();
} else {
bootPromise = Ember.RSVP.Promise.resolve();
}
});
return bootPromise;
}).then(() => {
done();
});
},

afterEach: function () {
Ember.run(App, 'destroy');
}
});

function db() {
return adapter().get('db');
}

function adapter() {
// the default adapter in the dummy app is an ember-pouch adapter
return App.__container__.lookup('adapter:application');
}

function store() {
return App.__container__.lookup('service:store');
}
moduleForIntegration('Integration | Adapter | Basic CRUD Ops');

test('can find all', function (assert) {
assert.expect(3);

var done = assert.async();
Ember.RSVP.Promise.resolve().then(() => {
return db().bulkDocs([
return this.db().bulkDocs([
{ _id: 'tacoSoup_2_A', data: { flavor: 'al pastor' } },
{ _id: 'tacoSoup_2_B', data: { flavor: 'black bean' } },
{ _id: 'burritoShake_2_X', data: { consistency: 'smooth' } }
]);
}).then(() => {
return store().findAll('taco-soup');
return this.store().findAll('taco-soup');
}).then((found) => {
assert.equal(found.get('length'), 2, 'should have found the two taco soup items only');
assert.deepEqual(found.mapBy('id'), ['A', 'B'],
Expand All @@ -81,12 +40,12 @@ test('can find one', function (assert) {

var done = assert.async();
Ember.RSVP.Promise.resolve().then(() => {
return db().bulkDocs([
return this.db().bulkDocs([
{ _id: 'tacoSoup_2_C', data: { flavor: 'al pastor' } },
{ _id: 'tacoSoup_2_D', data: { flavor: 'black bean' } },
]);
}).then(() => {
return store().find('taco-soup', 'D');
return this.store().find('taco-soup', 'D');
}).then((found) => {
assert.equal(found.get('id'), 'D',
'should have found the requested item');
Expand All @@ -105,15 +64,15 @@ test('can find associated records', function (assert) {

var done = assert.async();
Ember.RSVP.Promise.resolve().then(() => {
return db().bulkDocs([
return this.db().bulkDocs([
{ _id: 'tacoSoup_2_C', data: { flavor: 'al pastor', ingredients: ['X', 'Y'] } },
{ _id: 'tacoSoup_2_D', data: { flavor: 'black bean', ingredients: ['Z'] } },
{ _id: 'foodItem_2_X', data: { name: 'pineapple' }},
{ _id: 'foodItem_2_Y', data: { name: 'pork loin' }},
{ _id: 'foodItem_2_Z', data: { name: 'black beans' }}
]);
}).then(() => {
return store().find('taco-soup', 'C');
return this.store().find('taco-soup', 'C');
}).then((found) => {
assert.equal(found.get('id'), 'C',
'should have found the requested item');
Expand All @@ -136,14 +95,14 @@ test('create a new record', function (assert) {

var done = assert.async();
Ember.RSVP.Promise.resolve().then(() => {
var newSoup = store().createRecord('taco-soup', { id: 'E', flavor: 'balsamic' });
var newSoup = this.store().createRecord('taco-soup', { id: 'E', flavor: 'balsamic' });
return newSoup.save();
}).then(() => {
return db().get('tacoSoup_2_E');
return this.db().get('tacoSoup_2_E');
}).then((newDoc) => {
assert.equal(newDoc.data.flavor, 'balsamic', 'should have saved the attribute');

var recordInStore = store().peekRecord('tacoSoup', 'E');
var recordInStore = this.store().peekRecord('tacoSoup', 'E');
assert.equal(newDoc._rev, recordInStore.get('rev'),
'should have associated the ember-data record with the rev for the new record');

Expand All @@ -164,21 +123,21 @@ if (!DS.VERSION.match(/^2\.0/)) {

var done = assert.async();
Ember.RSVP.Promise.resolve().then(() => {
return db().bulkDocs([
return this.db().bulkDocs([
{ _id: 'tacoSoup_2_C', data: { flavor: 'al pastor' } },
{ _id: 'tacoSoup_2_D', data: { flavor: 'black bean' } },
]);
}).then(() => {
return store().find('taco-soup', 'C');
return this.store().find('taco-soup', 'C');
}).then((found) => {
found.set('flavor', 'pork');
return found.save();
}).then(() => {
return db().get('tacoSoup_2_C');
return this.db().get('tacoSoup_2_C');
}).then((updatedDoc) => {
assert.equal(updatedDoc.data.flavor, 'pork', 'should have updated the attribute');

var recordInStore = store().peekRecord('tacoSoup', 'C');
var recordInStore = this.store().peekRecord('tacoSoup', 'C');
assert.equal(updatedDoc._rev, recordInStore.get('rev'),
'should have associated the ember-data record with the updated rev');

Expand All @@ -196,16 +155,16 @@ test('delete an existing record', function (assert) {

var done = assert.async();
Ember.RSVP.Promise.resolve().then(() => {
return db().bulkDocs([
return this.db().bulkDocs([
{ _id: 'tacoSoup_2_C', data: { flavor: 'al pastor' } },
{ _id: 'tacoSoup_2_D', data: { flavor: 'black bean' } },
]);
}).then(() => {
return store().find('taco-soup', 'C');
return this.store().find('taco-soup', 'C');
}).then((found) => {
return found.destroyRecord();
}).then(() => {
return db().get('tacoSoup_2_C');
return this.db().get('tacoSoup_2_C');
}).then((doc) => {
assert.ok(!doc, 'document should no longer exist');
}, (result) => {
Expand Down
144 changes: 144 additions & 0 deletions tests/integration/adapters/pouch-default-change-watcher-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { test } from 'qunit';
import moduleForIntegration from '../../helpers/module-for-acceptance';

import Ember from 'ember';

/*
* Tests for the default automatic change listener.
*/

moduleForIntegration('Integration | Adapter | Default Change Watcher', {
beforeEach(assert) {
var done = assert.async();

Ember.RSVP.Promise.resolve().then(() => {
return this.db().bulkDocs([
{ _id: 'tacoSoup_2_A', data: { flavor: 'al pastor', ingredients: ['X', 'Y'] } },
{ _id: 'tacoSoup_2_B', data: { flavor: 'black bean', ingredients: ['Z'] } },
{ _id: 'foodItem_2_X', data: { name: 'pineapple' } },
{ _id: 'foodItem_2_Y', data: { name: 'pork loin' } },
{ _id: 'foodItem_2_Z', data: { name: 'black beans' } }
]);
}).finally(done);
}
});

function promiseToRunLater(callback, timeout) {
return new Ember.RSVP.Promise((resolve) => {
Ember.run.later(() => {
callback();
resolve();
}, timeout);
});
}

test('a loaded instance automatically reflects directly-made database changes', function (assert) {
assert.expect(2);
var done = assert.async();

Ember.RSVP.resolve().then(() => {
return this.store().find('taco-soup', 'B');
}).then((soupB) => {
assert.equal('black bean', soupB.get('flavor'),
'the loaded instance should reflect the initial test data');

return this.db().get('tacoSoup_2_B');
}).then((soupBRecord) => {
soupBRecord.data.flavor = 'carnitas';
return this.db().put(soupBRecord);
}).then(() => {
return promiseToRunLater(() => {
var alreadyLoadedSoupB = this.store().peekRecord('taco-soup', 'B');
assert.equal(alreadyLoadedSoupB.get('flavor'), 'carnitas',
'the loaded instance should automatically reflect the change in the database');
}, 15);
}).finally(done);
});

test('a record that is not loaded stays not loaded when it is changed', function (assert) {
assert.expect(2);
var done = assert.async();

Ember.RSVP.resolve().then(() => {
assert.equal(null, this.store().peekRecord('taco-soup', 'A'),
'test setup: record should not be loaded already');

return this.db().get('tacoSoup_2_A');
}).then((soupARecord) => {
soupARecord.data.flavor = 'barbacoa';
return this.db().put(soupARecord);
}).then(() => {
return promiseToRunLater(() => {
assert.equal(null, this.store().peekRecord('taco-soup', 'A'),
'the corresponding instance should still not be loaded');
}, 15);
}).finally(done);
});

test('a new record is not automatically loaded', function (assert) {
assert.expect(2);
var done = assert.async();

Ember.RSVP.resolve().then(() => {
assert.equal(null, this.store().peekRecord('taco-soup', 'C'),
'test setup: record should not be loaded already');

return this.db().put({
_id: 'tacoSoup_2_C', data: { flavor: 'sofritas' }
});
}).then(() => {
return promiseToRunLater(() => {
assert.equal(null, this.store().peekRecord('taco-soup', 'C'),
'the corresponding instance should still not be loaded');
}, 15);
}).finally(done);
});

test('a deleted record is automatically unloaded', function (assert) {
assert.expect(2);
var done = assert.async();

Ember.RSVP.resolve().then(() => {
return this.store().find('taco-soup', 'B');
}).then((soupB) => {
assert.equal('black bean', soupB.get('flavor'),
'the loaded instance should reflect the initial test data');

return this.db().get('tacoSoup_2_B');
}).then((soupBRecord) => {
return this.db().remove(soupBRecord);
}).then(() => {
return promiseToRunLater(() => {
assert.equal(null, this.store().peekRecord('taco-soup', 'B'),
'the corresponding instance should no longer be loaded');
}, 15);
}).finally(done);
});

test('a change to a record with a non-relational-pouch ID does not cause an error', function (assert) {
assert.expect(0);
var done = assert.async();

Ember.RSVP.resolve().then(() => {
// do some op to cause relational-pouch to be initialized
return this.store().find('taco-soup', 'B');
}).then(() => {
return this.db().put({
_id: '_design/ingredient-use'
});
}).finally(done);
});

test('a change to a record of an unknown type does not cause an error', function (assert) {
assert.expect(0);
var done = assert.async();

Ember.RSVP.resolve().then(() => {
// do some op to cause relational-pouch to be initialized
return this.store().find('taco-soup', 'B');
}).then(() => {
return this.db().put({
_id: 'burritoShake_2_X', data: { consistency: 'chunky' }
});
}).finally(done);
});

0 comments on commit 408ada8

Please sign in to comment.