-
-
Save Fl0pZz/9ddd0467bc04bd79d73c366229751d58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as user from './user-api' | |
export default { | |
user | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
axios.defaults.baseURL = 'http://127.0.0.1:8000' | |
export const setToken = (token) => { axios.defaults.headers.common['Authorization'] = token } | |
export const deleteToken = () => { delete axios.defaults.headers.common['Authorization'] } | |
export default axios |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as axios from './../../../api/axios-custom' | |
import * as types from './../../mutation-types' | |
import api from './../../../api/api' | |
export const asyncLogin = ({ commit, getters }, { email, password }) => { | |
return api.user.login({ | |
email: email, | |
password: password | |
}) | |
.then(response => { | |
commit(types.LOGIN, response.data) | |
axios.setToken(getters.getToken) | |
return response | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { expect } from 'chai' | |
import { describe, it } from 'mocha' | |
import { state as initState } from './index' | |
import * as types from './../../mutation-types' | |
const actionsInjector = require('inject!./user-actions') | |
const actions = actionsInjector({ | |
'./../../../api/api': { | |
'user.login': () => new Promise((resolve) => { | |
resolve({ | |
data: { | |
token: '9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b', | |
username: 'ololo' | |
} | |
}) | |
}) | |
} | |
}) | |
const testAction = (action, payload, state, expectedMutations, done) => { | |
let count = 0 | |
const commit = (type, payload) => { | |
const mutation = expectedMutations[count] | |
expect(mutation.type).to.equal(type) | |
if (payload) { | |
expect(mutation.payload).to.eql(payload) | |
} | |
++count | |
if (count >= expectedMutations.length) { | |
done() | |
} | |
} | |
action({ commit, state }, payload) | |
if (expectedMutations.length === 0) { | |
expect(count).to.equal(0) | |
done() | |
} | |
} | |
describe('user', () => { | |
describe('actions', () => { | |
it('login', done => { | |
const payload = { | |
email: 'email', | |
password: 'password' | |
} | |
const response = { | |
token: '9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b', | |
username: 'ololo' | |
} | |
testAction(actions.asyncLogin, payload, initState, [ | |
{ type: types.LOGIN, payload: response } | |
], done) | |
}) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from './axios-custom' | |
export const login = (data) => axios.post('/account/login/', data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment