Created
August 29, 2017 23:40
-
-
Save mb8z/1acf934d129942693d793f879ab13c85 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 configureMockStore from 'redux-mock-store'; | |
import thunk from 'redux-thunk'; | |
import { middleware as reduxPackMiddleware } from 'redux-pack'; | |
import * as Cookies from 'js-cookie'; | |
import nock from 'nock'; | |
import httpAdapter from 'axios/lib/adapters/http'; | |
import API from '../../services/api'; | |
import { login } from '../authentication'; | |
/* eslint no-param-reassign:0 */ | |
const setupStore = (cookies, api) => { | |
api.instance.defaults.adapter = httpAdapter; | |
api.instance.defaults.host = 'http://localhost:8000/api'; | |
const thunkArguments = { api, cookies }; | |
const middlewares = [thunk.withExtraArgument(thunkArguments), reduxPackMiddleware]; | |
return configureMockStore(middlewares)(); | |
}; | |
describe('actions/authentication', () => { | |
let store; | |
let cookies; | |
let api; | |
beforeAll(() => { | |
cookies = Cookies; | |
api = API.create(); | |
store = setupStore(cookies, api); | |
}); | |
it('creates correct flow during login process', () => { | |
// Mocks | |
const token = 'some.random.token'; | |
// Intercepting the api call: | |
nock('http://localhost:8000/api') | |
.post('/login/') | |
.reply(200, { | |
token, | |
}); | |
console.log('Test', typeof document); // Tutaj document jest widziany jako obiekt | |
// Dispatching: | |
store.dispatch(login(validUser)).then(() => { | |
// TODO: Expect statements | |
}).catch((err) => { | |
// TODO: Catch errors? | |
}); | |
}); | |
}); | |
// Login method: | |
export const login = data => (dispatch, getState, { api, cookies }) => | |
dispatch({ | |
type: LOGIN_LOAD, | |
promise: api.login(data), | |
}).then((response) => { | |
if (response.error) { | |
// TODO: handle error | |
} | |
const { token } = response.payload.data; | |
console.log('Document:', typeof document); // Tutaj już dostaję undefined | |
if (token) { | |
cookies.set('jwt', token, { path: '/' }); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment