Skip to content

Instantly share code, notes, and snippets.

@beijaflor
Created May 17, 2017 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beijaflor/69a499a055818d25a1789c69aa85739b to your computer and use it in GitHub Desktop.
Save beijaflor/69a499a055818d25a1789c69aa85739b to your computer and use it in GitHub Desktop.
test helper for promise and vuex
// TEST PROMISE OBJECT TO BE FULFILLED OR REJECTED
// ---
// describe('promise', () => {
// it('should be fulfilled', () => {
// return shouldFulfilled(promise()).then((value) => {
// expect(velue).to.be.true
// })
// })
// })
//
// describe('promise', () => {
// it('should be rejected', () => {
// return shouldRejected(promise()).catch((error) => {
// expect(error).have.property('message')
// .and.equal('error')
// })
// })
// })
export function shouldRejected (promise) {
return {
'catch': (fn) => {
return promise.then(
() => { throw new Error('Expected promise to be rejected but it was fulfilled') },
(reason) => { fn.call(promise, reason) }
)
}
}
}
export function shouldFulfilled (promise) {
return {
'then': (fn) => {
return promise.then(
(value) => { fn.call(promise, value) },
(reason) => { throw reason }
)
}
}
}
// TEST VUEX ACTION
// ---
// describe('actions', () => {
// it('getAllProducts', done => {
// testAction(actions.getAllProducts, null, {}, [
// { type: 'REQUEST_PRODUCTS' },
// { type: 'RECEIVE_PRODUCTS', payload: { /* ... */ } }
// ], done)
// })
// })
export function testAction (action, payload, state, expectedMutations, done) {
let count = 0
const commit = (type, payload) => {
const mutation = expectedMutations[count]
try {
expect(mutation.type).to.equal(type)
if (mutation.payload) {
expect(mutation.payload).to.deep.equal(payload)
}
} catch (error) {
done(error)
}
count++
if (count >= expectedMutations.length) {
done()
}
}
action({ commit, state }, payload)
if (expectedMutations.length === 0) {
expect(count).to.equal(0)
done()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment