hooks tests
parent
cdef0ce630
commit
9429cc78fb
|
@ -13,6 +13,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@testing-library/react": "^12.1.5",
|
"@testing-library/react": "^12.1.5",
|
||||||
|
"@testing-library/react-hooks": "^7.0.2",
|
||||||
"@types/jest": "^29.4.0",
|
"@types/jest": "^29.4.0",
|
||||||
"@types/react": "^18.0.27",
|
"@types/react": "^18.0.27",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.51.0",
|
"@typescript-eslint/eslint-plugin": "^5.51.0",
|
||||||
|
@ -1225,6 +1226,35 @@
|
||||||
"react-dom": "<18.0.0"
|
"react-dom": "<18.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@testing-library/react-hooks": {
|
||||||
|
"version": "7.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/@testing-library/react-hooks/-/react-hooks-7.0.2.tgz",
|
||||||
|
"integrity": "sha512-dYxpz8u9m4q1TuzfcUApqi8iFfR6R0FaMbr2hjZJy1uC8z+bO/K4v8Gs9eogGKYQop7QsrBTFkv/BCF7MzD2Cg==",
|
||||||
|
"dev": true,
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/runtime": "^7.12.5",
|
||||||
|
"@types/react": ">=16.9.0",
|
||||||
|
"@types/react-dom": ">=16.9.0",
|
||||||
|
"@types/react-test-renderer": ">=16.9.0",
|
||||||
|
"react-error-boundary": "^3.1.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"react": ">=16.9.0",
|
||||||
|
"react-dom": ">=16.9.0",
|
||||||
|
"react-test-renderer": ">=16.9.0"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"react-dom": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"react-test-renderer": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@tootallnate/once": {
|
"node_modules/@tootallnate/once": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz",
|
||||||
|
@ -1390,6 +1420,15 @@
|
||||||
"csstype": "^3.0.2"
|
"csstype": "^3.0.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/react-test-renderer": {
|
||||||
|
"version": "18.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/react-test-renderer/-/react-test-renderer-18.0.0.tgz",
|
||||||
|
"integrity": "sha512-C7/5FBJ3g3sqUahguGi03O79b8afNeSD6T8/GU50oQrJCU0bVCCGQHaGKUbg2Ce8VQEEqTw8/HiS6lXHHdgkdQ==",
|
||||||
|
"dev": true,
|
||||||
|
"dependencies": {
|
||||||
|
"@types/react": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@types/scheduler": {
|
"node_modules/@types/scheduler": {
|
||||||
"version": "0.16.2",
|
"version": "0.16.2",
|
||||||
"resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.2.tgz",
|
"resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.2.tgz",
|
||||||
|
@ -5790,6 +5829,22 @@
|
||||||
"react": "^16.14.0"
|
"react": "^16.14.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/react-error-boundary": {
|
||||||
|
"version": "3.1.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/react-error-boundary/-/react-error-boundary-3.1.4.tgz",
|
||||||
|
"integrity": "sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==",
|
||||||
|
"dev": true,
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/runtime": "^7.12.5"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10",
|
||||||
|
"npm": ">=6"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"react": ">=16.13.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/react-is": {
|
"node_modules/react-is": {
|
||||||
"version": "18.2.0",
|
"version": "18.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/react-is/-/react-is-18.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/react-is/-/react-is-18.2.0.tgz",
|
||||||
|
@ -7747,6 +7802,19 @@
|
||||||
"@types/react-dom": "<18.0.0"
|
"@types/react-dom": "<18.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"@testing-library/react-hooks": {
|
||||||
|
"version": "7.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/@testing-library/react-hooks/-/react-hooks-7.0.2.tgz",
|
||||||
|
"integrity": "sha512-dYxpz8u9m4q1TuzfcUApqi8iFfR6R0FaMbr2hjZJy1uC8z+bO/K4v8Gs9eogGKYQop7QsrBTFkv/BCF7MzD2Cg==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"@babel/runtime": "^7.12.5",
|
||||||
|
"@types/react": ">=16.9.0",
|
||||||
|
"@types/react-dom": ">=16.9.0",
|
||||||
|
"@types/react-test-renderer": ">=16.9.0",
|
||||||
|
"react-error-boundary": "^3.1.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"@tootallnate/once": {
|
"@tootallnate/once": {
|
||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/@tootallnate/once/-/once-2.0.0.tgz",
|
||||||
|
@ -7911,6 +7979,15 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"@types/react-test-renderer": {
|
||||||
|
"version": "18.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/react-test-renderer/-/react-test-renderer-18.0.0.tgz",
|
||||||
|
"integrity": "sha512-C7/5FBJ3g3sqUahguGi03O79b8afNeSD6T8/GU50oQrJCU0bVCCGQHaGKUbg2Ce8VQEEqTw8/HiS6lXHHdgkdQ==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"@types/react": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
"@types/scheduler": {
|
"@types/scheduler": {
|
||||||
"version": "0.16.2",
|
"version": "0.16.2",
|
||||||
"resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.2.tgz",
|
"resolved": "https://registry.npmjs.org/@types/scheduler/-/scheduler-0.16.2.tgz",
|
||||||
|
@ -11134,6 +11211,15 @@
|
||||||
"scheduler": "^0.19.1"
|
"scheduler": "^0.19.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"react-error-boundary": {
|
||||||
|
"version": "3.1.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/react-error-boundary/-/react-error-boundary-3.1.4.tgz",
|
||||||
|
"integrity": "sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==",
|
||||||
|
"dev": true,
|
||||||
|
"requires": {
|
||||||
|
"@babel/runtime": "^7.12.5"
|
||||||
|
}
|
||||||
|
},
|
||||||
"react-is": {
|
"react-is": {
|
||||||
"version": "18.2.0",
|
"version": "18.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/react-is/-/react-is-18.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/react-is/-/react-is-18.2.0.tgz",
|
||||||
|
|
|
@ -55,6 +55,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@testing-library/react": "^12.1.5",
|
"@testing-library/react": "^12.1.5",
|
||||||
|
"@testing-library/react-hooks": "^7.0.2",
|
||||||
"@types/jest": "^29.4.0",
|
"@types/jest": "^29.4.0",
|
||||||
"@types/react": "^18.0.27",
|
"@types/react": "^18.0.27",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.51.0",
|
"@typescript-eslint/eslint-plugin": "^5.51.0",
|
||||||
|
|
|
@ -0,0 +1,84 @@
|
||||||
|
import { renderHook, act } from "@testing-library/react-hooks";
|
||||||
|
import { TuitioClient } from "@flare/tuitio-client";
|
||||||
|
import { useTuitioClient } from "../src/hooks";
|
||||||
|
|
||||||
|
jest.mock("@flare/tuitio-client", () => ({
|
||||||
|
TuitioClient: jest.fn().mockImplementation(() => ({
|
||||||
|
authenticate: jest.fn().mockResolvedValue({
|
||||||
|
token: {
|
||||||
|
raw: "mocked-token",
|
||||||
|
validFrom: new Date(),
|
||||||
|
validUntil: new Date()
|
||||||
|
},
|
||||||
|
status: "success"
|
||||||
|
})
|
||||||
|
})),
|
||||||
|
invalidate: jest.fn(),
|
||||||
|
fetch: jest.fn().mockResolvedValue({
|
||||||
|
token: {
|
||||||
|
raw: "mocked-token",
|
||||||
|
validFrom: new Date(),
|
||||||
|
validUntil: new Date()
|
||||||
|
},
|
||||||
|
userName: "user"
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe("useTuitioClient", () => {
|
||||||
|
it("should call onLoginSuccess when login is successful", async () => {
|
||||||
|
const onLoginSuccess = jest.fn();
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
useTuitioClient({
|
||||||
|
onLoginSuccess,
|
||||||
|
onLoginFailed: jest.fn(),
|
||||||
|
onLoginError: jest.fn()
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
await result.current.login("user", "password");
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(onLoginSuccess).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
token: {
|
||||||
|
raw: "mocked-token",
|
||||||
|
validFrom: expect.any(Date),
|
||||||
|
validUntil: expect.any(Date)
|
||||||
|
},
|
||||||
|
status: "success"
|
||||||
|
},
|
||||||
|
"user"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should call onLoginFailed when login is unsuccessful", async () => {
|
||||||
|
const onLoginFailed = jest.fn();
|
||||||
|
(TuitioClient as jest.Mocked<any>).mockImplementation(() => ({
|
||||||
|
authenticate: jest.fn().mockResolvedValue({
|
||||||
|
token: null,
|
||||||
|
status: "failed"
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
useTuitioClient({
|
||||||
|
onLoginSuccess: jest.fn(),
|
||||||
|
onLoginFailed,
|
||||||
|
onLoginError: jest.fn()
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
await result.current.login("user", "password");
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(onLoginFailed).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
token: null,
|
||||||
|
status: "failed"
|
||||||
|
},
|
||||||
|
"user"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue