|
1 |
| -import { html, fixture, expect } from '@open-wc/testing'; |
| 1 | +import { |
| 2 | + html, |
| 3 | + fixture, |
| 4 | + expect, |
| 5 | + waitUntil, |
| 6 | + elementUpdated, |
| 7 | +} from '@open-wc/testing'; |
2 | 8 | import { UUIPaginationElement } from './uui-pagination.element';
|
3 | 9 | import '.';
|
4 | 10 |
|
5 | 11 | describe('UUIPaginationElement', () => {
|
6 | 12 | let element: UUIPaginationElement;
|
7 | 13 |
|
8 | 14 | beforeEach(async () => {
|
9 |
| - element = await fixture(html` <uui-pagination></uui-pagination> `); |
| 15 | + element = await fixture( |
| 16 | + html` <uui-pagination .total=${30}></uui-pagination> ` |
| 17 | + ); |
| 18 | + }); |
| 19 | + |
| 20 | + // it('shows the correct number of pages', async () => { |
| 21 | + // // (element.shadowRoot?.querySelector('#group') as HTMLElement).style.width = |
| 22 | + // // '500px'; |
| 23 | + // // await waitUntil(() => element.visiblePages.length > 1); |
| 24 | + // // console.log(element.shadowRoot); |
| 25 | + |
| 26 | + // // const buttons = element.shadowRoot?.querySelector('#group')?.children; |
| 27 | + |
| 28 | + // // console.log(buttons); |
| 29 | + |
| 30 | + // expect(true).to.be.false; |
| 31 | + // }); |
| 32 | + |
| 33 | + it('sets active class on current page', async () => { |
| 34 | + await waitUntil(() => element.visiblePages.length > 1); |
| 35 | + element.current = 2; |
| 36 | + const button = element.shadowRoot?.querySelector('#group')?.children[3]!; |
| 37 | + await elementUpdated(button); |
| 38 | + |
| 39 | + await expect(button).to.have.class('active-button'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('goes to selected page on click', async () => { |
| 43 | + await waitUntil(() => element.visiblePages.length > 1); |
| 44 | + const button = element.shadowRoot?.querySelector('#group') |
| 45 | + ?.children[3]! as HTMLElement; |
| 46 | + button.click(); |
| 47 | + |
| 48 | + await elementUpdated(element); |
| 49 | + |
| 50 | + await expect(button).to.have.class('active-button'); |
| 51 | + await expect(element.current).to.equal(2); |
| 52 | + }); |
| 53 | + |
| 54 | + it('goes to previous page on click', async () => { |
| 55 | + await waitUntil(() => element.visiblePages.length > 1); |
| 56 | + element.current = 2; |
| 57 | + const buttons = element.shadowRoot?.querySelector('#group')?.children; |
| 58 | + const prevButton = buttons![1] as HTMLElement; |
| 59 | + const activeButton = buttons![2] as HTMLElement; |
| 60 | + prevButton.click(); |
| 61 | + |
| 62 | + await elementUpdated(element); |
| 63 | + |
| 64 | + await expect(element.current).to.equal(1); |
| 65 | + await expect(activeButton).to.have.class('active-button'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('goes to next page on click', async () => { |
| 69 | + await waitUntil(() => element.visiblePages.length > 1); |
| 70 | + element.current = 2; |
| 71 | + const buttons = element.shadowRoot?.querySelector('#group')?.children; |
| 72 | + const nextButton = buttons![6] as HTMLElement; |
| 73 | + const activeButton = buttons![3] as HTMLElement; |
| 74 | + nextButton.click(); |
| 75 | + |
| 76 | + await elementUpdated(element); |
| 77 | + |
| 78 | + await expect(element.current).to.equal(3); |
| 79 | + await expect(activeButton).to.have.class('active-button'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('goes to last page on click and disables last and next buttons', async () => { |
| 83 | + await waitUntil(() => element.visiblePages.length > 1); |
| 84 | + let buttons = element.shadowRoot?.querySelector('#group')?.children; |
| 85 | + const lastButton = buttons![7] as HTMLElement; |
| 86 | + const nextButton = buttons![6] as HTMLElement; |
| 87 | + lastButton.click(); |
| 88 | + |
| 89 | + await elementUpdated(element); |
| 90 | + |
| 91 | + buttons = element.shadowRoot?.querySelector('#group')?.children; |
| 92 | + const activeButton = buttons![5] as HTMLElement; |
| 93 | + |
| 94 | + await expect(element.current).to.equal(30); |
| 95 | + await expect(activeButton).to.have.class('active-button'); |
| 96 | + await expect(nextButton).to.have.attribute('disabled'); |
| 97 | + await expect(lastButton).to.have.attribute('disabled'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('goes to first page on click and disables first and previous buttons', async () => { |
| 101 | + await waitUntil(() => element.visiblePages.length > 1); |
| 102 | + element.current = 3; |
| 103 | + const buttons = element.shadowRoot?.querySelector('#group')?.children; |
| 104 | + const firstButton = buttons![0] as HTMLElement; |
| 105 | + const previousButton = buttons![1] as HTMLElement; |
| 106 | + const activeButton = buttons![2] as HTMLElement; |
| 107 | + firstButton.click(); |
| 108 | + |
| 109 | + await elementUpdated(element); |
| 110 | + |
| 111 | + await expect(element.current).to.equal(1); |
| 112 | + await expect(activeButton).to.have.class('active-button'); |
| 113 | + await expect(firstButton).to.have.attribute('disabled'); |
| 114 | + await expect(previousButton).to.have.attribute('disabled'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('shows the dots when more pages than visible', async () => { |
| 118 | + element = await fixture( |
| 119 | + html` <uui-pagination .total=${30}></uui-pagination> ` |
| 120 | + ); |
| 121 | + |
| 122 | + const children = element.shadowRoot?.querySelector('#group')?.children; |
| 123 | + const arr = [].slice.call(children); |
| 124 | + |
| 125 | + const hasDots = |
| 126 | + arr.filter((e: HTMLElement) => e.classList.contains('dots-button')) |
| 127 | + .length > 0; |
| 128 | + await expect(hasDots).to.be.true; |
| 129 | + }); |
| 130 | + |
| 131 | + it('hides the dots when only one page', async () => { |
| 132 | + element = await fixture( |
| 133 | + html` <uui-pagination .total=${1}></uui-pagination> ` |
| 134 | + ); |
| 135 | + |
| 136 | + const children = element.shadowRoot?.querySelector('#group')?.children; |
| 137 | + const arr = [].slice.call(children); |
| 138 | + |
| 139 | + const hasDots = |
| 140 | + arr.filter((e: HTMLElement) => e.classList.contains('dots-button')) |
| 141 | + .length > 0; |
| 142 | + await expect(hasDots).to.be.false; |
10 | 143 | });
|
11 | 144 |
|
12 | 145 | it('passes the a11y audit', async () => {
|
|
0 commit comments