-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathCheckoutButton.test.tsx
95 lines (80 loc) · 3.22 KB
/
CheckoutButton.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { fireEvent, render, screen } from '@testing-library/react';
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest';
import { CHECKOUT_LIFECYCLESTATUS } from '../constants';
import { CheckoutButton } from './CheckoutButton';
import { useCheckoutContext } from './CheckoutProvider';
vi.mock('./CheckoutProvider', () => ({
useCheckoutContext: vi.fn(),
}));
vi.mock('../../internal/components/Spinner', () => ({
Spinner: () => <div data-testid="spinner">Loading...</div>,
}));
vi.mock('../../internal/hooks/useIcon', () => ({
useIcon: vi.fn(() => <svg data-testid="icon" />),
}));
const useCheckoutContextMock = useCheckoutContext as Mock;
describe('CheckoutButton', () => {
const mockOnSubmit = vi.fn();
beforeEach(() => {
mockOnSubmit.mockClear();
useCheckoutContextMock.mockReturnValue({
lifecycleStatus: { statusName: CHECKOUT_LIFECYCLESTATUS.INIT },
onSubmit: mockOnSubmit,
});
});
it('should render button with default text "Pay" when not loading', () => {
render(<CheckoutButton />);
const button: HTMLInputElement = screen.getByRole('button');
expect(button.textContent).toBe('Pay');
expect(button.disabled).toBe(false);
});
it('should render Spinner when loading', () => {
useCheckoutContextMock.mockReturnValue({
lifecycleStatus: { statusName: CHECKOUT_LIFECYCLESTATUS.PENDING },
onSubmit: mockOnSubmit,
});
render(<CheckoutButton />);
expect(screen.getByTestId('spinner')).toBeDefined();
expect((screen.getByRole('button') as HTMLInputElement).disabled).toBe(
true,
);
});
it('should render "View payment details" when transaction is successful', () => {
useCheckoutContextMock.mockReturnValue({
lifecycleStatus: { statusName: CHECKOUT_LIFECYCLESTATUS.SUCCESS },
onSubmit: mockOnSubmit,
});
render(<CheckoutButton />);
expect(screen.getByRole('button').textContent).toBe('View payment details');
});
it('should call onSubmit when clicked', () => {
render(<CheckoutButton />);
fireEvent.click(screen.getByRole('button'));
expect(mockOnSubmit).toHaveBeenCalledTimes(1);
});
it('should apply additional className correctly', () => {
const customClass = 'custom-class';
render(<CheckoutButton className={customClass} />);
expect(screen.getByRole('button').className).toContain(customClass);
});
it('should render Coinbase branded button when coinbaseBranded prop is true', () => {
render(<CheckoutButton coinbaseBranded={true} />);
const button = screen.getByRole('button');
expect(button.textContent).toBe('Pay with Crypto');
expect(screen.getByTestId('icon')).toBeDefined();
});
it('should render custom text when provided', () => {
render(<CheckoutButton text="Custom Text" />);
expect(screen.getByRole('button').textContent).toBe('Custom Text');
});
it('should render icon when provided and not in a special state', () => {
render(<CheckoutButton icon="someIcon" />);
expect(screen.getByTestId('icon')).toBeDefined();
});
it('should be disabled when disabled prop is true', () => {
render(<CheckoutButton disabled={true} />);
expect((screen.getByRole('button') as HTMLInputElement).disabled).toBe(
true,
);
});
});