2026-03-17 22:42:55 +01:00
|
|
|
import { describe, it, expect, vi } from 'vitest'
|
|
|
|
|
import { render, screen, fireEvent, act } from '@testing-library/react'
|
|
|
|
|
import { NoteIcon } from './NoteIcon'
|
2026-04-07 21:09:06 +02:00
|
|
|
import { FOCUS_NOTE_ICON_PROPERTY_EVENT } from './noteIconPropertyEvents'
|
2026-03-17 22:42:55 +01:00
|
|
|
|
|
|
|
|
describe('NoteIcon', () => {
|
|
|
|
|
it('shows add button when no icon is set', () => {
|
2026-04-07 21:09:06 +02:00
|
|
|
render(<NoteIcon icon={null} />)
|
2026-03-17 22:42:55 +01:00
|
|
|
expect(screen.getByTestId('note-icon-add')).toBeInTheDocument()
|
|
|
|
|
expect(screen.queryByTestId('note-icon-display')).not.toBeInTheDocument()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('displays the emoji when icon is set', () => {
|
2026-04-07 21:09:06 +02:00
|
|
|
render(<NoteIcon icon="🎯" />)
|
2026-03-17 22:42:55 +01:00
|
|
|
expect(screen.getByTestId('note-icon-display')).toHaveTextContent('🎯')
|
|
|
|
|
expect(screen.queryByTestId('note-icon-add')).not.toBeInTheDocument()
|
|
|
|
|
})
|
|
|
|
|
|
2026-04-07 21:09:06 +02:00
|
|
|
it('displays Phosphor icons when icon is set', () => {
|
|
|
|
|
render(<NoteIcon icon="rocket" />)
|
|
|
|
|
expect(screen.getByTestId('note-icon-display').querySelector('svg')).toBeInTheDocument()
|
2026-03-17 22:42:55 +01:00
|
|
|
})
|
|
|
|
|
|
2026-04-07 21:09:06 +02:00
|
|
|
it('displays image icons when icon is set to a url', () => {
|
|
|
|
|
render(<NoteIcon icon="https://example.com/favicon.png" />)
|
|
|
|
|
expect(screen.getByTestId('note-icon-display').querySelector('img')).toBeInTheDocument()
|
2026-03-17 22:42:55 +01:00
|
|
|
})
|
|
|
|
|
|
2026-04-07 21:09:06 +02:00
|
|
|
it('focuses the icon property when add button is clicked', () => {
|
|
|
|
|
const handler = vi.fn()
|
|
|
|
|
window.addEventListener(FOCUS_NOTE_ICON_PROPERTY_EVENT, handler)
|
2026-03-17 22:42:55 +01:00
|
|
|
|
2026-04-07 21:09:06 +02:00
|
|
|
render(<NoteIcon icon={null} />)
|
|
|
|
|
fireEvent.click(screen.getByTestId('note-icon-add'))
|
|
|
|
|
|
|
|
|
|
expect(handler).toHaveBeenCalledOnce()
|
|
|
|
|
window.removeEventListener(FOCUS_NOTE_ICON_PROPERTY_EVENT, handler)
|
2026-03-17 22:42:55 +01:00
|
|
|
})
|
|
|
|
|
|
2026-04-07 21:09:06 +02:00
|
|
|
it('focuses the icon property when existing icon is clicked', () => {
|
|
|
|
|
const handler = vi.fn()
|
|
|
|
|
window.addEventListener(FOCUS_NOTE_ICON_PROPERTY_EVENT, handler)
|
|
|
|
|
|
|
|
|
|
render(<NoteIcon icon="🔥" />)
|
2026-03-17 22:42:55 +01:00
|
|
|
fireEvent.click(screen.getByTestId('note-icon-display'))
|
|
|
|
|
|
2026-04-07 21:09:06 +02:00
|
|
|
expect(handler).toHaveBeenCalledOnce()
|
|
|
|
|
window.removeEventListener(FOCUS_NOTE_ICON_PROPERTY_EVENT, handler)
|
2026-03-17 22:42:55 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('hides add button when not editable', () => {
|
2026-04-07 21:09:06 +02:00
|
|
|
render(<NoteIcon icon={null} editable={false} />)
|
2026-03-17 22:42:55 +01:00
|
|
|
expect(screen.queryByTestId('note-icon-add')).not.toBeInTheDocument()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('disables icon click when not editable', () => {
|
2026-04-07 21:09:06 +02:00
|
|
|
render(<NoteIcon icon="🎯" editable={false} />)
|
2026-03-17 22:42:55 +01:00
|
|
|
const display = screen.getByTestId('note-icon-display')
|
|
|
|
|
expect(display).toBeDisabled()
|
|
|
|
|
})
|
|
|
|
|
|
2026-04-07 21:09:06 +02:00
|
|
|
it('maps the legacy picker event to the icon property focus event', () => {
|
|
|
|
|
const handler = vi.fn()
|
|
|
|
|
window.addEventListener(FOCUS_NOTE_ICON_PROPERTY_EVENT, handler)
|
|
|
|
|
|
|
|
|
|
render(<NoteIcon icon={null} />)
|
2026-03-17 22:42:55 +01:00
|
|
|
act(() => { window.dispatchEvent(new CustomEvent('laputa:open-icon-picker')) })
|
2026-04-07 21:09:06 +02:00
|
|
|
|
|
|
|
|
expect(handler).toHaveBeenCalledOnce()
|
|
|
|
|
window.removeEventListener(FOCUS_NOTE_ICON_PROPERTY_EVENT, handler)
|
2026-03-17 22:42:55 +01:00
|
|
|
})
|
|
|
|
|
})
|