Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 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
5 changes: 3 additions & 2 deletions app/__tests__/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Message, MessageArgs, MessageCompiledInstruction, MessageV0, MessageV0Args,PublicKey, VersionedMessage } from "@solana/web3.js";
import { Message, MessageArgs, MessageCompiledInstruction, MessageV0, MessageV0Args, PublicKey, VersionedMessage } from "@solana/web3.js";
import { useSearchParams } from 'next/navigation';
import { vi } from 'vitest';

// stub a test to not allow passing without tests
test('stub', () => expect(true).toBeTruthy());

jest.mock('next/navigation');
vi.mock('next/navigation');
export function mockUseSearchParams(cluster = 'mainnet-beta', customUrl?: string) {
// @ts-expect-error mockReturnValue is not present
useSearchParams.mockReturnValue({
Expand Down
33 changes: 17 additions & 16 deletions app/api/metadata/proxy/__tests__/endpoint.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* @jest-environment node
*/
import _dns from 'dns';
import fetch, { Headers } from 'node-fetch';
import { vi } from 'vitest';

import { GET } from '../route';

Expand All @@ -12,28 +10,31 @@ function setEnvironment(key: string, value: string) {
Object.assign(process.env, { ...process.env, [key]: value });
}

jest.mock('node-fetch', () => {
const originalFetch = jest.requireActual('node-fetch');
const mockFn = jest.fn();

Object.assign(mockFn, originalFetch);

return mockFn;
vi.mock('node-fetch', async () => {
const actual = await vi.importActual('node-fetch');
return {
...actual,
default: vi.fn()
};
});

jest.mock('dns', () => {
const originalDns = jest.requireActual('dns');
const lookupFn = jest.fn();
vi.mock('dns', async () => {
const originalDns = await vi.importActual('dns');
const lookupFn = vi.fn();
return {
...originalDns,
default: {
promises: {
lookup: lookupFn,
}
},
promises: {
...originalDns.promises,
lookup: lookupFn,
}
};
});

async function mockFileResponseOnce(data: any, headers: Headers){
async function mockFileResponseOnce(data: any, headers: Headers) {
// @ts-expect-error unavailable mock method for fetch
fetch.mockResolvedValueOnce({ headers, json: async () => data });
}
Expand All @@ -53,7 +54,7 @@ describe('metadata/[network] endpoint', () => {
const unsupportedUri = encodeURIComponent('ftp://unsupported.resource/file.json');

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('should return status when disabled', async () => {
Expand Down
31 changes: 14 additions & 17 deletions app/api/metadata/proxy/__tests__/fetch-resource.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/**
* @jest-environment node
*/
import fetch, { Headers } from 'node-fetch';
import { vi } from 'vitest';

import { fetchResource } from '../feature';

jest.mock('node-fetch', () => {
const originalFetch = jest.requireActual('node-fetch');
const mockFn = jest.fn();

Object.assign(mockFn, originalFetch);

return mockFn;
vi.mock('node-fetch', async () => {
const actual = await vi.importActual('node-fetch');
return {
...actual,
default: vi.fn()
};
});

/**
Expand All @@ -34,11 +31,11 @@ function mockRejectOnce<T extends Error>(error: T) {
}

describe('fetchResource', () => {
const uri = 'http://hello.world/data.json' ;
const uri = 'http://hello.world/data.json';
const headers = new Headers({ 'Content-Type': 'application/json' });

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

it('should be called with proper arguments', async () => {
Expand All @@ -53,15 +50,15 @@ describe('fetchResource', () => {
it('should throw exception for unsupported media', async () => {
mockFetchOnce();

expect(() => {
await expect(() => {
return fetchResource(uri, headers, 100, 100);
}).rejects.toThrowError('Unsupported Media Type');
});

it('should throw exception upon exceeded size', async () => {
mockRejectOnce(new Error('FetchError: content size at https://path/to/resour.ce over limit: 100'));

expect(() => {
await expect(() => {
return fetchResource(uri, headers, 100, 100);
}).rejects.toThrowError('Max Content Size Exceeded');
});
Expand All @@ -75,15 +72,15 @@ describe('fetchResource', () => {
}
mockRejectOnce(new TimeoutError());

expect(() => {
await expect(() => {
return fetchResource(uri, headers, 100, 100);
}).rejects.toThrowError('Gateway Timeout');
});

it('should handle size overflow', async () => {
mockRejectOnce(new Error('file is over limit: 100'));

expect(() => {
await expect(() => {
return fetchResource(uri, headers, 100, 100);
}).rejects.toThrowError('Max Content Size Exceeded');
});
Expand All @@ -98,7 +95,7 @@ describe('fetchResource', () => {

try {
await fn();
} catch(e: any) {
} catch (e: any) {
expect(e.message).toEqual('General Error');
expect(e.status).toEqual(500);
}
Expand Down
26 changes: 14 additions & 12 deletions app/api/metadata/proxy/__tests__/ip.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/**
* @jest-environment node
*/
import _dns from 'dns';
import { beforeEach, describe, expect, test, vi } from 'vitest';

import { checkURLForPrivateIP } from '../feature/ip';

const dns = _dns.promises;

jest.mock('dns', () => {
const originalDns = jest.requireActual('dns');
const lookupFn = jest.fn();
vi.mock('dns', async () => {
const originalDns = await vi.importActual('dns');
const lookupFn = vi.fn();
return {
...originalDns,
default: {
promises: {
lookup: lookupFn,
}
},
promises: {
...originalDns.promises,
lookup: lookupFn,
}
};
Expand All @@ -26,13 +28,13 @@ jest.mock('dns', () => {
type LookupAddress = { address: string };

function mockLookupOnce(addresses: LookupAddress | LookupAddress[] | undefined) {
// @ts-expect-error lookup does not have mocked fn
// @ts-expect-error lookup does not have mockImplementation
dns.lookup.mockResolvedValueOnce(addresses);
}

describe('ip::checkURLForPrivateIP', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

// do not throw exceptions forinvalid input to not break the execution flow
Expand Down Expand Up @@ -80,15 +82,15 @@ describe('ip::checkURLForPrivateIP', () => {
});

test('should handle DNS resolution failure gracefully', async () => {
// @ts-expect-error fetch does not have mocked fn
// @ts-expect-error lookup does not have mockImplementation
dns.lookup.mockRejectedValueOnce(new Error('DNS resolution failed'));
await expect(checkURLForPrivateIP('http://unknown.domain')).resolves.toBe(true);
});
});

describe('ip::checkURLForPrivateIP with single resolved address', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('should handle single address positively', async () => {
Expand All @@ -100,7 +102,7 @@ describe('ip::checkURLForPrivateIP with single resolved address', () => {
// move case for localhost to a separate test case as it's a special case and doesn't require DNS resolution
describe('ip::checkURLForPrivateIP with localhost', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('should block localhost', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { intoTransactionInstructionFromVersionedMessage } from '@components/inspector/utils';
import { ASSOCIATED_TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { render, screen } from '@testing-library/react';
import { vi } from 'vitest';

import * as stubs from '@/app/__tests__/mock-stubs';
import * as mock from '@/app/__tests__/mocks';
Expand All @@ -16,7 +17,7 @@ describe('AddressTableLookupAddress', () => {
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('should render static address', async () => {
Expand Down
3 changes: 2 additions & 1 deletion app/components/common/__tests__/BaseInstructionCard.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { intoTransactionInstructionFromVersionedMessage } from '@components/inspector/utils';
import { ASSOCIATED_TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { render, screen } from '@testing-library/react';
import { vi } from 'vitest';

import * as stubs from '@/app/__tests__/mock-stubs';
import * as mock from '@/app/__tests__/mocks';
Expand All @@ -15,7 +16,7 @@ describe('BaseInstructionCard', () => {
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('should render "BaseInstructionCard"', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { intoTransactionInstructionFromVersionedMessage } from '@components/inspector/utils';
import * as spl from '@solana/spl-token';
import { render, screen } from '@testing-library/react';
import { vi } from 'vitest';

import * as stubs from '@/app/__tests__/mock-stubs';
import * as mock from '@/app/__tests__/mocks';
Expand All @@ -11,15 +12,15 @@ import { ScrollAnchorProvider } from '@/app/providers/scroll-anchor';
import { intoParsedInstruction } from '../../inspector/into-parsed-data';
import { AssociatedTokenDetailsCard } from '../associated-token/AssociatedTokenDetailsCard';

jest.mock('next/navigation');
vi.mock('next/navigation');

describe('inspector::AssociatedTokenDetailsCard', () => {
beforeEach(() => {
mock.mockUseSearchParams();
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('should render "CreateIdempotent" card', async () => {
Expand Down Expand Up @@ -127,7 +128,7 @@ describe('inspector::AssociatedTokenDetailsCard with inner cards', () => {
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('should render "CreateIdempotentDetailsCard"', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { intoTransactionInstructionFromVersionedMessage } from '@components/insp
import * as spl from '@solana/spl-token';
import { PublicKey } from '@solana/web3.js';
import { render, screen } from '@testing-library/react';
import { vi } from 'vitest';

import * as stubs from '@/app/__tests__/mock-stubs';
import * as mock from '@/app/__tests__/mocks';
Expand All @@ -12,15 +13,15 @@ import { ScrollAnchorProvider } from '@/app/providers/scroll-anchor';
import { intoParsedInstruction, intoParsedTransaction } from '../../inspector/into-parsed-data';
import { AssociatedTokenDetailsCard } from '../associated-token/AssociatedTokenDetailsCard';

jest.mock('next/navigation');
vi.mock('next/navigation');

describe('instruction::AssociatedTokenDetailsCard', () => {
beforeEach(() => {
mock.mockUseSearchParams();
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('should render "CreateIdempotentDetailsCard"', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { BaseInstructionCard } from '@components/common/BaseInstructionCard';
import { intoTransactionInstructionFromVersionedMessage } from '@components/inspector/utils';
import { ComputeBudgetProgram, MessageCompiledInstruction } from '@solana/web3.js';
import { render, screen } from '@testing-library/react';
import { useSearchParams } from 'next/navigation';
import { vi } from 'vitest';

import * as stubs from '@/app/__tests__/mock-stubs';
import * as mock from '@/app/__tests__/mocks';
Expand All @@ -10,15 +12,21 @@ import { ScrollAnchorProvider } from '@/app/providers/scroll-anchor';

import { ComputeBudgetDetailsCard } from '../ComputeBudgetDetailsCard';

jest.mock('next/navigation');
vi.mock('next/navigation');
// @ts-expect-error does not contain `mockReturnValue`
useSearchParams.mockReturnValue({
get: () => 'devnet',
has: (_query?: string) => false,
toString: () => '',
});

describe('instruction::ComputeBudgetDetailsCard', () => {
beforeEach(() => {
mock.mockUseSearchParams();
});

afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('should render "SetComputeUnitPrice"', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { ParsedTransaction, PublicKey } from '@solana/web3.js';
import { render, screen } from '@testing-library/react';
import bs58 from 'bs58';
import { vi } from 'vitest';

import { Ed25519DetailsCard } from '../Ed25519DetailsCard';

// Mock the dependencies
jest.mock('../../../common/Address', () => ({
vi.mock('../../../common/Address', () => ({
Address: ({ pubkey, alignRight, link }: { pubkey: PublicKey; alignRight?: boolean; link?: boolean }) => (
<div data-testid="address" className={`${alignRight ? 'text-end' : ''} ${link ? 'text-link' : ''}`}>
{pubkey.toBase58()}
</div>
),
}));

jest.mock('../../../common/Copyable', () => ({
vi.mock('../../../common/Copyable', () => ({
Copyable: ({ text, children }: { text: string; children: React.ReactNode }) => (
<div data-testid="copyable" data-text={text}>
{children}
</div>
),
}));

jest.mock('../../InstructionCard', () => ({
vi.mock('../../InstructionCard', () => ({
InstructionCard: ({ children, title }: { children: React.ReactNode; title: string }) => (
<div data-testid="instruction-card">
<div>{title}</div>
Expand Down
Loading
Loading