primevue-mirror/components/lib/datatable/DataTable.spec.js

1413 lines
49 KiB
JavaScript
Raw Normal View History

2022-09-06 12:03:37 +00:00
import { mount } from '@vue/test-utils';
2022-09-14 11:26:01 +00:00
import { FilterMatchMode } from 'primevue/api';
2022-12-08 14:13:16 +00:00
import Button from 'primevue/button';
2022-12-08 11:04:25 +00:00
import PrimeVue from 'primevue/config';
2022-12-08 14:13:16 +00:00
import InputText from 'primevue/inputtext';
import Column from '../column/Column.vue';
import ColumnGroup from '../columngroup/ColumnGroup.vue';
import Row from '../row/Row.vue';
2022-12-08 11:04:25 +00:00
import DataTable from './DataTable.vue';
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
window.URL.createObjectURL = function () {};
2022-09-06 12:03:37 +00:00
const smallData = [
{
2022-09-14 11:26:01 +00:00
id: '1000',
code: 'vbb124btr',
name: 'Game Controller'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1001',
code: 'nvklal433',
name: 'Black Watch'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1002',
code: 'zz21cz3c1',
name: 'Blue Band'
2022-09-06 12:03:37 +00:00
}
];
const data = [
{
2022-09-14 11:26:01 +00:00
id: '1000',
code: 'vbb124btr',
name: 'Game Controller'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1001',
code: 'nvklal433',
name: 'Black Watch'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1002',
code: 'zz21cz3c1',
name: 'Blue Band'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1003',
code: '244wgerg2',
name: 'Blue T-Shirt'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1004',
code: 'h456wer53',
name: 'Bracelet'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1005',
code: 'cm230f032',
name: 'Gaming Set'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1006',
code: 'bib36pfvm',
name: 'Chakra Bracelet'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1007',
code: 'mbvjkgip5',
name: 'Galaxy Earrings'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1008',
code: 'f230fh0g3',
name: 'Bamboo Watch'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1009',
code: 'av2231fwg',
name: 'Brown Purse'
2022-09-06 12:03:37 +00:00
}
];
describe('DataTable.vue', () => {
let wrapper;
beforeEach(() => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column,
Button
}
},
props: {
value: smallData,
expandedRows: [],
paginatorTemplate: 'CurrentPageReport FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink RowsPerPageDropdown',
rowsPerPageOptions: [5, 6, 7],
currentPageReportTemplate: 'Showing {first} to {last} of {totalRecords}'
},
slots: {
default: `
<Column :expander="true" />
<Column field="code" header="Code" sortable></Column>
<Column field="name" header="Name" sortable></Column>
`,
header: `Header Templating`,
footer: `Footer Templating`,
expansion: `Expansion Templating`,
empty: `Empty Templating`,
paginatorstart: `Paginator Start Templating`,
paginatorend: `Paginator End Templating`
}
});
});
it('should exist', () => {
expect(wrapper.find('.p-datatable.p-component').exists()).toBe(true);
expect(wrapper.find('.p-datatable-wrapper').exists()).toBe(true);
expect(wrapper.find('table.p-datatable-table').exists()).toBe(true);
expect(wrapper.find('thead.p-datatable-thead').exists()).toBe(true);
expect(wrapper.find('tbody.p-datatable-tbody').exists()).toBe(true);
});
it('should have basic demo', () => {
expect(wrapper.findAll('.p-column-header-content').length).toEqual(3);
const tbody = wrapper.find('.p-datatable-tbody');
expect(tbody.findAll('tr').length).toEqual(3);
const rows = tbody.findAll('tr');
expect(rows[0].findAll('td').length).toEqual(3);
});
// table templating
it('should have header template', () => {
expect(wrapper.find('.p-datatable-header').exists()).toBe(true);
expect(wrapper.find('.p-datatable-header').text()).toBe('Header Templating');
});
it('should have footer template', () => {
expect(wrapper.find('.p-datatable-footer').exists()).toBe(true);
expect(wrapper.find('.p-datatable-footer').text()).toBe('Footer Templating');
});
it('should have expansion template', async () => {
await wrapper.setProps({ expandedRows: [smallData[0]] });
expect(wrapper.find('tr.p-datatable-row-expansion').exists()).toBe(true);
expect(wrapper.find('tr.p-datatable-row-expansion').text()).toBe('Expansion Templating');
});
it('should have empty templating', async () => {
await wrapper.setProps({ value: [] });
expect(wrapper.find('tr.p-datatable-emptymessage').exists()).toBe(true);
expect(wrapper.find('tr.p-datatable-emptymessage').text()).toBe('Empty Templating');
});
it('should have paginatorstart templating', async () => {
await wrapper.setProps({ value: data, paginator: true, rows: 5 });
expect(wrapper.find('.p-paginator-left-content').exists()).toBe(true);
expect(wrapper.find('.p-paginator-left-content').text()).toBe('Paginator Start Templating');
});
it('should have paginatorend templating', async () => {
await wrapper.setProps({ value: data, paginator: true, rows: 5 });
expect(wrapper.find('.p-paginator-right-content').exists()).toBe(true);
expect(wrapper.find('.p-paginator-right-content').text()).toBe('Paginator End Templating');
});
// column templating
// column grouping
it('should exist', () => {
wrapper = mount({
components: {
DataTable,
ColumnGroup,
Row,
Column
},
template: `
<DataTable :value="sales" responsiveLayout="scroll">
<ColumnGroup type="header">
<Row>
<Column header="Product" :rowspan="3" />
<Column header="Sale Rate" :colspan="4" />
</Row>
<Row>
<Column header="Sales" :colspan="2" />
<Column header="Profits" :colspan="2" />
</Row>
<Row>
<Column header="Last Year"/>
<Column header="This Year"/>
<Column header="Last Year"/>
<Column header="This Year"/>
</Row>
</ColumnGroup>
<ColumnGroup type="footer">
<Row>
<Column footer="Totals:" :colspan="3"/>
<Column footer="Last Year Total" />
<Column footer="This Year Total" />
</Row>
</ColumnGroup>
</DataTable>
`,
data() {
return {
sales: null
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
created() {
this.sales = [
2022-09-14 11:26:01 +00:00
{ product: 'Bamboo Watch', lastYearSale: 51, thisYearSale: 40, lastYearProfit: 54406, thisYearProfit: 43342 },
{ product: 'Black Watch', lastYearSale: 83, thisYearSale: 9, lastYearProfit: 423132, thisYearProfit: 312122 },
{ product: 'Blue Band', lastYearSale: 38, thisYearSale: 5, lastYearProfit: 12321, thisYearProfit: 8500 }
2022-09-06 12:03:37 +00:00
];
},
methods: {
formatCurrency(value) {
2022-09-14 11:26:01 +00:00
return value.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
2022-09-06 12:03:37 +00:00
}
},
computed: {
lastYearTotal() {
let total = 0;
2022-09-14 11:26:01 +00:00
for (let sale of this.sales) {
2022-09-06 12:03:37 +00:00
total += sale.lastYearProfit;
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return this.formatCurrency(total);
},
thisYearTotal() {
let total = 0;
2022-09-14 11:26:01 +00:00
for (let sale of this.sales) {
2022-09-06 12:03:37 +00:00
total += sale.thisYearProfit;
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return this.formatCurrency(total);
}
}
});
expect(wrapper.find('.p-datatable').classes()).toContain('p-datatable-grouped-header');
expect(wrapper.find('.p-datatable').classes()).toContain('p-datatable-grouped-footer');
const headerRows = wrapper.findAll('.p-datatable-thead > tr');
expect(headerRows.length).toEqual(3);
expect(headerRows[0].findAll('th')[0].attributes().rowspan).toEqual('3');
expect(headerRows[0].findAll('th')[1].attributes().colspan).toEqual('4');
expect(headerRows[0].findAll('th')[0].text()).toEqual('Product');
expect(headerRows[0].findAll('th')[1].text()).toEqual('Sale Rate');
expect(headerRows[1].findAll('th')[0].attributes().colspan).toEqual('2');
expect(headerRows[1].findAll('th')[1].attributes().colspan).toEqual('2');
expect(headerRows[1].findAll('th')[0].text()).toEqual('Sales');
expect(headerRows[1].findAll('th')[1].text()).toEqual('Profits');
expect(headerRows[2].findAll('th')[0].text()).toEqual('Last Year');
expect(headerRows[2].findAll('th')[1].text()).toEqual('This Year');
expect(headerRows[2].findAll('th')[2].text()).toEqual('Last Year');
expect(headerRows[2].findAll('th')[3].text()).toEqual('This Year');
const footerRows = wrapper.findAll('.p-datatable-tfoot > tr');
expect(footerRows.length).toEqual(1);
expect(footerRows[0].findAll('td')[0].attributes().colspan).toEqual('3');
expect(footerRows[0].findAll('td')[1].text()).toEqual('Last Year Total');
expect(footerRows[0].findAll('td')[2].text()).toEqual('This Year Total');
});
// sorting
it('should single sort', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData
},
slots: {
default: `
<Column field="code" header="Code" sortable></Column>
<Column field="name" header="Name" sortable></Column>
`
}
});
const sortableTH = wrapper.findAll('.p-sortable-column')[0];
const firstCellText = wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].text();
2022-12-08 14:13:16 +00:00
const headerClick = vi.spyOn(wrapper.vm, 'onColumnHeaderClick');
2022-09-06 12:03:37 +00:00
await sortableTH.trigger('click');
expect(headerClick).toHaveBeenCalled();
expect(sortableTH.classes()).toContain('p-highlight');
expect(wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].text()).not.toEqual(firstCellText);
expect(wrapper.emitted()['update:sortField'][0]).toEqual(['code']);
expect(wrapper.emitted()['value-change'][0]).not.toBeNull();
});
it('should multiple sort', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
sortMode: 'multiple'
},
slots: {
default: `
<Column field="code" header="Code" sortable></Column>
<Column field="name" header="Name" sortable></Column>
`
}
});
const sortableTHs = wrapper.findAll('.p-sortable-column');
const firstCellText = wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].text();
2022-12-08 14:13:16 +00:00
const headerClick = vi.spyOn(wrapper.vm, 'onColumnHeaderClick');
2022-09-06 12:03:37 +00:00
await sortableTHs[0].trigger('click');
expect(headerClick).toHaveBeenCalled();
expect(wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].text()).not.toEqual(firstCellText);
const seconCellText = wrapper.findAll('.p-datatable-tbody > tr')[1].findAll('td')[1].text();
await sortableTHs[1].trigger('click');
expect(headerClick).toHaveBeenCalled();
expect(sortableTHs[1].classes()).toContain('p-highlight');
expect(wrapper.emitted()['update:multiSortMeta'][0]).toEqual([[{ field: 'code', order: 1 }]]);
expect(wrapper.emitted()['update:multiSortMeta'][1]).toEqual([[{ field: 'name', order: 1 }]]);
expect(wrapper.emitted()['value-change'][0]).not.toEqual(wrapper.emitted()['value-change'][1]);
expect(wrapper.findAll('.p-datatable-tbody > tr')[1].findAll('td')[1].text()).not.toEqual(seconCellText);
});
it('should have presort', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
sortOrder: -1,
sortField: 'code'
},
slots: {
default: `
<Column field="code" header="Code" sortable></Column>
<Column field="name" header="Name" sortable></Column>
`
}
});
const presortedTH = wrapper.findAll('.p-sortable-column')[0];
expect(wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].text()).not.toEqual('Game Controller');
expect(presortedTH.classes()).toContain('p-highlight');
});
it('should remove sort', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
removableSort: true
},
slots: {
default: `
<Column field="code" header="Code" sortable></Column>
<Column field="name" header="Name" sortable></Column>
`
}
});
const sortableTH = wrapper.findAll('.p-sortable-column')[0];
const firstCellText = wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].text();
await sortableTH.trigger('click');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
expect(wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].text()).not.toEqual(firstCellText);
expect(sortableTH.attributes()['aria-sort']).toBe('ascending');
const updatedFirstCellText = wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].text();
await sortableTH.trigger('click');
expect(wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].text()).not.toEqual(updatedFirstCellText);
expect(sortableTH.attributes()['aria-sort']).toBe('descending');
const latestUpdatedFirstCellText = wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].text();
await sortableTH.trigger('click');
expect(wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].text()).not.toEqual(latestUpdatedFirstCellText);
expect(sortableTH.attributes()['aria-sort']).toBe('none');
});
// filtering
it('should filtered globally', async () => {
2022-09-14 11:26:01 +00:00
await wrapper.setProps({ filters: { global: { value: 'b', matchMode: FilterMatchMode.STARTS_WITH } } });
2022-09-06 12:03:37 +00:00
await wrapper.vm.filter(smallData);
expect(wrapper.emitted().filter[0][0].filteredValue.length).not.toEqual(3);
});
it('should filtered with menu display', async () => {
2022-09-14 11:26:01 +00:00
await wrapper.setProps({ filters: { name: { value: 'b', matchMode: FilterMatchMode.STARTS_WITH } }, filterDisplay: 'menu', globalFilterFields: ['name'] });
2022-09-06 12:03:37 +00:00
await wrapper.vm.filter(smallData);
expect(wrapper.emitted()['filter'][0][0].filteredValue.length).not.toEqual(3);
expect(wrapper.emitted()['value-change'][0][0].length).not.toEqual(3);
});
it('should filtered with row display', async () => {
2022-09-14 11:26:01 +00:00
await wrapper.setProps({ filters: { name: { value: 'b', matchMode: FilterMatchMode.STARTS_WITH } }, filterDisplay: 'row', globalFilterFields: ['name'] });
2022-09-06 12:03:37 +00:00
await wrapper.vm.filter(smallData);
expect(wrapper.emitted()['filter'][0][0].filteredValue.length).not.toEqual(3);
expect(wrapper.emitted()['value-change'][0][0].length).not.toEqual(3);
});
// selection
it('should single select', async () => {
2022-09-14 11:26:01 +00:00
await wrapper.setProps({ selection: null, selectionMode: 'single' });
2022-09-06 12:03:37 +00:00
await wrapper.vm.onRowClick({
2023-03-02 14:45:27 +00:00
originalEvent: { target: wrapper.findAll('tr.p-selectable-row')[0].element },
2022-09-06 12:03:37 +00:00
data: smallData[0],
index: 0
});
expect(wrapper.emitted()['row-click'][0][0].index).toEqual(0);
expect(wrapper.emitted()['update:selection'][0][0]).toEqual(smallData[0]);
expect(wrapper.emitted()['row-select'][0][0].index).toEqual(0);
});
it('should multiple selection with meta key', async () => {
2022-09-14 11:26:01 +00:00
await wrapper.setProps({ selection: null, selectionMode: 'multiple' });
2022-09-06 12:03:37 +00:00
await wrapper.vm.onRowClick({
2023-03-02 14:45:27 +00:00
originalEvent: { shiftKey: true, target: wrapper.findAll('tr.p-selectable-row')[0].element },
2022-09-06 12:03:37 +00:00
data: smallData[0],
index: 0
});
await wrapper.vm.onRowClick({
2023-03-02 14:45:27 +00:00
originalEvent: { shiftKey: true, target: wrapper.findAll('tr.p-selectable-row')[1].element },
2022-09-06 12:03:37 +00:00
data: smallData[1],
index: 1
});
expect(wrapper.emitted()['row-click'][0][0].index).toEqual(0);
expect(wrapper.emitted()['row-click'][1][0].index).toEqual(1);
expect(wrapper.emitted()['update:selection'][1][0]).toEqual([smallData[0], smallData[1]]);
});
it('should multiple selection without meta key', async () => {
2022-09-14 11:26:01 +00:00
await wrapper.setProps({ selection: null, selectionMode: 'multiple', metaKeySelection: false });
2022-09-06 12:03:37 +00:00
await wrapper.vm.onRowClick({
2023-03-02 14:45:27 +00:00
originalEvent: { target: wrapper.findAll('tr.p-selectable-row')[0].element },
2022-09-06 12:03:37 +00:00
data: smallData[0],
index: 0
});
await wrapper.vm.onRowClick({
2023-03-02 14:45:27 +00:00
originalEvent: { target: wrapper.findAll('tr.p-selectable-row')[1].element },
2022-09-06 12:03:37 +00:00
data: smallData[1],
index: 1
});
expect(wrapper.emitted()['row-click'][0][0].index).toEqual(0);
expect(wrapper.emitted()['row-click'][1][0].index).toEqual(1);
expect(wrapper.emitted()['update:selection'][0][0]).toEqual([smallData[0]]);
expect(wrapper.emitted()['update:selection'][1][0]).toEqual([smallData[1]]);
expect(wrapper.emitted()['row-select'][0][0].index).toBe(0);
expect(wrapper.emitted()['row-select'][1][0].index).toBe(1);
});
// radio selection
it('should select when radiobutton selection is enabled', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
selection: null
},
slots: {
default: `
<Column selectionMode="single" />
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`
}
});
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
expect(wrapper.findAll('td.p-selection-column').length).toBe(3);
expect(wrapper.findAll('.p-radiobutton').length).toBe(3);
2022-09-14 11:26:01 +00:00
await wrapper.vm.toggleRowWithRadio({ originalEvent: {}, data: smallData[0], index: 0 });
2022-09-06 12:03:37 +00:00
expect(wrapper.emitted()['update:selection'][0][0]).toEqual(smallData[0]);
expect(wrapper.emitted()['row-select'][0][0].index).toBe(0);
2022-09-14 11:26:01 +00:00
await wrapper.vm.toggleRowWithRadio({ originalEvent: {}, data: smallData[1], index: 1 });
2022-09-06 12:03:37 +00:00
expect(wrapper.emitted()['update:selection'][1][0]).toEqual(smallData[1]);
expect(wrapper.emitted()['row-select'][1][0].index).toBe(1);
});
// checkbox selection
it('should select when checkbox selection is enabled', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
selection: null
},
slots: {
default: `
<Column selectionMode="multiple" />
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`
}
});
expect(wrapper.findAll('.p-checkbox').length).toBe(4);
2022-09-14 11:26:01 +00:00
await wrapper.vm.toggleRowWithCheckbox({ originalEvent: {}, data: smallData[0], index: 0 });
2022-09-06 12:03:37 +00:00
expect(wrapper.emitted()['update:selection'][0][0]).toEqual([smallData[0]]);
expect(wrapper.emitted()['row-select'][0][0].index).toBe(0);
2022-09-14 11:26:01 +00:00
await wrapper.vm.toggleRowWithCheckbox({ originalEvent: {}, data: smallData[1], index: 1 });
2022-09-06 12:03:37 +00:00
expect(wrapper.emitted()['update:selection'][1][0]).toEqual([smallData[1]]);
expect(wrapper.emitted()['row-select'][1][0].index).toBe(1);
});
it('should select all rows', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
selection: null
},
slots: {
default: `
<Column selectionMode="multiple" />
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`
}
});
2022-09-14 11:26:01 +00:00
await wrapper.vm.toggleRowsWithCheckbox({ originalEvent: {}, checked: true });
2022-09-06 12:03:37 +00:00
expect(wrapper.emitted()['row-select-all'][0][0].data).toEqual(smallData);
expect(wrapper.emitted()['update:selection'][0][0]).toEqual(smallData);
});
it('should unselect all rows', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
selection: smallData
},
slots: {
default: `
<Column selectionMode="multiple" />
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`
}
});
2022-09-14 11:26:01 +00:00
await wrapper.vm.toggleRowsWithCheckbox({ originalEvent: {}, checked: false });
2022-09-06 12:03:37 +00:00
expect(wrapper.emitted()['row-unselect-all'][0][0].data).toBe(undefined);
expect(wrapper.emitted()['update:selection'][0][0]).toEqual([]);
});
// scrolling
it('should scrolling', async () => {
2022-09-14 11:26:01 +00:00
await wrapper.setProps({ scrollable: true });
2022-09-06 12:03:37 +00:00
expect(wrapper.find('.p-datatable-scrollable').exists()).toBe(true);
});
it('should vertical scroll', async () => {
2022-09-14 11:26:01 +00:00
await wrapper.setProps({ scrollable: true, scrollHeight: '100px' });
2022-09-06 12:03:37 +00:00
2023-08-31 09:46:08 +00:00
expect(wrapper.find('.p-datatable-wrapper').attributes().style).toBe('overflow: auto; max-height: 100px;');
2022-09-06 12:03:37 +00:00
});
it('should flex scrolling', async () => {
2022-09-14 11:26:01 +00:00
await wrapper.setProps({ scrollable: true, scrollHeight: 'flex' });
2022-09-06 12:03:37 +00:00
expect(wrapper.find('.p-datatable-flex-scrollable').exists()).toBe(true);
});
it('should have frozen rows', async () => {
2022-09-14 11:26:01 +00:00
await wrapper.setProps({ frozenValue: [smallData[0]], scrollable: true, scrollHeight: '100px', scrollDirection: 'both' });
2022-09-06 12:03:37 +00:00
expect(wrapper.findAll('.p-datatable-tbody')[0].classes()).toContain('p-datatable-frozen-tbody');
expect(wrapper.findAll('.p-datatable-tbody')[0].attributes().style).toBe('top: 0px;');
});
// frozen columns
it('should have frozen columns', () => {
wrapper = null;
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
selection: null,
scrollable: true,
scrollDirection: 'both'
},
slots: {
default: `
<Column field="code" header="Code" frozen></Column>
<Column field="name" header="Name"></Column>
`
}
});
expect(wrapper.find('th.p-frozen-column').exists()).toBe(true);
// expect(wrapper.find('th.p-frozen-column').attributes().style).toBe('left: 0px;');
expect(wrapper.findAll('td.p-frozen-column').length).toBe(3);
// expect(wrapper.findAll('td.p-frozen-column')[0].attributes().style).toBe('left: 0px;');
});
// lazy loading
// row expansion
it('should have row toggler', () => {
expect(wrapper.findAll('.p-row-toggler').length).toBe(3);
});
it('should expand a row', async () => {
await wrapper.setProps({ expandedRows: [] });
2022-09-14 11:26:01 +00:00
await wrapper.vm.toggleRow({ originalEvent: {}, data: smallData[0] });
2022-09-06 12:03:37 +00:00
expect(wrapper.emitted()['update:expandedRows'][0][0]).toEqual([smallData[0]]);
expect(wrapper.emitted()['row-expand'][0][0].data).toEqual(smallData[0]);
});
it('should collapse a row', async () => {
await wrapper.setProps({ expandedRows: [smallData[0]] });
2022-09-14 11:26:01 +00:00
await wrapper.vm.toggleRow({ originalEvent: {}, data: smallData[0] });
2022-09-06 12:03:37 +00:00
expect(wrapper.emitted()['update:expandedRows'][0][0]).toEqual([]);
expect(wrapper.emitted()['row-collapse'][0][0].data).toEqual(smallData[0]);
});
// editing
// cell editing
// row editing
it('should init row editing', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column,
InputText
}
},
props: {
value: smallData,
editMode: 'row',
editingRows: []
},
slots: {
default: `
<Column field="code" header="Code">
<template #editor="{ data, field }">
<InputText v-model="data[field]" autofocus />
</template>
</Column>
<Column field="name" header="Name">
<template #editor="{ data, field }">
<InputText v-model="data[field]" autofocus />
</template>
</Column>
<Column :rowEditor="true" />
`
}
});
expect(wrapper.findAll('.p-editable-column').length).toBe(6);
expect(wrapper.findAll('.p-row-editor-init').length).toBe(3);
await wrapper.vm.onRowEditInit({ data: smallData[0] });
expect(wrapper.emitted()['update:editingRows'][0][0]).toEqual([smallData[0]]);
expect(wrapper.emitted()['row-edit-init'][0][0].data).toEqual(smallData[0]);
expect(wrapper.findAll('.p-datatable-tbody > tr > td')[wrapper.findAll('.p-datatable-tbody > tr > td').length - 1].find('.p-row-editor-init').exists()).toBe(true);
});
it('should save row editing', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column,
InputText
}
},
props: {
value: smallData,
editMode: 'row',
editingRows: []
},
slots: {
default: `
<Column field="code" header="Code">
<template #editor="{ data, field }">
<InputText v-model="data[field]" autofocus />
</template>
</Column>
<Column field="name" header="Name">
<template #editor="{ data, field }">
<InputText v-model="data[field]" autofocus />
</template>
</Column>
<Column :rowEditor="true" />
`
}
});
2022-09-14 11:26:01 +00:00
await wrapper.vm.onRowEditSave({ data: { id: '9999', code: 'vbb124btr', name: 'Game Controller' } });
2022-09-06 12:03:37 +00:00
expect(wrapper.emitted()['update:editingRows'][0][0]).toEqual([]);
2022-09-14 11:26:01 +00:00
expect(wrapper.emitted()['row-edit-save'][0][0].data).toEqual({ id: '9999', code: 'vbb124btr', name: 'Game Controller' });
2022-09-06 12:03:37 +00:00
});
it('should cancel row editing', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column,
InputText
}
},
props: {
value: smallData,
editMode: 'row',
editingRows: []
},
slots: {
default: `
<Column field="code" header="Code">
<template #editor="{ data, field }">
<InputText v-model="data[field]" autofocus />
</template>
</Column>
<Column field="name" header="Name">
<template #editor="{ data, field }">
<InputText v-model="data[field]" autofocus />
</template>
</Column>
<Column :rowEditor="true" />
`
}
});
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
await wrapper.vm.onRowEditCancel({ data: smallData[0] });
expect(wrapper.emitted()['update:editingRows'][0][0]).toEqual([]);
expect(wrapper.emitted()['row-edit-cancel'][0][0].data).toEqual(smallData[0]);
});
// column resize
it('should fit mode expanding exists', () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
resizableColumns: true,
columnResizeMode: 'fit'
},
slots: {
default: `
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`
}
});
expect(wrapper.find('.p-datatable.p-component').classes()).toContain('p-datatable-resizable');
expect(wrapper.find('.p-datatable.p-component').classes()).toContain('p-datatable-resizable-fit');
expect(wrapper.findAll('.p-column-resizer').length).toBe(2);
});
it('should fit mode resize start', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
resizableColumns: true,
columnResizeMode: 'fit'
},
slots: {
default: `
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`
}
});
const resizer = wrapper.findAll('.p-column-resizer')[0];
2022-09-14 11:26:01 +00:00
await wrapper.vm.onColumnResizeStart({ target: resizer.element });
2022-09-06 12:03:37 +00:00
expect(wrapper.componentVM.columnResizing).toBe(true);
expect(wrapper.find('.p-column-resizer-helper').attributes().style).toContain('display: none;');
});
it('should fit mode resize', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
resizableColumns: true,
columnResizeMode: 'fit'
},
slots: {
default: `
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`
}
});
await wrapper.vm.onColumnResize({});
expect(wrapper.find('.p-column-resizer-helper').attributes().style).toContain('display: block;');
});
it('should fit mode column resize end', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
resizableColumns: true,
columnResizeMode: 'fit'
},
slots: {
default: `
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`
}
});
const resizer = wrapper.findAll('.p-column-resizer')[0];
2022-09-14 11:26:01 +00:00
await wrapper.vm.onColumnResizeStart({ target: resizer.element });
2022-09-06 12:03:37 +00:00
await wrapper.vm.onColumnResizeEnd();
expect(wrapper.find('.p-column-resizer-helper').attributes().style).toContain('display: none;');
});
it('should expand mode resize start', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
resizableColumns: true,
columnResizeMode: 'expand'
},
slots: {
default: `
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`
}
});
const resizer = wrapper.findAll('.p-column-resizer')[0];
2022-09-14 11:26:01 +00:00
await wrapper.vm.onColumnResizeStart({ target: resizer.element });
2022-09-06 12:03:37 +00:00
expect(wrapper.componentVM.columnResizing).toBe(true);
expect(wrapper.find('.p-column-resizer-helper').attributes().style).toContain('display: none;');
});
it('should fit mode resize', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
resizableColumns: true,
columnResizeMode: 'expand'
},
slots: {
default: `
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`
}
});
await wrapper.vm.onColumnResize({});
expect(wrapper.find('.p-column-resizer-helper').attributes().style).toContain('display: block;');
});
it('should fit mode column resize end', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
resizableColumns: true,
columnResizeMode: 'expand'
},
slots: {
default: `
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`
}
});
const resizer = wrapper.findAll('.p-column-resizer')[0];
2022-09-14 11:26:01 +00:00
await wrapper.vm.onColumnResizeStart({ target: resizer.element });
2022-09-06 12:03:37 +00:00
await wrapper.vm.onColumnResizeEnd();
expect(wrapper.find('.p-column-resizer-helper').attributes().style).toContain('display: none;');
});
// column reorder
it('should reorder columns', async () => {
await wrapper.setProps({ reorderableColumns: true });
expect(wrapper.find('.p-datatable-reorder-indicator-up').exists()).toBe(true);
expect(wrapper.find('.p-datatable-reorder-indicator-down').exists()).toBe(true);
});
// row reorder
it('should exist', () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData
},
slots: {
default: `
<Column :rowReorder="true" />
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`
}
});
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
expect(wrapper.findAll('.p-datatable-reorderablerow-handle').length).toBe(3);
});
// row group
// subheader grouping
it('should exist', () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
rowGroupMode: 'subheader',
groupRowsBy: 'name',
sortMode: 'single',
sortField: 'name',
sortOrder: 1,
scrollable: true
},
slots: {
default: `
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`,
groupheader: `GroupHeader Templating`,
groupfooter: `GroupFooter Templating`
}
});
expect(wrapper.find('.p-datatable-tbody').attributes().role).toBe('rowgroup');
expect(wrapper.findAll('.p-column-header-content').length).toBe(1);
expect(wrapper.find('.p-column-header-content').text()).toBe('Code');
});
it('should have groupheader templating', () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
rowGroupMode: 'subheader',
groupRowsBy: 'name',
sortMode: 'single',
sortField: 'name',
sortOrder: 1,
scrollable: true
},
slots: {
default: `
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`,
groupheader: `GroupHeader Templating`,
groupfooter: `GroupFooter Templating`
}
});
expect(wrapper.findAll('.p-rowgroup-header').length).toBe(3);
expect(wrapper.find('.p-rowgroup-header').text()).toBe('GroupHeader Templating');
});
it('should have groupfooter templating', () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: smallData,
rowGroupMode: 'subheader',
groupRowsBy: 'name',
sortMode: 'single',
sortField: 'name',
sortOrder: 1,
scrollable: true
},
slots: {
default: `
<Column field="code" header="Code"></Column>
<Column field="name" header="Name"></Column>
`,
groupheader: `GroupHeader Templating`,
groupfooter: `GroupFooter Templating`
}
});
expect(wrapper.findAll('.p-rowgroup-header').length).toBe(3);
expect(wrapper.find('.p-rowgroup-footer').text()).toBe('GroupFooter Templating');
});
it('should have expandable row groups and expand rows', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: [
{
2022-09-14 11:26:01 +00:00
id: 1000,
name: 'James Butt',
country: {
name: 'Algeria',
code: 'dz'
2022-09-06 12:03:37 +00:00
},
2022-09-14 11:26:01 +00:00
company: 'Benton, John B Jr',
representative: {
name: 'Ioni Bowcher',
image: 'ionibowcher.png'
2022-09-06 12:03:37 +00:00
}
},
{
2022-09-14 11:26:01 +00:00
id: 1001,
name: 'Josephine Darakjy',
country: {
name: 'Egypt',
code: 'eg'
2022-09-06 12:03:37 +00:00
},
2022-09-14 11:26:01 +00:00
company: 'Chanay, Jeffrey A Esq',
representative: {
name: 'Amy Elsner',
image: 'amyelsner.png'
2022-09-06 12:03:37 +00:00
}
},
{
2022-09-14 11:26:01 +00:00
id: 1013,
name: 'Graciela Ruta',
country: {
name: 'Chile',
code: 'cl'
2022-09-06 12:03:37 +00:00
},
2022-09-14 11:26:01 +00:00
company: 'Buckley Miller & Wright',
representative: {
name: 'Amy Elsner',
image: 'amyelsner.png'
2022-09-06 12:03:37 +00:00
}
},
{
2022-09-14 11:26:01 +00:00
id: 1021,
name: 'Veronika Inouye',
country: {
name: 'Ecuador',
code: 'ec'
2022-09-06 12:03:37 +00:00
},
2022-09-14 11:26:01 +00:00
company: 'C 4 Network Inc',
representative: {
name: 'Ioni Bowcher',
image: 'ionibowcher.png'
2022-09-06 12:03:37 +00:00
}
},
{
2022-09-14 11:26:01 +00:00
id: 1026,
name: 'Chanel Caudy',
country: {
name: 'Argentina',
code: 'ar'
2022-09-06 12:03:37 +00:00
},
2022-09-14 11:26:01 +00:00
company: 'Professional Image Inc',
representative: {
name: 'Ioni Bowcher',
image: 'ionibowcher.png'
2022-09-06 12:03:37 +00:00
}
},
{
2022-09-14 11:26:01 +00:00
id: 1027,
name: 'Ezekiel Chui',
country: {
name: 'Ireland',
code: 'ie'
2022-09-06 12:03:37 +00:00
},
2022-09-14 11:26:01 +00:00
company: 'Sider, Donald C Esq',
representative: {
name: 'Amy Elsner',
image: 'amyelsner.png'
2022-09-06 12:03:37 +00:00
}
}
],
rowGroupMode: 'subheader',
groupRowsBy: 'representative.name',
sortMode: 'single',
sortField: 'representative.name',
sortOrder: 1,
scrollable: true,
expandableRowGroups: true,
expandedRowGroups: null,
responsiveLayout: 'scroll'
},
slots: {
default: `
<Column field="representative.name" header="Representative"></Column>
<Column field="name" header="Name"></Column>
<Column field="country" header="Country"></Column>
<Column field="company" header="Company"></Column>
`,
groupheader: `
<template #groupheader="slotProps">
<span class="image-text">{{slotProps.data.representative.name}}</span>
</template>
`
}
});
expect(wrapper.findAll('.p-datatable-thead > tr > th').length).toBe(3);
expect(wrapper.findAll('.p-datatable-tbody > tr.p-rowgroup-header').length).toBe(2);
expect(wrapper.findAll('.p-datatable-tbody > tr.p-rowgroup-header')[0].text()).toBe('Amy Elsner');
const firstToggler = wrapper.findAll('.p-row-toggler')[0];
await firstToggler.trigger('click');
expect(wrapper.emitted()['update:expandedRowGroups'][0]).toEqual([['Amy Elsner']]);
expect(wrapper.emitted()['rowgroup-expand'][0][0].data).toBe('Amy Elsner');
});
it('should have rowspan grouping', async () => {
wrapper = mount(DataTable, {
global: {
2022-12-08 11:04:25 +00:00
plugins: [PrimeVue],
2022-09-06 12:03:37 +00:00
components: {
Column
}
},
props: {
value: [
{
2022-09-14 11:26:01 +00:00
id: '1000',
code: 'vbb124btr',
name: 'Game Controller'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1001',
code: 'nvklal433',
name: 'Black Watch'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1002',
code: 'zz21cz3c1',
name: 'Blue Band'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1003',
code: 'vbb124btrvbb124btr',
name: 'Game Controller'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1004',
code: 'nvklal433nvklal433',
name: 'Black Watch'
2022-09-06 12:03:37 +00:00
},
{
2022-09-14 11:26:01 +00:00
id: '1006',
code: 'zz21cz3c1zz21cz3c1',
name: 'Blue Band'
2022-09-06 12:03:37 +00:00
}
],
rowGroupMode: 'rowspan',
groupRowsBy: 'name',
sortMode: 'single',
sortField: 'name',
sortOrder: 1,
scrollable: true,
responsiveLayout: 'scroll'
},
slots: {
default: `
<Column header="#">
<template #body="slotProps">
{{slotProps.index}}
</template>
</Column>
<Column field="name" header="Name"></Column>
<Column field="code" header="Code"></Column>
`
}
});
expect(wrapper.find('.p-datatable-thead > tr').findAll('th')[0].text()).toBe('#');
expect(wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[0].text()).toBe('0');
expect(wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].text()).toBe('Black Watch');
expect(wrapper.findAll('.p-datatable-tbody > tr')[0].findAll('td')[1].attributes().rowspan).toBe('2');
expect(wrapper.findAll('.p-datatable-tbody > tr')[2].findAll('td')[1].attributes().rowspan).toBe('2');
expect(wrapper.findAll('.p-datatable-tbody > tr')[4].findAll('td')[1].attributes().rowspan).toBe('2');
});
// export
2023-04-05 23:06:03 +00:00
/* it('should export table', async () => {
2022-12-08 14:13:16 +00:00
const exportCSV = vi.spyOn(wrapper.vm, 'exportCSV');
2022-09-06 12:03:37 +00:00
await wrapper.vm.exportCSV();
expect(exportCSV).toHaveBeenCalled();
2023-04-05 23:06:03 +00:00
}); */
2022-09-06 12:03:37 +00:00
// state
it('should get storage', async () => {
await wrapper.setProps({ stateStorage: 'session', stateKey: 'dt-state-demo-session', paginator: true });
await wrapper.vm.getStorage();
expect(wrapper.emitted()['state-save'][0]).not.toBeNull();
});
it('should save session storage', async () => {
2022-12-08 14:13:16 +00:00
vi.spyOn(window.sessionStorage.__proto__, 'setItem');
window.sessionStorage.__proto__.setItem = vi.fn();
2022-09-06 12:03:37 +00:00
await wrapper.vm.saveState();
expect(sessionStorage.setItem).toHaveBeenCalled();
expect(wrapper.emitted()['state-save'][0]).not.toBeNull();
});
it('should save local storage', async () => {
2022-12-08 14:13:16 +00:00
vi.spyOn(window.localStorage.__proto__, 'setItem');
window.localStorage.__proto__.setItem = vi.fn();
2022-09-06 12:03:37 +00:00
await wrapper.vm.saveState();
expect(localStorage.setItem).toHaveBeenCalled();
expect(wrapper.emitted()['state-save'][0]).not.toBeNull();
});
// contextmenu
// responsive
2023-02-28 11:51:50 +00:00
it('should have stack layout', async () => {
await wrapper.setProps({ responsiveLayout: 'stack' });
2022-09-06 12:03:37 +00:00
expect(wrapper.find('.p-datatable').classes()).toContain('p-datatable-responsive-stack');
});
2023-02-28 11:51:50 +00:00
it('should have scroll layout', () => {
2022-09-06 12:03:37 +00:00
expect(wrapper.find('.p-datatable').classes()).toContain('p-datatable-responsive-scroll');
});
// row styling
2022-09-14 11:26:01 +00:00
});