import '@testing-library/jest-dom';
import { render } from '@testing-library/react';
import { act } from 'react';
import { describe, expect, it, vi } from 'vitest';
import type { NFTData } from '../types';
import { NFTProvider, useNFTContext } from './NFTProvider';
const useNFTData = vi.fn(
() =>
({
name: 'Test NFT',
description: 'This is a test NFT',
imageUrl: 'http://example.com/test-nft.png',
}) as NFTData,
);
const MockComponent = () => {
const context = useNFTContext();
return (
{context.name}
{context.description}
{context.quantity}
);
};
describe('NFTProvider', () => {
it('should provide NFT data to its children', () => {
const { getByText, getByAltText } = render(
,
);
expect(getByText('Test NFT')).toBeInTheDocument();
expect(getByText('This is a test NFT')).toBeInTheDocument();
expect(getByAltText('Test NFT')).toHaveAttribute(
'src',
'http://example.com/test-nft.png',
);
});
it('should update the quantity', () => {
const { getByTestId } = render(
,
);
expect(getByTestId('quantity').textContent).toBe('1');
act(() => {
getByTestId('setQuantity').click();
});
expect(getByTestId('quantity').textContent).toBe('2');
});
it('should throw an error when useNFTContext is used outside of NFTProvider', () => {
const consoleError = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
expect(() => render()).toThrow(
'useNFTContext must be used within an NFTView or NFTMint component',
);
consoleError.mockRestore();
});
});