|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + * |
| 8 | + * Tests for CalendarView optimizations: |
| 9 | + * - Event index (Map-based lookup instead of O(N) per cell) |
| 10 | + * - Stable default date reference |
| 11 | + * - HEX color text-white fix |
| 12 | + * - onEventDrop / locale passthrough from ObjectCalendar |
| 13 | + */ |
| 14 | + |
| 15 | +import { describe, it, expect, vi } from 'vitest'; |
| 16 | +import { render, screen } from '@testing-library/react'; |
| 17 | +import '@testing-library/jest-dom'; |
| 18 | +import React from 'react'; |
| 19 | +import { CalendarView, type CalendarEvent } from '../CalendarView'; |
| 20 | + |
| 21 | +// Mock @object-ui/components |
| 22 | +vi.mock('@object-ui/components', () => ({ |
| 23 | + cn: (...classes: (string | undefined | boolean)[]) => classes.filter(Boolean).join(' '), |
| 24 | + Button: ({ children, ...props }: any) => <button {...props}>{children}</button>, |
| 25 | + Select: ({ children, value, onValueChange }: any) => <div data-testid="select">{children}</div>, |
| 26 | + SelectContent: ({ children }: any) => <div>{children}</div>, |
| 27 | + SelectItem: ({ children, value }: any) => <div data-value={value}>{children}</div>, |
| 28 | + SelectTrigger: ({ children }: any) => <div>{children}</div>, |
| 29 | + SelectValue: () => <span>Month</span>, |
| 30 | + Calendar: (props: any) => <div data-testid="calendar-picker">Calendar Picker</div>, |
| 31 | + Popover: ({ children }: any) => <div>{children}</div>, |
| 32 | + PopoverContent: ({ children }: any) => <div>{children}</div>, |
| 33 | + PopoverTrigger: ({ children }: any) => <div>{children}</div>, |
| 34 | +})); |
| 35 | + |
| 36 | +// Mock lucide-react icons |
| 37 | +vi.mock('lucide-react', () => ({ |
| 38 | + ChevronLeftIcon: (props: any) => <svg data-testid="chevron-left" {...props} />, |
| 39 | + ChevronRightIcon: (props: any) => <svg data-testid="chevron-right" {...props} />, |
| 40 | + CalendarIcon: (props: any) => <svg data-testid="calendar-icon" {...props} />, |
| 41 | + PlusIcon: (props: any) => <svg data-testid="plus-icon" {...props} />, |
| 42 | +})); |
| 43 | + |
| 44 | +// Mock ResizeObserver |
| 45 | +class ResizeObserver { |
| 46 | + observe() {} |
| 47 | + unobserve() {} |
| 48 | + disconnect() {} |
| 49 | +} |
| 50 | +global.ResizeObserver = ResizeObserver; |
| 51 | + |
| 52 | +const baseDate = new Date(2024, 0, 15); // Jan 15, 2024 |
| 53 | + |
| 54 | +describe('CalendarView optimizations', () => { |
| 55 | + describe('event index (Map-based lookup)', () => { |
| 56 | + it('renders single-day events correctly with the new index', () => { |
| 57 | + const events: CalendarEvent[] = [ |
| 58 | + { id: '1', title: 'Morning Meeting', start: new Date(2024, 0, 15, 9, 0), end: new Date(2024, 0, 15, 10, 0) }, |
| 59 | + { id: '2', title: 'Lunch', start: new Date(2024, 0, 15, 12, 0), end: new Date(2024, 0, 15, 13, 0) }, |
| 60 | + ]; |
| 61 | + render(<CalendarView currentDate={baseDate} events={events} view="month" locale="en-US" />); |
| 62 | + expect(screen.getByText('Morning Meeting')).toBeInTheDocument(); |
| 63 | + expect(screen.getByText('Lunch')).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('renders multi-day events on all spanned days', () => { |
| 67 | + const events: CalendarEvent[] = [ |
| 68 | + { |
| 69 | + id: 'multi-1', |
| 70 | + title: 'Conference', |
| 71 | + start: new Date(2024, 0, 15, 9, 0), |
| 72 | + end: new Date(2024, 0, 17, 17, 0), |
| 73 | + allDay: true, |
| 74 | + }, |
| 75 | + ]; |
| 76 | + const { container } = render( |
| 77 | + <CalendarView currentDate={baseDate} events={events} view="month" locale="en-US" /> |
| 78 | + ); |
| 79 | + // The multi-day event should appear on Jan 15, 16, and 17 — 3 cells |
| 80 | + const eventElements = container.querySelectorAll('[role="button"][aria-label="Conference"]'); |
| 81 | + expect(eventElements.length).toBe(3); |
| 82 | + }); |
| 83 | + |
| 84 | + it('renders events with no end date on their start day only', () => { |
| 85 | + const events: CalendarEvent[] = [ |
| 86 | + { id: 'no-end', title: 'No End Event', start: new Date(2024, 0, 20, 14, 0) }, |
| 87 | + ]; |
| 88 | + const { container } = render( |
| 89 | + <CalendarView currentDate={baseDate} events={events} view="month" locale="en-US" /> |
| 90 | + ); |
| 91 | + const eventElements = container.querySelectorAll('[role="button"][aria-label="No End Event"]'); |
| 92 | + expect(eventElements.length).toBe(1); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('HEX color text-white fix', () => { |
| 97 | + it('applies text-white class when event color is a HEX value in month view', () => { |
| 98 | + const events: CalendarEvent[] = [ |
| 99 | + { id: 'hex-1', title: 'HEX Event', start: new Date(2024, 0, 15), color: '#3b82f6' }, |
| 100 | + ]; |
| 101 | + const { container } = render( |
| 102 | + <CalendarView currentDate={baseDate} events={events} view="month" locale="en-US" /> |
| 103 | + ); |
| 104 | + const eventEl = container.querySelector('[aria-label="HEX Event"]'); |
| 105 | + expect(eventEl).toBeInTheDocument(); |
| 106 | + expect(eventEl!.className).toContain('text-white'); |
| 107 | + expect((eventEl as HTMLElement).style.backgroundColor).toBe('#3b82f6'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('uses default color class when event color is a Tailwind class', () => { |
| 111 | + const events: CalendarEvent[] = [ |
| 112 | + { id: 'tw-1', title: 'Tailwind Event', start: new Date(2024, 0, 15), color: 'bg-red-500 text-white' }, |
| 113 | + ]; |
| 114 | + const { container } = render( |
| 115 | + <CalendarView currentDate={baseDate} events={events} view="month" locale="en-US" /> |
| 116 | + ); |
| 117 | + const eventEl = container.querySelector('[aria-label="Tailwind Event"]'); |
| 118 | + expect(eventEl).toBeInTheDocument(); |
| 119 | + expect(eventEl!.className).toContain('bg-red-500'); |
| 120 | + expect(eventEl!.className).toContain('text-white'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('falls back to DEFAULT_EVENT_COLOR when no color is specified', () => { |
| 124 | + const events: CalendarEvent[] = [ |
| 125 | + { id: 'no-color', title: 'No Color', start: new Date(2024, 0, 15) }, |
| 126 | + ]; |
| 127 | + const { container } = render( |
| 128 | + <CalendarView currentDate={baseDate} events={events} view="month" locale="en-US" /> |
| 129 | + ); |
| 130 | + const eventEl = container.querySelector('[aria-label="No Color"]'); |
| 131 | + expect(eventEl).toBeInTheDocument(); |
| 132 | + expect(eventEl!.className).toContain('bg-blue-500'); |
| 133 | + expect(eventEl!.className).toContain('text-white'); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('stable default date', () => { |
| 138 | + it('renders without currentDate prop without errors', () => { |
| 139 | + const { container } = render(<CalendarView events={[]} locale="en-US" />); |
| 140 | + expect(container).toBeTruthy(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('does not trigger re-render loop when currentDate is not provided', () => { |
| 144 | + const onNavigate = vi.fn(); |
| 145 | + const { rerender } = render(<CalendarView events={[]} locale="en-US" onNavigate={onNavigate} />); |
| 146 | + // Re-render the same component — should NOT trigger onNavigate from the effect |
| 147 | + rerender(<CalendarView events={[]} locale="en-US" onNavigate={onNavigate} />); |
| 148 | + expect(onNavigate).not.toHaveBeenCalled(); |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + describe('drag-and-drop enablement', () => { |
| 153 | + it('events are draggable when onEventDrop is provided', () => { |
| 154 | + const events: CalendarEvent[] = [ |
| 155 | + { id: 'd-1', title: 'Drag Event', start: new Date(2024, 0, 15) }, |
| 156 | + ]; |
| 157 | + const onEventDrop = vi.fn(); |
| 158 | + const { container } = render( |
| 159 | + <CalendarView currentDate={baseDate} events={events} view="month" onEventDrop={onEventDrop} locale="en-US" /> |
| 160 | + ); |
| 161 | + const eventEl = container.querySelector('[aria-label="Drag Event"]'); |
| 162 | + expect(eventEl).toBeInTheDocument(); |
| 163 | + expect(eventEl!.getAttribute('draggable')).toBe('true'); |
| 164 | + }); |
| 165 | + |
| 166 | + it('events are not draggable when onEventDrop is not provided', () => { |
| 167 | + const events: CalendarEvent[] = [ |
| 168 | + { id: 'nd-1', title: 'No Drag Event', start: new Date(2024, 0, 15) }, |
| 169 | + ]; |
| 170 | + const { container } = render( |
| 171 | + <CalendarView currentDate={baseDate} events={events} view="month" locale="en-US" /> |
| 172 | + ); |
| 173 | + const eventEl = container.querySelector('[aria-label="No Drag Event"]'); |
| 174 | + expect(eventEl).toBeInTheDocument(); |
| 175 | + expect(eventEl!.getAttribute('draggable')).toBe('false'); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments