|
| 1 | +import { fixture, expect } from '@open-wc/testing'; |
| 2 | +import type { TemplateResult } from 'lit'; |
| 3 | + |
| 4 | +import { html } from 'lit/static-html.js'; |
| 5 | + |
| 6 | +import type { PharosTable } from './pharos-table'; |
| 7 | +import type { PharosLink } from '../link/pharos-link'; |
| 8 | +import type { PharosPagination } from '../pagination/pharos-pagination'; |
| 9 | +import type { PharosSelect } from '../select/pharos-select'; |
| 10 | + |
| 11 | +describe('pharos-table', () => { |
| 12 | + let component: PharosTable, componentWithPagination: PharosTable; |
| 13 | + const columns = [ |
| 14 | + { |
| 15 | + name: 'Item', |
| 16 | + field: 'item', |
| 17 | + }, |
| 18 | + { |
| 19 | + name: 'Filename', |
| 20 | + field: 'filename', |
| 21 | + }, |
| 22 | + ]; |
| 23 | + const rowData = [ |
| 24 | + { |
| 25 | + item: 1, |
| 26 | + filename: '12345.jpg', |
| 27 | + expired_date: '2020-1-1', |
| 28 | + created_on: '2010-1-1', |
| 29 | + university: 'University of Michigan', |
| 30 | + }, |
| 31 | + { |
| 32 | + item: 2, |
| 33 | + filename: '123456.jpg', |
| 34 | + expired_date: '2020-1-1', |
| 35 | + created_on: '2010-1-1', |
| 36 | + university: 'University of Michigan', |
| 37 | + }, |
| 38 | + ]; |
| 39 | + |
| 40 | + beforeEach(async () => { |
| 41 | + component = await fixture(html` |
| 42 | + <test-pharos-table |
| 43 | + .columns="${columns}" |
| 44 | + .rowData="${rowData}" |
| 45 | + .totalResults="${2}" |
| 46 | + caption="test table" |
| 47 | + > |
| 48 | + </test-pharos-table> |
| 49 | + `); |
| 50 | + |
| 51 | + componentWithPagination = await fixture(html` |
| 52 | + <test-pharos-table |
| 53 | + .columns="${columns}" |
| 54 | + .rowData="${rowData}" |
| 55 | + .showPagination="${true}" |
| 56 | + .totalResults="${2}" |
| 57 | + .pageSizeOptions="${[1, 2]}" |
| 58 | + caption="test table with pagination" |
| 59 | + > |
| 60 | + </test-pharos-table> |
| 61 | + `); |
| 62 | + }); |
| 63 | + |
| 64 | + it('is accessible', async () => { |
| 65 | + await expect(component).to.be.accessible(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('is accessible with pagination', async () => { |
| 69 | + await expect(componentWithPagination).to.be.accessible(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('has the correct number of rows', async () => { |
| 73 | + const rows = Array.prototype.slice.call( |
| 74 | + component.renderRoot.querySelectorAll(`tr`) |
| 75 | + ) as TemplateResult[]; |
| 76 | + expect(rows.length).to.be.eq(3); |
| 77 | + }); |
| 78 | + |
| 79 | + it('renders rows according to page size', async () => { |
| 80 | + const rows = Array.prototype.slice.call( |
| 81 | + componentWithPagination.renderRoot.querySelectorAll(`tr`) |
| 82 | + ) as TemplateResult[]; |
| 83 | + expect(rows.length).to.be.eq(2); |
| 84 | + }); |
| 85 | + |
| 86 | + it('shows correct page start number according to page size', async () => { |
| 87 | + let pageNumber: HTMLElement | null = |
| 88 | + componentWithPagination.renderRoot.querySelector(`.page-number-display`); |
| 89 | + expect(pageNumber?.innerText).contains('Displaying 1-1 of 2'); |
| 90 | + |
| 91 | + const selectDropdown = componentWithPagination.renderRoot.querySelector( |
| 92 | + `pharos-select` |
| 93 | + ) as PharosSelect; |
| 94 | + selectDropdown['_select'].value = '2'; |
| 95 | + selectDropdown['_select'].dispatchEvent(new Event('change')); |
| 96 | + |
| 97 | + await componentWithPagination.updateComplete; |
| 98 | + |
| 99 | + pageNumber = componentWithPagination.renderRoot.querySelector(`.page-number-display`); |
| 100 | + expect(pageNumber?.innerText).contains('Displaying 1-2 of 2'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('updates correctly after page size selection', async () => { |
| 104 | + let rows = Array.prototype.slice.call( |
| 105 | + componentWithPagination.renderRoot.querySelectorAll(`tr`) |
| 106 | + ) as TemplateResult[]; |
| 107 | + expect(rows.length).to.be.eq(2); |
| 108 | + |
| 109 | + const selectDropdown = componentWithPagination.renderRoot.querySelector( |
| 110 | + `pharos-select` |
| 111 | + ) as PharosSelect; |
| 112 | + selectDropdown['_select'].value = '2'; |
| 113 | + selectDropdown['_select'].dispatchEvent(new Event('change')); |
| 114 | + |
| 115 | + await componentWithPagination.updateComplete; |
| 116 | + |
| 117 | + rows = Array.prototype.slice.call( |
| 118 | + componentWithPagination.renderRoot.querySelectorAll(`tr`) |
| 119 | + ) as TemplateResult[]; |
| 120 | + expect(rows.length).to.be.eq(3); |
| 121 | + }); |
| 122 | + |
| 123 | + it('fires a custom event when going to previous and next page', async () => { |
| 124 | + let prevWasFired = false; |
| 125 | + let nextWasFired = false; |
| 126 | + const handlePrevPage = (): void => { |
| 127 | + prevWasFired = true; |
| 128 | + }; |
| 129 | + const handleNextPage = (): void => { |
| 130 | + nextWasFired = true; |
| 131 | + }; |
| 132 | + componentWithPagination.addEventListener('pharos-table-prev-page', handlePrevPage); |
| 133 | + componentWithPagination.addEventListener('pharos-table-next-page', handleNextPage); |
| 134 | + |
| 135 | + const pagination = componentWithPagination.renderRoot.querySelector( |
| 136 | + `pharos-pagination` |
| 137 | + ) as PharosPagination; |
| 138 | + const nextPageLink = pagination.renderRoot.querySelector(`.next`) as PharosLink; |
| 139 | + nextPageLink.click(); |
| 140 | + await componentWithPagination.updateComplete; |
| 141 | + |
| 142 | + expect(nextWasFired).to.be.true; |
| 143 | + |
| 144 | + const prevPageLink = pagination.renderRoot.querySelector(`.prev`) as PharosLink; |
| 145 | + prevPageLink.click(); |
| 146 | + await componentWithPagination.updateComplete; |
| 147 | + |
| 148 | + expect(prevWasFired).to.be.true; |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +it('throws an error if caption is not provided', async () => { |
| 153 | + let errorThrown = false; |
| 154 | + try { |
| 155 | + await fixture( |
| 156 | + html`<test-pharos-table |
| 157 | + .columns="${[]}" |
| 158 | + .rowData="${[]}" |
| 159 | + .showPagination="${true}" |
| 160 | + .totalResults="${2}" |
| 161 | + .pageSizeOptions="${[1, 2]}" |
| 162 | + > |
| 163 | + </test-pharos-table> ` |
| 164 | + ); |
| 165 | + } catch (error) { |
| 166 | + if (error instanceof Error) { |
| 167 | + errorThrown = true; |
| 168 | + expect(error?.message).to.be.equal( |
| 169 | + 'Table must have an accessible name. Please provide a caption for the table using the `caption` attribute. You can hide the caption visually by setting the `hide-caption-visually` property.' |
| 170 | + ); |
| 171 | + } |
| 172 | + } |
| 173 | + expect(errorThrown).to.be.true; |
| 174 | +}); |
0 commit comments