Skip to content
Snippets Groups Projects
Verified Commit 736caa39 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Unit tests for radios store

parent e4edf55c
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
"js-logger": "^1.3.0", "js-logger": "^1.3.0",
"jwt-decode": "^2.2.0", "jwt-decode": "^2.2.0",
"lodash": "^4.17.4", "lodash": "^4.17.4",
"moxios": "^0.4.0",
"semantic-ui-css": "^2.2.10", "semantic-ui-css": "^2.2.10",
"vue": "^2.3.3", "vue": "^2.3.3",
"vue-lazyload": "^1.1.4", "vue-lazyload": "^1.1.4",
......
...@@ -12,7 +12,7 @@ module.exports = function (config) { ...@@ -12,7 +12,7 @@ module.exports = function (config) {
// http://karma-runner.github.io/0.13/config/browsers.html // http://karma-runner.github.io/0.13/config/browsers.html
// 2. add it to the `browsers` array below. // 2. add it to the `browsers` array below.
browsers: ['PhantomJS'], browsers: ['PhantomJS'],
frameworks: ['mocha', 'sinon-chai', 'phantomjs-shim'], frameworks: ['mocha', 'sinon-stub-promise', 'sinon-chai', 'phantomjs-shim'],
reporters: ['spec', 'coverage'], reporters: ['spec', 'coverage'],
files: [ files: [
'../../node_modules/es6-promise/dist/es6-promise.auto.js', '../../node_modules/es6-promise/dist/es6-promise.auto.js',
......
var sinon = require('sinon')
import _ from 'lodash' import _ from 'lodash'
import store from '@/store/queue' import store from '@/store/queue'
import { testAction } from '../../utils' import { testAction } from '../../utils'
describe('store/queue', () => { 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', () => { describe('mutations', () => {
it('currentIndex', () => { it('currentIndex', () => {
const state = {} const state = {}
...@@ -302,7 +314,7 @@ describe('store/queue', () => { ...@@ -302,7 +314,7 @@ describe('store/queue', () => {
}, done) }, done)
}) })
it('shuffle', (done) => { it('shuffle', (done) => {
let _shuffle = sinon.stub(_, 'shuffle') let _shuffle = sandbox.stub(_, 'shuffle')
let tracks = [1, 2, 3] let tracks = [1, 2, 3]
let shuffledTracks = [2, 3, 1] let shuffledTracks = [2, 3, 1]
_shuffle.returns(shuffledTracks) _shuffle.returns(shuffledTracks)
......
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)
})
})
})
...@@ -33,7 +33,6 @@ export const testAction = ({action, payload, params, expectedMutations, expected ...@@ -33,7 +33,6 @@ export const testAction = ({action, payload, params, expectedMutations, expected
// mock dispatch // mock dispatch
const dispatch = (type, payload, options) => { const dispatch = (type, payload, options) => {
const a = expectedActions[actionsCount] const a = expectedActions[actionsCount]
try { try {
expect(a.type).to.equal(type) expect(a.type).to.equal(type)
if (payload) { if (payload) {
...@@ -52,17 +51,23 @@ export const testAction = ({action, payload, params, expectedMutations, expected ...@@ -52,17 +51,23 @@ export const testAction = ({action, payload, params, expectedMutations, expected
} }
} }
// call the action with mocked store and arguments let end = function () {
action({ commit, dispatch, ...params }, payload) // check if no mutations should have been dispatched
if (expectedMutations.length === 0) {
// check if no mutations should have been dispatched expect(mutationsCount).to.equal(0)
if (expectedMutations.length === 0) { }
expect(mutationsCount).to.equal(0) if (expectedActions.length === 0) {
} expect(actionsCount).to.equal(0)
if (expectedActions.length === 0) { }
expect(actionsCount).to.equal(0) if (isOver()) {
done()
}
} }
if (isOver()) { // call the action with mocked store and arguments
done() let promise = action({ commit, dispatch, ...params }, payload)
if (promise) {
promise.then(end)
} else {
end()
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment