Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix m1 tests #6

Merged
merged 4 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 7 additions & 11 deletions src/hooks/common/useLoading.test.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import { act, fireEvent, renderHook, waitFor } from "@testing-library/react";
import { act, renderHook } from "@testing-library/react";
import { useLoading } from "./useLoading";

describe("useLoading", () => {
it("should set isLoading to true", () => {
const {
result: { current },
} = renderHook(() => useLoading());
const { result } = renderHook(() => useLoading());

act(() => {
current.starLoading();
result.current.starLoading();
});
waitFor(() => expect(current.isLoading).toBe(true));
expect(result.current.isLoading).toBe(true);
});

it("should set isLoading to false", () => {
const {
result: { current },
} = renderHook(() => useLoading(true));
const { result } = renderHook(() => useLoading(true));

act(() => {
current.endLoading();
result.current.endLoading();
});
waitFor(() => expect(current.isLoading).toBe(false));
expect(result.current.isLoading).toBe(false);
});
});
6 changes: 3 additions & 3 deletions src/hooks/common/useLoading.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useState } from "react";
import { useState, useCallback } from "react";

export const useLoading = (defaultState = false) => {
const [isLoading, setIsLoading] = useState(defaultState);

const starLoading = () => setIsLoading(true);
const starLoading = useCallback(() => setIsLoading(true), []);

const endLoading = () => setIsLoading(false);
const endLoading = useCallback(() => setIsLoading(false), []);

return {
isLoading,
Expand Down
6 changes: 4 additions & 2 deletions src/pages/balance/components/ChainSelector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ describe("ChainSelector", () => {
await act(() => {
fireEvent.click(button);
});
waitFor(() => {
const account = screen.getByText(CHAINS[0].name);
await waitFor(() => {
const account = screen.getByText(CHAINS[0].chains[0].name);
expect(account).toBeDefined();
});
});
Expand All @@ -88,6 +88,8 @@ describe("ChainSelector", () => {
expect(account).toBeDefined();
});
const account = screen.getByText(CHAINS[0].chains[1].name);

(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
await act(() => {
fireEvent.click(account.parentElement as HTMLElement);
});
Expand Down
1 change: 0 additions & 1 deletion src/pages/settings/advanced_settings/ManageNetworks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const ManageNetworks = () => {
const getNetworks = async () => {
try {
const networks = await Extension.getAllChains();
console.log(networks);
setNetworks(networks);
} catch (error) {
setNetworks([]);
Expand Down
5 changes: 3 additions & 2 deletions src/pages/signIn/SignIn.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe("SignIn", () => {
}));

vi.mock("@src/Extension", () => ({
signIn: () => signIn(),
default: { signIn: () => signIn() },
}));
});

Expand All @@ -47,7 +47,8 @@ describe("SignIn", () => {
fireEvent.change(passwordInput, { target: { value: "Test.123" } });
fireEvent.click(button);
});
waitFor(() => expect(signIn).toHaveBeenCalled());

expect(signIn).toHaveBeenCalledOnce();
});

it("should go to forgot password", async () => {
Expand Down
10 changes: 7 additions & 3 deletions src/providers/accountProvider/AccountProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
import { AccountProvider, useAccountContext } from "./AccountProvider";
import { selectedEVMAccountMock } from "../../tests/mocks/account-mocks";
import Account from "@src/storage/entities/Account";
import { I18nextProvider } from "react-i18next";
import i18n from "@src/utils/i18n";

const testIds = {
createAccount: "create-account",
Expand Down Expand Up @@ -61,9 +63,11 @@ const TestComponent = () => {

const renderComponent = () => {
return render(
<AccountProvider>
<TestComponent />
</AccountProvider>
<I18nextProvider i18n={i18n}>
<AccountProvider>
<TestComponent />
</AccountProvider>
</I18nextProvider>
);
};

Expand Down
59 changes: 36 additions & 23 deletions src/providers/authProvider/AuthProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { vi } from "vitest";
import { FC, useState } from "react";
import { AccountFormType } from "@src/pages";
import { AccountType } from "@src/accounts/types";
import { I18nextProvider } from "react-i18next";
import i18n from "@src/utils/i18n";

interface ResponseState {
create: null | boolean;
Expand Down Expand Up @@ -89,20 +91,23 @@ const mockAccountForm: AccountFormType = {

const renderComponent = (accountForm: AccountFormType) => {
return render(
<AuthProvider>
<TestComponent createdAccount={accountForm} />
</AuthProvider>
<I18nextProvider i18n={i18n}>
<AuthProvider>
<TestComponent createdAccount={accountForm} />
</AuthProvider>
</I18nextProvider>
);
};

describe("AuthProvider", () => {
beforeAll(() => {
vi.mock("@src/Extension", () => ({
defualt: {
default: {
createAccounts: vi.fn().mockResolvedValue(true),
isUnlocked: vi.fn().mockResolvedValue(true),
importAccount: vi.fn().mockResolvedValue(true),
restorePassword: vi.fn().mockResolvedValue(true),
deriveAccount: vi.fn().mockResolvedValue(true),
},
}));
});
Expand All @@ -124,10 +129,10 @@ describe("AuthProvider", () => {
it("should return true", async () => {
renderComponent(mockAccountForm);

act(() => {
await act(() => {
fireEvent.click(screen.getByTestId(testIds.createBtn));
});
waitFor(() =>
await waitFor(() =>
expect(screen.getByTestId(testIds.createResponse).innerHTML).toEqual(
"true"
)
Expand All @@ -150,27 +155,33 @@ describe("AuthProvider", () => {

describe("importAccount", () => {
it("should return true", async () => {
const Extension = await import("@src/Extension");
Extension.default.isUnlocked = vi.fn().mockReturnValue(true);

renderComponent(mockAccountForm);

act(() => {
fireEvent.click(screen.getByTestId(testIds.importBtn));
});

waitFor(() =>
await waitFor(() => {
expect(screen.getByTestId(testIds.importResonse).innerHTML).toEqual(
"true"
)
);
);
});
});

it("should return password_required error", async () => {
const Extension = await import("@src/Extension");
Extension.default.isUnlocked = vi.fn().mockReturnValue(false);

renderComponent({ ...mockAccountForm, password: "" });

act(() => {
await act(() => {
fireEvent.click(screen.getByTestId(testIds.importBtn));
});

waitFor(() =>
await waitFor(() =>
expect(screen.getByTestId(testIds.importResonse).innerHTML).toEqual(
"false"
)
Expand All @@ -180,11 +191,11 @@ describe("AuthProvider", () => {
it("should return private_key_or_seed_required error", async () => {
renderComponent({ ...mockAccountForm, privateKeyOrSeed: "" });

act(() => {
await act(() => {
fireEvent.click(screen.getByTestId(testIds.importBtn));
});

waitFor(() =>
await waitFor(() =>
expect(screen.getByTestId(testIds.importResonse).innerHTML).toEqual(
"false"
)
Expand All @@ -194,11 +205,11 @@ describe("AuthProvider", () => {
it("should return account_type_required error", async () => {
renderComponent({ ...mockAccountForm, accountType: undefined });

act(() => {
await act(() => {
fireEvent.click(screen.getByTestId(testIds.importBtn));
});

waitFor(() =>
await waitFor(() =>
expect(screen.getByTestId(testIds.importResonse).innerHTML).toEqual(
"false"
)
Expand All @@ -208,13 +219,15 @@ describe("AuthProvider", () => {

describe("deriveAccount", () => {
it("should return true", async () => {
const Extension = await import("@src/Extension");
Extension.default.isUnlocked = vi.fn().mockReturnValue(true);
renderComponent(mockAccountForm);

act(() => {
await act(() => {
fireEvent.click(screen.getByTestId(testIds.deriveBtn));
});

waitFor(() =>
await waitFor(() =>
expect(screen.getByTestId(testIds.deriveResponse).innerHTML).toEqual(
"true"
)
Expand All @@ -224,11 +237,11 @@ describe("AuthProvider", () => {
it("should return account_type_required error", async () => {
renderComponent({ ...mockAccountForm, accountType: undefined });

act(() => {
await act(() => {
fireEvent.click(screen.getByTestId(testIds.deriveBtn));
});

waitFor(() =>
await waitFor(() =>
expect(screen.getByTestId(testIds.deriveResponse).innerHTML).toEqual(
"false"
)
Expand All @@ -240,11 +253,11 @@ describe("AuthProvider", () => {
it("should return true", async () => {
renderComponent(mockAccountForm);

act(() => {
await act(() => {
fireEvent.click(screen.getByTestId(testIds.restoreBtn));
});

waitFor(() =>
await waitFor(() =>
expect(screen.getByTestId(testIds.restoreResponse).innerHTML).toEqual(
"true"
)
Expand All @@ -258,7 +271,7 @@ describe("AuthProvider", () => {
fireEvent.click(screen.getByTestId(testIds.restoreBtn));
});

waitFor(() =>
await waitFor(() =>
expect(screen.getByTestId(testIds.restoreResponse).innerHTML).toEqual(
"false"
)
Expand All @@ -272,7 +285,7 @@ describe("AuthProvider", () => {
fireEvent.click(screen.getByTestId(testIds.restoreBtn));
});

waitFor(() =>
await waitFor(() =>
expect(screen.getByTestId(testIds.restoreResponse).innerHTML).toEqual(
"false"
)
Expand Down
36 changes: 29 additions & 7 deletions src/providers/networkProvider/NetworkProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
selectedMultiSupportChain,
} from "../../tests/mocks/chain-mocks";
import { selectedWASMAccountMock } from "../../tests/mocks/account-mocks";
import { I18nextProvider } from "react-i18next";
import i18n from "@src/utils/i18n";

const initialState: InitialState = {
chains: CHAINS,
Expand Down Expand Up @@ -65,9 +67,11 @@ const TestComponent: FC<TestComponentProps> = ({ newChain, type }) => {

const renderComponent = (props?: TestComponentProps) => {
return render(
<NetworkProvider>
<TestComponent {...props} />
</NetworkProvider>
<I18nextProvider i18n={i18n}>
<NetworkProvider>
<TestComponent {...props} />
</NetworkProvider>
</I18nextProvider>
);
};

Expand Down Expand Up @@ -170,8 +174,17 @@ describe("NetworkProvider", () => {
});
describe("useEffect", () => {
it("should init", async () => {
const Extension: any = await import("@src/Extension");
Extension.default.getNetwork = vi
.fn()
.mockResolvedValue({ chain: selectedWASMChainMock });

Extension.default.getSelectedAccount = vi
.fn()
.mockResolvedValue(selectedWASMAccountMock);

renderComponent({});
waitFor(() => {
await waitFor(() => {
const state = JSON.parse(screen.getByTestId(testIds.state).innerHTML);
expect(state).toHaveProperty("type", "WASM");
expect(state).toHaveProperty("rpc", selectedWASMChainMock.rpc.wasm);
Expand Down Expand Up @@ -255,14 +268,23 @@ describe("NetworkProvider", () => {
});
describe("setNewRpc", () => {
it("should keep the current rpc", async () => {
const Extension: any = await import("@src/Extension");
Extension.default.getNetwork = vi
.fn()
.mockResolvedValue({ chain: selectedWASMChainMock });

Extension.default.getSelectedAccount = vi
.fn()
.mockResolvedValue(selectedWASMAccountMock);

renderComponent();
act(() => {
await act(() => {
fireEvent.click(screen.getByTestId(testIds.selectedBtn));
});
act(() => {
await act(() => {
fireEvent.click(screen.getByTestId(testIds.newRpcBtn));
});
waitFor(() =>
await waitFor(() =>
expect(
JSON.parse(screen.getByTestId(testIds.state).innerHTML)
).toHaveProperty("rpc", selectedWASMChainMock.rpc.wasm)
Expand Down
Loading