import { reducer, dispatchActions } from "../src/reducer"; import { TuitioReactState, TuitioDispatchActions } from "../src/types"; import { initialState } from "../src/initialState"; import { Dispatch } from "react"; const userName = "tuitio.user"; const token = "abc123"; const validUntil = new Date(); describe("reducer", () => { it("should return the initial state by default", () => { const result = reducer(undefined, { type: "UNKNOWN_ACTION" }); expect(result).toEqual(initialState); }); it('should handle the "onLoginSuccess" action', () => { const result = reducer(initialState, { type: "onLoginSuccess", token, validUntil, userName }); const expected = { ...initialState, token, validUntil, userName }; expect(result).toEqual(expected); }); it('should handle the "onLogoutSuccess" action', () => { const state: TuitioReactState = { ...initialState, configuration: { tuitioUrl: "https://test.com/api" } }; const result = reducer(state, { type: "onLogoutSuccess" }); expect(result).toEqual({ ...initialState, configuration: state.configuration }); }); }); describe("dispatchActions", () => { let dispatch: jest.Mocked>; let actions: TuitioDispatchActions; beforeEach(() => { dispatch = jest.fn(); actions = dispatchActions(dispatch); }); it("should dispatch an action with type 'onLoginSuccess' and payload when 'onLoginSuccess' is called", () => { actions.onLoginSuccess(token, validUntil, userName); expect(dispatch).toHaveBeenCalledWith({ type: "onLoginSuccess", token, validUntil, userName }); }); it("should dispatch an action with type 'onLogoutSuccess' when 'onLogoutSuccess' is called", () => { actions.onLogoutSuccess(); expect(dispatch).toHaveBeenCalledWith({ type: "onLogoutSuccess" }); }); });