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

Added missing ajax unit tests

parent 736caa39
No related branches found
No related tags found
No related merge requests found
......@@ -39,7 +39,6 @@ export default {
state.token = value
if (value) {
state.tokenData = jwtDecode(value)
console.log(state.tokenData)
} else {
state.tokenData = {}
}
......@@ -50,7 +49,7 @@ export default {
},
actions: {
// 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 => {
logger.default.info('Successfully logged in as', credentials.username)
commit('token', response.data.token)
......
var sinon = require('sinon')
import moxios from 'moxios'
import store from '@/store/auth'
import { testAction } from '../../utils'
describe('store/auth', () => {
var sandbox
beforeEach(function () {
sandbox = sinon.sandbox.create()
moxios.install()
})
afterEach(function () {
sandbox.restore()
moxios.uninstall()
})
describe('mutations', () => {
it('profile', () => {
const state = {}
......@@ -104,5 +117,84 @@ describe('store/auth', () => {
]
}, 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
let promise = action({ commit, dispatch, ...params }, payload)
if (promise) {
promise.then(end)
return promise.then(end)
} else {
end()
return end()
}
}
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