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

Added tests for auth, favorites and player in store

parent 03364dd0
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
"start": "node build/dev-server.js", "start": "node build/dev-server.js",
"build": "node build/build.js", "build": "node build/build.js",
"unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run", "unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run",
"unit-watch": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js",
"e2e": "node test/e2e/runner.js", "e2e": "node test/e2e/runner.js",
"test": "npm run unit && npm run e2e", "test": "npm run unit && npm run e2e",
"lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs" "lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs"
......
...@@ -44,6 +44,7 @@ export default { ...@@ -44,6 +44,7 @@ export default {
state.token = value state.token = value
if (value) { if (value) {
state.tokenData = jwtDecode(value) state.tokenData = jwtDecode(value)
console.log(state.tokenData)
} else { } else {
state.tokenData = {} state.tokenData = {}
} }
......
...@@ -61,8 +61,8 @@ export default { ...@@ -61,8 +61,8 @@ export default {
} }
}, },
actions: { actions: {
incrementVolume (context, value) { incrementVolume ({commit, state}, value) {
context.commit('volume', context.state.volume + value) commit('volume', state.volume + value)
}, },
stop (context) { stop (context) {
}, },
......
...@@ -21,7 +21,7 @@ module.exports = function (config) { ...@@ -21,7 +21,7 @@ module.exports = function (config) {
preprocessors: { preprocessors: {
'./index.js': ['webpack', 'sourcemap'] './index.js': ['webpack', 'sourcemap']
}, },
captureTimeout: 5000, captureTimeout: 15000,
retryLimit: 1, retryLimit: 1,
webpack: webpackConfig, webpack: webpackConfig,
webpackMiddleware: { webpackMiddleware: {
......
import store from '@/store/player' import store from '@/store/auth'
describe('mutations', () => { import { testAction } from '../../utils'
it('set volume', () => {
// mock state describe('store/auth', () => {
const state = { volume: 0 } describe('mutations', () => {
// apply mutation it('profile', () => {
store.mutations.volume(state, 0.9) const state = {}
// assert result store.mutations.profile(state, {})
expect(state.volume).to.equal(0.9) expect(state.profile).to.deep.equal({})
}) })
it('set volume max 1', () => { it('username', () => {
// mock state const state = {}
const state = { volume: 0 } store.mutations.username(state, 'world')
// apply mutation expect(state.username).to.equal('world')
store.mutations.volume(state, 2) })
// assert result it('authenticated true', () => {
expect(state.volume).to.equal(1) const state = {}
}) store.mutations.authenticated(state, true)
it('set volume min to 0', () => { expect(state.authenticated).to.equal(true)
// mock state })
const state = { volume: 0.5 } it('authenticated false', () => {
// apply mutation const state = {
store.mutations.volume(state, -2) username: 'dummy',
// assert result token: 'dummy',
expect(state.volume).to.equal(0) tokenData: 'dummy',
}) profile: 'dummy',
it('increment volume', () => { availablePermissions: 'dummy'
// mock state }
const state = { volume: 0 } store.mutations.authenticated(state, false)
// apply mutation expect(state.authenticated).to.equal(false)
store.mutations.incrementVolume(state, 0.1) expect(state.username).to.equal(null)
// assert result expect(state.token).to.equal(null)
expect(state.volume).to.equal(0.1) expect(state.tokenData).to.equal(null)
expect(state.profile).to.equal(null)
expect(state.availablePermissions).to.deep.equal({})
})
it('token null', () => {
const state = {}
store.mutations.token(state, null)
expect(state.token).to.equal(null)
expect(state.tokenData).to.deep.equal({})
})
it('token real', () => {
// generated on http://kjur.github.io/jsjws/tool_jwt.html
const state = {}
let token = 'eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJpc3MiOiJodHRwczovL2p3dC1pZHAuZXhhbXBsZS5jb20iLCJzdWIiOiJtYWlsdG86bWlrZUBleGFtcGxlLmNvbSIsIm5iZiI6MTUxNTUzMzQyOSwiZXhwIjoxNTE1NTM3MDI5LCJpYXQiOjE1MTU1MzM0MjksImp0aSI6ImlkMTIzNDU2IiwidHlwIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9yZWdpc3RlciJ9.'
let tokenData = {
iss: 'https://jwt-idp.example.com',
sub: 'mailto:mike@example.com',
nbf: 1515533429,
exp: 1515537029,
iat: 1515533429,
jti: 'id123456',
typ: 'https://example.com/register'
}
store.mutations.token(state, token)
expect(state.token).to.equal(token)
expect(state.tokenData).to.deep.equal(tokenData)
})
it('permissions', () => {
const state = { availablePermissions: {} }
store.mutations.permission(state, {key: 'admin', status: true})
expect(state.availablePermissions).to.deep.equal({admin: true})
})
}) })
it('increment volume max 1', () => { describe('getters', () => {
// mock state it('header', () => {
const state = { volume: 0 } const state = { token: 'helloworld' }
// apply mutation expect(store.getters['header'](state)).to.equal('JWT helloworld')
store.mutations.incrementVolume(state, 2) })
// assert result
expect(state.volume).to.equal(1)
}) })
it('increment volume min to 0', () => { describe('actions', () => {
// mock state it('logout', (done) => {
const state = { volume: 0.5 } testAction({
// apply mutation action: store.actions.logout,
store.mutations.incrementVolume(state, -2) params: {state: {}},
// assert result expectedMutations: [
expect(state.volume).to.equal(0) { type: 'authenticated', payload: false }
]
}, done)
})
it('check jwt null', (done) => {
testAction({
action: store.actions.check,
params: {state: {}},
expectedMutations: [
{ type: 'authenticated', payload: false }
]
}, done)
})
it('check jwt set', (done) => {
testAction({
action: store.actions.check,
params: {state: {token: 'test', username: 'user'}},
expectedMutations: [
{ type: 'authenticated', payload: true },
{ type: 'username', payload: 'user' },
{ type: 'token', payload: 'test' }
],
expectedActions: [
{ type: 'fetchProfile' },
{ type: 'refreshToken' }
]
}, done)
})
}) })
}) })
import store from '@/store/favorites'
import { testAction } from '../../utils'
describe('store/favorites', () => {
describe('mutations', () => {
it('track true', () => {
const state = { tracks: [] }
store.mutations.track(state, {id: 1, value: true})
expect(state.tracks).to.deep.equal([1])
expect(state.count).to.deep.equal(1)
})
it('track false', () => {
const state = { tracks: [1] }
store.mutations.track(state, {id: 1, value: false})
expect(state.tracks).to.deep.equal([])
expect(state.count).to.deep.equal(0)
})
})
describe('getters', () => {
it('isFavorite true', () => {
const state = { tracks: [1] }
expect(store.getters['isFavorite'](state)(1)).to.equal(true)
})
it('isFavorite false', () => {
const state = { tracks: [] }
expect(store.getters['isFavorite'](state)(1)).to.equal(false)
})
})
describe('actions', () => {
it('toggle true', (done) => {
testAction({
action: store.actions.toggle,
payload: 1,
params: {getters: {isFavorite: () => false}},
expectedActions: [
{ type: 'set', payload: {id: 1, value: true} }
]
}, done)
})
it('toggle true', (done) => {
testAction({
action: store.actions.toggle,
payload: 1,
params: {getters: {isFavorite: () => true}},
expectedActions: [
{ type: 'set', payload: {id: 1, value: false} }
]
}, done)
})
})
})
import store from '@/store/player'
import { testAction } from '../../utils'
describe('store/player', () => {
describe('mutations', () => {
it('set volume', () => {
const state = { volume: 0 }
store.mutations.volume(state, 0.9)
expect(state.volume).to.equal(0.9)
})
it('set volume max 1', () => {
const state = { volume: 0 }
store.mutations.volume(state, 2)
expect(state.volume).to.equal(1)
})
it('set volume min to 0', () => {
const state = { volume: 0.5 }
store.mutations.volume(state, -2)
expect(state.volume).to.equal(0)
})
it('increment volume', () => {
const state = { volume: 0 }
store.mutations.incrementVolume(state, 0.1)
expect(state.volume).to.equal(0.1)
})
it('increment volume max 1', () => {
const state = { volume: 0 }
store.mutations.incrementVolume(state, 2)
expect(state.volume).to.equal(1)
})
it('increment volume min to 0', () => {
const state = { volume: 0.5 }
store.mutations.incrementVolume(state, -2)
expect(state.volume).to.equal(0)
})
it('set duration', () => {
const state = { duration: 42 }
store.mutations.duration(state, 14)
expect(state.duration).to.equal(14)
})
it('set errored', () => {
const state = { errored: false }
store.mutations.errored(state, true)
expect(state.errored).to.equal(true)
})
it('set looping', () => {
const state = { looping: 1 }
store.mutations.looping(state, 2)
expect(state.looping).to.equal(2)
})
it('set playing', () => {
const state = { playing: false }
store.mutations.playing(state, true)
expect(state.playing).to.equal(true)
})
it('set current time', () => {
const state = { currentTime: 1 }
store.mutations.currentTime(state, 2)
expect(state.currentTime).to.equal(2)
})
it('toggle looping from 0', () => {
const state = { looping: 0 }
store.mutations.toggleLooping(state)
expect(state.looping).to.equal(1)
})
it('toggle looping from 1', () => {
const state = { looping: 1 }
store.mutations.toggleLooping(state)
expect(state.looping).to.equal(2)
})
it('toggle looping from 2', () => {
const state = { looping: 2 }
store.mutations.toggleLooping(state)
expect(state.looping).to.equal(0)
})
})
describe('getters', () => {
it('durationFormatted', () => {
const state = { duration: 12.51 }
expect(store.getters['durationFormatted'](state)).to.equal('00:13')
})
it('currentTimeFormatted', () => {
const state = { currentTime: 12.51 }
expect(store.getters['currentTimeFormatted'](state)).to.equal('00:13')
})
it('progress', () => {
const state = { currentTime: 4, duration: 10 }
expect(store.getters['progress'](state)).to.equal(40)
})
})
describe('actions', () => {
it('incrementVolume', (done) => {
testAction({
action: store.actions.incrementVolume,
payload: 0.2,
params: {state: {volume: 0.7}},
expectedMutations: [
{ type: 'volume', payload: 0.7 + 0.2 }
]
}, done)
})
it('toggle play false', (done) => {
testAction({
action: store.actions.togglePlay,
params: {state: {playing: false}},
expectedMutations: [
{ type: 'playing', payload: true }
]
}, done)
})
it('toggle play true', (done) => {
testAction({
action: store.actions.togglePlay,
params: {state: {playing: true}},
expectedMutations: [
{ type: 'playing', payload: false }
]
}, done)
})
it('trackEnded', (done) => {
testAction({
action: store.actions.trackEnded,
payload: {test: 'track'},
expectedActions: [
{ type: 'trackListened', payload: {test: 'track'} },
{ type: 'queue/next', payload: null, options: {root: true} }
]
}, done)
})
it('trackErrored', (done) => {
testAction({
action: store.actions.trackErrored,
payload: {test: 'track'},
expectedMutations: [
{ type: 'errored', payload: true }
],
expectedActions: [
{ type: 'queue/next', payload: null, options: {root: true} }
]
}, done)
})
it('updateProgress', (done) => {
testAction({
action: store.actions.updateProgress,
payload: 1,
expectedMutations: [
{ type: 'currentTime', payload: 1 }
]
}, done)
})
})
})
// 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()
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment