describe("root saga", () => { it("should handle 4 button clicks one after another", async () => { const sagaTester = new SagaTester({ reducers: rootReducer }); sagaTester.start(rootSaga); sagaTester.dispatch(queuedSagaAction()); sagaTester.dispatch(queuedSagaAction()); sagaTester.dispatch(queuedSagaAction()); sagaTester.dispatch(queuedSagaAction()); await delay(5000); // wait for 4 request * max 1sec each const state = sagaTester.getState(); expect(state.exampleReducer.counter).toEqual(4); }, 30000); });
test("filter saga", () => { const sagaTester = new SagaTester({ initialState: {}, reducers: { filter: filter, prices: prices, }, }); sagaTester.start(watchPeriod); sagaTester.dispatch(symbolSelected("S")); sagaTester.dispatch(periodSelected("W")); const state = sagaTester.getState(); expect(state.filter).toEqual({ period: "W", symbol: "S", }); expect(state.prices.loading).toEqual(true); });
test("search saga", async () => { const delay = (t) => new Promise((res) => setTimeout(() => res(), t)); const sagaTester = new SagaTester({ initialState: {}, reducers: { search: search, }, }); const mockSuccessResponse = { bestMatches: [], }; const mockJsonPromise = Promise.resolve(mockSuccessResponse); const mockFetchPromise = Promise.resolve({ json: () => mockJsonPromise, }); jest.spyOn(global, "fetch").mockImplementation(() => mockFetchPromise); sagaTester.start(watchSearch); sagaTester.dispatch(searchSymbol("S")); expect(global.fetch).toHaveBeenCalledTimes(1); await delay(0); expect(global.fetch).toHaveBeenCalledTimes(1); expect(sagaTester.getState().search.options).toEqual([]); expect(sagaTester.wasCalled(types.SYMBOL_FOUND)).toEqual(true); });
test("prices saga", async () => { const delay = (t) => new Promise((res) => setTimeout(() => res(), t)); const sagaTester = new SagaTester({ initialState: {}, reducers: { filter: filter, prices: prices, }, }); const mockSuccessResponse = { "Time Series": {}, }; const mockJsonPromise = Promise.resolve(mockSuccessResponse); const mockFetchPromise = Promise.resolve({ json: () => mockJsonPromise, }); jest.spyOn(global, "fetch").mockImplementation(() => mockFetchPromise); sagaTester.start(watchPrices); sagaTester.dispatch(fetchPrices()); expect(sagaTester.getState().prices.loading).toEqual(true); expect(global.fetch).toHaveBeenCalledTimes(1); await delay(0); expect(global.fetch).toHaveBeenCalledTimes(1); expect(sagaTester.getState().prices.data.labels).toEqual([]); expect(sagaTester.wasCalled(types.PRICES_FETCHED)).toEqual(true); });