Skip to content

Instantly share code, notes, and snippets.

@Fl0pZz

Fl0pZz/api.js Secret

Last active January 21, 2017 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fl0pZz/9ddd0467bc04bd79d73c366229751d58 to your computer and use it in GitHub Desktop.
Save Fl0pZz/9ddd0467bc04bd79d73c366229751d58 to your computer and use it in GitHub Desktop.
import * as user from './user-api'
export default {
user
}
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
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
})
}
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)
})
})
})
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