|
| 1 | +import { configureStore, Tuple } from "@reduxjs/toolkit"; |
| 2 | +import thunk from "redux-thunk"; |
| 3 | +import MockAdapter from "axios-mock-adapter"; |
| 4 | + |
| 5 | +import api from "../redux/api/api"; |
| 6 | +import reviewSlice, { |
| 7 | + fetchReviews, |
| 8 | + addReview, |
| 9 | + deleteReview, |
| 10 | + updateReview, |
| 11 | +} from "../redux/reducers/reviewSlice"; |
| 12 | + |
| 13 | +const mockApi = new MockAdapter(api); |
| 14 | +const mockStore = (initialState) => |
| 15 | + configureStore({ |
| 16 | + reducer: { |
| 17 | + // @ts-ignore |
| 18 | + review: reviewSlice, |
| 19 | + }, |
| 20 | + preloadedState: initialState, |
| 21 | + }); |
| 22 | + |
| 23 | +describe("Review Slice Thunks", () => { |
| 24 | + let store; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + mockApi.reset(); |
| 28 | + store = mockStore({ |
| 29 | + review: { |
| 30 | + reviews: [], |
| 31 | + isLoading: false, |
| 32 | + error: null, |
| 33 | + }, |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should handle fetchReviews pending", async () => { |
| 38 | + mockApi.onGet("/products/productId/reviews").reply(200, []); |
| 39 | + |
| 40 | + store.dispatch(fetchReviews({ productId: "productId" })); |
| 41 | + expect(store.getState().review.isLoading).toBe(true); |
| 42 | + }); |
| 43 | + it("should handle fetchReviews fulfilled", async () => { |
| 44 | + const mockData = [ |
| 45 | + { |
| 46 | + id: "1", |
| 47 | + productId: "productId", |
| 48 | + user: { id: 1, name: "John" }, |
| 49 | + rating: "5", |
| 50 | + feedback: "Great!", |
| 51 | + }, |
| 52 | + ]; |
| 53 | + mockApi |
| 54 | + .onGet("/products/productId/reviews") |
| 55 | + .reply(200, { reviewProduct: mockData }); |
| 56 | + |
| 57 | + await store.dispatch(fetchReviews({ productId: "productId" })); |
| 58 | + |
| 59 | + expect(store.getState().review.reviews).toEqual(mockData); |
| 60 | + expect(store.getState().review.isLoading).toBe(false); |
| 61 | + expect(store.getState().review.error).toBe(null); |
| 62 | + }); |
| 63 | + |
| 64 | + it("should handle fetchReviews rejected", async () => { |
| 65 | + mockApi.onGet("/products/productId/reviews").reply(500); |
| 66 | + |
| 67 | + await store.dispatch(fetchReviews({ productId: "productId" })); |
| 68 | + |
| 69 | + expect(store.getState().review.isLoading).toBe(false); |
| 70 | + expect(store.getState().review.error).toBe( |
| 71 | + "Request failed with status code 500", |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should handle fetchReviews axios error", async () => { |
| 76 | + mockApi |
| 77 | + .onGet("/products/productId/reviews") |
| 78 | + .reply(500, { message: "Server error" }); |
| 79 | + |
| 80 | + await store.dispatch(fetchReviews({ productId: "productId" })); |
| 81 | + expect(store.getState().review.isLoading).toBe(false); |
| 82 | + expect(store.getState().review.error).toBe( |
| 83 | + "Request failed with status code 500", |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should handle addReview fulfilled", async () => { |
| 88 | + const mockData = { |
| 89 | + id: "3", |
| 90 | + productId: "productId", |
| 91 | + user: { id: 3, name: "bwiza" }, |
| 92 | + rating: "4", |
| 93 | + feedback: "Good!", |
| 94 | + }; |
| 95 | + mockApi.onPost("/products/productId/reviews").reply(200, mockData); |
| 96 | + |
| 97 | + await store.dispatch( |
| 98 | + addReview({ productId: "productId", rating: "4", feedback: "loop it" }), |
| 99 | + ); |
| 100 | + expect(store.getState().review.reviews).toContainEqual(mockData); |
| 101 | + expect(store.getState().review.isLoading).toBe(false); |
| 102 | + expect(store.getState().review.error).toBe(null); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should handle addReview axios error", async () => { |
| 106 | + mockApi |
| 107 | + .onPost("/products/prod1/reviews") |
| 108 | + .reply(500, { message: "Add review failed" }); |
| 109 | + |
| 110 | + await store.dispatch( |
| 111 | + addReview({ productId: "prod1", rating: "4", feedback: "Good!" }), |
| 112 | + ); |
| 113 | + |
| 114 | + expect(store.getState().review.isLoading).toBe(false); |
| 115 | + expect(store.getState().review.error).toBeTruthy(); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should handle deleteReview fulfilled", async () => { |
| 119 | + const initialState = { |
| 120 | + review: { |
| 121 | + reviews: [ |
| 122 | + { |
| 123 | + id: "2", |
| 124 | + productId: "productId", |
| 125 | + user: { id: 2, name: "bwiza" }, |
| 126 | + rating: "4", |
| 127 | + feedback: "Good!", |
| 128 | + }, |
| 129 | + ], |
| 130 | + isLoading: false, |
| 131 | + error: null, |
| 132 | + }, |
| 133 | + }; |
| 134 | + store = mockStore(initialState); |
| 135 | + mockApi.onDelete("/products/productId/reviews").reply(200); |
| 136 | + // @ts-ignore |
| 137 | + await store.dispatch(deleteReview({ productId: "productId", id: "2" })); |
| 138 | + expect(store.getState().review.reviews).toEqual([]); |
| 139 | + expect(store.getState().review.isLoading).toBe(false); |
| 140 | + expect(store.getState().review.error).toBe(null); |
| 141 | + }); |
| 142 | + it("should handle updateReview fulfilled", async () => { |
| 143 | + const initialState = { |
| 144 | + review: { |
| 145 | + reviews: [ |
| 146 | + { |
| 147 | + id: "2", |
| 148 | + productId: "productId", |
| 149 | + user: { id: 2, name: "bwiza" }, |
| 150 | + rating: "4", |
| 151 | + feedback: "loop it", |
| 152 | + }, |
| 153 | + ], |
| 154 | + isLoading: false, |
| 155 | + error: null, |
| 156 | + }, |
| 157 | + }; |
| 158 | + store = mockStore(initialState); |
| 159 | + const updatedReview = { |
| 160 | + id: "2", |
| 161 | + productId: "productId", |
| 162 | + user: { id: 2, name: "Jane" }, |
| 163 | + rating: "5", |
| 164 | + feedback: "loop througth!", |
| 165 | + }; |
| 166 | + mockApi.onPatch("/products/productId/reviews").reply(200, updatedReview); |
| 167 | + await store.dispatch( |
| 168 | + updateReview({ |
| 169 | + productId: "productId", |
| 170 | + id: 2, |
| 171 | + rating: "5", |
| 172 | + feedback: "loop througth!", |
| 173 | + }), |
| 174 | + ); |
| 175 | + |
| 176 | + expect(store.getState().review.reviews).toContainEqual(updatedReview); |
| 177 | + expect(store.getState().review.isLoading).toBe(false); |
| 178 | + expect(store.getState().review.error).toBe(null); |
| 179 | + }); |
| 180 | + |
| 181 | + it("should handle deleteReview axios error", async () => { |
| 182 | + mockApi |
| 183 | + .onDelete("/products/prod1/reviews") |
| 184 | + .reply(500, { message: "Delete review failed" }); |
| 185 | + // @ts-ignore |
| 186 | + await store.dispatch(deleteReview({ productId: "prod1", id: "2" })); |
| 187 | + expect(store.getState().review.isLoading).toBe(false); |
| 188 | + expect(store.getState().review.error).toBeTruthy(); |
| 189 | + }); |
| 190 | + |
| 191 | + it("should handle updateReview axios error", async () => { |
| 192 | + mockApi |
| 193 | + .onPatch("/products/prod1/reviews") |
| 194 | + .reply(500, { message: "Update review failed" }); |
| 195 | + // @ts-ignore |
| 196 | + await store.dispatch( |
| 197 | + updateReview({ |
| 198 | + productId: "prod1", |
| 199 | + id: 2, |
| 200 | + rating: "5", |
| 201 | + feedback: "Excellent!", |
| 202 | + }), |
| 203 | + ); |
| 204 | + expect(store.getState().review.isLoading).toBe(false); |
| 205 | + expect(store.getState().review.error).toBeTruthy(); |
| 206 | + }); |
| 207 | + |
| 208 | + it("should handle axios network error", async () => { |
| 209 | + mockApi.onGet("/products/prod1/reviews").networkError(); |
| 210 | + |
| 211 | + await store.dispatch(fetchReviews({ productId: "prod1" })); |
| 212 | + expect(store.getState().review.isLoading).toBe(false); |
| 213 | + expect(store.getState().review.error).toBe("Network Error"); |
| 214 | + }); |
| 215 | +}); |
0 commit comments