Skip to content

Commit

Permalink
fix: better error if local changes cannot be synced (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjinlo authored and gr2m committed Sep 22, 2016
1 parent 5727440 commit b16c2fa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ function init (hoodie) {
hoodie.account.on('pre:signout', function (options) {
options.hooks.push(function () {
return hoodie.store.push()
.catch(function (error) {
if (error.status !== 401) {
throw error
}

error.message = 'Local changes could not be synced, sign in first'
throw error
})
})
})
hoodie.account.on('post:signout', function (options) {
Expand Down
32 changes: 32 additions & 0 deletions tests/specs/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ test('"hoodie.store.push" is called on "pre:signout"', function (t) {
store: {
push: function () {
t.pass('store.push called on "signout"')
return {catch: function () {}}
}
},
connectionStatus: {
Expand All @@ -225,6 +226,37 @@ test('"hoodie.store.push" is called on "pre:signout"', function (t) {
hooks[0]()
})

test('"hoodie.store.push" returns better error message if local changes cannot be synced', function (t) {
t.plan(2)

var UnauthorizedError = new Error('unauthorized')
UnauthorizedError.status = 401

var hoodie = {
account: {
id: 0,
on: simple.stub(),
isSignedIn: simple.stub()
},
store: {
push: simple.stub().rejectWith(UnauthorizedError)
},
connectionStatus: {
on: simple.stub()
}
}

init(hoodie)
var signOutHandler = findEventHandler(hoodie.account.on.calls, 'pre:signout')
var hooks = []
signOutHandler({hooks: hooks})
t.is(hooks.length, 1, 'one pre:signout hook registered')
hooks[0]()
.catch(function (error) {
t.is(error.message, 'Local changes could not be synced, sign in first')
})
})

test('"hoodie.store.*" is *not* called when "hoodie.account.isSignedIn()" returns "false"', function (t) {
t.plan(2)

Expand Down

0 comments on commit b16c2fa

Please sign in to comment.