Skip to content

Commit

Permalink
test(packages/ui): Add more unit tests for RawInput and EtherDeposit …
Browse files Browse the repository at this point in the history
…forms
  • Loading branch information
nevendyulgerov committed Mar 11, 2024
1 parent 1c4d663 commit 92852fc
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
121 changes: 121 additions & 0 deletions packages/ui/test/EtherDepositForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,127 @@ describe("Rollups EtherDepositForm", () => {

expect(onSearchApplicationsMock).toHaveBeenCalledWith("");
});

it('should enable "usePrepareEtherPortalDepositEther" only when the form is valid', async () => {
const rollupsWagmi = await import("@cartesi/rollups-wagmi");
const mockedHook = vi.fn().mockReturnValue({
...rollupsWagmi.usePrepareEtherPortalDepositEther,
loading: false,
error: null,
});
rollupsWagmi.usePrepareEtherPortalDepositEther = vi
.fn()
.mockImplementation(mockedHook);

const { container } = render(<Component {...defaultProps} />);
const applicationsInput = container.querySelector(
"input",
) as HTMLInputElement;
const amountInput = container.querySelectorAll(
"input",
)[1] as HTMLInputElement;
const textarea = container.querySelector(
"textarea",
) as HTMLTextAreaElement;

const [application] = applications;

fireEvent.change(applicationsInput, {
target: {
value: "",
},
});

fireEvent.change(amountInput, {
target: {
value: "0.1",
},
});

fireEvent.change(textarea, {
target: {
value: "0x",
},
});

expect(mockedHook).toHaveBeenLastCalledWith({
args: ["0x0000000000000000000000000000000000000000", "0x"],
enabled: false,
value: 100000000000000000n,
});

fireEvent.change(applicationsInput, {
target: {
value: application,
},
});

fireEvent.change(amountInput, {
target: {
value: "",
},
});

fireEvent.change(textarea, {
target: {
value: "0x",
},
});

expect(mockedHook).toHaveBeenLastCalledWith({
args: [getAddress(application), "0x"],
enabled: false,
value: undefined,
});

fireEvent.change(applicationsInput, {
target: {
value: application,
},
});

fireEvent.change(amountInput, {
target: {
value: "0.1",
},
});

fireEvent.change(textarea, {
target: {
value: "",
},
});

expect(mockedHook).toHaveBeenLastCalledWith({
args: ["0x60a7048c3136293071605a4eaffef49923e981cc", "0x"],
enabled: false,
value: 100000000000000000n,
});

fireEvent.change(applicationsInput, {
target: {
value: application,
},
});

fireEvent.change(amountInput, {
target: {
value: "0.1",
},
});

fireEvent.change(textarea, {
target: {
value: "0x123",
},
});

expect(mockedHook).toHaveBeenLastCalledWith({
args: ["0x60a7048c3136293071605a4eaffef49923e981cc", "0x123"],
enabled: true,
value: 100000000000000000n,
});
});
});

describe("ApplicationAutocomplete", () => {
Expand Down
70 changes: 70 additions & 0 deletions packages/ui/test/RawInputForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,76 @@ describe("Rollups RawInputForm", () => {

expect(onSearchApplicationsMock).toHaveBeenCalledWith("");
});

it('should enable "usePrepareInputBoxAddInput" only when the form is valid', async () => {
const rollupsWagmi = await import("@cartesi/rollups-wagmi");
const mockedHook = vi.fn().mockReturnValue({
...rollupsWagmi.usePrepareInputBoxAddInput,
loading: false,
error: null,
});
rollupsWagmi.usePrepareInputBoxAddInput = vi
.fn()
.mockImplementation(mockedHook);

const { container } = render(<Component {...defaultProps} />);
const input = container.querySelector("input") as HTMLInputElement;
const textarea = container.querySelector(
"textarea",
) as HTMLTextAreaElement;

const [application] = applications;
fireEvent.change(input, {
target: {
value: application,
},
});

fireEvent.change(textarea, {
target: {
value: "",
},
});

expect(mockedHook).toHaveBeenLastCalledWith({
args: [getAddress(application), ""],
enabled: false,
});

fireEvent.change(input, {
target: {
value: "",
},
});

fireEvent.change(textarea, {
target: {
value: "0x",
},
});

expect(mockedHook).toHaveBeenLastCalledWith({
args: ["0x0000000000000000000000000000000000000000", "0x"],
enabled: false,
});

fireEvent.change(input, {
target: {
value: application,
},
});

fireEvent.change(textarea, {
target: {
value: "0x",
},
});

expect(mockedHook).toHaveBeenLastCalledWith({
args: [getAddress(application), "0x"],
enabled: true,
});
});
});

describe("ApplicationAutocomplete", () => {
Expand Down

0 comments on commit 92852fc

Please sign in to comment.