47 lines
1.9 KiB
TypeScript
47 lines
1.9 KiB
TypeScript
|
import { reducer, dispatchActions } from "../src/reducer";
|
||
|
import { TuitioToken } from "@flare/tuitio-client";
|
||
|
import { TuitioReactState, TuitioDispatchActions } from "../src/types";
|
||
|
import { initialState } from "../src/initialState";
|
||
|
import { Dispatch } from "react";
|
||
|
|
||
|
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 token: TuitioToken = { raw: "abc123", validFrom: new Date(), validUntil: new Date() };
|
||
|
const result = reducer(initialState, { type: "onLoginSuccess", token, userName: "user" });
|
||
|
expect(result).toEqual({ ...initialState, token, userName: "user" });
|
||
|
});
|
||
|
|
||
|
it('should handle the "onLogout" action', () => {
|
||
|
const state: TuitioReactState = { ...initialState, configuration: { tuitioUrl: "https://test.com/api" } };
|
||
|
const result = reducer(state, { type: "onLogout" });
|
||
|
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", () => {
|
||
|
const token: TuitioToken = { raw: "test", validFrom: new Date(), validUntil: new Date() };
|
||
|
const userName = "test";
|
||
|
actions.onLoginSuccess(token, userName);
|
||
|
expect(dispatch).toHaveBeenCalledWith({ type: "onLoginSuccess", token, userName });
|
||
|
});
|
||
|
|
||
|
it("should dispatch an action with type 'onLogout' when 'onLogout' is called", () => {
|
||
|
actions.onLogout();
|
||
|
expect(dispatch).toHaveBeenCalledWith({ type: "onLogout" });
|
||
|
});
|
||
|
});
|