|
| 1 | +import { configureStore } from "@reduxjs/toolkit"; |
| 2 | +import MockAdapter from "axios-mock-adapter"; |
| 3 | + |
| 4 | +import api from "../redux/api/api"; |
| 5 | +import chatsSlice, { |
| 6 | + fetchUsers, |
| 7 | + fetchChats, |
| 8 | + sendMessage, |
| 9 | + setChats, |
| 10 | + setUsers, |
| 11 | +} from "../redux/reducers/chatSlice"; |
| 12 | + |
| 13 | +const mockApi = new MockAdapter(api); |
| 14 | +const mockStore = (initialState) => |
| 15 | + configureStore({ |
| 16 | + reducer: { |
| 17 | + // @ts-ignore |
| 18 | + chats: chatsSlice, |
| 19 | + }, |
| 20 | + preloadedState: initialState, |
| 21 | + }); |
| 22 | + |
| 23 | +describe("Chats Slice Thunks", () => { |
| 24 | + let store; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + mockApi.reset(); |
| 28 | + store = mockStore({ |
| 29 | + chats: { |
| 30 | + chats: { |
| 31 | + loading: false, |
| 32 | + data: [], |
| 33 | + error: null, |
| 34 | + sending: false, |
| 35 | + sendError: null, |
| 36 | + }, |
| 37 | + users: { |
| 38 | + loading: false, |
| 39 | + data: [], |
| 40 | + error: null, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should handle fetchUsers pending", async () => { |
| 47 | + mockApi.onGet("/users").reply(200, { users: [] }); |
| 48 | + |
| 49 | + store.dispatch(fetchUsers()); |
| 50 | + expect(store.getState().chats.users.loading).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should handle fetchUsers fulfilled", async () => { |
| 54 | + const mockData = [{ id: "1", name: "John Doe" }]; |
| 55 | + mockApi.onGet("/users").reply(200, { users: mockData }); |
| 56 | + |
| 57 | + await store.dispatch(fetchUsers()); |
| 58 | + |
| 59 | + expect(store.getState().chats.users.loading).toBe(false); |
| 60 | + expect(store.getState().chats.users.data).toEqual(mockData); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should handle fetchUsers rejected", async () => { |
| 64 | + mockApi.onGet("/users").reply(500); |
| 65 | + |
| 66 | + await store.dispatch(fetchUsers()); |
| 67 | + |
| 68 | + expect(store.getState().chats.users.loading).toBe(false); |
| 69 | + expect(store.getState().chats.users.error).toBeTruthy(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should handle fetchChats pending", async () => { |
| 73 | + mockApi.onGet("/chats/private").reply(200, {}); |
| 74 | + |
| 75 | + store.dispatch(fetchChats()); |
| 76 | + expect(store.getState().chats.chats.loading).toBe(true); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should handle fetchChats fulfilled", async () => { |
| 80 | + const mockData = [{ id: "1", messages: [] }]; |
| 81 | + mockApi.onGet("/chats/private").reply(200, mockData); |
| 82 | + |
| 83 | + await store.dispatch(fetchChats()); |
| 84 | + |
| 85 | + expect(store.getState().chats.chats.loading).toBe(false); |
| 86 | + expect(store.getState().chats.chats.data).toEqual(mockData); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should handle fetchChats rejected", async () => { |
| 90 | + mockApi.onGet("/chats/private").reply(500); |
| 91 | + |
| 92 | + await store.dispatch(fetchChats()); |
| 93 | + |
| 94 | + expect(store.getState().chats.chats.loading).toBe(false); |
| 95 | + expect(store.getState().chats.chats.error).toBeTruthy(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should handle sendMessage pending", async () => { |
| 99 | + mockApi.onPost("/chats/private/1").reply(200, {}); |
| 100 | + |
| 101 | + store.dispatch(sendMessage({ message: "Hello", id: 1 })); |
| 102 | + expect(store.getState().chats.chats.sending).toBe(true); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should handle sendMessage fulfilled", async () => { |
| 106 | + const mockMessage = { message: "Hello", recipientId: 1 }; |
| 107 | + const initialChats = [{ id: "1", messages: [{ recipientId: 1 }] }]; |
| 108 | + store = mockStore({ |
| 109 | + chats: { |
| 110 | + chats: { |
| 111 | + loading: false, |
| 112 | + data: initialChats, |
| 113 | + error: null, |
| 114 | + sending: false, |
| 115 | + sendError: null, |
| 116 | + }, |
| 117 | + users: { |
| 118 | + loading: false, |
| 119 | + data: [], |
| 120 | + error: null, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }); |
| 124 | + |
| 125 | + mockApi.onPost("/chats/private/1").reply(200, mockMessage); |
| 126 | + |
| 127 | + await store.dispatch(sendMessage({ message: "Hello", id: 1 })); |
| 128 | + |
| 129 | + expect(store.getState().chats.chats.sending).toBe(false); |
| 130 | + expect(store.getState().chats.chats.data[0].messages).toContainEqual( |
| 131 | + mockMessage, |
| 132 | + ); |
| 133 | + }); |
| 134 | + |
| 135 | + it("should handle sendMessage rejected", async () => { |
| 136 | + mockApi.onPost("/chats/private/1").reply(500); |
| 137 | + |
| 138 | + await store.dispatch(sendMessage({ message: "Hello", id: 1 })); |
| 139 | + |
| 140 | + expect(store.getState().chats.chats.sending).toBe(false); |
| 141 | + expect(store.getState().chats.chats.sendError).toBeTruthy(); |
| 142 | + }); |
| 143 | + |
| 144 | + it("should handle setChats", () => { |
| 145 | + const mockChats = [{ id: "2", messages: [] }]; |
| 146 | + store.dispatch(setChats(mockChats)); |
| 147 | + |
| 148 | + expect(store.getState().chats.chats.data).toEqual(mockChats); |
| 149 | + }); |
| 150 | + |
| 151 | + it("should handle setUsers", () => { |
| 152 | + const mockUsers = [{ id: "3", name: "Jane Doe" }]; |
| 153 | + store.dispatch(setUsers(mockUsers)); |
| 154 | + |
| 155 | + expect(store.getState().chats.users.data).toEqual(mockUsers); |
| 156 | + }); |
| 157 | +}); |
0 commit comments