- a button next to the logo folds the menu to icons and opens it again - folded, each link keeps its name for a screen reader and shows it as a tooltip, the group names stay for a screen reader, the logo becomes the mark - the choice is kept in this device's storage - no button on a phone, where the tabs replace the menu
195 lines
9.2 KiB
TypeScript
195 lines
9.2 KiB
TypeScript
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
import { cleanup, render, screen, within } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { MemoryRouter } from 'react-router-dom'
|
|
import { beforeEach, expect, test, vi } from 'vitest'
|
|
import { sessionStore } from '@/auth/session'
|
|
import { initI18n } from '@/i18n'
|
|
import { mockFetch } from '@/test/mockFetch'
|
|
import Sidebar from './Sidebar'
|
|
|
|
beforeEach(async () => {
|
|
vi.spyOn(navigator, 'language', 'get').mockReturnValue('en-US')
|
|
await initI18n()
|
|
sessionStore.setState({ status: 'authenticated', user: { id: 1, email: 'tom@example.org', is_admin: true } })
|
|
})
|
|
|
|
function renderSidebar(friends: { id: number; name: string }[] = [], path = '/albums') {
|
|
mockFetch({ 'GET /api/v1/friends': () => ({ friends }) })
|
|
render(
|
|
<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false } } })}>
|
|
<MemoryRouter initialEntries={[path]}>
|
|
<Sidebar />
|
|
</MemoryRouter>
|
|
</QueryClientProvider>,
|
|
)
|
|
return screen.getByRole('navigation', { name: 'Main' })
|
|
}
|
|
|
|
test('the menu has a link for each page, and the page that is open is the current one', () => {
|
|
const nav = renderSidebar()
|
|
for (const name of ['Home', 'Search', 'Discover', 'Artists', 'Albums', 'Songs', 'Mixes', 'Playlists', 'Smart playlists', 'History', 'Import', 'Admin', 'Settings']) {
|
|
expect(within(nav).getByRole('link', { name })).toBeInTheDocument()
|
|
}
|
|
expect(within(nav).getByRole('link', { name: 'Albums' })).toHaveAttribute('aria-current', 'page')
|
|
expect(within(nav).getByRole('link', { name: 'Artists' })).not.toHaveAttribute('aria-current')
|
|
})
|
|
|
|
test('the menu starts open, with a button that folds it', () => {
|
|
const nav = renderSidebar()
|
|
expect(nav).not.toHaveAttribute('data-collapsed')
|
|
const button = within(nav).getByRole('button', { name: 'Collapse menu' })
|
|
expect(button).toHaveAttribute('aria-expanded', 'true')
|
|
})
|
|
|
|
test('folding leaves the icons: the words stay as the names of the links, and the logo becomes the mark', async () => {
|
|
const nav = renderSidebar()
|
|
await userEvent.click(within(nav).getByRole('button', { name: 'Collapse menu' }))
|
|
expect(nav).toHaveAttribute('data-collapsed')
|
|
const button = within(nav).getByRole('button', { name: 'Expand menu' })
|
|
expect(button).toHaveAttribute('aria-expanded', 'false')
|
|
const albums = within(nav).getByRole('link', { name: 'Albums' })
|
|
expect(albums).toHaveAttribute('title', 'Albums')
|
|
expect(albums.querySelector('[data-icon]')).toBeInTheDocument()
|
|
expect(albums.querySelector('span')).toHaveClass('sr-only')
|
|
expect(within(nav).getAllByRole('img', { name: 'Sonda' })).toHaveLength(1)
|
|
expect(within(nav).queryByText(/Nothing leaves this server/)).not.toBeInTheDocument()
|
|
})
|
|
|
|
test('when open the links have no tooltip: the words are there', () => {
|
|
const nav = renderSidebar()
|
|
expect(within(nav).getByRole('link', { name: 'Albums' })).not.toHaveAttribute('title')
|
|
})
|
|
|
|
test('the group names stay for a screen reader when the menu is folded', async () => {
|
|
const nav = renderSidebar()
|
|
await userEvent.click(within(nav).getByRole('button', { name: 'Collapse menu' }))
|
|
expect(within(nav).getByRole('list', { name: 'Library' })).toBeInTheDocument()
|
|
expect(within(nav).getByText('Library')).toHaveClass('sr-only')
|
|
})
|
|
|
|
test('the choice is kept on this device and applies the next time', async () => {
|
|
const first = renderSidebar()
|
|
await userEvent.click(within(first).getByRole('button', { name: 'Collapse menu' }))
|
|
expect(localStorage.getItem('sonda.sidebar')).toBe('collapsed')
|
|
cleanup()
|
|
const again = renderSidebar()
|
|
expect(again).toHaveAttribute('data-collapsed')
|
|
await userEvent.click(within(again).getByRole('button', { name: 'Expand menu' }))
|
|
expect(localStorage.getItem('sonda.sidebar')).toBe('open')
|
|
})
|
|
|
|
test('every link has an icon, and the words stay: the icon is decoration, not the name', () => {
|
|
const nav = renderSidebar()
|
|
const links = within(nav).getAllByRole('link')
|
|
expect(links.length).toBeGreaterThan(10)
|
|
for (const link of links) {
|
|
const icon = link.querySelector('[data-icon]')
|
|
expect(icon, link.textContent ?? '').toBeInTheDocument()
|
|
expect(icon).toHaveAttribute('aria-hidden', 'true')
|
|
expect(link.textContent?.trim()).not.toBe('')
|
|
}
|
|
})
|
|
|
|
test('no two pages share an icon', () => {
|
|
const nav = renderSidebar()
|
|
const kinds = within(nav)
|
|
.getAllByRole('link')
|
|
.map((a) => a.querySelector('[data-icon]')?.getAttribute('class')?.match(/lucide-[\w-]+/)?.[0])
|
|
expect(new Set(kinds).size).toBe(kinds.length)
|
|
})
|
|
|
|
test('the icon of the page that is open is in the colour of this server', () => {
|
|
const nav = renderSidebar()
|
|
const icon = (name: string) => within(nav).getByRole('link', { name }).querySelector('[data-icon]')
|
|
expect(icon('Albums')).toHaveClass('text-primary')
|
|
expect(icon('Artists')).not.toHaveClass('text-primary')
|
|
})
|
|
|
|
test('the Sonda logo is the name of the app', () => {
|
|
const nav = renderSidebar()
|
|
expect(within(nav).getAllByRole('img', { name: 'Sonda' })).toHaveLength(1)
|
|
})
|
|
|
|
test('only an admin has an Admin link', () => {
|
|
sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'ann@example.org', is_admin: false } })
|
|
const nav = renderSidebar()
|
|
expect(within(nav).queryByRole('link', { name: 'Admin' })).not.toBeInTheDocument()
|
|
})
|
|
|
|
test('Friends is in the menu only when there is a friend to browse', async () => {
|
|
const none = renderSidebar()
|
|
await screen.findByRole('link', { name: 'Albums' })
|
|
expect(within(none).queryByRole('link', { name: 'Friends' })).not.toBeInTheDocument()
|
|
})
|
|
|
|
test('with a friend the menu has a Friends link, with the icon of what comes from elsewhere', async () => {
|
|
const nav = renderSidebar([{ id: 3, name: 'Anna' }])
|
|
const link = await within(nav).findByRole('link', { name: 'Friends' })
|
|
expect(link).toHaveAttribute('href', '/friends')
|
|
expect(link.querySelector('[data-icon]')).toHaveClass('text-friend')
|
|
})
|
|
|
|
test('the icon of Friends is the friend colour also when it is the page that is open', async () => {
|
|
const nav = renderSidebar([{ id: 3, name: 'Anna' }], '/friends/3')
|
|
const link = await within(nav).findByRole('link', { name: 'Friends' })
|
|
expect(link).toHaveAttribute('aria-current', 'page')
|
|
expect(link.querySelector('[data-icon]')).toHaveClass('text-friend')
|
|
})
|
|
|
|
test('on a phone the links are left to the bar of tabs: only the logo stays', () => {
|
|
const nav = renderSidebar()
|
|
expect(nav.querySelector(':scope > div:nth-of-type(2)')).toHaveClass('max-sm:hidden')
|
|
expect(within(nav).getByRole('button', { name: 'Collapse menu' })).toHaveClass('max-sm:hidden')
|
|
})
|
|
|
|
const names = (nav: HTMLElement) => within(nav).getAllByRole('link').map((a) => a.textContent)
|
|
|
|
test('the links are in groups, in the order of what you do: find, the library, your listening, friends, then the foot', async () => {
|
|
const nav = renderSidebar([{ id: 3, name: 'Anna' }])
|
|
await within(nav).findByRole('link', { name: 'Friends' })
|
|
expect(names(nav)).toEqual([
|
|
'Home', 'Search', 'Discover', 'Requests',
|
|
'Artists', 'Albums', 'Songs', 'Playlists', 'Smart playlists', 'Mixes',
|
|
'History', 'Import',
|
|
'Friends',
|
|
'Settings', 'Admin',
|
|
])
|
|
})
|
|
|
|
test('the library and the listening have a quiet heading, and the groups that explain themselves have none', () => {
|
|
const nav = renderSidebar()
|
|
const library = within(nav).getByRole('list', { name: 'Library' })
|
|
expect(within(library).getAllByRole('link').map((a) => a.textContent)).toEqual(['Artists', 'Albums', 'Songs', 'Playlists', 'Smart playlists', 'Mixes'])
|
|
const listening = within(nav).getByRole('list', { name: 'Your listening' })
|
|
expect(within(listening).getAllByRole('link').map((a) => a.textContent)).toEqual(['History', 'Import'])
|
|
// The headings are words, not links, and not headings of the page.
|
|
expect(within(nav).queryByRole('heading')).not.toBeInTheDocument()
|
|
expect(within(nav).queryByRole('link', { name: 'Your listening' })).not.toBeInTheDocument()
|
|
})
|
|
|
|
test('Settings and Admin are at the foot, and a person who is not an admin has Settings alone', () => {
|
|
sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'ann@example.org', is_admin: false } })
|
|
const nav = renderSidebar()
|
|
const all = names(nav)
|
|
expect(all.slice(-1)).toEqual(['Settings'])
|
|
expect(all).not.toContain('Admin')
|
|
})
|
|
|
|
test('a page inside a section lights the section: an artist, a new smart playlist, a friend', () => {
|
|
for (const [path, name] of [['/artists/7', 'Artists'], ['/smart-playlists/new', 'Smart playlists'], ['/playlists/2', 'Playlists'], ['/admin/users', 'Admin']] as const) {
|
|
document.body.innerHTML = ''
|
|
const nav = renderSidebar([], path)
|
|
expect(within(nav).getByRole('link', { name })).toHaveAttribute('aria-current', 'page')
|
|
expect(within(nav).getAllByRole('link').filter((a) => a.getAttribute('aria-current') === 'page')).toHaveLength(1)
|
|
}
|
|
})
|
|
|
|
test('Requests, the artists sent to Lidarr, is a link of admins only', () => {
|
|
const admin = renderSidebar()
|
|
expect(within(admin).getByRole('link', { name: 'Requests' })).toHaveAttribute('href', '/requests')
|
|
document.body.innerHTML = ''
|
|
sessionStore.setState({ status: 'authenticated', user: { id: 2, email: 'ann@example.org', is_admin: false } })
|
|
const user = renderSidebar()
|
|
expect(within(user).queryByRole('link', { name: 'Requests' })).not.toBeInTheDocument()
|
|
})
|