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";
|
|
|
|
|
2023-03-18 02:24:30 +02:00
|
|
|
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', () => {
|
2023-03-18 02:24:30 +02:00
|
|
|
const result = reducer(initialState, { type: "onLoginSuccess", token, validUntil, userName });
|
2023-03-18 02:29:06 +02:00
|
|
|
const expected = { ...initialState, auth: { token, validUntil, userName } };
|
2023-03-18 02:24:30 +02:00
|
|
|
expect(result).toEqual(expected);
|
2023-02-11 22:12:13 +02:00
|
|
|
});
|
|
|
|
|
2023-03-18 02:24:30 +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" } };
|
2023-03-18 02:24:30 +02:00
|
|
|
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", () => {
|
2023-03-18 02:24:30 +02:00
|
|
|
actions.onLoginSuccess(token, validUntil, userName);
|
|
|
|
expect(dispatch).toHaveBeenCalledWith({ type: "onLoginSuccess", token, validUntil, userName });
|
2023-02-11 22:12:13 +02:00
|
|
|
});
|
|
|
|
|
2023-03-18 02:24:30 +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
|
|
|
});
|
|
|
|
});
|