From f41549dd0df0c9e95cfee27369161b4e51a59c47 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sat, 11 Feb 2023 22:12:13 +0200 Subject: [PATCH] 100% tests coverage --- jestconfig.json | 2 +- ...tialState.test.ts => initialState.test.ts} | 0 tests/reducer.test.ts | 46 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) rename tests/{i2nitialState.test.ts => initialState.test.ts} (100%) create mode 100644 tests/reducer.test.ts diff --git a/jestconfig.json b/jestconfig.json index d86c8d5..343de71 100644 --- a/jestconfig.json +++ b/jestconfig.json @@ -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, diff --git a/tests/i2nitialState.test.ts b/tests/initialState.test.ts similarity index 100% rename from tests/i2nitialState.test.ts rename to tests/initialState.test.ts diff --git a/tests/reducer.test.ts b/tests/reducer.test.ts new file mode 100644 index 0000000..c3dc898 --- /dev/null +++ b/tests/reducer.test.ts @@ -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>; + 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" }); + }); +});