100% test coverage

master
Tudor Stanciu 2023-02-12 03:27:22 +02:00
parent 385dd3325b
commit dbed1a4ce9
7 changed files with 119 additions and 27 deletions

19
.vscode/launch.json vendored
View File

@ -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": ["<node_internals>/**"],
"type": "node"
}
]
}

14
package-lock.json generated
View File

@ -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"

View File

@ -48,7 +48,7 @@
"README.md"
],
"dependencies": {
"@flare/tuitio-client": "^1.0.3"
"@flare/tuitio-client": "^1.0.4"
},
"peerDependencies": {
"react": "^16.14.0"

View File

@ -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);

View File

@ -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;

View File

@ -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 }) => (
<TuitioContext.Provider value={testState}>{children}</TuitioContext.Provider>
);
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 }) => (
<TuitioContext.Provider value={testState}>{children}</TuitioContext.Provider>
);
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);
});
});

View File

@ -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 }) => (
<TuitioContext.Provider value={testState}>{children}</TuitioContext.Provider>
);
const { result } = renderHook(() => useTuitioUser(), { wrapper });
expect(result.current.userName).toEqual("test-user");
expect(result.current.lastLoginDate).toBe(undefined);
});
});