123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import { waitFor, within } from '@testing-library/vue'
- import { vi } from 'vitest'
- import { ref } from 'vue'
- import {
- type ExtendedMountingOptions,
- renderComponent,
- } from '#tests/support/components/index.ts'
- import { i18n } from '#shared/i18n.ts'
- import type { MenuItem } from '#desktop/components/CommonPopoverMenu/types.ts'
- import CommonSimpleTable, { type Props } from '../CommonSimpleTable.vue'
- const tableHeaders = [
- {
- key: 'name',
- label: 'User name',
- },
- {
- key: 'role',
- label: 'Role',
- },
- ]
- const tableItems = [
- {
- id: 1,
- name: 'Lindsay Walton',
- role: 'Member',
- },
- ]
- const tableActions: MenuItem[] = [
- {
- key: 'download',
- label: 'Download this row',
- icon: 'download',
- },
- {
- key: 'delete',
- label: 'Delete this row',
- icon: 'trash3',
- },
- ]
- const renderTable = (
- props: Props,
- options: ExtendedMountingOptions<Props> = { form: true },
- ) => {
- return renderComponent(CommonSimpleTable, {
- ...options,
- props,
- })
- }
- beforeEach(() => {
- i18n.setTranslationMap(new Map([['Role', 'Rolle']]))
- })
- describe('CommonSimpleTable', () => {
- it('displays the table without actions', async () => {
- const wrapper = renderTable({
- headers: tableHeaders,
- items: tableItems,
- })
- expect(wrapper.getByText('User name')).toBeInTheDocument()
- expect(wrapper.getByText('Rolle')).toBeInTheDocument()
- expect(wrapper.getByText('Lindsay Walton')).toBeInTheDocument()
- expect(wrapper.getByText('Member')).toBeInTheDocument()
- expect(wrapper.queryByText('Actions')).toBeNull()
- })
- it('displays the table with actions', async () => {
- const wrapper = renderTable(
- {
- headers: tableHeaders,
- items: tableItems,
- actions: tableActions,
- },
- { router: true },
- )
- expect(wrapper.getByText('Actions')).toBeInTheDocument()
- expect(wrapper.getByLabelText('Action menu button')).toBeInTheDocument()
- })
- it('displays the additional data with the item suffix slot', async () => {
- const wrapper = renderTable(
- {
- headers: tableHeaders,
- items: tableItems,
- actions: tableActions,
- },
- {
- router: true,
- slots: {
- 'item-suffix-role': '<span>Additional Example</span>',
- },
- },
- )
- expect(wrapper.getByText('Additional Example')).toBeInTheDocument()
- })
- it('generates expected DOM', async () => {
- // TODO: check if such snapshot test is really the way we want to go.
- const view = renderTable(
- {
- headers: tableHeaders,
- items: tableItems,
- actions: tableActions,
- },
- // NB: Please don't remove this, otherwise snapshot would contain markup of many more components other than the
- // one under the test, which can lead to false positives.
- {
- shallow: true,
- },
- )
- expect(view.baseElement.querySelector('table')).toMatchFileSnapshot(
- `${__filename}.snapshot.txt`,
- )
- })
- it('supports text truncation in cell content', async () => {
- const wrapper = renderTable({
- headers: [
- ...tableHeaders,
- {
- key: 'truncated',
- label: 'Truncated',
- truncate: true,
- },
- ],
- items: [
- ...tableItems,
- {
- id: 2,
- name: 'Max Mustermann',
- role: 'Admin',
- truncated: 'Some text to be truncated',
- },
- ],
- })
- const truncatedText = wrapper.getByText('Some text to be truncated')
- expect(truncatedText.parentElement).toHaveClass('truncate')
- })
- it('supports tooltip on truncated cell content', async () => {
- const wrapper = renderTable({
- headers: [
- ...tableHeaders,
- {
- key: 'truncated',
- label: 'Truncated',
- truncate: true,
- },
- ],
- items: [
- ...tableItems,
- {
- id: 2,
- name: 'Max Mustermann',
- role: 'Admin',
- truncated: 'Some text to be truncated',
- },
- ],
- })
- await wrapper.events.hover(wrapper.getByText('Max Mustermann'))
- await waitFor(() => {
- expect(wrapper.getByText('Some text to be truncated')).toBeInTheDocument()
- expect(
- wrapper.getByLabelText('Some text to be truncated'),
- ).toBeInTheDocument()
- })
- })
- it('supports header slot', () => {
- const wrapper = renderTable(
- {
- headers: tableHeaders,
- items: tableItems,
- actions: tableActions,
- },
- {
- slots: {
- 'column-header-name': '<div>Custom header</div>',
- },
- },
- )
- expect(wrapper.getByText('Custom header')).toBeInTheDocument()
- })
- it('supports listening for row click events', async () => {
- const mockedCallback = vi.fn()
- const item = tableItems[0]
- const wrapper = renderComponent({
- components: { CommonSimpleTable },
- setup() {
- return {
- mockedCallback,
- tableHeaders,
- items: [item],
- }
- },
- template: `<CommonSimpleTable @click-row="mockedCallback" :headers="tableHeaders" :items="items"/>`,
- })
- expect(
- wrapper.getByRole('button', { name: 'Select table row' }),
- ).toBeInTheDocument()
- await wrapper.events.click(wrapper.getByText('Lindsay Walton'))
- expect(mockedCallback).toHaveBeenCalledWith(item)
- wrapper.getByRole('button', { name: 'Select table row' }).focus()
- await wrapper.events.keyboard('{enter}')
- expect(mockedCallback).toHaveBeenCalledWith(item)
- })
- it('supports marking row in active color', () => {
- const wrapper = renderTable({
- headers: [
- ...tableHeaders,
- {
- key: 'name',
- label: 'name',
- },
- ],
- selectedRowId: '2',
- items: [
- {
- id: '2',
- name: 'foo',
- },
- ],
- })
- const row = wrapper.getByTestId('simple-table-row')
- expect(row).toHaveClass('!bg-blue-800')
- })
- it('supports marking row in active color', () => {
- const wrapper = renderTable({
- headers: [
- {
- key: 'name',
- label: 'name',
- },
- ],
- selectedRowId: '2',
- items: [
- {
- id: '2',
- name: 'foo cell',
- },
- ],
- })
- const row = wrapper.getByTestId('simple-table-row')
- expect(row).toHaveClass('!bg-blue-800')
- expect(within(row).getByText('foo cell')).toHaveClass(
- 'text-black dark:text-white',
- )
- })
- it('supports adding class to table header', () => {
- const wrapper = renderTable({
- headers: [
- {
- key: 'name',
- label: 'Awesome Cell Header',
- labelClass: 'text-red-500 font-bold',
- },
- ],
- items: [
- {
- id: '2',
- name: 'foo cell',
- },
- ],
- })
- expect(wrapper.getByText('Awesome Cell Header')).toHaveClass(
- 'text-red-500 font-bold',
- )
- })
- it('supports adding a link to a cell', () => {
- const wrapper = renderTable(
- {
- headers: [
- {
- key: 'urlTest',
- label: 'Link Row',
- type: 'link',
- },
- ],
- items: [
- {
- id: 1,
- urlTest: {
- label: 'Example',
- link: 'https://example.com',
- openInNewTab: true,
- external: true,
- },
- },
- ],
- },
- { router: true },
- )
- const linkCell = wrapper.getByRole('link')
- expect(linkCell).toHaveTextContent('Example')
- expect(linkCell).toHaveAttribute('href', 'https://example.com')
- expect(linkCell).toHaveAttribute('target', '_blank')
- })
- it('adds hover a')
- it('supports row selection', async () => {
- const checkedRows = ref([])
- const items = [
- {
- id: 1,
- label: 'selection data 1',
- },
- {
- id: 2,
- label: 'selection data 2',
- },
- ]
- const wrapper = renderTable(
- {
- headers: [
- {
- key: 'urlTest',
- label: 'Link Row',
- },
- ],
- items,
- hasCheckboxColumn: true,
- },
- { form: true, vModel: { checkedRows } },
- )
- expect(wrapper.getAllByRole('checkbox')).toHaveLength(3)
- const selectAllCheckbox = wrapper.getByLabelText('Select all entries')
- expect(selectAllCheckbox).not.toHaveAttribute('checked')
- const rowCheckboxes = wrapper.getAllByRole('checkbox', {
- name: 'Select this entry',
- })
- await wrapper.events.click(rowCheckboxes[0])
- expect(rowCheckboxes[0]).toHaveAttribute('checked')
- await wrapper.events.click(rowCheckboxes[1])
- await waitFor(() => expect(checkedRows.value).toEqual(items))
- await waitFor(() => expect(selectAllCheckbox).toHaveAttribute('checked'))
- await wrapper.events.click(wrapper.getByLabelText('Deselect all entries'))
- await waitFor(() => expect(rowCheckboxes[0]).not.toHaveAttribute('checked'))
- expect(rowCheckboxes[1]).not.toHaveAttribute('checked')
- await wrapper.events.click(rowCheckboxes[1])
- expect(
- await wrapper.findByLabelText('Deselect this entry'),
- ).toBeInTheDocument()
- })
- it('supports disabling checkbox item for specific rows', async () => {
- const checkedRows = ref([])
- const items = [
- {
- id: 1,
- checked: false,
- disabled: true,
- label: 'selection data 1',
- },
- {
- id: 1,
- checked: true,
- disabled: true,
- label: 'selection data 1',
- },
- ]
- const wrapper = renderTable(
- {
- headers: [
- {
- key: 'urlTest',
- label: 'Link Row',
- },
- ],
- items,
- hasCheckboxColumn: true,
- },
- { form: true, vModel: { checkedRows } },
- )
- const checkboxes = wrapper.getAllByRole('checkbox')
- expect(checkboxes).toHaveLength(3)
- expect(checkboxes[1]).toBeDisabled()
- expect(checkboxes[1]).not.toBeChecked()
- expect(checkboxes[2]).toHaveAttribute('value', 'true')
- await wrapper.events.click(checkboxes[1])
- expect(checkedRows.value).toEqual([])
- await wrapper.events.click(checkboxes[0])
- expect(checkedRows.value).toEqual([])
- })
- })
|