-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1409e29
commit 1dfdd75
Showing
6 changed files
with
160 additions
and
5 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { assert } from 'chai' | ||
import Alt from '../dist/alt-with-runtime' | ||
import sinon from 'sinon' | ||
|
||
export default { | ||
'catch failed dispatches': { | ||
'uncaught dispatches result in an error'() { | ||
const alt = new Alt() | ||
const actions = alt.generateActions('fire') | ||
|
||
class Uncaught { | ||
constructor() { | ||
this.bindListeners({ fire: actions.FIRE }) | ||
} | ||
|
||
fire() { | ||
throw new Error('oops') | ||
} | ||
} | ||
|
||
const uncaught = alt.createStore(Uncaught) | ||
|
||
assert.throws(() => actions.fire()) | ||
}, | ||
|
||
'errors can be caught though'() { | ||
const alt = new Alt() | ||
const actions = alt.generateActions('fire') | ||
|
||
class Caught { | ||
constructor() { | ||
this.x = 0 | ||
this.bindListeners({ fire: actions.FIRE }) | ||
|
||
this.on('error', () => { | ||
this.x = 1 | ||
}) | ||
} | ||
|
||
fire() { | ||
throw new Error('oops') | ||
} | ||
} | ||
|
||
const caught = alt.createStore(Caught) | ||
|
||
const storeListener = sinon.spy() | ||
|
||
caught.listen(storeListener) | ||
|
||
assert(caught.getState().x === 0) | ||
assert.doesNotThrow(() => actions.fire()) | ||
assert(caught.getState().x === 1) | ||
|
||
assert.notOk(storeListener.calledOnce, 'the store did not emit a change') | ||
|
||
caught.unlisten(storeListener) | ||
}, | ||
|
||
'you have to emit changes yourself'() { | ||
const alt = new Alt() | ||
const actions = alt.generateActions('fire') | ||
|
||
class CaughtReturn { | ||
constructor() { | ||
this.x = 0 | ||
this.bindListeners({ fire: actions.FIRE }) | ||
|
||
this.on('error', () => { | ||
this.x = 1 | ||
this.emitChange() | ||
}) | ||
} | ||
|
||
fire() { | ||
throw new Error('oops') | ||
} | ||
} | ||
|
||
const caughtReturn = alt.createStore(CaughtReturn) | ||
|
||
const storeListener = sinon.spy() | ||
|
||
caughtReturn.listen(storeListener) | ||
|
||
assert(caughtReturn.getState().x === 0) | ||
assert.doesNotThrow(() => actions.fire()) | ||
assert(caughtReturn.getState().x === 1) | ||
|
||
assert.ok(storeListener.calledOnce) | ||
|
||
caughtReturn.unlisten(storeListener) | ||
}, | ||
} | ||
} |