Skip to content

Commit

Permalink
Merge pull request #1148 from trilitech/use-add-account-helper-in-tests
Browse files Browse the repository at this point in the history
Use addAccount in tests
  • Loading branch information
serjonya-trili authored Apr 19, 2024
2 parents f62ebce + 536827e commit 90f1d21
Show file tree
Hide file tree
Showing 54 changed files with 230 additions and 403 deletions.
15 changes: 7 additions & 8 deletions src/components/AccountDrawer/AccountDrawerDisplay.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
mockMultisigAccount,
mockNFTToken,
} from "../../mocks/factories";
import { addAccount } from "../../mocks/helpers";
import { multisigOperation, multisigs } from "../../mocks/multisig";
import { act, render, screen, userEvent, waitFor, within } from "../../mocks/testUtils";
import { mockTzktTezTransfer } from "../../mocks/transfers";
import { GHOSTNET, MAINNET } from "../../types/Network";
import { formatPkh, prettyTezAmount } from "../../utils/format";
import { Multisig } from "../../utils/multisig/types";
import { accountsSlice } from "../../utils/redux/slices/accountsSlice";
import { assetsSlice } from "../../utils/redux/slices/assetsSlice";
import { multisigActions, multisigsSlice } from "../../utils/redux/slices/multisigsSlice";
import { multisigActions } from "../../utils/redux/slices/multisigsSlice";
import { networksActions } from "../../utils/redux/slices/networks";
import { tokensSlice } from "../../utils/redux/slices/tokensSlice";
import { store } from "../../utils/redux/store";
Expand All @@ -27,8 +27,6 @@ import * as useGetOperationsModule from "../../views/operations/useGetOperations
import { AccountCard } from ".";

const { updateTezBalance, updateTokenBalance } = assetsSlice.actions;
const { addMockMnemonicAccounts } = accountsSlice.actions;
const { setMultisigs } = multisigsSlice.actions;

const selectedAccount = mockMnemonicAccount(0);
const pkh = selectedAccount.address.pkh;
Expand All @@ -51,7 +49,8 @@ beforeEach(() => {
loadMore: jest.fn(),
});
store.dispatch(networksActions.setCurrent(MAINNET));
store.dispatch(addMockMnemonicAccounts([selectedAccount, mockMnemonicAccount(1)]));
addAccount(selectedAccount);
addAccount(mockMnemonicAccount(1));
});

describe("<AccountCard />", () => {
Expand Down Expand Up @@ -323,7 +322,7 @@ describe("<AccountCard />", () => {
isLoading: false,
loadMore: jest.fn(),
});
store.dispatch(setMultisigs(multisigs));
multisigs.forEach(addAccount);
});

it('hides "Buy Tez" button', async () => {
Expand All @@ -344,7 +343,7 @@ describe("<AccountCard />", () => {
});

it("displays pending operation if any", async () => {
store.dispatch(multisigActions.setMultisigs(multisigs));
multisigs.forEach(addAccount);
store.dispatch(multisigActions.setPendingOperations([multisigOperation]));

render(<AccountCard accountPkh={multisigAccount.address.pkh} />);
Expand All @@ -362,7 +361,7 @@ describe("<AccountCard />", () => {
...multisigs[2],
pendingOperationsBigmapId: 0,
};
store.dispatch(setMultisigs([multisigWithNoOps]));
addAccount(multisigWithNoOps);

render(<AccountCard accountPkh={multisigAccount.address.pkh} />);
await screen.findByTestId("account-card-operations-tab");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
mockMnemonicAccount,
mockMultisigAccount,
} from "../../../../mocks/factories";
import { mockEstimatedFee } from "../../../../mocks/helpers";
import { addAccount, mockEstimatedFee } from "../../../../mocks/helpers";
import { pendingOps } from "../../../../mocks/multisig";
import { act, render, screen, userEvent, within } from "../../../../mocks/testUtils";
import { ImplicitAccount, MnemonicAccount } from "../../../../types/Account";
Expand All @@ -17,8 +17,6 @@ import { MAINNET } from "../../../../types/Network";
import { makeMultisigApproveOrExecuteOperation } from "../../../../types/Operation";
import * as getAccountDataHooks from "../../../../utils/hooks/getAccountDataHooks";
import { MultisigOperation } from "../../../../utils/multisig/types";
import { accountsSlice } from "../../../../utils/redux/slices/accountsSlice";
import { store } from "../../../../utils/redux/store";
import { estimate, executeOperations, makeToolkit } from "../../../../utils/tezos";

const MOCK_TEZOS_TOOLKIT = {};
Expand Down Expand Up @@ -76,7 +74,7 @@ describe("<MultisigPendingOperation />", () => {
opHash: "mockHash",
} as BatchWalletOperation);

store.dispatch(accountsSlice.actions.addMockMnemonicAccounts([account]));
addAccount(account);

const executablePendingOp: MultisigOperation = pendingOps[0];
const multisig = { ...mockMultisigAccount(0), signers: [account.address] };
Expand Down Expand Up @@ -120,7 +118,7 @@ describe("<MultisigPendingOperation />", () => {
opHash: "mockHash",
} as BatchWalletOperation);

store.dispatch(accountsSlice.actions.addMockMnemonicAccounts([signer]));
addAccount(signer);
const multisig = { ...mockMultisigAccount(0), signers: [signer.address] };
const approvablePendingOp: MultisigOperation = { ...pendingOps[0], approvals: [] };
render(<MultisigPendingOperation operation={approvablePendingOp} sender={multisig} />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import {
mockMnemonicAccount,
mockMultisigAccount,
} from "../../../../mocks/factories";
import { mockEstimatedFee } from "../../../../mocks/helpers";
import { addAccount, mockEstimatedFee } from "../../../../mocks/helpers";
import { pendingOps } from "../../../../mocks/multisig";
import { fireEvent, render, screen, within } from "../../../../mocks/testUtils";
import { MnemonicAccount } from "../../../../types/Account";
import { accountsSlice } from "../../../../utils/redux/slices/accountsSlice";
import { multisigsSlice } from "../../../../utils/redux/slices/multisigsSlice";
import { store } from "../../../../utils/redux/store";

Expand All @@ -28,7 +27,7 @@ describe("<MultisigPendingOperations />", () => {
address: mockImplicitAddress(0),
};

store.dispatch(accountsSlice.actions.addMockMnemonicAccounts([mockAccount]));
addAccount(mockAccount);

render(<MultisigPendingOperations account={multisig} />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,13 @@ import {
mockMnemonicAccount,
mockMultisigAccount,
} from "../../../../mocks/factories";
import { addAccount } from "../../../../mocks/helpers";
import { pendingOps } from "../../../../mocks/multisig";
import { render, screen } from "../../../../mocks/testUtils";
import { accountsSlice } from "../../../../utils/redux/slices/accountsSlice";
import { store } from "../../../../utils/redux/store";

const { addMockMnemonicAccounts } = accountsSlice.actions;

const signer = mockMnemonicAccount(0);
describe("<MultisigSignerTile />", () => {
beforeEach(() => {
store.dispatch(addMockMnemonicAccounts([signer]));
});
beforeEach(() => addAccount(signer));

it("displays the action button", () => {
render(
Expand Down
11 changes: 3 additions & 8 deletions src/components/AccountDrawer/RenameAccount.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
mockImplicitAccount,
mockMnemonicAccount,
} from "../../mocks/factories";
import { addAccount } from "../../mocks/helpers";
import { fireEvent, render, screen, waitFor } from "../../mocks/testUtils";
import { Account } from "../../types/Account";
import { MAINNET } from "../../types/Network";
import { accountsSlice } from "../../utils/redux/slices/accountsSlice";
import { contactsActions } from "../../utils/redux/slices/contactsSlice";
import { networksActions } from "../../utils/redux/slices/networks";
import { store } from "../../utils/redux/store";
Expand Down Expand Up @@ -42,7 +42,7 @@ describe("<RenameAccountModal />", () => {

it("does not allow the same name", async () => {
const account = mockMnemonicAccount(0);
store.dispatch(accountsSlice.actions.addMockMnemonicAccounts([account]));
addAccount(account);
render(fixture(account));

setName(account.label);
Expand All @@ -54,12 +54,7 @@ describe("<RenameAccountModal />", () => {

it("does not allow existing account name", async () => {
const account = mockMnemonicAccount(0);
store.dispatch(
accountsSlice.actions.addMockMnemonicAccounts([
account,
mockMnemonicAccount(1, "Existing Account Name"),
])
);
[account, mockMnemonicAccount(1, "Existing Account Name")].forEach(addAccount);
render(fixture(account));

setName("Existing Account Name");
Expand Down
3 changes: 1 addition & 2 deletions src/components/AccountDrawer/RenameRemoveMenuSwitch.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { mockLedgerAccount, mockMnemonicAccount, mockSocialAccount } from "../..
import { addAccount } from "../../mocks/helpers";
import { act, render, screen, userEvent } from "../../mocks/testUtils";
import { WalletClient } from "../../utils/beacon/WalletClient";
import { accountsSlice } from "../../utils/redux/slices/accountsSlice";
import { store } from "../../utils/redux/store";

beforeEach(() => {
Expand All @@ -29,7 +28,7 @@ describe("<RenameRemoveMenuSwitch />", () => {
it("shows offboarding message for last account", async () => {
const user = userEvent.setup();
const social = mockSocialAccount(0);
store.dispatch(accountsSlice.actions.addAccount(social));
addAccount(social);
render(<RenameRemoveMenuSwitch account={social} />);

await act(() => user.click(screen.getByTestId("popover-cta")));
Expand Down
13 changes: 3 additions & 10 deletions src/components/AccountTile/AccountTile.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import {
mockNFTRaw,
mockSocialAccount,
} from "../../mocks/factories";
import { addAccount } from "../../mocks/helpers";
import { act, render, screen, userEvent } from "../../mocks/testUtils";
import { MnemonicAccount } from "../../types/Account";
import { MAINNET } from "../../types/Network";
import { RawTokenBalance } from "../../types/TokenBalance";
import { accountsSlice } from "../../utils/redux/slices/accountsSlice";
import { assetsActions } from "../../utils/redux/slices/assetsSlice";
import { tokensActions } from "../../utils/redux/slices/tokensSlice";
import { store } from "../../utils/redux/store";
Expand All @@ -23,30 +22,24 @@ describe("<AccountTile />", () => {
balance: "3",
iconTestId: "identicon",
account: mockMnemonicAccount(0),
createAction: (acc: MnemonicAccount) => accountsSlice.actions.addMockMnemonicAccounts([acc]),
},
{
balance: "3",
iconTestId: "ledger-icon",
account: mockLedgerAccount(0),
createAction: accountsSlice.actions.addAccount,
},
{
balance: "3",
iconTestId: "social-icon",
account: mockSocialAccount(0),
createAction: accountsSlice.actions.addAccount,
},
{
balance: "3",
iconTestId: "key-icon",
account: mockMultisigAccount(0),
createAction: accountsSlice.actions.addAccount,
},
])("$account.type account", ({ balance, iconTestId, createAction, account }) => {
beforeEach(() => {
store.dispatch(createAction(account as any));
});
])("$account.type account", ({ balance, iconTestId, account }) => {
beforeEach(() => addAccount(account));

it("renders icon", () => {
render(<AccountTile account={account} balance={balance} />);
Expand Down
6 changes: 3 additions & 3 deletions src/components/AddressPill/useAddressKind.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {
mockImplicitAddress,
mockMnemonicAccount,
} from "../../mocks/factories";
import { addAccount } from "../../mocks/helpers";
import { multisigs } from "../../mocks/multisig";
import { renderHook } from "../../mocks/testUtils";
import { parseContractPkh, parseImplicitPkh, parsePkh } from "../../types/Address";
import { MAINNET } from "../../types/Network";
import { accountsSlice } from "../../utils/redux/slices/accountsSlice";
import { assetsSlice } from "../../utils/redux/slices/assetsSlice";
import { contactsSlice } from "../../utils/redux/slices/contactsSlice";
import { multisigsSlice } from "../../utils/redux/slices/multisigsSlice";
Expand All @@ -29,7 +29,7 @@ beforeEach(() => {
describe("useAddressKind", () => {
it("returns owned implicit account", () => {
const mnemonicAccount = mockMnemonicAccount(0);
store.dispatch(accountsSlice.actions.addMockMnemonicAccounts([mockMnemonicAccount(0)]));
addAccount(mockMnemonicAccount(0));

const { result: addressKindRef } = renderHook(() => useAddressKind(mnemonicAccount.address));

Expand Down Expand Up @@ -148,8 +148,8 @@ describe("useAddressKind", () => {
{ type: "fa2", address: uUSD(mockImplicitAddress(0)).token.contract.address },
{ type: "baker", address: mockBaker(1).address },
])("prioritizes $type over the contact", ({ type, address }) => {
addAccount(mockMnemonicAccount(0));
store.dispatch(multisigsSlice.actions.setMultisigs(multisigs));
store.dispatch(accountsSlice.actions.addMockMnemonicAccounts([mockMnemonicAccount(0)]));
store.dispatch(
tokensSlice.actions.addTokens({
network: MAINNET,
Expand Down
9 changes: 4 additions & 5 deletions src/components/AddressTile/AddressTile.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { mockMnemonicAccount } from "../../mocks/factories";
import { addAccount } from "../../mocks/helpers";
import { act, render, screen, userEvent } from "../../mocks/testUtils";
import { formatPkh } from "../../utils/format";
import { accountsSlice } from "../../utils/redux/slices/accountsSlice";
import { assetsActions } from "../../utils/redux/slices/assetsSlice";
import { store } from "../../utils/redux/store";

describe("<AddressTileIcon />", () => {
it("displays label", () => {
const account = mockMnemonicAccount(0);
store.dispatch(accountsSlice.actions.addMockMnemonicAccounts([account]));
addAccount(account);

render(<AddressTile address={account.address} />);

Expand All @@ -20,7 +19,7 @@ describe("<AddressTileIcon />", () => {
describe("Full name tooltip", () => {
it("is hidden when cursor is not on account label", () => {
const account = mockMnemonicAccount(0);
store.dispatch(accountsSlice.actions.addMockMnemonicAccounts([account]));
addAccount(account);

render(<AddressTile address={account.address} />);

Expand All @@ -30,7 +29,7 @@ describe("<AddressTileIcon />", () => {
it("is shown when cursor is on account tile", async () => {
const user = userEvent.setup();
const account = mockMnemonicAccount(0);
store.dispatch(accountsSlice.actions.addMockMnemonicAccounts([account]));
addAccount(account);

render(<AddressTile address={account.address} />);

Expand All @@ -46,7 +45,7 @@ describe("<AddressTileIcon />", () => {
describe("address", () => {
it("is formatted when known", () => {
const account = mockMnemonicAccount(0);
store.dispatch(accountsSlice.actions.addMockMnemonicAccounts([account]));
addAccount(account);

render(<AddressTile address={account.address} />);

Expand Down
8 changes: 4 additions & 4 deletions src/components/AddressTile/useAddressKind.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
mockMnemonicAccount,
mockSocialAccount,
} from "../../mocks/factories";
import { addAccount } from "../../mocks/helpers";
import { multisigs } from "../../mocks/multisig";
import { renderHook } from "../../mocks/testUtils";
import { parseImplicitPkh, parsePkh } from "../../types/Address";
import { MAINNET } from "../../types/Network";
import { accountsSlice } from "../../utils/redux/slices/accountsSlice";
import { assetsSlice } from "../../utils/redux/slices/assetsSlice";
import { contactsSlice } from "../../utils/redux/slices/contactsSlice";
import { multisigsSlice } from "../../utils/redux/slices/multisigsSlice";
Expand All @@ -21,7 +21,7 @@ import { store } from "../../utils/redux/store";
describe("useAddressKind", () => {
it("returns mnemonic account", () => {
const account = mockMnemonicAccount(0);
store.dispatch(accountsSlice.actions.addMockMnemonicAccounts([account]));
addAccount(account);

const { result: addressKindRef } = renderHook(() => useAddressKind(account.address));

Expand All @@ -34,7 +34,7 @@ describe("useAddressKind", () => {

it("returns social account", () => {
const account = mockSocialAccount(0);
store.dispatch(accountsSlice.actions.addAccount(account));
addAccount(account);

const { result: addressKindRef } = renderHook(() => useAddressKind(account.address));

Expand All @@ -47,7 +47,7 @@ describe("useAddressKind", () => {

it("returns ledger account", () => {
const account = mockLedgerAccount(0);
store.dispatch(accountsSlice.actions.addAccount(account));
addAccount(account);

const { result: addressKindRef } = renderHook(() => useAddressKind(account.address));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ConnectOptions } from "./ConnectOptions";
import { mockSocialAccount } from "../../../mocks/factories";
import { addAccount } from "../../../mocks/helpers";
import { act, render, screen, userEvent } from "../../../mocks/testUtils";
import { accountsActions } from "../../../utils/redux/slices/accountsSlice";
import { store } from "../../../utils/redux/store";
import { OnboardingStep } from "../OnboardingStep";

const goToStepMock = jest.fn();
Expand Down Expand Up @@ -30,7 +29,7 @@ describe("<ConnectOptions />", () => {
});

it("hides the button if a user tries to add another account", () => {
store.dispatch(accountsActions.addAccount(mockSocialAccount(0)));
addAccount(mockSocialAccount(0));

render(fixture(goToStepMock));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { MasterPassword } from "./MasterPassword";
import { mockMnemonicAccount } from "../../../mocks/factories";
import { addAccount } from "../../../mocks/helpers";
import { mnemonic1 } from "../../../mocks/mockMnemonic";
import { render, screen } from "../../../mocks/testUtils";
import { accountsSlice } from "../../../utils/redux/slices/accountsSlice";
import { store } from "../../../utils/redux/store";

const onClose = jest.fn(() => {});

Expand All @@ -27,7 +26,7 @@ describe("<MasterPassword />", () => {
});

test("Display enter password", () => {
store.dispatch(accountsSlice.actions.addMockMnemonicAccounts([account]));
addAccount(account);
render(fixture());

expect(screen.getByTestId("confirmation")).toBeInTheDocument();
Expand Down
Loading

1 comment on commit 90f1d21

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 86.6% 3140/3626
🟡 Branches 79.28% 1083/1366
🟢 Functions 85.32% 994/1165
🟢 Lines 86.36% 2969/3438

Test suite run success

1374 tests passing in 172 suites.

Report generated by 🧪jest coverage report action from 90f1d21

Please sign in to comment.