-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Included more tests for the doc source, expanded the capabilities and…
… configurability of the StubbedClient.
- Loading branch information
Spencer Alger
committed
Mar 12, 2014
1 parent
41083d6
commit b4917b7
Showing
4 changed files
with
292 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,177 @@ | ||
define(function (require) { | ||
var createCourier = require('test_utils/create_courier'); | ||
var stubbedClient = require('test_utils/stubbed_client'); | ||
var sinon = require('test_utils/auto_release_sinon'); | ||
var _ = require('lodash'); | ||
|
||
return function extendCourierSuite() { | ||
describe('DocSource class', function () { | ||
it('tracks the version of the document'); | ||
it('can be used without saving the doc'); | ||
it('provides a way to keep two objects synced between tabs'); | ||
it('tracks the version of the document', function (done) { | ||
var version = 51; | ||
var courier = createCourier(stubbedClient(function (method, params, cb) { | ||
cb(void 0, stubbedClient.doc({ _version: version })); | ||
})); | ||
|
||
var source = courier | ||
.createSource('doc').index('fake').type('fake').id('fake') | ||
.on('results', function (doc) { | ||
expect(source._getVersion()).to.eql(version); | ||
expect(courier._getRefFor(source).version).to.eql(version); | ||
done(); | ||
}); | ||
|
||
courier.start(); | ||
}); | ||
|
||
it('updates to a doc will propogate to other docs with the same index/type/id', function (done) { | ||
var client = (function () { | ||
// fake server state | ||
var version = 0; | ||
var doc = { hi: 'fallacy' }; | ||
|
||
return stubbedClient({ | ||
update: function (params, cb) { | ||
_.assign(doc, params.body.doc); | ||
version++; | ||
cb(void 0, { ok: true }); | ||
}, | ||
default: function (method, params, cb) { | ||
cb(void 0, stubbedClient.doc({ _source: doc, _version: version })); | ||
} | ||
}); | ||
}()); | ||
var courier = createCourier(client); | ||
|
||
var update = { hi: 'truth' }; | ||
|
||
// updating this | ||
var pitcher = courier.createSource('doc').index('fake').type('fake').id('fake') | ||
.doUpdate(update); | ||
|
||
// should update this | ||
var catcher = courier.createSource('doc').index('fake').type('fake').id('fake') | ||
.on('results', function (doc) { | ||
expect(doc._source).to.eql(update); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('clears the stored version when a document has been deleted', function (done) { | ||
var client = (function () { | ||
// fake server state | ||
var doc = { hi: 'fallacy' }; | ||
|
||
return stubbedClient({ | ||
delete: function (params, cb) { | ||
doc = null; | ||
cb(void 0, { ok: true }); | ||
}, | ||
default: function (method, params, cb) { | ||
if (doc) { | ||
cb(void 0, stubbedClient.doc({ _source: doc })); | ||
} else { | ||
cb(void 0, stubbedClient.doc({ found: false, _source: null, _version: null })); | ||
} | ||
} | ||
}); | ||
}()); | ||
var courier = createCourier(client); | ||
|
||
var source = courier.createSource('doc') | ||
.index('fake') | ||
.type('fake') | ||
.id('fake') | ||
.on('results', function (doc) { | ||
if (doc.found) { | ||
client.delete({}, function () { | ||
source.fetch(); | ||
}); | ||
} else { | ||
expect(courier._getRefFor(source).version).to.be(void 0); | ||
expect(source._getVersion()).to.be(void 0); | ||
done(); | ||
} | ||
}); | ||
|
||
courier.start(); | ||
}); | ||
|
||
it('checks localStorage for changes to the stored version, which will trigger the doc to be refetched', function (done) { | ||
var version = 11234; | ||
var courier = createCourier(stubbedClient(function (method, params, cb) { | ||
cb(void 0, stubbedClient.doc({ _version: version })); | ||
})); | ||
|
||
var count = 0; | ||
courier.docInterval(10); | ||
|
||
var source = courier.createSource('doc') | ||
.index('fake') | ||
.type('fake') | ||
.id('fake') | ||
.on('results', function (doc) { | ||
switch (count++) { | ||
case 0: | ||
// simulate removing the version in another tab | ||
localStorage.removeItem(source._versionKey()); | ||
// get version should now be returning undefined | ||
expect(source._getVersion()).to.eql(void 0); | ||
// tell the courier to check docs 1 ms now | ||
courier.docInterval(1); | ||
break; | ||
case 1: | ||
// doc version should now be populated | ||
expect(source._getVersion()).to.eql(version); | ||
done(); | ||
} | ||
}); | ||
|
||
courier.start(); | ||
}); | ||
|
||
describe('#doIndex', function () { | ||
it('reindexes the doc using the hash passed in', function (done) { | ||
var client = (function () { | ||
var version = 1; | ||
var doc = { initial: true }; | ||
|
||
return stubbedClient({ | ||
index: function (params, cb) { | ||
doc = _.clone(params.body); | ||
version ++; | ||
cb(void 0, { ok: true }); | ||
}, | ||
mget: function (params, cb) { | ||
cb(void 0, stubbedClient.doc({ _version: version, _source: doc })); | ||
} | ||
}); | ||
}()); | ||
var courier = createCourier(client); | ||
|
||
var count = 0; | ||
courier.docInterval(10); | ||
|
||
var source = courier.createSource('doc') | ||
.index('fake') | ||
.type('fake') | ||
.id('fake') | ||
.on('results', function (doc) { | ||
switch (count ++) { | ||
case 0: | ||
expect(doc._source).to.have.property('initial', true); | ||
source.doIndex({ second: true }); | ||
break; | ||
case 1: | ||
expect(doc._source).to.not.have.property('initial'); | ||
expect(doc._source).to.have.property('second', true); | ||
done(); | ||
break; | ||
} | ||
}); | ||
|
||
courier.start(); | ||
}); | ||
}); | ||
}); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters