|
1 | | -import { render, screen } from '@testing-library/react'; |
| 1 | +import { render, screen, fireEvent } from '@testing-library/react'; |
2 | 2 | import { SetupGuide } from '../SetupGuide'; |
3 | | -import { Props } from '../../../utils/types'; |
4 | | -import { url } from '@/components/utils/URLs'; |
5 | 3 |
|
6 | | -// Mocking the CopyableCode component |
| 4 | +// Using jest.mock to mock external dependencies |
| 5 | +jest.mock('@/components/utils/URLs', () => ({ |
| 6 | + url: { |
| 7 | + containerOrigin: 'https://test-container', |
| 8 | + }, |
| 9 | +})); |
| 10 | + |
| 11 | +// Mock CopyableCode component |
7 | 12 | jest.mock('../CopyableCode', () => ({ |
8 | | - CopyableCode: ({ |
9 | | - text, |
10 | | - copyText, |
11 | | - isSensitive, |
12 | | - }: { |
13 | | - text: string; |
14 | | - copyText: string; |
15 | | - isSensitive?: boolean; |
16 | | - }) => ( |
17 | | - <div |
18 | | - data-testid="copyable-code" |
19 | | - data-text={text} |
20 | | - data-copytext={copyText} |
21 | | - data-issensitive={isSensitive} |
22 | | - > |
23 | | - {text} |
24 | | - </div> |
| 13 | + CopyableCode: ({ text }: { text: string }) => ( |
| 14 | + <div data-testid="copyable-code">{text}</div> |
25 | 15 | ), |
26 | 16 | })); |
27 | 17 |
|
28 | | -const props: Props = { |
29 | | - name: 'test-name', |
30 | | - encryption_secret: 'test-encryption-secret', |
31 | | - uuid: 'test-uuid', |
| 18 | +// Mock exportConfigSetup utility |
| 19 | +const mockExportConfigSetup = jest.fn(); |
| 20 | +jest.mock('../utils', () => ({ |
| 21 | + exportConfigSetup: (props: any) => mockExportConfigSetup(props), |
| 22 | +})); |
| 23 | + |
| 24 | +const defaultProps = { |
| 25 | + name: 'Test User', |
| 26 | + encryption_secret: 'secret123', |
| 27 | + uuid: 'uuid-1234', |
32 | 28 | }; |
33 | | -describe('SetupGuide Component', () => { |
34 | | - test('renders SetupGuide component with correct text', () => { |
35 | | - render(<SetupGuide {...props} />); |
| 29 | + |
| 30 | +describe('SetupGuide', () => { |
| 31 | + test('renders setup guide sections', () => { |
| 32 | + render(<SetupGuide {...defaultProps} />); |
| 33 | + |
| 34 | + // Section exists |
| 35 | + expect(document.querySelector('#setup-guide')).toBeInTheDocument(); |
| 36 | + |
| 37 | + // Sub-section headings |
| 38 | + expect(screen.getByText('PREREQUISITES')).toBeInTheDocument(); |
| 39 | + expect(screen.getByText('CONFIGURATION')).toBeInTheDocument(); |
| 40 | + expect(screen.getByText('SYNC')).toBeInTheDocument(); |
36 | 41 | }); |
37 | 42 |
|
38 | | - test('renders CopyableCode components with correct props', () => { |
39 | | - render(<SetupGuide {...props} />); |
40 | | - |
41 | | - // Check for CopyableCode components |
42 | | - const copyableCodeElements = screen.getAllByTestId('copyable-code'); |
43 | | - expect(copyableCodeElements.length).toBe(5); |
44 | | - |
45 | | - // Validate the text and copyText props of each CopyableCode component |
46 | | - expect(copyableCodeElements[0]).toHaveAttribute( |
47 | | - 'data-text', |
48 | | - 'task --version' |
49 | | - ); |
50 | | - expect(copyableCodeElements[0]).toHaveAttribute( |
51 | | - 'data-copytext', |
52 | | - 'task --version' |
53 | | - ); |
54 | | - expect(copyableCodeElements[1]).toHaveAttribute( |
55 | | - 'data-text', |
56 | | - `task config sync.encryption_secret ${props.encryption_secret}` |
57 | | - ); |
58 | | - expect(copyableCodeElements[1]).toHaveAttribute( |
59 | | - 'data-copytext', |
60 | | - `task config sync.encryption_secret ${props.encryption_secret}` |
61 | | - ); |
62 | | - expect(copyableCodeElements[2]).toHaveAttribute( |
63 | | - 'data-text', |
64 | | - `task config sync.server.origin ${url.containerOrigin}` |
65 | | - ); |
66 | | - expect(copyableCodeElements[2]).toHaveAttribute( |
67 | | - 'data-copytext', |
68 | | - `task config sync.server.origin ${url.containerOrigin}` |
69 | | - ); |
70 | | - expect(copyableCodeElements[3]).toHaveAttribute( |
71 | | - 'data-text', |
72 | | - `task config sync.server.client_id ${props.uuid}` |
73 | | - ); |
74 | | - expect(copyableCodeElements[3]).toHaveAttribute( |
75 | | - 'data-copytext', |
76 | | - `task config sync.server.client_id ${props.uuid}` |
77 | | - ); |
| 43 | + test('renders configuration commands using props', () => { |
| 44 | + render(<SetupGuide {...defaultProps} />); |
| 45 | + |
| 46 | + expect( |
| 47 | + screen.getByText( |
| 48 | + `task config sync.encryption_secret ${defaultProps.encryption_secret}` |
| 49 | + ) |
| 50 | + ).toBeInTheDocument(); |
| 51 | + |
| 52 | + expect( |
| 53 | + screen.getByText(`task config sync.server.client_id ${defaultProps.uuid}`) |
| 54 | + ).toBeInTheDocument(); |
| 55 | + |
| 56 | + expect( |
| 57 | + screen.getByText('task config sync.server.origin https://test-container') |
| 58 | + ).toBeInTheDocument(); |
78 | 59 | }); |
79 | | -}); |
80 | 60 |
|
81 | | -describe('SetupGuide component using snapshot', () => { |
82 | | - test('renders correctly', () => { |
83 | | - const { asFragment } = render(<SetupGuide {...props} />); |
84 | | - expect(asFragment()).toMatchSnapshot(); |
| 61 | + test('clicking download configuration triggers download logic', () => { |
| 62 | + mockExportConfigSetup.mockReturnValue('config-content'); |
| 63 | + |
| 64 | + // Polyfill missing browser APIs |
| 65 | + Object.defineProperty(global.URL, 'createObjectURL', { |
| 66 | + writable: true, |
| 67 | + value: jest.fn(() => 'blob:http://localhost/file'), |
| 68 | + }); |
| 69 | + |
| 70 | + Object.defineProperty(global.URL, 'revokeObjectURL', { |
| 71 | + writable: true, |
| 72 | + value: jest.fn(), |
| 73 | + }); |
| 74 | + |
| 75 | + const appendSpy = jest.spyOn(document.body, 'appendChild'); |
| 76 | + const removeSpy = jest.spyOn(document.body, 'removeChild'); |
| 77 | + |
| 78 | + render(<SetupGuide {...defaultProps} />); |
| 79 | + |
| 80 | + fireEvent.click(screen.getByText(/DOWNLOAD CONFIGURATION/i)); |
| 81 | + |
| 82 | + expect(mockExportConfigSetup).toHaveBeenCalledWith(defaultProps); |
| 83 | + expect(URL.createObjectURL).toHaveBeenCalled(); |
| 84 | + expect(URL.revokeObjectURL).toHaveBeenCalled(); |
| 85 | + expect(appendSpy).toHaveBeenCalled(); |
| 86 | + expect(removeSpy).toHaveBeenCalled(); |
85 | 87 | }); |
86 | 88 | }); |
0 commit comments