From efef5a012485e2c67cd3bbfc6805019167acbb7e Mon Sep 17 00:00:00 2001 From: "Felix C. Morency" <1102868+fmorency@users.noreply.github.com> Date: Fri, 23 Aug 2024 14:43:28 -0400 Subject: [PATCH] test: my denoms --- components/factory/components/MyDenoms.tsx | 2 +- .../components/__tests__/MyDenoms.test.tsx | 71 +++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 components/factory/components/__tests__/MyDenoms.test.tsx diff --git a/components/factory/components/MyDenoms.tsx b/components/factory/components/MyDenoms.tsx index 841ab401..899705e7 100644 --- a/components/factory/components/MyDenoms.tsx +++ b/components/factory/components/MyDenoms.tsx @@ -60,7 +60,7 @@ export default function MyDenoms({ const renderSkeleton = () => (
-
+
); diff --git a/components/factory/components/__tests__/MyDenoms.test.tsx b/components/factory/components/__tests__/MyDenoms.test.tsx new file mode 100644 index 00000000..67688f1a --- /dev/null +++ b/components/factory/components/__tests__/MyDenoms.test.tsx @@ -0,0 +1,71 @@ +import { afterEach, describe, expect, test, jest, mock } from "bun:test"; +import React from "react"; +import { screen, cleanup, fireEvent } from "@testing-library/react"; +import MyDenoms from "@/components/factory/components/MyDenoms"; +import matchers from "@testing-library/jest-dom/matchers"; +import { renderWithChainProvider } from "@/tests/render"; +import {mockDenom, mockMfxDenom} from "@/tests/mock"; + +expect.extend(matchers); + +// Mock next/router +const m = jest.fn() +mock.module('next/router', () => ({ + useRouter: m.mockReturnValue({ + query: {}, + push: jest.fn(), + }) +})) + +// TODO: Mock DenomImage until we can fix the URL parsing issue +mock.module('@/components/factory/components/DenomImage', () => ({ + DenomImage: () =>
DenomImage
, +})) + +const renderWithProps = (props = {}) => { + const defaultProps = { + denoms: [], + isLoading: false, + isError: null, + refetchDenoms: jest.fn(), + onSelectDenom: jest.fn(), + }; + return renderWithChainProvider(); +}; + +const allDenoms = [mockDenom, mockMfxDenom]; + +describe("MyDenoms", () => { + afterEach(cleanup); + + test("renders loading skeleton when isLoading is true", () => { + renderWithProps({ isLoading: true }); + expect(screen.getByLabelText("skeleton")).toBeInTheDocument(); + }); + + test("renders and selects denoms correctly", () => { + const onSelectDenom = jest.fn(); + renderWithProps({ denoms: allDenoms, onSelectDenom }); + + const denom1 = screen.getByText("TEST"); + fireEvent.click(denom1); + expect(onSelectDenom).toHaveBeenCalledWith(mockDenom); + }); + + test("filters denoms based on search query", () => { + renderWithProps({ denoms: allDenoms }); + + const searchInput = screen.getByPlaceholderText("Search..."); + fireEvent.change(searchInput, { target: { value: "TEST" } }); + expect(screen.getByText("TEST")).toBeInTheDocument(); + expect(screen.queryByText("MFX")).not.toBeInTheDocument(); + }); + + test("displays 'No tokens found' when no denoms match search query", () => { + renderWithProps({ denoms: allDenoms }); + + const searchInput = screen.getByPlaceholderText("Search..."); + fireEvent.change(searchInput, { target: { value: "Nonexistent Denom" } }); + expect(screen.getByText("No tokens found")).toBeInTheDocument(); + }); +});