-
Notifications
You must be signed in to change notification settings - Fork 16
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
emit correct add/update events and emit event before resolving promises #154
Conversation
7be4e39
to
8c765b4
Compare
8c765b4
to
9f80b73
Compare
9f80b73
to
d5b29c0
Compare
lib/reset.js
Outdated
@@ -11,7 +11,7 @@ function reset (state) { | |||
}) | |||
|
|||
.then(function () { | |||
return state.db.destroy | |||
return state.db.destroy() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ha! Thanks Rob :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're most welcome, Gregor. If I had a dollar every time I did that, I'd be retired already! :)
@gr2m -- I'm trying to rebase master and refactor the tests to get back to 100% code coverage. Should we be resolving somewhere in here? I see where we do it when result.length == 0, but otherwise, it doesn't look like we resolve. |
thanks for looking into it 👍 We set Does that make sense? |
Ah, I see it now. Let me see if I can figure out the problem with the test. |
0184060
to
518b4e3
Compare
@gr2m -- I'm almost there. I figured out the Any ideas on the best ways to approach these tests? |
@gr2m -- looks like the travis builds are failing because |
checking. They just did a release, it might be related to that Update: yep: pouchdb/pouchdb#6613 |
I’ve restarted the builds, I think the problem got fixed by another patch release. I also started a PR that fixes the versions for |
cfc37e8
to
c381cf8
Compare
Ok, looks like we're back! 🙌 I could just use your input on how to cover the lines below. In the meantime, I'll keep plugging away to see if I can figure it out.
|
I would test it with a unit test. Pass in
Here I think we could do an integration test. Create the remote database with PouchDB, create a document init, then delete it. Now initialise Let me know if that works :) |
53946dd
to
56a4351
Compare
Worked perfectly! Thanks for your help! I increased the code coverage to 100% and rebased to a single test and fix commit each. Please let me know what you think when you get a chance. 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really good work! I just have a few comments :) Let me know if I can help with anything
tests/utils/pouchdb.js
Outdated
|
||
// module.exports = require('pouchdb-monorepo/packages/node_modules/pouchdb-core') | ||
// .plugin(require('pouchdb-monorepo/packages/node_modules/pouchdb-replication')) | ||
// .plugin(require('pouchdb-monorepo/packages/node_modules/pouchdb-adapter-memory')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess these are remains from debugging?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I was wondering about that, too. I should have removed it. Sorry about that!
lib/with-id-prefix.js
Outdated
@@ -3,7 +3,7 @@ module.exports = withIdPrefix | |||
var EventEmitter = require('events').EventEmitter | |||
|
|||
/** | |||
* returns API with all CRUD methods scoped to id prefix | |||
* returns API with all CRUD methods scoped to id prefix` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you remove the backtick?
tests/integration/events.js
Outdated
@@ -388,6 +390,28 @@ test('store.on("change") with adding one', function (t) { | |||
}) | |||
}) | |||
|
|||
test('store.on("add") should emit after store.add() promise resolved', function (t) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should say:
store.on("add") should not emit after store.add() promise resolved
tests/unit/handle-changes-test.js
Outdated
|
||
handleChanges(state) | ||
|
||
Promise.resolve(state.bootstrap) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you do here exactly :) state.bootstrap
is undefined, so you this is basically a noop
What I meant in my comment was to do something like this
var resolveAllDocs
state.db.allDocs = new Promise(function (resolve) {
resolveAllDocs = resolve
})
That way you can resolve db.allDocs()
at any point you like. Now you can emit the events and then call resolveAllDocs()
.
I think the current est works because handleChanges
runs all synchronous, so the callback passed to state.db.changes().on('change', callback)
gets executed immediately, while simple.spy().resolveWith({ rows: [] })
resolves on next tick.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
state.bootstrap is undefined
Actually, state.bootstrap
is a Promise, per the assignment here. What happens is, handleChange
runs synchronously and stores the pending Promise from state.db.allDocs().then()
call into state.bootstrap
, so state.bootstrap
is actually a Promise.
But I see your point -- there's nothing to prevent resolving the .then()
call chain after the state.db.allDocs()
, which we want to control the flow of. I'll work on refactoring that. 👍
tests/unit/handle-changes-test.js
Outdated
var state = { | ||
db: { | ||
allDocs: simple.spy().resolveWith({ rows: [] }), | ||
changes: simple.spy().returnWith(changesSpy) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m surprised that simple.spy() works here :) I think using simple.mock()
would make more sense, a .spy()
is usually used to count how often a method was called without altering its behaviour, while a .mock()
can change the behavior, too. I guess both work in this case, but I’d prefer simple.mock()
Here is an example of what I mean: https://runkit.com/embed/2gcmr9w1xhjs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that's what I like best about simple-mock
-- they know that the dividing lines between mock, stubs, spies, etc is contentious and so they don't provide much of an opinion on the matter. It just works! 😄
But you're right -- they should be mocks.
9c0b592
to
1adb0c7
Compare
I tried this: var resolveAllDocs
state.db.allDocs = new Promise(function (resolve) {
resolveAllDocs = resolve
}) but it didn't work because var resolveAllDocs
state.db.allDocs = function () {
return new Promise(function (resolve) {
resolveAllDocs = resolve
})
} but that didn't work either, as the simple.mock(state.db, 'allDocs').returnWith({
then: simple.spy().callFn(function (callback) {
handleResults = callback
})
}) Please let me know if there's a better way to do this and thanks so much for your help! p.s. I noticed that the travis build is failing, but it's not obvious to me why. |
1adb0c7
to
44b00b0
Compare
Actually, I found another typo that I could fix, so I was able to do another commit. 🙂 But for future reference, is there a way to restart a travis build, or is that something that only the travis account owner can do? |
I’m sorry, it was late yesterday and I didn’t try it out myself. Try this var resolveAllDocs
var promise = new Promise(function (resolve) {
resolveAllDocs = resolve
})
state.db.allDocs = function () { return promise } That should do. This is the so called "deferred pattern" which is an anti pattern, but I don’t have a better idea, and it’s only a test, not implementation, so we should be fine :D |
44b00b0
to
240b61b
Compare
@gr2m - ok, I think I've got it now. Please let me know what you think. Thanks! |
Okay this looks good now, great work, thanks Rob! |
Thanks, Gregor! It would be disingenuous to take much of the credit, though, as you did most of the hard work! 🙂 |
This is a workaround for the PouchDB feature request at pouchdb/pouchdb#6552