Files
summercms/admin/tests/list/ListToolbar.test.ts
Jakub Zych 11c4e5466b test(10.1-04): bring the SPA extension point under Vitest
- WidgetField: skeleton and aria-busy, the 5000 ms whenDefined timeout,
  script failure, attributes only, fill-values and locale sync, one POST
  while busy, fill-key-only patching, danger toasts, unmount
- PartialHost and partialNodes: exhaustive tag, attribute and URL
  allowlist, depth and node caps, skeleton sizes, empty, failure,
  busy refetch and ?id= only with a record
- PartialField, ListToolbar, ListView, registry, formState, FormField and
  FormView: group labels, registered toolbar actions, header refetch
  rules, form context provision and asset loading
2026-09-29 02:59:13 +02:00

111 lines
4.9 KiB
TypeScript

import { beforeEach, describe, expect, it } from 'vitest'
import { mount } from '@vue/test-utils'
import ListToolbar from '../../src/components/list/ListToolbar.vue'
import { resetState } from '../helpers'
const base = {
showSearch: true,
search: 'blue',
searchPrompt: 'Search widgets…',
buttons: ['delete'],
selectedCount: 0,
selectedLabel: 'Selected 0',
deleteLabel: 'Delete selected',
}
beforeEach(() => {
resetState()
})
describe('list toolbar (D-14)', () => {
it('has a labelled search input that emits every change', async () => {
const wrapper = mount(ListToolbar, { props: base })
const input = wrapper.find('[data-list-search]')
expect(input.attributes('type')).toBe('search')
expect(input.attributes('placeholder')).toBe('Search widgets…')
expect((input.element as HTMLInputElement).value).toBe('blue')
expect(wrapper.find('label .sr-only').text()).toBe('Szukaj')
await input.setValue('red')
expect(wrapper.emitted('update:search')).toEqual([['red']])
})
it('hides the search when the schema turns it off', () => {
const wrapper = mount(ListToolbar, { props: { ...base, showSearch: false } })
expect(wrapper.find('[data-list-search]').exists()).toBe(false)
})
it('disables delete without a selection and shows the pill and danger button with one', async () => {
const wrapper = mount(ListToolbar, { props: base })
const button = () => wrapper.find('[data-action="delete"]')
expect(button().attributes('disabled')).toBeDefined()
expect(button().classes()).toContain('border-border-strong')
expect(wrapper.find('[data-selected-pill]').exists()).toBe(false)
await wrapper.setProps({ selectedCount: 2, selectedLabel: 'Selected 2' })
expect(button().attributes('disabled')).toBeUndefined()
expect(button().classes()).toContain('text-danger')
expect(button().text()).toBe('Delete selected')
expect(wrapper.find('[data-selected-pill]').text()).toBe('Selected 2')
await button().trigger('click')
expect(wrapper.emitted('delete')).toHaveLength(1)
})
it('renders only the declared toolbar buttons', () => {
const wrapper = mount(ListToolbar, { props: { ...base, buttons: [] } })
expect(wrapper.find('[data-action="delete"]').exists()).toBe(false)
})
})
describe('registered toolbar actions (D-12, UI-SPEC S4)', () => {
const actions = [
{ name: 'archive', label: 'Archive' },
{ name: 'recount', label: 'Recount' },
]
function names(wrapper: ReturnType<typeof mount>): (string | null)[] {
return wrapper.findAll('[data-action]').map((button) => button.attributes('data-action') ?? null)
}
it('renders custom names after delete in declared order as outline buttons', () => {
const wrapper = mount(ListToolbar, { props: { ...base, buttons: ['delete', 'recount', 'archive'], actions } })
expect(names(wrapper)).toEqual(['delete', 'recount', 'archive'])
const recount = wrapper.find('[data-action="recount"]')
expect(recount.text()).toBe('Recount')
expect(recount.classes()).toContain('border-border-strong')
expect(recount.find('svg').exists()).toBe(false)
})
it('keeps the actions enabled without a selection and while one exists', async () => {
const wrapper = mount(ListToolbar, { props: { ...base, buttons: ['delete', 'recount'], actions } })
const recount = () => wrapper.find('[data-action="recount"]')
expect(recount().attributes('disabled')).toBeUndefined()
expect(recount().attributes('aria-busy')).toBeUndefined()
await wrapper.setProps({ selectedCount: 3, selectedLabel: 'Selected 3' })
expect(recount().attributes('disabled')).toBeUndefined()
await recount().trigger('click')
expect(wrapper.emitted('action')).toEqual([['recount']])
expect(wrapper.emitted('delete')).toBeUndefined()
})
it('disables only the busy action and marks it aria-busy', async () => {
const wrapper = mount(ListToolbar, { props: { ...base, buttons: ['recount', 'archive'], actions, busyAction: 'recount' } })
const recount = wrapper.find('[data-action="recount"]')
expect(recount.attributes('disabled')).toBeDefined()
expect(recount.attributes('aria-busy')).toBe('true')
await recount.trigger('click')
expect(wrapper.emitted('action')).toBeUndefined()
const archive = wrapper.find('[data-action="archive"]')
expect(archive.attributes('disabled')).toBeUndefined()
expect(archive.attributes('aria-busy')).toBeUndefined()
await archive.trigger('click')
expect(wrapper.emitted('action')).toEqual([['archive']])
})
it('does not render names missing from actions or the create button', () => {
const wrapper = mount(ListToolbar, { props: { ...base, buttons: ['create', 'delete', 'launch', 'recount'], actions: [actions[1]!] } })
expect(names(wrapper)).toEqual(['delete', 'recount'])
const bare = mount(ListToolbar, { props: { ...base, buttons: ['recount'] } })
expect(bare.find('[data-action]').exists()).toBe(false)
})
})