Unit tests for radios store

This commit is contained in:
Eliot Berriot 2018-01-11 22:32:37 +01:00
parent e4edf55c47
commit 736caa399a
No known key found for this signature in database
GPG Key ID: DD6965E2476E5C27
5 changed files with 118 additions and 14 deletions

View File

@ -20,6 +20,7 @@
"js-logger": "^1.3.0",
"jwt-decode": "^2.2.0",
"lodash": "^4.17.4",
"moxios": "^0.4.0",
"semantic-ui-css": "^2.2.10",
"vue": "^2.3.3",
"vue-lazyload": "^1.1.4",

View File

@ -12,7 +12,7 @@ module.exports = function (config) {
// http://karma-runner.github.io/0.13/config/browsers.html
// 2. add it to the `browsers` array below.
browsers: ['PhantomJS'],
frameworks: ['mocha', 'sinon-chai', 'phantomjs-shim'],
frameworks: ['mocha', 'sinon-stub-promise', 'sinon-chai', 'phantomjs-shim'],
reporters: ['spec', 'coverage'],
files: [
'../../node_modules/es6-promise/dist/es6-promise.auto.js',

View File

@ -1,9 +1,21 @@
var sinon = require('sinon')
import _ from 'lodash'
import store from '@/store/queue'
import { testAction } from '../../utils'
describe('store/queue', () => {
var sandbox
beforeEach(function () {
// Create a sandbox for the test
sandbox = sinon.sandbox.create()
})
afterEach(function () {
// Restore all the things made through the sandbox
sandbox.restore()
})
describe('mutations', () => {
it('currentIndex', () => {
const state = {}
@ -302,7 +314,7 @@ describe('store/queue', () => {
}, done)
})
it('shuffle', (done) => {
let _shuffle = sinon.stub(_, 'shuffle')
let _shuffle = sandbox.stub(_, 'shuffle')
let tracks = [1, 2, 3]
let shuffledTracks = [2, 3, 1]
_shuffle.returns(shuffledTracks)

View File

@ -0,0 +1,86 @@
var sinon = require('sinon')
import moxios from 'moxios'
import store from '@/store/radios'
import { testAction } from '../../utils'
describe('store/radios', () => {
var sandbox
beforeEach(function () {
sandbox = sinon.sandbox.create()
moxios.install()
})
afterEach(function () {
sandbox.restore()
moxios.uninstall()
})
describe('mutations', () => {
it('current', () => {
const state = {}
store.mutations.current(state, 1)
expect(state.current).to.equal(1)
})
it('running', () => {
const state = {}
store.mutations.running(state, false)
expect(state.running).to.equal(false)
})
})
describe('actions', () => {
it('start', (done) => {
moxios.stubRequest('radios/sessions/', {
status: 200,
response: {id: 2}
})
testAction({
action: store.actions.start,
payload: {type: 'favorites', objectId: 0, customRadioId: null},
expectedMutations: [
{
type: 'current',
payload: {
type: 'favorites',
objectId: 0,
customRadioId: null,
session: 2
}
},
{ type: 'running', payload: true }
],
expectedActions: [
{ type: 'populateQueue' }
]
}, done)
})
it('stop', (done) => {
testAction({
action: store.actions.stop,
expectedMutations: [
{ type: 'current', payload: null },
{ type: 'running', payload: false }
]
}, done)
})
it('populateQueue', (done) => {
moxios.stubRequest('radios/tracks/', {
status: 201,
response: {track: {id: 1}}
})
testAction({
action: store.actions.populateQueue,
params: {state: {running: true, current: {session: 1}}},
expectedActions: [
{ type: 'queue/append', payload: {track: {id: 1}}, options: {root: true} }
]
}, done)
})
it('populateQueue does nothing when not running', (done) => {
testAction({
action: store.actions.populateQueue,
params: {state: {running: false}},
expectedActions: []
}, done)
})
})
})

View File

@ -33,7 +33,6 @@ export const testAction = ({action, payload, params, expectedMutations, expected
// mock dispatch
const dispatch = (type, payload, options) => {
const a = expectedActions[actionsCount]
try {
expect(a.type).to.equal(type)
if (payload) {
@ -52,17 +51,23 @@ export const testAction = ({action, payload, params, expectedMutations, expected
}
}
let end = function () {
// 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()
}
}
// 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()
let promise = action({ commit, dispatch, ...params }, payload)
if (promise) {
promise.then(end)
} else {
end()
}
}