import { useIsMounted } from '@/internal/hooks/useIsMounted';
import { render, screen } from '@testing-library/react';
import { encodeAbiParameters } from 'viem';
import { base } from 'viem/chains';
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest';
import { Signature } from '../components/Signature';
vi.mock('@/internal/hooks/useIsMounted');
vi.mock('../components/SignatureProvider', () => ({
SignatureProvider: vi.fn(({ children }) => (
{children}
)),
}));
vi.mock('../components/SignatureStatus', () => ({
SignatureStatus: vi.fn(() => SignatureStatus
),
}));
vi.mock('../components/SignatureToast', () => ({
SignatureToast: vi.fn(() => SignatureToast
),
}));
vi.mock('../components/SignatureButton', () => ({
SignatureButton: vi.fn(({ label, disabled }) => (
)),
}));
const SCHEMA_UID =
'0xf58b8b212ef75ee8cd7e8d803c37c03e0519890502d5e99ee2412aae1456cafe';
const EAS_CONTRACT = '0x4200000000000000000000000000000000000021';
const domain = {
name: 'EAS Attestation',
version: '1.0.0',
chainId: base.id,
verifyingContract: EAS_CONTRACT,
} as const;
const types = {
Attest: [
{ name: 'schema', type: 'bytes32' },
{ name: 'recipient', type: 'address' },
{ name: 'time', type: 'uint64' },
{ name: 'revocable', type: 'bool' },
{ name: 'refUID', type: 'bytes32' },
{ name: 'data', type: 'bytes' },
{ name: 'value', type: 'uint256' },
],
} as const;
const message = {
schema: SCHEMA_UID,
recipient: '0x0000000000000000000000000000000000000000',
time: BigInt(0),
revocable: false,
refUID: '0x0000000000000000000000000000000000000000000000000000000000000000',
data: encodeAbiParameters([{ type: 'string' }], ['test attestation']),
value: BigInt(0),
} as const;
describe('Signature', () => {
beforeEach(() => {
(useIsMounted as Mock).mockReturnValue(true);
});
it('should render an empty div if not mounted', () => {
(useIsMounted as Mock).mockReturnValue(false);
render();
expect(screen.queryByTestId('SignatureProvider')).not.toBeInTheDocument();
});
it('should render with valid EIP712 typed data', () => {
render(
,
);
expect(screen.getByText('EIP712')).toBeInTheDocument();
});
it('should render with valid signable message', () => {
render(
,
);
expect(screen.getByText('personal_sign')).toBeInTheDocument();
});
it('should render custom children instead of default button', () => {
render(
,
);
expect(screen.getByText('Custom Button')).toBeInTheDocument();
});
it('should apply a custom className', () => {
render(
,
);
expect(screen.getByTestId('SignatureProvider').firstChild).toHaveClass(
'custom-class',
);
});
it('should disable the button when disabled prop is true', () => {
render(
,
);
expect(screen.getByRole('button')).toBeDisabled();
});
});