Skip to content

Commit

Permalink
Add Vitest testing to the CI pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
ilamanov committed Jan 22, 2025
1 parent cdcbe3e commit 5650a12
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .env.test
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DUNE_API_KEY=
DUNE_API_KEY=V21AGastKizKLIkeLey9y7KiMDaZrLct
5 changes: 4 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ jobs:
- name: Install dependencies
run: npm install --no-audit --no-fund

- name: "Test: prettier"
- name: "Test: Prettier"
run: npm run test:prettier

- name: "Test: Vitest"
run: npm test
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from "react";
import { renderHook, waitFor } from "@testing-library/react";
import { DuneProvider } from "../../src/provider";
import { useTokenBalances } from "../../src/useTokenBalances";
import { fetchBalances } from "../../src/duneApi";
import { useEvmTokenBalances } from "../../src/evm/useEvmTokenBalances";
import { fetchEvmBalances } from "../../src/evm/duneApi";
import { vi } from "vitest";

// Mock the Dune API
vi.mock("../../src/duneApi", () => ({
fetchBalances: vi.fn(),
vi.mock("../../src/evm/duneApi", () => ({
fetchEvmBalances: vi.fn(),
}));

const mockFetchBalances = fetchBalances as jest.Mock;
const mockFetchBalances = fetchEvmBalances as jest.Mock;

// A wrapper for the hook that provides the required context
const wrapper = ({ children }: { children: React.ReactNode }) => (
Expand All @@ -19,13 +19,15 @@ const wrapper = ({ children }: { children: React.ReactNode }) => (
</DuneProvider>
);

describe("useTokenBalances", () => {
describe("useEvmTokenBalances", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("should return null if the wallet address is not a valid address", () => {
const { result } = renderHook(() => useTokenBalances("0x123"), { wrapper });
const { result } = renderHook(() => useEvmTokenBalances("0x123"), {
wrapper,
});

expect(result.current).toEqual({
data: null,
Expand Down Expand Up @@ -56,7 +58,7 @@ describe("useTokenBalances", () => {
};
mockFetchBalances.mockResolvedValueOnce(mockResponse);

const { result } = renderHook(() => useTokenBalances(walletAddress), {
const { result } = renderHook(() => useEvmTokenBalances(walletAddress), {
wrapper,
});

Expand Down Expand Up @@ -84,7 +86,7 @@ describe("useTokenBalances", () => {
const mockError = new Error("Failed to fetch token balances");
mockFetchBalances.mockRejectedValueOnce(mockError);

const { result } = renderHook(() => useTokenBalances(walletAddress), {
const { result } = renderHook(() => useEvmTokenBalances(walletAddress), {
wrapper,
});

Expand Down Expand Up @@ -113,7 +115,7 @@ describe("useTokenBalances", () => {
<DuneProvider duneApiKey="">{children}</DuneProvider>
);

const { result } = renderHook(() => useTokenBalances(walletAddress), {
const { result } = renderHook(() => useEvmTokenBalances(walletAddress), {
wrapper: localWrapper,
});

Expand All @@ -126,7 +128,7 @@ describe("useTokenBalances", () => {
});

it("should not fetch data if the wallet address is missing", () => {
const { result } = renderHook(() => useTokenBalances(""), { wrapper });
const { result } = renderHook(() => useEvmTokenBalances(""), { wrapper });

expect(mockFetchBalances).not.toHaveBeenCalled();
expect(result.current).toEqual({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from "react";
import { renderHook, act, waitFor } from "@testing-library/react";
import { DuneProvider } from "../../src/provider";
import { useTransactions } from "../../src/useTransactions";
import { fetchTransactions } from "../../src/duneApi";
import { useEvmTransactions } from "../../src/evm/useEvmTransactions";
import { fetchEvmTransactions } from "../../src/evm/duneApi";
import { vi } from "vitest";

// Mock the Dune API
vi.mock("../../src/duneApi", () => ({
fetchTransactions: vi.fn(),
vi.mock("../../src/evm/duneApi", () => ({
fetchEvmTransactions: vi.fn(),
}));

const mockFetchTransactions = fetchTransactions as jest.Mock;
const mockFetchTransactions = fetchEvmTransactions as jest.Mock;

// A wrapper for the hook that provides the required context
const wrapper = ({ children }: { children: React.ReactNode }) => (
Expand All @@ -19,13 +19,15 @@ const wrapper = ({ children }: { children: React.ReactNode }) => (
</DuneProvider>
);

describe("useTransactions", () => {
describe("useEvmTransactions", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("should return null if the wallet address is not a valid address", () => {
const { result } = renderHook(() => useTransactions("0x123"), { wrapper });
const { result } = renderHook(() => useEvmTransactions("0x123"), {
wrapper,
});

expect(result.current).toEqual({
data: null,
Expand Down Expand Up @@ -74,7 +76,7 @@ describe("useTransactions", () => {
};
mockFetchTransactions.mockResolvedValueOnce(mockResponse);

const { result } = renderHook(() => useTransactions(walletAddress), {
const { result } = renderHook(() => useEvmTransactions(walletAddress), {
wrapper,
});

Expand Down Expand Up @@ -102,7 +104,7 @@ describe("useTransactions", () => {
const mockError = new Error("Failed to fetch transactions");
mockFetchTransactions.mockRejectedValueOnce(mockError);

const { result } = renderHook(() => useTransactions(walletAddress), {
const { result } = renderHook(() => useEvmTransactions(walletAddress), {
wrapper,
});

Expand Down Expand Up @@ -144,7 +146,7 @@ describe("useTransactions", () => {
.mockResolvedValueOnce(page2Response)
.mockResolvedValueOnce(page1Response);

const { result } = renderHook(() => useTransactions(walletAddress), {
const { result } = renderHook(() => useEvmTransactions(walletAddress), {
wrapper,
});

Expand Down Expand Up @@ -190,7 +192,7 @@ describe("useTransactions", () => {
<DuneProvider duneApiKey="">{children}</DuneProvider>
);

const { result } = renderHook(() => useTransactions(walletAddress), {
const { result } = renderHook(() => useEvmTransactions(walletAddress), {
wrapper: localWrapper,
});

Expand All @@ -208,7 +210,7 @@ describe("useTransactions", () => {
});

it("should not fetch data if the wallet address is missing", () => {
const { result } = renderHook(() => useTransactions(""), { wrapper });
const { result } = renderHook(() => useEvmTransactions(""), { wrapper });

expect(mockFetchTransactions).not.toHaveBeenCalled();
expect(result.current).toEqual({
Expand Down

0 comments on commit 5650a12

Please sign in to comment.