|
| 1 | +import '@testing-library/jest-dom'; |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | + |
| 5 | +import { describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +import { ThemeProvider } from '@mui/material/styles'; |
| 8 | +import { TagConfigModel } from '@root/src/shared/models/tagConfigModel'; |
| 9 | +import { appTheme } from '@root/src/theme'; |
| 10 | +import { render, screen } from '@testing-library/react'; |
| 11 | + |
| 12 | +import TagStatus from './TagStatus'; |
| 13 | + |
| 14 | +const renderWithTheme = (component: React.ReactElement) => { |
| 15 | + return render(<ThemeProvider theme={appTheme}>{component}</ThemeProvider>); |
| 16 | +}; |
| 17 | + |
| 18 | +describe('TagStatus', () => { |
| 19 | + const mockTagConfig: TagConfigModel = { |
| 20 | + version: '4.2.1', |
| 21 | + cid: ['test-account-123'], |
| 22 | + stream: 'production', |
| 23 | + cookie: 'seerid', |
| 24 | + entity: { byFieldKey: 'email' }, |
| 25 | + loadid: true, |
| 26 | + }; |
| 27 | + |
| 28 | + const legacyTagConfig: TagConfigModel = { |
| 29 | + version: '2.8.5', |
| 30 | + cid: ['legacy-account'], |
| 31 | + stream: 'default', |
| 32 | + cookie: 'seerid', |
| 33 | + entity: { byFieldKey: 'user_id' }, |
| 34 | + loadid: false, |
| 35 | + }; |
| 36 | + |
| 37 | + describe('when tag is installed', () => { |
| 38 | + it('renders status header correctly', () => { |
| 39 | + renderWithTheme(<TagStatus tagIsInstalled={true} tagConfig={mockTagConfig} />); |
| 40 | + |
| 41 | + expect(screen.getByText('Status')).toBeInTheDocument(); |
| 42 | + expect(screen.getByTestId('MonitorHeartOutlinedIcon')).toBeInTheDocument(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('shows SDK installed message for current version', () => { |
| 46 | + renderWithTheme(<TagStatus tagIsInstalled={true} tagConfig={mockTagConfig} />); |
| 47 | + |
| 48 | + expect(screen.getByText('Lytics JavaScript SDK Installed')).toBeInTheDocument(); |
| 49 | + expect(screen.getByText('v4.2.1')).toBeInTheDocument(); |
| 50 | + expect(screen.getByTestId('CheckCircleIcon')).toBeInTheDocument(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('shows deprecated version warning for legacy tag', () => { |
| 54 | + renderWithTheme(<TagStatus tagIsInstalled={true} tagConfig={legacyTagConfig} />); |
| 55 | + |
| 56 | + expect(screen.getByText('You are using a deprecated version of the Lytics SDK')).toBeInTheDocument(); |
| 57 | + expect(screen.getByText('v2.8.5')).toBeInTheDocument(); |
| 58 | + expect(screen.getByTestId('CancelIcon')).toBeInTheDocument(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('renders configuration table with correct data', () => { |
| 62 | + renderWithTheme(<TagStatus tagIsInstalled={true} tagConfig={mockTagConfig} />); |
| 63 | + |
| 64 | + expect(screen.getByText('Account ID')).toBeInTheDocument(); |
| 65 | + expect(screen.getByText('Stream')).toBeInTheDocument(); |
| 66 | + expect(screen.getByText('Cookie Name')).toBeInTheDocument(); |
| 67 | + expect(screen.getByText('Profile Key')).toBeInTheDocument(); |
| 68 | + expect(screen.getByText('3rd Party Cookies')).toBeInTheDocument(); |
| 69 | + |
| 70 | + expect(screen.getByText('test-account-123')).toBeInTheDocument(); |
| 71 | + expect(screen.getByText('production')).toBeInTheDocument(); |
| 72 | + expect(screen.getByText('seerid')).toBeInTheDocument(); |
| 73 | + expect(screen.getByText('email')).toBeInTheDocument(); |
| 74 | + expect(screen.getByText('Enabled')).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('shows disabled for 3rd party cookies when loadid is false', () => { |
| 78 | + const configWithoutLoadid = { ...mockTagConfig, loadid: false }; |
| 79 | + |
| 80 | + renderWithTheme(<TagStatus tagIsInstalled={true} tagConfig={configWithoutLoadid} />); |
| 81 | + |
| 82 | + expect(screen.getByText('Disabled')).toBeInTheDocument(); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('when tag is not installed', () => { |
| 87 | + it('shows loading spinner', () => { |
| 88 | + renderWithTheme(<TagStatus tagIsInstalled={false} tagConfig={mockTagConfig} />); |
| 89 | + |
| 90 | + expect(screen.getByRole('progressbar')).toBeInTheDocument(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('shows searching message', () => { |
| 94 | + renderWithTheme(<TagStatus tagIsInstalled={false} tagConfig={mockTagConfig} />); |
| 95 | + |
| 96 | + expect(screen.getByText('Searching for Lytics JavaScript SDK')).toBeInTheDocument(); |
| 97 | + expect(screen.getByTestId('ErrorIcon')).toBeInTheDocument(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('shows installation instructions with documentation link', () => { |
| 101 | + renderWithTheme(<TagStatus tagIsInstalled={false} tagConfig={mockTagConfig} />); |
| 102 | + |
| 103 | + expect(screen.getByText(/We have not been able to find the Lytics JavaScript SDK/)).toBeInTheDocument(); |
| 104 | + |
| 105 | + const docLink = screen.getByRole('link', { name: 'Lytics JavaScript SDK documentation' }); |
| 106 | + expect(docLink).toBeInTheDocument(); |
| 107 | + expect(docLink).toHaveAttribute('href', 'https://docs.lytics.com/docs/lytics-javascript-tag#installation'); |
| 108 | + expect(docLink).toHaveAttribute('target', '_blank'); |
| 109 | + expect(docLink).toHaveAttribute('rel', 'noreferrer'); |
| 110 | + }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments