이것은 이전 질문 의 연속입니다 .
REGISTER_FAIL 케이스에 대한 테스트를 시도했습니다. 이것이 내가 한 일입니다.
test("should not register a user", async () => {
axios.mockRejectedValue({
status: 500,
});
const userInfo = {
name: "",
email: "",
password: "",
};
await store.dispatch(register(userInfo)).then(() => {
expect(store.getActions()[0]).toEqual({
type: REGISTER_FAIL,
payload: {
token: null,
isAuthenticated: false,
loading: true,
// user: null,
},
});
});
});
나는 문제가 된 공유 store
를 사용하여 문제를 추측하고 있습니다. 각 테스트마다 별도의 상점을 제안합니다. 아이디어는 다음과 같습니다.
/** mock-store */
const createMockStore = configureMockStore([thunk]);
// Create a store maker to create store for each test
const storeMaker = () => {
const defaultState = [];
const store = createMockStore(defaultState);
return store;
}
/** reset mock */
afterEach(() => jest.resetAllMocks());
test("should register a user ", async () => {
// Run to create at each test
const store = storeMaker();
axios.mockImplementation(() => {
return Promise.resolve({
status: 200,
data: {
token: "testToken",
},
});
});
// const res = await axios.post("/api/users");
// console.log(res.body);
const testUser = {
name: "testName",
email: "[email protected]",
password: "testPassword",
};
await store.dispatch(register(testUser)).then(() => {
expect(store.getActions()[0]).toEqual({
type: REGISTER_SUCCESS,
payload: {
token: "testToken",
isAuthenticated: true,
loading: false,
},
});
});
});
test("should not register a user", async () => {
// Likewise above
const store = storeMaker();
axios.mockRejectedValue({
status: 500,
});
const userInfo = {
name: "",
email: "",
password: "",
};
await store.dispatch(register(userInfo)).then(() => {
expect(store.getActions()[0]).toEqual({
type: REGISTER_FAIL,
payload: {
token: null,
isAuthenticated: false,
loading: true,
// user: null,
},
});
});
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다