Skip to content

Commit a6ab6de

Browse files
committed
update unit tests
1 parent 764882c commit a6ab6de

File tree

2 files changed

+182
-2
lines changed

2 files changed

+182
-2
lines changed

src/ui/pages/stripe/CreateLink.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import { MemoryRouter } from 'react-router-dom';
1010
describe('StripeCreateLinkPanel Tests', () => {
1111
const createLinkMock = vi.fn();
1212

13-
const renderComponent = async (isLoading = false) => {
13+
const renderComponent = async () => {
1414
await act(async () => {
1515
render(
1616
<MemoryRouter>
1717
<MantineProvider withGlobalClasses withCssVariables forceColorScheme="light">
18-
<StripeCreateLinkPanel createLink={createLinkMock} isLoading={isLoading} />
18+
<StripeCreateLinkPanel createLink={createLinkMock} />
1919
</MantineProvider>
2020
</MemoryRouter>
2121
);
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import { render, screen, act } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
3+
import { vi } from 'vitest';
4+
import { MantineProvider } from '@mantine/core';
5+
import { notifications } from '@mantine/notifications';
6+
import { MemoryRouter } from 'react-router-dom';
7+
import StripeCurrentLinksPanel from './CurrentLinks';
8+
9+
vi.mock('@ui/components/AuthContext', async () => {
10+
return {
11+
useAuth: vi.fn().mockReturnValue({
12+
userData: { email: '[email protected]' },
13+
}),
14+
};
15+
});
16+
17+
describe('StripeCurrentLinksPanel Tests', () => {
18+
const getLinksMock = vi.fn();
19+
20+
const renderComponent = async () => {
21+
await act(async () => {
22+
render(
23+
<MemoryRouter>
24+
<MantineProvider withGlobalClasses withCssVariables forceColorScheme="light">
25+
<StripeCurrentLinksPanel getLinks={getLinksMock} />
26+
</MantineProvider>
27+
</MemoryRouter>
28+
);
29+
});
30+
};
31+
32+
beforeEach(() => {
33+
vi.clearAllMocks();
34+
});
35+
36+
it('renders table with no items', async () => {
37+
getLinksMock.mockResolvedValue([]);
38+
await renderComponent();
39+
40+
expect(getLinksMock).toHaveBeenCalledOnce();
41+
expect(await screen.findByText('Current Links')).toBeInTheDocument();
42+
expect(screen.getByRole('table')).toBeInTheDocument();
43+
expect(screen.getAllByRole('row')).toHaveLength(1); // Only header row
44+
});
45+
46+
it('renders table with a few items', async () => {
47+
getLinksMock.mockResolvedValue([
48+
{
49+
id: '1',
50+
active: true,
51+
invoiceId: 'INV-001',
52+
invoiceAmountUsd: 5000,
53+
userId: '[email protected]',
54+
createdAt: '2024-02-01',
55+
link: 'http://example.com',
56+
},
57+
{
58+
id: '2',
59+
active: false,
60+
invoiceId: 'INV-002',
61+
invoiceAmountUsd: 7500,
62+
userId: '[email protected]',
63+
createdAt: null,
64+
link: 'http://example.com/2',
65+
},
66+
]);
67+
await renderComponent();
68+
69+
expect(getLinksMock).toHaveBeenCalledOnce();
70+
const rows = screen.getAllByRole('row');
71+
expect(rows).toHaveLength(3); // Header + 2 data rows
72+
expect(rows[1]).toHaveTextContent('INV-001');
73+
expect(rows[1]).toHaveTextContent('$50');
74+
expect(rows[2]).toHaveTextContent('INV-002');
75+
expect(rows[2]).toHaveTextContent('$75');
76+
expect(screen.getByText('You')).toBeInTheDocument();
77+
expect(screen.getByText('Unknown')).toBeInTheDocument();
78+
const user = userEvent.setup();
79+
const copyButtons = screen.getAllByRole('button', { name: /copy/i });
80+
await user.click(copyButtons[0]);
81+
await act(async () => {
82+
const clipboardText = await navigator.clipboard.readText();
83+
expect(clipboardText).toBe('http://example.com');
84+
});
85+
await user.click(copyButtons[1]);
86+
await act(async () => {
87+
const clipboardText = await navigator.clipboard.readText();
88+
expect(clipboardText).toBe('http://example.com/2');
89+
});
90+
});
91+
92+
it('correctly replaces the user email with "You"', async () => {
93+
getLinksMock.mockResolvedValue([
94+
{
95+
id: '3',
96+
active: true,
97+
invoiceId: 'INV-003',
98+
invoiceAmountUsd: 10000,
99+
userId: '[email protected]',
100+
createdAt: '2024-02-05',
101+
link: 'http://example.com/3',
102+
},
103+
]);
104+
await renderComponent();
105+
106+
expect(getLinksMock).toHaveBeenCalledOnce();
107+
expect(await screen.findByText('You')).toBeInTheDocument();
108+
});
109+
110+
it('handles API failure gracefully', async () => {
111+
const notificationsMock = vi.spyOn(notifications, 'show');
112+
getLinksMock.mockRejectedValue(new Error('API Error'));
113+
await renderComponent();
114+
115+
expect(getLinksMock).toHaveBeenCalledOnce();
116+
expect(notificationsMock).toHaveBeenCalledWith(
117+
expect.objectContaining({
118+
title: 'Error',
119+
message: 'Failed to get payment links. Please try again or contact support.',
120+
color: 'red',
121+
})
122+
);
123+
124+
notificationsMock.mockRestore();
125+
});
126+
127+
it('allows selecting and deselecting rows', async () => {
128+
getLinksMock.mockResolvedValue([
129+
{
130+
id: '1',
131+
active: true,
132+
invoiceId: 'INV-001',
133+
invoiceAmountUsd: 5000,
134+
userId: '[email protected]',
135+
createdAt: '2024-02-01',
136+
link: 'http://example.com',
137+
},
138+
]);
139+
await renderComponent();
140+
141+
const checkbox = screen.getByLabelText('Select row');
142+
await userEvent.click(checkbox);
143+
expect(checkbox).toBeChecked();
144+
145+
await userEvent.click(checkbox);
146+
expect(checkbox).not.toBeChecked();
147+
});
148+
149+
it('triggers deactivation when clicking deactivate button', async () => {
150+
getLinksMock.mockResolvedValue([
151+
{
152+
id: '1',
153+
active: true,
154+
invoiceId: 'INV-001',
155+
invoiceAmountUsd: 5000,
156+
userId: '[email protected]',
157+
createdAt: '2024-02-01',
158+
link: 'http://example.com',
159+
},
160+
]);
161+
const notificationsMock = vi.spyOn(notifications, 'show');
162+
await renderComponent();
163+
164+
const checkbox = screen.getByLabelText('Select row');
165+
await userEvent.click(checkbox);
166+
167+
const deactivateButton = await screen.findByText(/Deactivate 1 link/);
168+
await userEvent.click(deactivateButton);
169+
170+
expect(notificationsMock).toHaveBeenCalledWith(
171+
expect.objectContaining({
172+
title: 'Feature not available',
173+
message: 'Coming soon!',
174+
color: 'yellow',
175+
})
176+
);
177+
178+
notificationsMock.mockRestore();
179+
});
180+
});

0 commit comments

Comments
 (0)