Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 121f3d1

Browse files
committed
test: test coverage for copy button and reponsive table
1 parent 7c4c2d5 commit 121f3d1

File tree

3 files changed

+95
-8
lines changed

3 files changed

+95
-8
lines changed

src/components/CustomAccordion/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ const AccordionContent: React.FC = ({ children }) => (
2323
);
2424

2525
const CustomAccordion: React.FC<TCustomAccordionProps> = ({ items }) => (
26-
<Accordion.Root className='accordion_root' type='single' defaultValue='item-1' collapsible>
26+
<Accordion.Root
27+
data-testid='dt_accordion_root'
28+
className='accordion_root'
29+
type='single'
30+
collapsible
31+
>
2732
{items.map((item) => (
2833
<Accordion.Item className='accordion_root__item' key={item.header} value={item.header}>
2934
<AccordionTrigger>{item.header}</AccordionTrigger>

src/features/dashboard/components/AppsTable/__tests__/apps-table.test.tsx

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import { ApplicationObject } from '@deriv/api-types';
22
import useAuthContext from '@site/src/hooks/useAuthContext';
3-
import { render, screen, cleanup, within } from '@site/src/test-utils';
3+
import { render, screen, within } from '@site/src/test-utils';
44
import userEvent from '@testing-library/user-event';
55
import React from 'react';
66
import AppsTable from '..';
7+
import useDeviceType from '@site/src/hooks/useDeviceType';
8+
import useAppManager from '@site/src/hooks/useAppManager';
79

810
jest.mock('@site/src/hooks/useAuthContext');
911
const mockUseAuthContext = useAuthContext as jest.MockedFunction<
1012
() => Partial<ReturnType<typeof useAuthContext>>
1113
>;
1214

15+
jest.mock('@site/src/hooks/useDeviceType');
16+
const mockDeviceType = useDeviceType as jest.MockedFunction<
17+
() => Partial<ReturnType<typeof useDeviceType>>
18+
>;
19+
mockDeviceType.mockImplementation(() => ({
20+
deviceType: 'desktop',
21+
}));
22+
1323
mockUseAuthContext.mockImplementation(() => ({
1424
is_authorized: true,
1525
currentLoginAccount: {
@@ -19,6 +29,18 @@ mockUseAuthContext.mockImplementation(() => ({
1929
},
2030
}));
2131

32+
jest.mock('@site/src/hooks/useAppManager');
33+
const mockUseAppManager = useAppManager as jest.MockedFunction<
34+
() => Partial<ReturnType<typeof useAppManager>>
35+
>;
36+
const mockUpdateCurrentTab = jest.fn();
37+
mockUseAppManager.mockImplementation(() => ({
38+
getApps: jest.fn(),
39+
apps: undefined,
40+
tokens: undefined,
41+
updateCurrentTab: mockUpdateCurrentTab,
42+
}));
43+
2244
const fakeApplications: ApplicationObject[] = [
2345
{
2446
active: 1,
@@ -51,15 +73,12 @@ const fakeApplications: ApplicationObject[] = [
5173
];
5274

5375
describe('Apps Table', () => {
54-
beforeEach(() => {
76+
const renderAppTable = () => {
5577
render(<AppsTable apps={fakeApplications} />);
56-
});
57-
58-
afterEach(() => {
59-
cleanup();
60-
});
78+
};
6179

6280
it('Should render all applications properly', () => {
81+
renderAppTable();
6382
const rows = screen.getAllByRole('row');
6483
expect(rows.length).toBe(3);
6584
});
@@ -139,4 +158,28 @@ describe('Apps Table', () => {
139158
const updateDialogTitle = await screen.findByText('Update App');
140159
expect(updateDialogTitle).toBeInTheDocument();
141160
});
161+
162+
it('Should render responsive view properly', () => {
163+
mockDeviceType.mockImplementation(() => ({
164+
deviceType: 'mobile',
165+
}));
166+
renderAppTable();
167+
const accordion = screen.getAllByTestId('dt_accordion_root');
168+
expect(accordion.length).toBe(1);
169+
});
170+
171+
it('Should update current tab on clicking Register new application button', async () => {
172+
renderAppTable();
173+
const registerButton = screen.getByText('Register new application');
174+
await userEvent.click(registerButton);
175+
expect(mockUpdateCurrentTab).toBeCalled();
176+
});
177+
178+
it('Should open first accordion on item click', async () => {
179+
renderAppTable();
180+
const item = screen.getByText('first app');
181+
await userEvent.click(item);
182+
const content = screen.getByText('11111');
183+
expect(content).toBeInTheDocument();
184+
});
142185
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React from 'react';
2+
import { render, screen } from '@site/src/test-utils';
3+
import CopyTextCell from '../copy-text.cell';
4+
import userEvent from '@testing-library/user-event';
5+
6+
describe('CopyTextCell', () => {
7+
beforeAll(() => {
8+
Object.assign(navigator, {
9+
clipboard: {
10+
writeText: jest.fn(),
11+
},
12+
});
13+
});
14+
15+
it('Should render the copy button', () => {
16+
render(
17+
<CopyTextCell
18+
cell={{
19+
value: '1234',
20+
}}
21+
/>,
22+
);
23+
const label = screen.getByText(/1234/i);
24+
expect(label).toBeInTheDocument();
25+
});
26+
27+
it('Should copy text in the clipboard', async () => {
28+
render(
29+
<CopyTextCell
30+
cell={{
31+
value: '1234',
32+
}}
33+
/>,
34+
);
35+
const label = screen.getByText(/1234/i);
36+
await userEvent.click(label);
37+
expect(navigator.clipboard.writeText).toHaveBeenCalledWith('1234');
38+
});
39+
});

0 commit comments

Comments
 (0)