diff --git a/package.json b/package.json index 829e17c3..43f9fb17 100755 --- a/package.json +++ b/package.json @@ -13,7 +13,9 @@ "build:firefox": "BROWSER_TARGET=FIREFOX vite build", "build": "NODE_ENV=production tsc --noEmit && npm run build:chrome && npm run build:firefox", "dev": "NODE_ENV=develop BROWSER_TARGET=CHROME nodemon", - "dev:firefox": "NODE_ENV=develop BROWSER_TARGET=FIREFOX nodemon" + "dev:firefox": "NODE_ENV=develop BROWSER_TARGET=FIREFOX nodemon", + "test": "vitest", + "coverage": "vitest run --coverage" }, "type": "module", "dependencies": { @@ -42,6 +44,9 @@ "devDependencies": { "@esbuild-plugins/node-globals-polyfill": "^0.2.3", "@esbuild-plugins/node-modules-polyfill": "^0.2.2", + "@testing-library/jest-dom": "^5.16.5", + "@testing-library/react": "^14.0.0", + "@testing-library/react-hooks": "^8.0.1", "@types/chrome": "^0.0.208", "@types/node": "^18.11.18", "@types/react": "^18.0.26", @@ -49,6 +54,7 @@ "@typescript-eslint/eslint-plugin": "^5.48.2", "@typescript-eslint/parser": "^5.48.2", "@vitejs/plugin-react": "^3.0.1", + "@vitest/coverage-c8": "^0.29.1", "autoprefixer": "^10.4.13", "eslint": "^8.32.0", "eslint-config-prettier": "^8.6.0", @@ -57,12 +63,14 @@ "eslint-plugin-react": "^7.32.1", "eslint-plugin-react-hooks": "^4.6.0", "fs-extra": "^11.1.0", + "jsdom": "^21.1.0", "nodemon": "^2.0.18", "postcss": "^8.4.21", "rollup-plugin-polyfill-node": "^0.12.0", "tailwindcss": "^3.2.4", "ts-node": "^10.9.1", "typescript": "^4.9.4", - "vite": "^4.0.4" + "vite": "^4.0.4", + "vitest": "^0.29.1" } } diff --git a/src/providers/authProvider/AuthProvider.test.tsx b/src/providers/authProvider/AuthProvider.test.tsx new file mode 100644 index 00000000..eaf5877e --- /dev/null +++ b/src/providers/authProvider/AuthProvider.test.tsx @@ -0,0 +1,282 @@ +import { + act, + fireEvent, + render, + screen, + waitFor, +} from "@testing-library/react"; +import { AuthProvider, useAuthContext, reducer } from "./AuthProvider"; +import { vi } from "vitest"; +import { FC, useState } from "react"; +import { AccountFormType } from "@src/pages"; +import { AccountType } from "@src/accounts/types"; + +vi.mock("@src/Extension", () => { + const Extension = { + createAccounts: vi.fn().mockResolvedValue(true), + isUnlocked: vi.fn().mockResolvedValue(true), + importAccount: vi.fn().mockResolvedValue(true), + restorePassword: vi.fn().mockResolvedValue(true), + }; + + return { + default: Extension, + }; +}); + +interface ResponseState { + create: null | boolean; + derive: null | boolean; + import: null | boolean; + restore: null | boolean; +} + +interface TestComponentProps { + createdAccount: AccountFormType; +} + +const testIds = { + createBtn: "create-button", + deriveBtn: "derive-button", + importBtn: "import-button", + restoreBtn: "restore-button", + createResponse: "create-response", + deriveResponse: "derive-response", + importResonse: "import-response", + restoreResponse: "restore-response", +}; + +const TestComponent: FC = ({ createdAccount }) => { + const { createAccount, deriveAccount, importAccount, restorePassword } = + useAuthContext(); + + const [responses, setresponses] = useState({ + create: null, + derive: null, + import: null, + restore: null, + }); + + const _create = async () => { + const _res = await createAccount(createdAccount); + setresponses((state) => ({ ...state, create: _res })); + }; + + const _derive = async () => { + const _res = await deriveAccount(createdAccount); + setresponses((state) => ({ ...state, derive: _res })); + }; + + const _import = async () => { + const _res = await importAccount(createdAccount); + setresponses((state) => ({ ...state, import: _res })); + }; + + const _restore = async () => { + const _res = await restorePassword(createdAccount); + setresponses((state) => ({ ...state, restore: _res })); + }; + + return ( + <> +