Added missing ajax unit tests
This commit is contained in:
parent
736caa399a
commit
90611ffacc
|
@ -39,7 +39,6 @@ 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 = {}
|
||||||
}
|
}
|
||||||
|
@ -50,7 +49,7 @@ export default {
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
// Send a request to the login URL and save the returned JWT
|
// Send a request to the login URL and save the returned JWT
|
||||||
login ({commit, dispatch, state}, {next, credentials, onError}) {
|
login ({commit, dispatch}, {next, credentials, onError}) {
|
||||||
return axios.post('token/', credentials).then(response => {
|
return axios.post('token/', credentials).then(response => {
|
||||||
logger.default.info('Successfully logged in as', credentials.username)
|
logger.default.info('Successfully logged in as', credentials.username)
|
||||||
commit('token', response.data.token)
|
commit('token', response.data.token)
|
||||||
|
|
|
@ -1,8 +1,21 @@
|
||||||
|
var sinon = require('sinon')
|
||||||
|
import moxios from 'moxios'
|
||||||
import store from '@/store/auth'
|
import store from '@/store/auth'
|
||||||
|
|
||||||
import { testAction } from '../../utils'
|
import { testAction } from '../../utils'
|
||||||
|
|
||||||
describe('store/auth', () => {
|
describe('store/auth', () => {
|
||||||
|
var sandbox
|
||||||
|
|
||||||
|
beforeEach(function () {
|
||||||
|
sandbox = sinon.sandbox.create()
|
||||||
|
moxios.install()
|
||||||
|
})
|
||||||
|
afterEach(function () {
|
||||||
|
sandbox.restore()
|
||||||
|
moxios.uninstall()
|
||||||
|
})
|
||||||
|
|
||||||
describe('mutations', () => {
|
describe('mutations', () => {
|
||||||
it('profile', () => {
|
it('profile', () => {
|
||||||
const state = {}
|
const state = {}
|
||||||
|
@ -104,5 +117,84 @@ describe('store/auth', () => {
|
||||||
]
|
]
|
||||||
}, done)
|
}, done)
|
||||||
})
|
})
|
||||||
|
it('login success', (done) => {
|
||||||
|
moxios.stubRequest('token/', {
|
||||||
|
status: 200,
|
||||||
|
response: {
|
||||||
|
token: 'test'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const credentials = {
|
||||||
|
username: 'bob'
|
||||||
|
}
|
||||||
|
testAction({
|
||||||
|
action: store.actions.login,
|
||||||
|
payload: {credentials: credentials},
|
||||||
|
expectedMutations: [
|
||||||
|
{ type: 'token', payload: 'test' },
|
||||||
|
{ type: 'username', payload: 'bob' },
|
||||||
|
{ type: 'authenticated', payload: true }
|
||||||
|
],
|
||||||
|
expectedActions: [
|
||||||
|
{ type: 'fetchProfile' }
|
||||||
|
]
|
||||||
|
}, done)
|
||||||
|
})
|
||||||
|
it('login error', (done) => {
|
||||||
|
moxios.stubRequest('token/', {
|
||||||
|
status: 500,
|
||||||
|
response: {
|
||||||
|
token: 'test'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
const credentials = {
|
||||||
|
username: 'bob'
|
||||||
|
}
|
||||||
|
let spy = sandbox.spy()
|
||||||
|
testAction({
|
||||||
|
action: store.actions.login,
|
||||||
|
payload: {credentials: credentials, onError: spy}
|
||||||
|
}, () => {
|
||||||
|
expect(spy.calledOnce).to.equal(true)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
it('fetchProfile', (done) => {
|
||||||
|
const profile = {
|
||||||
|
username: 'bob',
|
||||||
|
permissions: {
|
||||||
|
admin: {
|
||||||
|
status: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
moxios.stubRequest('users/users/me/', {
|
||||||
|
status: 200,
|
||||||
|
response: profile
|
||||||
|
})
|
||||||
|
testAction({
|
||||||
|
action: store.actions.fetchProfile,
|
||||||
|
expectedMutations: [
|
||||||
|
{ type: 'profile', payload: profile },
|
||||||
|
{ type: 'permission', payload: {key: 'admin', status: true} }
|
||||||
|
],
|
||||||
|
expectedActions: [
|
||||||
|
{ type: 'favorites/fetch', payload: null, options: {root: true} }
|
||||||
|
]
|
||||||
|
}, done)
|
||||||
|
})
|
||||||
|
it('refreshToken', (done) => {
|
||||||
|
moxios.stubRequest('token/refresh/', {
|
||||||
|
status: 200,
|
||||||
|
response: {token: 'newtoken'}
|
||||||
|
})
|
||||||
|
testAction({
|
||||||
|
action: store.actions.refreshToken,
|
||||||
|
params: {state: {token: 'oldtoken'}},
|
||||||
|
expectedMutations: [
|
||||||
|
{ type: 'token', payload: 'newtoken' }
|
||||||
|
]
|
||||||
|
}, done)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -66,8 +66,8 @@ export const testAction = ({action, payload, params, expectedMutations, expected
|
||||||
// call the action with mocked store and arguments
|
// call the action with mocked store and arguments
|
||||||
let promise = action({ commit, dispatch, ...params }, payload)
|
let promise = action({ commit, dispatch, ...params }, payload)
|
||||||
if (promise) {
|
if (promise) {
|
||||||
promise.then(end)
|
return promise.then(end)
|
||||||
} else {
|
} else {
|
||||||
end()
|
return end()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue