Created
May 17, 2017 10:59
-
-
Save beijaflor/69a499a055818d25a1789c69aa85739b to your computer and use it in GitHub Desktop.
test helper for promise and vuex
This file contains hidden or 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
// 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