|
| 1 | +import React from 'react'; |
| 2 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 3 | +import { render, screen } from '@testing-library/react'; |
| 4 | +import '@testing-library/jest-dom'; |
| 5 | +import ProfileSummary from './ProfileSummary'; |
| 6 | +import { ThemeProvider } from '@mui/material/styles'; |
| 7 | +import { appTheme } from '@root/src/theme'; |
| 8 | + |
| 9 | +vi.mock('@root/src/pages/sidepanel/sections/profile/ProfileHeader', () => ({ |
| 10 | + default: ({ lyticsId }) => <div data-testid="profile-header">{lyticsId}</div>, |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock('@root/src/pages/sidepanel/sections/profile/AudienceMembership', () => ({ |
| 14 | + default: ({ audiences }) => <div data-testid="audience-membership">{audiences.join(', ')}</div>, |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock('@root/src/pages/sidepanel/sections/profile/Attributes', () => ({ |
| 18 | + default: ({ count }) => <div data-testid="attributes">Count: {count}</div>, |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock('@root/src/pages/sidepanel/sections/profile/BehaviorMetrics', () => ({ |
| 22 | + default: ({ metrics }) => <div data-testid="behavior-metrics">{metrics.length} metrics</div>, |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock('@root/src/pages/sidepanel/sections/profile/Interests', () => ({ |
| 26 | + default: ({ interests }) => ( |
| 27 | + <div data-testid="interests"> |
| 28 | + {interests.map((interest, index) => ( |
| 29 | + <div key={index} data-testid={`interest-${index}`}> |
| 30 | + {interest.label}: {interest.value}% |
| 31 | + </div> |
| 32 | + ))} |
| 33 | + </div> |
| 34 | + ), |
| 35 | +})); |
| 36 | + |
| 37 | +vi.mock('@root/src/pages/sidepanel/sections/profile/ProfileMetadata', () => ({ |
| 38 | + default: () => <div data-testid="profile-metadata">Metadata</div>, |
| 39 | +})); |
| 40 | + |
| 41 | +describe('ProfileSummary', () => { |
| 42 | + const renderWithTheme = component => { |
| 43 | + return render(<ThemeProvider theme={appTheme}>{component}</ThemeProvider>); |
| 44 | + }; |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + vi.clearAllMocks(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('transforms lytics_content object to interests array correctly', () => { |
| 51 | + const mockProfile = { |
| 52 | + data: { |
| 53 | + user: { |
| 54 | + _id: 'test-id', |
| 55 | + _uid: 'test-uid', |
| 56 | + lytics_content: { |
| 57 | + Home: 1, |
| 58 | + Soap: 0.8409454388537114, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }; |
| 63 | + |
| 64 | + renderWithTheme(<ProfileSummary profile={mockProfile} />); |
| 65 | + |
| 66 | + const interestsContainer = screen.getByTestId('interests'); |
| 67 | + expect(interestsContainer).toBeInTheDocument(); |
| 68 | + |
| 69 | + expect(screen.getByTestId('interest-0')).toHaveTextContent('Home: 100%'); |
| 70 | + expect(screen.getByTestId('interest-1')).toHaveTextContent('Soap: 84%'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('handles missing lytics_content gracefully', () => { |
| 74 | + const mockProfile = { |
| 75 | + data: { |
| 76 | + user: { |
| 77 | + _id: 'test-id', |
| 78 | + _uid: 'test-uid', |
| 79 | + }, |
| 80 | + }, |
| 81 | + }; |
| 82 | + |
| 83 | + renderWithTheme(<ProfileSummary profile={mockProfile} />); |
| 84 | + |
| 85 | + const interestsContainer = screen.getByTestId('interests'); |
| 86 | + expect(interestsContainer).toBeInTheDocument(); |
| 87 | + expect(interestsContainer.children).toHaveLength(0); |
| 88 | + }); |
| 89 | + |
| 90 | + it('converts interest values to percentages correctly', () => { |
| 91 | + const mockProfile = { |
| 92 | + data: { |
| 93 | + user: { |
| 94 | + _id: 'test-id', |
| 95 | + _uid: 'test-uid', |
| 96 | + lytics_content: { |
| 97 | + Technology: 0.95, |
| 98 | + Sports: 0.6, |
| 99 | + Music: 0.45, |
| 100 | + Full: 1.0, |
| 101 | + Zero: 0, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }; |
| 106 | + |
| 107 | + renderWithTheme(<ProfileSummary profile={mockProfile} />); |
| 108 | + |
| 109 | + expect(screen.getByTestId('interest-0')).toHaveTextContent('Technology: 95%'); |
| 110 | + expect(screen.getByTestId('interest-1')).toHaveTextContent('Sports: 60%'); |
| 111 | + expect(screen.getByTestId('interest-2')).toHaveTextContent('Music: 45%'); |
| 112 | + expect(screen.getByTestId('interest-3')).toHaveTextContent('Full: 100%'); |
| 113 | + expect(screen.getByTestId('interest-4')).toHaveTextContent('Zero: 0%'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('handles null or undefined lytics_content', () => { |
| 117 | + const mockProfile = { |
| 118 | + data: { |
| 119 | + user: { |
| 120 | + _id: 'test-id', |
| 121 | + _uid: 'test-uid', |
| 122 | + lytics_content: null, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }; |
| 126 | + |
| 127 | + renderWithTheme(<ProfileSummary profile={mockProfile} />); |
| 128 | + |
| 129 | + const interestsContainer = screen.getByTestId('interests'); |
| 130 | + expect(interestsContainer.children).toHaveLength(0); |
| 131 | + }); |
| 132 | + |
| 133 | + it('handles empty lytics_content object', () => { |
| 134 | + const mockProfile = { |
| 135 | + data: { |
| 136 | + user: { |
| 137 | + _id: 'test-id', |
| 138 | + _uid: 'test-uid', |
| 139 | + lytics_content: {}, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }; |
| 143 | + |
| 144 | + renderWithTheme(<ProfileSummary profile={mockProfile} />); |
| 145 | + |
| 146 | + const interestsContainer = screen.getByTestId('interests'); |
| 147 | + expect(interestsContainer.children).toHaveLength(0); |
| 148 | + }); |
| 149 | +}); |
0 commit comments