it('creates a async action to fetch the bitcoin value', () => { const expectedActions = [{bitcoin: mockResponse.body, type: FETCH_BITCOIN}] return store.dispatch(fetchBitcoin()).then(() => { expect(store.getActions()).toEqual(expectedActions) }) })
describe('fetchPosts', () => { it('creates FETCHED_POST when fetching posts is done', () => { mockFetch(200, null, { id: 1 }) const store = mockStore({}) return store.dispatch(fetchPosts(1)).then(() => { const expectedActions = [ { type: 'FETCHING_POST', payload: 1 }, { type: 'FETCHED_POST', payload: { id: 1 } }, ] expect(store.getActions()).toEqual(expectedActions) }) }) })
it('should has ADD_USER_PENDING and return ADD_USER_FAILED when name already exist', () => { //add user first getState.users = [...getState.users, {...user}]; store = mockStore(getState); const {name, role} = user; const pendingActions = { type: types.ADD_USER.PENDING }; return store.dispatch(userActions.addUser({name, role})) .then(res => { const expectedActions = store.getActions(); expect(expectedActions).toEqual(expect.arrayContaining([pendingActions])); expect(res).toEqual(types.ADD_USER.FAILED); }) });
test('Should fetch the expenses from firebase', (done) => { const store = createMockStore(defaultAuthState); store.dispatch(startSetExpenses()).then(() => { const actions = store.getActions(); expect(actions[0]).toEqual({ type: 'SET_EXPENSES', expenses }); done(); }) });
it('loads products', () => { const store = mockStore({ products: [] }); moxios.stubRequest('http://localhost:3002/products', { status: 200, response: [ { id: 1, name: 'Product 1', price: 100, image: ''}, { id: 2, name: 'Product 2', price: 200, image: ''}, ] }); store.dispatch(loadProducts()) .then(() => { const actions = store.getActions(); expect(actions.length).toBe(1); }); });
describe('Async Actions', () => { afterEach(() => nock.cleanAll()); it('should create BEGIN_PROMISE_CALL and LOAD_COURSES_SUCCESS when load courses', callback => { // nock('http://example.com/').get('/courses').reply(201, {body: { courses: [{id: 'clean-code', title: 'Clean Code'}]}}); const store = mockStore({ courses: [] }); store.dispatch(loadCourses()).then(() => { const actions = store.getActions(); expect(actions[0].type).to.equal(BEGIN_PROMISE_CALL); expect(actions[1].type).to.equal(LOAD_COURSES_SUCCESS); expect(actions[1].payload.length).to.equal(5); /* I expected the number as it simulate the courses in the mok api */ callback(); }); }); });
it('should return DELETE_USER_SUCCESS', () => { //add user first getState.users = [...getState.users, {...user}]; store = mockStore(getState); const successActions = { type: types.DELETE_USER.SUCCESS, payload: getState.users.filter(obj => obj.id !== user.id) }; return store.dispatch(userActions.deleteUser(user.id)) .then(res => { const expectedActions = store.getActions(); expect(expectedActions).toEqual(expect.arrayContaining([successActions])); expect(res).toEqual(types.DELETE_USER.SUCCESS); }); });
test('Should remove expense from firebase', (done) => { const store = createMockStore(defaultAuthState); const id = expenses[0].id store.dispatch(startRemoveExpense({ id })).then(() => { const actions = store.getActions(); expect(actions[0]).toEqual({ type: 'REMOVE_EXPENSE', id }) return database.ref(`users/${uid}/expenses/${id}`).once('value'); }).then((snapshot) => { expect(snapshot.val()).toBeFalsy(); done(); }) });
it('should login', () => { const expectedActions = [ {type: LOGIN_REQUEST}, {type: LOGIN_SUCCESS, payload: {status: 201}}, {type: LOGOUT} ] const store = mockStore({}) return store.dispatch(actions.login('foo', 'bar')) .then(() => { expect(store.getActions()[0].type).toEqual(expectedActions[0].type) expect(store.getActions()[1].type).toEqual(expectedActions[1].type) expect(store.getActions()[1].payload.status).toEqual(expectedActions[1].payload.status) expect(store.getActions()[2].type).toEqual(expectedActions[2].type) }) })
it('should resetPassword', () => { const expectedActions = [ {type: RESET_PASSWORD_REQUEST}, {type: LOGIN}, {type: RESET_PASSWORD_SUCCESS} ] const store = mockStore({}) return store.dispatch(actions.resetPassword('email')) .then(() => { expect(store.getActions()).toEqual(expectedActions) }) })
it('should deleteSessionToken', () => { const expectedActions = [ {type: DELETE_TOKEN_REQUEST}, {type: DELETE_TOKEN_SUCCESS} ] const store = mockStore({}) return store.dispatch(actions.deleteSessionToken()) .then(() => { expect(store.getActions()).toEqual(expectedActions) }) })
it('creates an async action to fetch bitcoin value', () => { const expectedAction = [{type: FETCH_BITCOIN, bitcoin: mockResponse.body}] store.dispatch(fetchBitcoin()).then(() => { return expect(store.getActions()).toEqual(expectedAction) }) })
test('should fetch expenses from firebase',(done) => { const store = createMockStore(defaultAuthState); store.dispatch(startSetExpenses()).then(() => { const actions = store.getActions(); expect(actions[0]).toEqual({ type: 'SET_EXPENSES', expenses }); done(); }); });
test('should remove expense from firebase', (done) => { const store = createMockStore(defaultAuthState); const id = expenses[2].id; store.dispatch(startRemoveExpense({ id })).then(() => { const actions = store.getActions(); expect(actions[0]).toEqual({ type: 'REMOVE_EXPENSE', id }); return database.ref(`users/${uid}/expenses/${id}`).once('value'); }).then((snapshot) => { expect(snapshot.val()).toBeFalsy(); done(); }); });
test('should remove expense from database and store',(done) => { const store = createMockStore({ auth: { uid } }); const id = expenses[2].id; store.dispatch(startRemoveExpense({ id })) .then(() => { const actions = store.getActions(); expect(actions[0]).toEqual({ type: 'REMOVE_EXPENSE', id }); return database.ref(`users/${uid}/expenses/${id}`).once('value'); }) .then((snapshot) => { expect(snapshot.val()).toBeFalsy(); done(); }); });