100% tests coverage

master
Tudor Stanciu 2023-02-11 22:12:13 +02:00
parent 4595953fc1
commit f41549dd0d
3 changed files with 47 additions and 1 deletions

View File

@ -5,7 +5,7 @@
"testRegex": "(/tests/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
"testEnvironment": "jsdom",
"collectCoverage": false,
"collectCoverage": true,
"coverageThreshold": {
"global": {
"branches": 50,

46
tests/reducer.test.ts Normal file
View File

@ -0,0 +1,46 @@
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" });
});
});