From dbed1a4ce9cf0a7259965d51cc189ad4fcf24905 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sun, 12 Feb 2023 03:27:22 +0200 Subject: [PATCH] 100% test coverage --- .vscode/launch.json | 21 ++++---- package-lock.json | 14 +++--- package.json | 2 +- src/hooks/useTuitioClient.ts | 4 +- src/types.ts | 2 +- tests/hooks/useTuitioToken.test.tsx | 75 +++++++++++++++++++++++++++++ tests/hooks/useTuitioUser.test.tsx | 28 +++++++++-- 7 files changed, 119 insertions(+), 27 deletions(-) create mode 100644 tests/hooks/useTuitioToken.test.tsx diff --git a/.vscode/launch.json b/.vscode/launch.json index b1c795f..c2fee9a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,14 +1,11 @@ { - "name": "Jest file", - "type": "node", - "request": "launch", - "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/jest", - "args": ["${fileBasenameNoExtension}", "--runInBand", "--watch", "--coverage=false", "--no-cache"], - "cwd": "${workspaceRoot}", - "console": "integratedTerminal", - "internalConsoleOptions": "neverOpen", - "sourceMaps": true, - "windows": { - "program": "${workspaceFolder}/node_modules/jest/bin/jest" - } + "configurations": [ + { + "name": "Attach", + "port": 9229, + "request": "attach", + "skipFiles": ["/**"], + "type": "node" + } + ] } diff --git a/package-lock.json b/package-lock.json index d2c870f..9bd197b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.0.0", "license": "MIT", "dependencies": { - "@flare/tuitio-client": "^1.0.3" + "@flare/tuitio-client": "^1.0.4" }, "devDependencies": { "@testing-library/react": "^12.1.5", @@ -707,9 +707,9 @@ "integrity": "sha512-VgXQHoQEVZ/71B6YQHQP8/Yd/w1smGD+kCCiNvJKZ1xMD3nkN9mjoHxIqbOJMZ2q5PZlV6gXYT7eVol8Wm+D0A==" }, "node_modules/@flare/tuitio-client": { - "version": "1.0.3", - "resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.0.3.tgz", - "integrity": "sha512-XlNje3e985ruGtJaKUZLY6kRsgcPG0UhoYZfWjDnAsaoC15rbHc+RF3iBOO42Z7eMPDm00UYu54NiqOagu9geg==", + "version": "1.0.4", + "resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.0.4.tgz", + "integrity": "sha512-NTG0eB0EztcUEl3xEiGo7FY8YQP7/TxNIko65U3Mhs7Zm5+IVXlr7HcLbEr89mqaVtJmRRt1i8u/z/y0lCChwg==", "dependencies": { "@flare/js-utils": "^1.0.3", "axios": "^1.3.2" @@ -7392,9 +7392,9 @@ "integrity": "sha512-VgXQHoQEVZ/71B6YQHQP8/Yd/w1smGD+kCCiNvJKZ1xMD3nkN9mjoHxIqbOJMZ2q5PZlV6gXYT7eVol8Wm+D0A==" }, "@flare/tuitio-client": { - "version": "1.0.3", - "resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.0.3.tgz", - "integrity": "sha512-XlNje3e985ruGtJaKUZLY6kRsgcPG0UhoYZfWjDnAsaoC15rbHc+RF3iBOO42Z7eMPDm00UYu54NiqOagu9geg==", + "version": "1.0.4", + "resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.0.4.tgz", + "integrity": "sha512-NTG0eB0EztcUEl3xEiGo7FY8YQP7/TxNIko65U3Mhs7Zm5+IVXlr7HcLbEr89mqaVtJmRRt1i8u/z/y0lCChwg==", "requires": { "@flare/js-utils": "^1.0.3", "axios": "^1.3.2" diff --git a/package.json b/package.json index 47b5375..36bbcf7 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "README.md" ], "dependencies": { - "@flare/tuitio-client": "^1.0.3" + "@flare/tuitio-client": "^1.0.4" }, "peerDependencies": { "react": "^16.14.0" diff --git a/src/hooks/useTuitioClient.ts b/src/hooks/useTuitioClient.ts index 72760d8..a3ffa7a 100644 --- a/src/hooks/useTuitioClient.ts +++ b/src/hooks/useTuitioClient.ts @@ -17,8 +17,8 @@ const useTuitioClient = (options: TuitioClientHookOptions) => { const login = async (userName: string, password: string) => { try { - const connector = new TuitioClient(state.configuration.tuitioUrl); - const response = await connector.authenticate(userName, password); + const client = new TuitioClient(state.configuration.tuitioUrl); + const response = await client.authenticate(userName, password); if (response.token) { dispatchActions.onLoginSuccess(response.token, userName); onLoginSuccess && onLoginSuccess(response, userName); diff --git a/src/types.ts b/src/types.ts index eda78a6..dc2e154 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,7 +1,7 @@ import type { TuitioToken } from "@flare/tuitio-client"; export type TuitioConfiguration = { tuitioUrl: string | null }; -export type TuitioReactState = { userName: string; token: TuitioToken; configuration: TuitioConfiguration }; +export type TuitioReactState = { userName: string; token: TuitioToken | null; configuration: TuitioConfiguration }; export type TuitioDispatchActions = { onLoginSuccess: (token: TuitioToken, userName: string) => void; onLogout: () => void; diff --git a/tests/hooks/useTuitioToken.test.tsx b/tests/hooks/useTuitioToken.test.tsx new file mode 100644 index 0000000..73652b4 --- /dev/null +++ b/tests/hooks/useTuitioToken.test.tsx @@ -0,0 +1,75 @@ +import React from "react"; +import { renderHook } from "@testing-library/react-hooks"; +import { TuitioContext } from "../../src/contexts"; +import { useTuitioToken } from "../../src/hooks"; + +describe("useTuitioToken: positive flow", () => { + const testState = { + userName: "test-user", + token: { + raw: "mocked-token", + validFrom: new Date(), + validUntil: new Date() + }, + configuration: { + tuitioUrl: null + } + }; + + testState.token.validFrom.setHours(testState.token.validFrom.getHours() - 1); + testState.token.validUntil.setHours(testState.token.validUntil.getHours() + 1); + + const wrapper = ({ children }: { children?: React.ReactNode }) => ( + {children} + ); + + it("should return the correct token object", () => { + const { result } = renderHook(() => useTuitioToken(), { wrapper }); + expect(result.current.token).toEqual({ + raw: "mocked-token", + validFrom: expect.any(Date), + validUntil: expect.any(Date) + }); + }); + + it("should return false value for valid", () => { + const { result } = renderHook(() => useTuitioToken(), { wrapper }); + expect(result.current.valid).toBe(true); + }); + + it("validate should return true value", () => { + const { result } = renderHook(() => useTuitioToken(), { wrapper }); + const valid = result.current.validate(); + expect(valid).toBe(true); + }); +}); + +describe("useTuitioToken: negative flow", () => { + const testState = { + userName: "test-user", + token: null, + configuration: { + tuitioUrl: null + } + }; + + const wrapper = ({ children }: { children?: React.ReactNode }) => ( + {children} + ); + + it("should return null for token", () => { + const { result } = renderHook(() => useTuitioToken(), { wrapper }); + expect(result.current.token).toBeNull(); + }); + + it("should return false value for valid", () => { + const { result } = renderHook(() => useTuitioToken(), { wrapper }); + expect(result.current.valid).toBe(false); + }); + + it("validate should return false value", () => { + const { result } = renderHook(() => useTuitioToken(), { wrapper }); + const valid = result.current.validate(); + expect(valid).toBe(false); + }); +}); diff --git a/tests/hooks/useTuitioUser.test.tsx b/tests/hooks/useTuitioUser.test.tsx index 53dc0fe..727b707 100644 --- a/tests/hooks/useTuitioUser.test.tsx +++ b/tests/hooks/useTuitioUser.test.tsx @@ -3,14 +3,14 @@ import { renderHook } from "@testing-library/react-hooks"; import { TuitioContext } from "../../src/contexts"; import { useTuitioUser } from "../../src/hooks"; -describe("useTuitioUser", () => { +describe("useTuitioUser: positive flow", () => { it("should return the userName and lastLoginDate from the TuitioContext", () => { const testState = { userName: "test-user", token: { raw: "mocked-token", - validFrom: new Date(2022, 1, 1), - validUntil: new Date(2022, 12, 31) + validFrom: new Date(2023, 1, 1), + validUntil: new Date(2023, 12, 31) }, configuration: { tuitioUrl: null @@ -23,6 +23,26 @@ describe("useTuitioUser", () => { const { result } = renderHook(() => useTuitioUser(), { wrapper }); expect(result.current.userName).toEqual("test-user"); - expect(result.current.lastLoginDate).toEqual(new Date(2022, 1, 1)); + expect(result.current.lastLoginDate).toEqual(new Date(2023, 1, 1)); + }); +}); + +describe("useTuitioUser: negative flow", () => { + it("should return the userName and lastLoginDate from the TuitioContext", () => { + const testState = { + userName: "test-user", + token: null, + configuration: { + tuitioUrl: null + } + }; + + const wrapper = ({ children }: { children?: React.ReactNode }) => ( + {children} + ); + const { result } = renderHook(() => useTuitioUser(), { wrapper }); + + expect(result.current.userName).toEqual("test-user"); + expect(result.current.lastLoginDate).toBe(undefined); }); });