mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-08 11:56:41 +00:00
Fix swapped examples for thunk and action tests
This commit is contained in:
parent
51d0a16f09
commit
a2aec9fc3f
1 changed files with 35 additions and 32 deletions
|
@ -53,32 +53,13 @@ import * as actions from "path/to/actionCreators";
|
||||||
// import eventual action types constants
|
// import eventual action types constants
|
||||||
// import mock data
|
// import mock data
|
||||||
|
|
||||||
describe("Async Actions", () => {
|
it("test description", () => {
|
||||||
afterEach(() => {
|
const data = /* mock data */
|
||||||
fetchMock.restore(); // init fecth mok for each test
|
const expectedAction = { type: TYPE, /* ... */ };
|
||||||
});
|
|
||||||
|
|
||||||
it("test description", () => {
|
const actualAction = actions.actionCreator(data);
|
||||||
// mimic API call
|
|
||||||
fetchMock.mock(
|
|
||||||
"*", // capture any fetch call
|
|
||||||
{
|
|
||||||
body: /* body contents */,
|
|
||||||
headers: { "content-type": "application/json" }
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const expectedActions = [
|
expect(actualAction).toEqual(expectedAction);
|
||||||
{ type: TYPE, /* ... */ },
|
|
||||||
{ type: TYPE, /* ... */ }
|
|
||||||
];
|
|
||||||
|
|
||||||
const store = mockStore({ data: value, ... });
|
|
||||||
return store.dispatch(actions.actionCreator())
|
|
||||||
.then(() => {
|
|
||||||
expect(store.getActions()).toEqual(expectedActions);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -130,22 +111,44 @@ Thunk testing requires the mocking of:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import thunk from "redux-thunk";
|
import thunk from "redux-thunk";
|
||||||
import fetchMock from "getch-mock";
|
import fetchMock from "fetch-mock";
|
||||||
import configureMockStore from "redux-mock-store";
|
import configureMockStore from "redux-mock-store";
|
||||||
|
|
||||||
|
// needed for testing async thunks
|
||||||
|
const middleware = [thunk]; // mock middlewares
|
||||||
|
const mockStore = configureMockStore(middleware); // mock the store
|
||||||
|
|
||||||
import * as actions from "path/to/actionCreators";
|
import * as actions from "path/to/actionCreators";
|
||||||
// import eventual action types constants
|
// import eventual action types constants
|
||||||
// import mock data
|
// import mock data
|
||||||
|
|
||||||
const middleware = [thunk]; // mock middlewares
|
describe("Async Actions", () => {
|
||||||
const mockStore = configureMockStore(middleware); // mock the store
|
afterEach(() => {
|
||||||
|
fetchMock.restore(); // init fecth mock for each test
|
||||||
|
});
|
||||||
|
|
||||||
it("test description", () => {
|
it("test description", () => {
|
||||||
const data = /* mock data */
|
// mimic API call
|
||||||
const expectedAction = { type: TYPE, /* ... */ };
|
fetchMock.mock(
|
||||||
|
"*", // capture any fetch call
|
||||||
|
{
|
||||||
|
body: /* body contents */,
|
||||||
|
headers: { "content-type": "application/json" }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const actualAction = actions.actionCreator(data);
|
// expected action fired from the thunk
|
||||||
|
const expectedActions = [
|
||||||
|
{ type: TYPE, /* ... */ },
|
||||||
|
{ type: TYPE, /* ... */ }
|
||||||
|
];
|
||||||
|
|
||||||
expect(actualAction).toEqual(expectedAction);
|
const store = mockStore({ data: value, ... }); // init mock store
|
||||||
|
|
||||||
|
return store.dispatch(actions.actionCreator()) // act
|
||||||
|
.then(() => {
|
||||||
|
expect(store.getActions()).toEqual(expectedActions); // assert
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
Loading…
Add table
Reference in a new issue