|
| 1 | +import { expect } from '@playwright/test'; |
| 2 | +import { test } from '@stencil/playwright'; |
| 3 | +import type { TableHeaderCellsPropType } from '../../schema'; |
| 4 | + |
| 5 | +const DATA = [ |
| 6 | + { id: '1001', name: 'John', age: 30 }, |
| 7 | + { id: '1002', name: 'Jane', age: 25 }, |
| 8 | + { id: '1003', name: 'Bob', age: 35 }, |
| 9 | +]; |
| 10 | + |
| 11 | +const HEADERS: TableHeaderCellsPropType = { |
| 12 | + horizontal: [ |
| 13 | + [ |
| 14 | + { key: 'id', label: 'ID' }, |
| 15 | + { key: 'name', label: 'Name' }, |
| 16 | + { key: 'age', label: 'Age' }, |
| 17 | + ], |
| 18 | + ], |
| 19 | +}; |
| 20 | + |
| 21 | +test.describe('kol-table-settings', () => { |
| 22 | + test.beforeEach(async ({ page }) => { |
| 23 | + await page.setContent(`<kol-table-stateless |
| 24 | + _label="Table with Settings" |
| 25 | + _header-cells='${JSON.stringify(HEADERS)}' |
| 26 | + _data='${JSON.stringify(DATA)}' |
| 27 | + />`); |
| 28 | + await page.waitForChanges(); |
| 29 | + }); |
| 30 | + |
| 31 | + test.describe('Basic Settings Popover Tests', () => { |
| 32 | + test('it opens the settings popover when clicking the settings button', async ({ page }) => { |
| 33 | + const settingsButton = page.getByTestId('popover-button').locator('button'); |
| 34 | + await settingsButton.click(); |
| 35 | + const popover = page.getByTestId('popover-content'); |
| 36 | + await expect(popover).toBeVisible(); |
| 37 | + }); |
| 38 | + |
| 39 | + test('it closes the popover when clicking the cancel button', async ({ page }) => { |
| 40 | + const settingsButton = page.getByTestId('popover-button').locator('button'); |
| 41 | + await settingsButton.click(); |
| 42 | + const cancelButton = page.getByTestId('table-settings-cancel'); |
| 43 | + await cancelButton.click(); |
| 44 | + const popover = page.getByTestId('popover-content'); |
| 45 | + await expect(popover).not.toBeVisible(); |
| 46 | + }); |
| 47 | + |
| 48 | + test('it persists settings after closing and reopening the popover', async ({ page }) => { |
| 49 | + const settingsButton = page.getByTestId('popover-button').locator('button'); |
| 50 | + await settingsButton.click(); |
| 51 | + |
| 52 | + // Hide the name column |
| 53 | + const nameCheckbox = page.getByRole('checkbox', { name: 'Name' }); |
| 54 | + await nameCheckbox.click(); |
| 55 | + |
| 56 | + // Apply changes |
| 57 | + const applyButton = page.getByTestId('table-settings-apply'); |
| 58 | + await applyButton.click(); |
| 59 | + |
| 60 | + // Reopen settings |
| 61 | + await settingsButton.click(); |
| 62 | + |
| 63 | + // Verify name column is still hidden |
| 64 | + await expect(nameCheckbox).not.toBeChecked(); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + test.describe('Column Visibility Management', () => { |
| 69 | + test('it lists all columns in the settings', async ({ page }) => { |
| 70 | + const settingsButton = page.getByTestId('popover-button').locator('button'); |
| 71 | + await settingsButton.click(); |
| 72 | + |
| 73 | + const columnLabels = page.locator('.kol-table-settings__column > span'); |
| 74 | + await expect(columnLabels).toHaveText(['ID', 'Name', 'Age']); |
| 75 | + }); |
| 76 | + |
| 77 | + test('it toggles visibility of individual columns', async ({ page }) => { |
| 78 | + const settingsButton = page.getByTestId('popover-button').locator('button'); |
| 79 | + await settingsButton.click(); |
| 80 | + |
| 81 | + const nameCheckbox = page.getByRole('checkbox', { name: 'Name' }); |
| 82 | + await nameCheckbox.click(); |
| 83 | + |
| 84 | + const applyButton = page.getByTestId('table-settings-apply'); |
| 85 | + await applyButton.click(); |
| 86 | + |
| 87 | + // Verify name column is hidden in the table |
| 88 | + const nameColumn = page.locator('kol-table-stateless-wc th').filter({ hasText: 'Name' }); |
| 89 | + await expect(nameColumn).not.toBeVisible(); |
| 90 | + }); |
| 91 | + |
| 92 | + test('it shows error message when all columns are hidden', async ({ page }) => { |
| 93 | + const settingsButton = page.getByTestId('popover-button').locator('button'); |
| 94 | + await settingsButton.click(); |
| 95 | + await page.waitForChanges(); |
| 96 | + |
| 97 | + // Hide all columns |
| 98 | + const checkboxes = page.getByRole('checkbox'); |
| 99 | + for (const checkbox of await checkboxes.all()) { |
| 100 | + await checkbox.click(); |
| 101 | + } |
| 102 | + |
| 103 | + const applyButton = page.getByTestId('table-settings-apply'); |
| 104 | + await applyButton.click(); |
| 105 | + |
| 106 | + const errorMessage = page.locator('kol-table-settings-wc kol-alert-wc'); |
| 107 | + await expect(errorMessage).toBeVisible(); |
| 108 | + }); |
| 109 | + |
| 110 | + test('it removes error message when at least one column is visible', async ({ page }) => { |
| 111 | + const settingsButton = page.getByTestId('popover-button').locator('button'); |
| 112 | + await settingsButton.click(); |
| 113 | + await page.waitForChanges(); |
| 114 | + |
| 115 | + // Hide all columns |
| 116 | + const checkboxes = page.getByRole('checkbox'); |
| 117 | + for (const checkbox of await checkboxes.all()) { |
| 118 | + await checkbox.click(); |
| 119 | + } |
| 120 | + |
| 121 | + const applyButton = page.getByTestId('table-settings-apply'); |
| 122 | + await applyButton.click(); |
| 123 | + |
| 124 | + // Show one column |
| 125 | + await checkboxes.first().click(); |
| 126 | + await applyButton.click(); |
| 127 | + |
| 128 | + const errorMessage = page.locator('kol-table-settings-wc kol-alert-wc'); |
| 129 | + await expect(errorMessage).not.toBeVisible(); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + test.describe('Column Width Management', () => { |
| 134 | + test('it accepts valid width values', async ({ page }) => { |
| 135 | + const settingsButton = page.getByTestId('popover-button').locator('button'); |
| 136 | + await settingsButton.click(); |
| 137 | + |
| 138 | + const idWidthInput = page.getByRole('spinbutton', { name: 'ID' }); |
| 139 | + await idWidthInput.fill('50'); |
| 140 | + |
| 141 | + const applyButton = page.getByTestId('table-settings-apply'); |
| 142 | + await applyButton.click(); |
| 143 | + |
| 144 | + // Verify width is applied |
| 145 | + const idColumn = page.locator('kol-table-stateless-wc th').filter({ hasText: 'ID' }); |
| 146 | + await expect(idColumn).toHaveAttribute('style', 'width: 50ch;'); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + test.describe('Column Order Management', () => { |
| 151 | + test('it disables up button for first column', async ({ page }) => { |
| 152 | + const settingsButton = page.getByTestId('popover-button').locator('button'); |
| 153 | + await settingsButton.click(); |
| 154 | + |
| 155 | + const firstUpButton = page.getByTestId('table-settings-move-up').first().locator('button'); |
| 156 | + await expect(firstUpButton).toBeDisabled(); |
| 157 | + }); |
| 158 | + |
| 159 | + test('it disables down button for last column', async ({ page }) => { |
| 160 | + const settingsButton = page.getByTestId('popover-button').locator('button'); |
| 161 | + await settingsButton.click(); |
| 162 | + |
| 163 | + const lastDownButton = page.getByTestId('table-settings-move-down').last().locator('button'); |
| 164 | + await expect(lastDownButton).toBeDisabled(); |
| 165 | + }); |
| 166 | + |
| 167 | + test('it moves a column up', async ({ page }) => { |
| 168 | + const settingsButton = page.getByTestId('popover-button').locator('button'); |
| 169 | + await settingsButton.click(); |
| 170 | + |
| 171 | + // Move name column up |
| 172 | + const nameUpButton = page.getByTestId('table-settings-move-up').filter({ hasText: 'Name' }); |
| 173 | + await nameUpButton.click(); |
| 174 | + |
| 175 | + const applyButton = page.getByTestId('table-settings-apply'); |
| 176 | + await applyButton.click(); |
| 177 | + |
| 178 | + // Verify column order in table |
| 179 | + const columns = page.locator('kol-table-stateless-wc th'); |
| 180 | + await expect(columns.nth(0)).toHaveText('Name'); |
| 181 | + await expect(columns.nth(1)).toHaveText('ID'); |
| 182 | + }); |
| 183 | + |
| 184 | + test('it moves a column down', async ({ page }) => { |
| 185 | + const settingsButton = page.getByTestId('popover-button').locator('button'); |
| 186 | + await settingsButton.click(); |
| 187 | + |
| 188 | + // Move ID column down |
| 189 | + const idDownButton = page.getByTestId('table-settings-move-down').filter({ hasText: 'ID' }); |
| 190 | + await idDownButton.click(); |
| 191 | + |
| 192 | + const applyButton = page.getByTestId('table-settings-apply'); |
| 193 | + await applyButton.click(); |
| 194 | + |
| 195 | + // Verify column order in table |
| 196 | + const columns = page.locator('kol-table-stateless-wc th'); |
| 197 | + await expect(columns.nth(0)).toHaveText('Name'); |
| 198 | + await expect(columns.nth(1)).toHaveText('ID'); |
| 199 | + }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments