|
| 1 | +import { describe, test, expect, vi } from 'vitest' |
| 2 | +import { render } from '@testing-library/react' |
| 3 | +import { RecordContextProvider } from 'react-admin' |
| 4 | +import { AlbumDatesField } from './AlbumDatesField' |
| 5 | +import { formatRange } from '../common/index.js' |
| 6 | + |
| 7 | +// Mock the formatRange function |
| 8 | +vi.mock('../common/index.js', () => ({ |
| 9 | + formatRange: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +describe('AlbumDatesField', () => { |
| 13 | + test('renders nothing when yearRange is "0"', () => { |
| 14 | + const record = { |
| 15 | + maxYear: '0', |
| 16 | + releaseDate: '2020-01-01', |
| 17 | + } |
| 18 | + |
| 19 | + vi.mocked(formatRange).mockReturnValue('0') |
| 20 | + |
| 21 | + const { container } = render( |
| 22 | + <RecordContextProvider value={record}> |
| 23 | + <AlbumDatesField /> |
| 24 | + </RecordContextProvider>, |
| 25 | + ) |
| 26 | + |
| 27 | + expect(container.firstChild).toBeNull() |
| 28 | + }) |
| 29 | + |
| 30 | + test('renders nothing when releaseYear is "0"', () => { |
| 31 | + const record = { |
| 32 | + maxYear: '2020', |
| 33 | + releaseDate: '0-01-01', |
| 34 | + } |
| 35 | + |
| 36 | + vi.mocked(formatRange).mockReturnValue('2020') |
| 37 | + |
| 38 | + const { container } = render( |
| 39 | + <RecordContextProvider value={record}> |
| 40 | + <AlbumDatesField /> |
| 41 | + </RecordContextProvider>, |
| 42 | + ) |
| 43 | + |
| 44 | + expect(container.firstChild).toBeNull() |
| 45 | + }) |
| 46 | + |
| 47 | + test('renders only yearRange when releaseYear is undefined', () => { |
| 48 | + const record = { |
| 49 | + maxYear: '2020', |
| 50 | + } |
| 51 | + |
| 52 | + vi.mocked(formatRange).mockReturnValue('2020') |
| 53 | + |
| 54 | + const { container } = render( |
| 55 | + <RecordContextProvider value={record}> |
| 56 | + <AlbumDatesField /> |
| 57 | + </RecordContextProvider>, |
| 58 | + ) |
| 59 | + |
| 60 | + expect(container.textContent).toBe('2020') |
| 61 | + }) |
| 62 | + |
| 63 | + test('renders both years when they are different', () => { |
| 64 | + const record = { |
| 65 | + maxYear: '2018', |
| 66 | + releaseDate: '2020-01-01', |
| 67 | + } |
| 68 | + |
| 69 | + vi.mocked(formatRange).mockReturnValue('2018') |
| 70 | + |
| 71 | + const { container } = render( |
| 72 | + <RecordContextProvider value={record}> |
| 73 | + <AlbumDatesField /> |
| 74 | + </RecordContextProvider>, |
| 75 | + ) |
| 76 | + |
| 77 | + expect(container.textContent).toBe('♫ 2018 · ○ 2020') |
| 78 | + }) |
| 79 | + |
| 80 | + test('renders only yearRange when both years are the same', () => { |
| 81 | + const record = { |
| 82 | + maxYear: '2020', |
| 83 | + releaseDate: '2020-01-01', |
| 84 | + } |
| 85 | + |
| 86 | + vi.mocked(formatRange).mockReturnValue('2020') |
| 87 | + |
| 88 | + const { container } = render( |
| 89 | + <RecordContextProvider value={record}> |
| 90 | + <AlbumDatesField /> |
| 91 | + </RecordContextProvider>, |
| 92 | + ) |
| 93 | + |
| 94 | + expect(container.textContent).toBe('2020') |
| 95 | + }) |
| 96 | + |
| 97 | + test('applies className when provided', () => { |
| 98 | + const record = { |
| 99 | + maxYear: '2020', |
| 100 | + } |
| 101 | + |
| 102 | + vi.mocked(formatRange).mockReturnValue('2020') |
| 103 | + |
| 104 | + const { container } = render( |
| 105 | + <RecordContextProvider value={record}> |
| 106 | + <AlbumDatesField className="test-class" /> |
| 107 | + </RecordContextProvider>, |
| 108 | + ) |
| 109 | + |
| 110 | + expect(container.firstChild).toHaveClass('test-class') |
| 111 | + }) |
| 112 | +}) |
0 commit comments