|
| 1 | +import React from 'react'; |
| 2 | +import { render, fireEvent, screen } from '@testing-library/react'; |
| 3 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 4 | +import MessageCheap from '../../cheaps/MessageCheap'; |
| 5 | + |
| 6 | +const mockEmail = { |
| 7 | + id: '1', |
| 8 | + from: { |
| 9 | + name: 'John Doe', |
| 10 | + avatar: 'https://example.com/avatar.jpg', |
| 11 | + }, |
| 12 | + subject: 'Test Subject', |
| 13 | + createdAt: '2024-01-15', |
| 14 | + body: 'This is a test email body', |
| 15 | + read: false, |
| 16 | +}; |
| 17 | + |
| 18 | +const mockOnClick = vi.fn(); |
| 19 | + |
| 20 | +const renderMessageCheap = (props = {}) => |
| 21 | + render(<MessageCheap email={mockEmail} onClick={mockOnClick} {...props} />); |
| 22 | + |
| 23 | +describe('MessageCheap', () => { |
| 24 | + afterEach(() => { |
| 25 | + vi.clearAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should match snapshot', () => { |
| 29 | + const messageCheap = renderMessageCheap(); |
| 30 | + expect(messageCheap).toMatchSnapshot(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should render email details correctly', () => { |
| 34 | + renderMessageCheap(); |
| 35 | + |
| 36 | + expect(screen.getByText('John Doe')).toBeInTheDocument(); |
| 37 | + expect(screen.getByText('Test Subject')).toBeInTheDocument(); |
| 38 | + expect(screen.getByText('This is a test email body')).toBeInTheDocument(); |
| 39 | + expect(screen.getByText('2024-01-15')).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should call onClick with email id when clicked', () => { |
| 43 | + renderMessageCheap(); |
| 44 | + |
| 45 | + const button = screen.getByRole('button'); |
| 46 | + fireEvent.click(button); |
| 47 | + |
| 48 | + expect(mockOnClick).toHaveBeenCalledTimes(1); |
| 49 | + expect(mockOnClick).toHaveBeenCalledWith('1'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should show unread indicator for unread emails', () => { |
| 53 | + const { container } = renderMessageCheap(); |
| 54 | + |
| 55 | + const unreadIndicator = container.querySelector('.bg-primary'); |
| 56 | + expect(unreadIndicator).toBeInTheDocument(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should not show unread indicator for read emails', () => { |
| 60 | + const readEmail = { ...mockEmail, read: true }; |
| 61 | + const { container } = render(<MessageCheap email={readEmail} onClick={mockOnClick} />); |
| 62 | + |
| 63 | + const unreadIndicator = container.querySelector('.bg-primary.h-2.w-2'); |
| 64 | + expect(unreadIndicator).not.toBeInTheDocument(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should apply highlighted styles when active', () => { |
| 68 | + const { container } = renderMessageCheap({ active: true }); |
| 69 | + |
| 70 | + const button = container.querySelector('button'); |
| 71 | + expect(button?.className).toContain('bg-primary/10'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should apply highlighted styles when selected', () => { |
| 75 | + const { container } = renderMessageCheap({ selected: true }); |
| 76 | + |
| 77 | + const button = container.querySelector('button'); |
| 78 | + expect(button?.className).toContain('bg-primary/10'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should not apply highlighted styles when neither active nor selected', () => { |
| 82 | + const { container } = renderMessageCheap({ active: false, selected: false }); |
| 83 | + |
| 84 | + const button = container.querySelector('button'); |
| 85 | + expect(button?.className).not.toContain('bg-primary/10'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should render Avatar component with correct props', () => { |
| 89 | + renderMessageCheap(); |
| 90 | + |
| 91 | + const avatarContainer = screen.getByText('John Doe').closest('.flex.flex-row'); |
| 92 | + expect(avatarContainer).toBeInTheDocument(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments