|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | +import { useState } from 'react' |
| 9 | +import { expect, test } from 'vitest' |
| 10 | +import { render } from 'vitest-browser-react' |
| 11 | +import { page, userEvent } from 'vitest/browser' |
| 12 | + |
| 13 | +import { TextField } from '~/components/form/fields/TextField' |
| 14 | + |
| 15 | +import { FilterPopover } from './FilterPopover' |
| 16 | + |
| 17 | +type Filters = { name: string; owner: string; types: string[] } |
| 18 | + |
| 19 | +function Harness() { |
| 20 | + const defaultValues = { name: '', owner: '', types: [] } |
| 21 | + const [applied, setApplied] = useState<Filters>(defaultValues) |
| 22 | + return ( |
| 23 | + <> |
| 24 | + <FilterPopover |
| 25 | + isFetching={false} |
| 26 | + resetFieldValues={defaultValues} |
| 27 | + lastApplied={applied} |
| 28 | + handleSubmit={setApplied} |
| 29 | + pluralFilteredItemName="widgets" |
| 30 | + > |
| 31 | + {(form) => ( |
| 32 | + <> |
| 33 | + <TextField |
| 34 | + name="name" |
| 35 | + label="Name" |
| 36 | + control={form.control} |
| 37 | + validate={(value) => (value === 'bad' ? 'No bad names' : undefined)} |
| 38 | + /> |
| 39 | + <TextField name="owner" label="Owner" control={form.control} /> |
| 40 | + <button |
| 41 | + type="button" |
| 42 | + onClick={() => setApplied((prev) => ({ ...prev, types: ['disk'] }))} |
| 43 | + > |
| 44 | + Apply type filter |
| 45 | + </button> |
| 46 | + </> |
| 47 | + )} |
| 48 | + </FilterPopover> |
| 49 | + <output aria-label="Applied name">{applied.name}</output> |
| 50 | + </> |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +const filterButton = () => page.getByRole('button', { name: /^Filter widgets/ }) |
| 55 | +const form = () => page.getByRole('form', { name: 'Filter widgets' }) |
| 56 | +const resetButton = () => page.getByRole('button', { name: 'Reset' }) |
| 57 | +const nameField = () => page.getByRole('textbox', { name: 'Name' }) |
| 58 | +const applyButton = () => page.getByRole('button', { name: 'Apply' }) |
| 59 | +const appliedName = () => page.getByRole('status', { name: 'Applied name' }) |
| 60 | + |
| 61 | +test('badge counts applied filters', async () => { |
| 62 | + await render(<Harness />) |
| 63 | + const noneApplied = page.getByRole('button', { name: 'Filter widgets', exact: true }) |
| 64 | + await expect.element(noneApplied).toBeVisible() |
| 65 | + |
| 66 | + await noneApplied.click() |
| 67 | + await page.getByRole('button', { name: 'Apply type filter' }).click() |
| 68 | + await userEvent.keyboard('{Escape}') |
| 69 | + const oneApplied = page.getByRole('button', { name: 'Filter widgets (1 applied)' }) |
| 70 | + await expect.element(oneApplied).toBeVisible() |
| 71 | + // this is the non-accessible text |
| 72 | + await expect.element(oneApplied.getByText('1')).toBeVisible() |
| 73 | + |
| 74 | + await oneApplied.click() |
| 75 | + await nameField().fill('abc') |
| 76 | + await applyButton().click() |
| 77 | + await expect |
| 78 | + .element(page.getByRole('button', { name: 'Filter widgets (2 applied)' })) |
| 79 | + .toBeVisible() |
| 80 | + |
| 81 | + await filterButton().click() |
| 82 | + await page.getByRole('textbox', { name: 'Owner' }).fill('abc') |
| 83 | + await applyButton().click() |
| 84 | + await expect |
| 85 | + .element(page.getByRole('button', { name: 'Filter widgets (3 applied)' })) |
| 86 | + .toBeVisible() |
| 87 | +}) |
| 88 | + |
| 89 | +test('apply submits the values and closes only if there are no validation errors', async () => { |
| 90 | + await render(<Harness />) |
| 91 | + await filterButton().click() |
| 92 | + await nameField().fill('bad') |
| 93 | + await applyButton().click() |
| 94 | + await expect.element(form().getByText('No bad names')).toBeVisible() |
| 95 | + await expect.element(appliedName()).toHaveTextContent('') |
| 96 | + |
| 97 | + await nameField().fill('good') |
| 98 | + await applyButton().click() |
| 99 | + await expect.element(form()).not.toBeInTheDocument() |
| 100 | + await expect.element(appliedName()).toHaveTextContent('good') |
| 101 | +}) |
| 102 | + |
| 103 | +test('reset applies the reset values', async () => { |
| 104 | + await render(<Harness />) |
| 105 | + await filterButton().click() |
| 106 | + await nameField().fill('abc') |
| 107 | + await applyButton().click() |
| 108 | + await expect.element(appliedName()).toHaveTextContent('abc') |
| 109 | + await filterButton().click() |
| 110 | + await resetButton().click() |
| 111 | + await expect.element(form()).not.toBeInTheDocument() |
| 112 | + await expect.element(appliedName()).toHaveTextContent('') |
| 113 | + await expect |
| 114 | + .element(page.getByRole('button', { name: 'Filter widgets', exact: true })) |
| 115 | + .toBeVisible() |
| 116 | +}) |
| 117 | + |
| 118 | +test('reset is disabled when nothing is applied', async () => { |
| 119 | + await render(<Harness />) |
| 120 | + await filterButton().click() |
| 121 | + await expect.element(resetButton()).toBeDisabled() |
| 122 | + |
| 123 | + // it's also disabled if you simply restore the default form values manually |
| 124 | + await nameField().fill('abc') |
| 125 | + await applyButton().click() |
| 126 | + await filterButton().click() |
| 127 | + await nameField().fill('') |
| 128 | + await applyButton().click() |
| 129 | + await filterButton().click() |
| 130 | + await expect.element(resetButton()).toBeDisabled() |
| 131 | +}) |
| 132 | + |
| 133 | +test('unapplied edits are discarded on close', async () => { |
| 134 | + await render(<Harness />) |
| 135 | + await filterButton().click() |
| 136 | + await nameField().fill('draft') |
| 137 | + await userEvent.keyboard('{Escape}') |
| 138 | + await expect.element(form()).not.toBeInTheDocument() |
| 139 | + |
| 140 | + await filterButton().click() |
| 141 | + await expect.element(nameField()).toHaveValue('') |
| 142 | +}) |
0 commit comments