Added missing ajax unit tests

This commit is contained in:
Eliot Berriot 2018-01-11 23:02:45 +01:00
parent 736caa399a
commit 90611ffacc
No known key found for this signature in database
GPG key ID: DD6965E2476E5C27
3 changed files with 95 additions and 4 deletions

View file

@ -1,8 +1,21 @@
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)
})
})
})