mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-06 03:09:55 +02:00
Added tests for auth, favorites and player in store
This commit is contained in:
parent
03364dd0d0
commit
bc76048b4a
8 changed files with 381 additions and 50 deletions
68
front/test/unit/utils.js
Normal file
68
front/test/unit/utils.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
// helper for testing action with expected mutations
|
||||
export const testAction = ({action, payload, params, expectedMutations, expectedActions}, done) => {
|
||||
let mutationsCount = 0
|
||||
let actionsCount = 0
|
||||
|
||||
if (!expectedMutations) {
|
||||
expectedMutations = []
|
||||
}
|
||||
if (!expectedActions) {
|
||||
expectedActions = []
|
||||
}
|
||||
const isOver = () => {
|
||||
return mutationsCount >= expectedMutations.length && actionsCount >= expectedActions.length
|
||||
}
|
||||
// mock commit
|
||||
const commit = (type, payload) => {
|
||||
const mutation = expectedMutations[mutationsCount]
|
||||
|
||||
try {
|
||||
expect(mutation.type).to.equal(type)
|
||||
if (payload) {
|
||||
expect(mutation.payload).to.deep.equal(payload)
|
||||
}
|
||||
} catch (error) {
|
||||
done(error)
|
||||
}
|
||||
|
||||
mutationsCount++
|
||||
if (isOver()) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
// mock dispatch
|
||||
const dispatch = (type, payload, options) => {
|
||||
const a = expectedActions[actionsCount]
|
||||
|
||||
try {
|
||||
expect(a.type).to.equal(type)
|
||||
if (payload) {
|
||||
expect(a.payload).to.deep.equal(payload)
|
||||
}
|
||||
if (a.options) {
|
||||
expect(options).to.deep.equal(a.options)
|
||||
}
|
||||
} catch (error) {
|
||||
done(error)
|
||||
}
|
||||
|
||||
actionsCount++
|
||||
if (isOver()) {
|
||||
done()
|
||||
}
|
||||
}
|
||||
|
||||
// call the action with mocked store and arguments
|
||||
action({ commit, dispatch, ...params }, payload)
|
||||
|
||||
// check if no mutations should have been dispatched
|
||||
if (expectedMutations.length === 0) {
|
||||
expect(mutationsCount).to.equal(0)
|
||||
}
|
||||
if (expectedActions.length === 0) {
|
||||
expect(actionsCount).to.equal(0)
|
||||
}
|
||||
if (isOver()) {
|
||||
done()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue