tuitio-client-react/tests/reducer.test.ts

48 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-02-11 22:12:13 +02:00
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();
2023-02-11 22:12:13 +02:00
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);
2023-02-11 22:12:13 +02:00
});
it('should handle the "onLogoutSuccess" action', () => {
2023-02-11 22:12:13 +02:00
const state: TuitioReactState = { ...initialState, configuration: { tuitioUrl: "https://test.com/api" } };
const result = reducer(state, { type: "onLogoutSuccess" });
2023-02-11 22:12:13 +02:00
expect(result).toEqual({ ...initialState, configuration: state.configuration });
});
});
describe("dispatchActions", () => {
let dispatch: jest.Mocked<Dispatch<any>>;
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 });
2023-02-11 22:12:13 +02:00
});
it("should dispatch an action with type 'onLogoutSuccess' when 'onLogoutSuccess' is called", () => {
actions.onLogoutSuccess();
expect(dispatch).toHaveBeenCalledWith({ type: "onLogoutSuccess" });
2023-02-11 22:12:13 +02:00
});
});