100% test coverage
parent
385dd3325b
commit
dbed1a4ce9
|
@ -1,14 +1,11 @@
|
||||||
{
|
{
|
||||||
"name": "Jest file",
|
"configurations": [
|
||||||
"type": "node",
|
{
|
||||||
"request": "launch",
|
"name": "Attach",
|
||||||
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/jest",
|
"port": 9229,
|
||||||
"args": ["${fileBasenameNoExtension}", "--runInBand", "--watch", "--coverage=false", "--no-cache"],
|
"request": "attach",
|
||||||
"cwd": "${workspaceRoot}",
|
"skipFiles": ["<node_internals>/**"],
|
||||||
"console": "integratedTerminal",
|
"type": "node"
|
||||||
"internalConsoleOptions": "neverOpen",
|
|
||||||
"sourceMaps": true,
|
|
||||||
"windows": {
|
|
||||||
"program": "${workspaceFolder}/node_modules/jest/bin/jest"
|
|
||||||
}
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@flare/tuitio-client": "^1.0.3"
|
"@flare/tuitio-client": "^1.0.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@testing-library/react": "^12.1.5",
|
"@testing-library/react": "^12.1.5",
|
||||||
|
@ -707,9 +707,9 @@
|
||||||
"integrity": "sha512-VgXQHoQEVZ/71B6YQHQP8/Yd/w1smGD+kCCiNvJKZ1xMD3nkN9mjoHxIqbOJMZ2q5PZlV6gXYT7eVol8Wm+D0A=="
|
"integrity": "sha512-VgXQHoQEVZ/71B6YQHQP8/Yd/w1smGD+kCCiNvJKZ1xMD3nkN9mjoHxIqbOJMZ2q5PZlV6gXYT7eVol8Wm+D0A=="
|
||||||
},
|
},
|
||||||
"node_modules/@flare/tuitio-client": {
|
"node_modules/@flare/tuitio-client": {
|
||||||
"version": "1.0.3",
|
"version": "1.0.4",
|
||||||
"resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.0.3.tgz",
|
"resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.0.4.tgz",
|
||||||
"integrity": "sha512-XlNje3e985ruGtJaKUZLY6kRsgcPG0UhoYZfWjDnAsaoC15rbHc+RF3iBOO42Z7eMPDm00UYu54NiqOagu9geg==",
|
"integrity": "sha512-NTG0eB0EztcUEl3xEiGo7FY8YQP7/TxNIko65U3Mhs7Zm5+IVXlr7HcLbEr89mqaVtJmRRt1i8u/z/y0lCChwg==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@flare/js-utils": "^1.0.3",
|
"@flare/js-utils": "^1.0.3",
|
||||||
"axios": "^1.3.2"
|
"axios": "^1.3.2"
|
||||||
|
@ -7392,9 +7392,9 @@
|
||||||
"integrity": "sha512-VgXQHoQEVZ/71B6YQHQP8/Yd/w1smGD+kCCiNvJKZ1xMD3nkN9mjoHxIqbOJMZ2q5PZlV6gXYT7eVol8Wm+D0A=="
|
"integrity": "sha512-VgXQHoQEVZ/71B6YQHQP8/Yd/w1smGD+kCCiNvJKZ1xMD3nkN9mjoHxIqbOJMZ2q5PZlV6gXYT7eVol8Wm+D0A=="
|
||||||
},
|
},
|
||||||
"@flare/tuitio-client": {
|
"@flare/tuitio-client": {
|
||||||
"version": "1.0.3",
|
"version": "1.0.4",
|
||||||
"resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.0.3.tgz",
|
"resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.0.4.tgz",
|
||||||
"integrity": "sha512-XlNje3e985ruGtJaKUZLY6kRsgcPG0UhoYZfWjDnAsaoC15rbHc+RF3iBOO42Z7eMPDm00UYu54NiqOagu9geg==",
|
"integrity": "sha512-NTG0eB0EztcUEl3xEiGo7FY8YQP7/TxNIko65U3Mhs7Zm5+IVXlr7HcLbEr89mqaVtJmRRt1i8u/z/y0lCChwg==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@flare/js-utils": "^1.0.3",
|
"@flare/js-utils": "^1.0.3",
|
||||||
"axios": "^1.3.2"
|
"axios": "^1.3.2"
|
||||||
|
|
|
@ -48,7 +48,7 @@
|
||||||
"README.md"
|
"README.md"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@flare/tuitio-client": "^1.0.3"
|
"@flare/tuitio-client": "^1.0.4"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"react": "^16.14.0"
|
"react": "^16.14.0"
|
||||||
|
|
|
@ -17,8 +17,8 @@ const useTuitioClient = (options: TuitioClientHookOptions) => {
|
||||||
|
|
||||||
const login = async (userName: string, password: string) => {
|
const login = async (userName: string, password: string) => {
|
||||||
try {
|
try {
|
||||||
const connector = new TuitioClient(state.configuration.tuitioUrl);
|
const client = new TuitioClient(state.configuration.tuitioUrl);
|
||||||
const response = await connector.authenticate(userName, password);
|
const response = await client.authenticate(userName, password);
|
||||||
if (response.token) {
|
if (response.token) {
|
||||||
dispatchActions.onLoginSuccess(response.token, userName);
|
dispatchActions.onLoginSuccess(response.token, userName);
|
||||||
onLoginSuccess && onLoginSuccess(response, userName);
|
onLoginSuccess && onLoginSuccess(response, userName);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import type { TuitioToken } from "@flare/tuitio-client";
|
import type { TuitioToken } from "@flare/tuitio-client";
|
||||||
|
|
||||||
export type TuitioConfiguration = { tuitioUrl: string | null };
|
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 = {
|
export type TuitioDispatchActions = {
|
||||||
onLoginSuccess: (token: TuitioToken, userName: string) => void;
|
onLoginSuccess: (token: TuitioToken, userName: string) => void;
|
||||||
onLogout: () => void;
|
onLogout: () => void;
|
||||||
|
|
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
|
@ -3,14 +3,14 @@ import { renderHook } from "@testing-library/react-hooks";
|
||||||
import { TuitioContext } from "../../src/contexts";
|
import { TuitioContext } from "../../src/contexts";
|
||||||
import { useTuitioUser } from "../../src/hooks";
|
import { useTuitioUser } from "../../src/hooks";
|
||||||
|
|
||||||
describe("useTuitioUser", () => {
|
describe("useTuitioUser: positive flow", () => {
|
||||||
it("should return the userName and lastLoginDate from the TuitioContext", () => {
|
it("should return the userName and lastLoginDate from the TuitioContext", () => {
|
||||||
const testState = {
|
const testState = {
|
||||||
userName: "test-user",
|
userName: "test-user",
|
||||||
token: {
|
token: {
|
||||||
raw: "mocked-token",
|
raw: "mocked-token",
|
||||||
validFrom: new Date(2022, 1, 1),
|
validFrom: new Date(2023, 1, 1),
|
||||||
validUntil: new Date(2022, 12, 31)
|
validUntil: new Date(2023, 12, 31)
|
||||||
},
|
},
|
||||||
configuration: {
|
configuration: {
|
||||||
tuitioUrl: null
|
tuitioUrl: null
|
||||||
|
@ -23,6 +23,26 @@ describe("useTuitioUser", () => {
|
||||||
const { result } = renderHook(() => useTuitioUser(), { wrapper });
|
const { result } = renderHook(() => useTuitioUser(), { wrapper });
|
||||||
|
|
||||||
expect(result.current.userName).toEqual("test-user");
|
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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue