|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import TagConfig from './TagConfig'; |
| 5 | +import { ThemeProvider } from '@mui/material/styles'; |
| 6 | +import { appTheme } from '@root/src/theme'; |
| 7 | +import { TagConfigModel } from '@root/src/shared/models/tagConfigModel'; |
| 8 | + |
| 9 | +// Mock TreeDisplay component |
| 10 | +vi.mock('@root/src/pages/sidepanel/components/TreeDisplay', () => ({ |
| 11 | + default: ({ data }: { data: any }) => <div data-testid="tree-display">{JSON.stringify(data)}</div>, |
| 12 | +})); |
| 13 | + |
| 14 | +describe('TagConfig', () => { |
| 15 | + const mockTagConfig: TagConfigModel = { |
| 16 | + stream: 'test-stream', |
| 17 | + cid: ['test-cid-456'], // cid is string[] according to model |
| 18 | + version: '3.5.0', |
| 19 | + url: 'https://example.com', |
| 20 | + src: 'https://c.lytics.io/api/tag/test-account-123/latest.min.js', |
| 21 | + }; |
| 22 | + |
| 23 | + const renderWithTheme = (component: React.ReactElement) => { |
| 24 | + return render(<ThemeProvider theme={appTheme}>{component}</ThemeProvider>); |
| 25 | + }; |
| 26 | + |
| 27 | + it('should render the TagConfig component', () => { |
| 28 | + renderWithTheme(<TagConfig tagConfig={mockTagConfig} />); |
| 29 | + |
| 30 | + const treeDisplay = screen.getByTestId('tree-display'); |
| 31 | + expect(treeDisplay).toBeInTheDocument(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should pass tagConfig data to TreeDisplay', () => { |
| 35 | + renderWithTheme(<TagConfig tagConfig={mockTagConfig} />); |
| 36 | + |
| 37 | + const treeDisplay = screen.getByTestId('tree-display'); |
| 38 | + expect(treeDisplay).toHaveTextContent(mockTagConfig.stream); |
| 39 | + expect(treeDisplay).toHaveTextContent(mockTagConfig.version); |
| 40 | + expect(treeDisplay).toHaveTextContent(mockTagConfig.url); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle empty tagConfig', () => { |
| 44 | + const emptyConfig = {} as TagConfigModel; |
| 45 | + renderWithTheme(<TagConfig tagConfig={emptyConfig} />); |
| 46 | + |
| 47 | + const treeDisplay = screen.getByTestId('tree-display'); |
| 48 | + expect(treeDisplay).toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should handle different tagConfig versions', () => { |
| 52 | + const legacyConfig: TagConfigModel = { |
| 53 | + ...mockTagConfig, |
| 54 | + version: '2.8.0', |
| 55 | + }; |
| 56 | + |
| 57 | + renderWithTheme(<TagConfig tagConfig={legacyConfig} />); |
| 58 | + |
| 59 | + const treeDisplay = screen.getByTestId('tree-display'); |
| 60 | + expect(treeDisplay).toHaveTextContent('2.8.0'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should handle tagConfig with missing optional fields', () => { |
| 64 | + const partialConfig = { |
| 65 | + stream: 'test-stream', |
| 66 | + version: '3.5.0', |
| 67 | + } as TagConfigModel; |
| 68 | + |
| 69 | + renderWithTheme(<TagConfig tagConfig={partialConfig} />); |
| 70 | + |
| 71 | + const treeDisplay = screen.getByTestId('tree-display'); |
| 72 | + expect(treeDisplay).toBeInTheDocument(); |
| 73 | + expect(treeDisplay).toHaveTextContent('test-stream'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should render TreeDisplay inside styled panel', () => { |
| 77 | + const { container } = renderWithTheme(<TagConfig tagConfig={mockTagConfig} />); |
| 78 | + |
| 79 | + const panel = container.querySelector('.MuiBox-root'); |
| 80 | + const treeDisplay = screen.getByTestId('tree-display'); |
| 81 | + |
| 82 | + expect(panel).toContainElement(treeDisplay); |
| 83 | + }); |
| 84 | +}); |
0 commit comments