|
| 1 | +/** |
| 2 | + * Copyright (c) HashiCorp, Inc. |
| 3 | + * SPDX-License-Identifier: MPL-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { module, test } from 'qunit'; |
| 7 | +import { click, findAll, render } from '@ember/test-helpers'; |
| 8 | +import { get } from '@ember/helper'; |
| 9 | +import { TrackedObject } from 'tracked-built-ins'; |
| 10 | + |
| 11 | +import { HdsAdvancedTable } from '@hashicorp/design-system-components/components'; |
| 12 | +import type { HdsAdvancedTableOnSelectionChangeSignature } from '@hashicorp/design-system-components/components/hds/advanced-table/types'; |
| 13 | + |
| 14 | +import { setupRenderingTest } from 'showcase/tests/helpers'; |
| 15 | + |
| 16 | +const DEFAULT_SELECTABLE_MODEL = [ |
| 17 | + { |
| 18 | + id: '1', |
| 19 | + type: 'folk', |
| 20 | + artist: 'Nick Drake', |
| 21 | + album: 'Pink Moon', |
| 22 | + year: '1972', |
| 23 | + }, |
| 24 | + { |
| 25 | + id: '2', |
| 26 | + type: 'folk', |
| 27 | + artist: 'The Beatles', |
| 28 | + album: 'Abbey Road', |
| 29 | + year: '1969', |
| 30 | + }, |
| 31 | + { |
| 32 | + id: '3', |
| 33 | + type: 'folk', |
| 34 | + artist: 'Melanie', |
| 35 | + album: 'Candles in the Rain', |
| 36 | + year: '1971', |
| 37 | + }, |
| 38 | +]; |
| 39 | + |
| 40 | +const DEFAULT_SELECTABLE_COLUMNS = [ |
| 41 | + { key: 'artist', label: 'Artist' }, |
| 42 | + { key: 'album', label: 'Album' }, |
| 43 | + { key: 'year', label: 'Year' }, |
| 44 | +]; |
| 45 | + |
| 46 | +const createSelectableTable = async (options: { |
| 47 | + selectionAriaLabelSuffix?: string; |
| 48 | + hasStickyFirstColumn?: boolean; |
| 49 | + onSelectionChange?: ( |
| 50 | + args: HdsAdvancedTableOnSelectionChangeSignature, |
| 51 | + ) => void; |
| 52 | +}) => { |
| 53 | + return await render( |
| 54 | + <template> |
| 55 | + <HdsAdvancedTable |
| 56 | + @model={{DEFAULT_SELECTABLE_MODEL}} |
| 57 | + @columns={{DEFAULT_SELECTABLE_COLUMNS}} |
| 58 | + id="data-test-selectable-advanced-table" |
| 59 | + @hasStickyFirstColumn={{options.hasStickyFirstColumn}} |
| 60 | + @onSelectionChange={{options.onSelectionChange}} |
| 61 | + @isSelectable={{true}} |
| 62 | + > |
| 63 | + <:body as |B|> |
| 64 | + <B.Tr |
| 65 | + {{! @glint-expect-error }} |
| 66 | + @selectionKey={{get B.data "id"}} |
| 67 | + @selectionAriaLabelSuffix={{options.selectionAriaLabelSuffix}} |
| 68 | + > |
| 69 | + {{! @glint-expect-error }} |
| 70 | + <B.Th>{{get B.data "name"}}</B.Th> |
| 71 | + {{! @glint-expect-error }} |
| 72 | + <B.Td>{{get B.data "status"}}</B.Td> |
| 73 | + {{! @glint-expect-error }} |
| 74 | + <B.Td>{{get B.data "description"}}</B.Td> |
| 75 | + </B.Tr> |
| 76 | + </:body> |
| 77 | + </HdsAdvancedTable> |
| 78 | + </template>, |
| 79 | + ); |
| 80 | +}; |
| 81 | + |
| 82 | +module('Integration | Component | hds/advanced-table/index', function (hooks) { |
| 83 | + setupRenderingTest(hooks); |
| 84 | + |
| 85 | + module('multi-selection', function () { |
| 86 | + const selectAllCheckboxSelector = |
| 87 | + '#data-test-selectable-advanced-table .hds-advanced-table__thead .hds-advanced-table__th[role="columnheader"] .hds-advanced-table__checkbox'; |
| 88 | + const rowCheckboxesSelector = |
| 89 | + '#data-test-selectable-advanced-table .hds-advanced-table__tbody .hds-advanced-table__th .hds-advanced-table__checkbox'; |
| 90 | + |
| 91 | + test('it renders a multi-select table when isSelectable is set to true for a table with a model', async function (assert) { |
| 92 | + await createSelectableTable({}); |
| 93 | + |
| 94 | + assert.dom(selectAllCheckboxSelector).exists({ count: 1 }); |
| 95 | + assert |
| 96 | + .dom(rowCheckboxesSelector) |
| 97 | + .exists({ count: DEFAULT_SELECTABLE_MODEL.length }); |
| 98 | + }); |
| 99 | + |
| 100 | + test('it selects all rows when the "select all" checkbox checked state is triggered', async function (assert) { |
| 101 | + await createSelectableTable({}); |
| 102 | + |
| 103 | + // Default should be unchecked: |
| 104 | + assert.dom(selectAllCheckboxSelector).isNotChecked(); |
| 105 | + assert.dom(rowCheckboxesSelector).isNotChecked().exists({ count: 3 }); |
| 106 | + // Should change to checked after it is triggered: |
| 107 | + await click(selectAllCheckboxSelector); |
| 108 | + assert.dom(selectAllCheckboxSelector).isChecked(); |
| 109 | + assert.dom(rowCheckboxesSelector).isChecked().exists({ count: 3 }); |
| 110 | + }); |
| 111 | + |
| 112 | + test('it deselects all rows when the "select all" checkbox unchecked state is triggered', async function (assert) { |
| 113 | + await createSelectableTable({}); |
| 114 | + // Trigger checked status: |
| 115 | + await click(selectAllCheckboxSelector); |
| 116 | + // Trigger unchecked state: |
| 117 | + await click(selectAllCheckboxSelector); |
| 118 | + assert.dom(selectAllCheckboxSelector).isNotChecked(); |
| 119 | + assert.dom(rowCheckboxesSelector).isNotChecked().exists({ count: 3 }); |
| 120 | + }); |
| 121 | + |
| 122 | + test('if some rows are selected but not all, the "select all" checkbox should be in an indeterminate state', async function (assert) { |
| 123 | + await createSelectableTable({}); |
| 124 | + const rowCheckboxes = findAll(rowCheckboxesSelector); |
| 125 | + const firstRowCheckbox = rowCheckboxes[0]; |
| 126 | + |
| 127 | + if (firstRowCheckbox) { |
| 128 | + // Check checkbox in just the first row: |
| 129 | + await click(firstRowCheckbox); |
| 130 | + assert |
| 131 | + .dom(selectAllCheckboxSelector) |
| 132 | + .hasProperty('indeterminate', true); |
| 133 | + } |
| 134 | + }); |
| 135 | + |
| 136 | + test('it should invoke the `onSelectionChange` callback when a checkbox is selected', async function (assert) { |
| 137 | + const context = new TrackedObject<{ |
| 138 | + keys: string[]; |
| 139 | + }>({ |
| 140 | + keys: [], |
| 141 | + }); |
| 142 | + |
| 143 | + const onSelectionChange = ({ |
| 144 | + selectedRowsKeys, |
| 145 | + }: { |
| 146 | + selectedRowsKeys: string[]; |
| 147 | + }) => { |
| 148 | + context.keys = selectedRowsKeys; |
| 149 | + }; |
| 150 | + |
| 151 | + await createSelectableTable({ onSelectionChange }); |
| 152 | + |
| 153 | + const rowCheckboxes = findAll(rowCheckboxesSelector); |
| 154 | + const firstRowCheckbox = rowCheckboxes[0]; |
| 155 | + |
| 156 | + if (firstRowCheckbox) { |
| 157 | + await click(firstRowCheckbox); |
| 158 | + assert.deepEqual(context.keys, ['1']); |
| 159 | + } |
| 160 | + |
| 161 | + await click(selectAllCheckboxSelector); |
| 162 | + assert.deepEqual(context.keys, ['1', '2', '3']); |
| 163 | + await click(selectAllCheckboxSelector); |
| 164 | + assert.deepEqual(context.keys, []); |
| 165 | + }); |
| 166 | + |
| 167 | + test('it renders the expected `aria-label` values for "select all" and rows by default', async function (assert) { |
| 168 | + await createSelectableTable({}); |
| 169 | + |
| 170 | + assert.dom(selectAllCheckboxSelector).hasAria('label', 'Select all rows'); |
| 171 | + assert.dom(rowCheckboxesSelector).hasAria('label', 'Select row'); |
| 172 | + |
| 173 | + await click(selectAllCheckboxSelector); |
| 174 | + await click(rowCheckboxesSelector); |
| 175 | + |
| 176 | + assert.dom(selectAllCheckboxSelector).hasAria('label', 'Select all rows'); |
| 177 | + assert.dom(rowCheckboxesSelector).hasAria('label', 'Select row'); |
| 178 | + }); |
| 179 | + |
| 180 | + test('it renders the expected `aria-label` for rows with `@selectionAriaLabelSuffix`', async function (assert) { |
| 181 | + await createSelectableTable({ |
| 182 | + selectionAriaLabelSuffix: 'custom suffix', |
| 183 | + }); |
| 184 | + |
| 185 | + assert |
| 186 | + .dom(rowCheckboxesSelector) |
| 187 | + .hasAria('label', 'Select custom suffix'); |
| 188 | + |
| 189 | + await click(rowCheckboxesSelector); |
| 190 | + |
| 191 | + assert |
| 192 | + .dom(rowCheckboxesSelector) |
| 193 | + .hasAria('label', 'Select custom suffix'); |
| 194 | + }); |
| 195 | + }); |
| 196 | +}); |
0 commit comments