-
-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: constraint values limit UI (#7501)
- Loading branch information
Showing
6 changed files
with
150 additions
and
11 deletions.
There are no files selected for viewing
121 changes: 121 additions & 0 deletions
121
.../ConstraintAccordionEdit/ConstraintAccordionEditBody/FreeTextInput/FreeTextInput.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { fireEvent, screen, waitFor } from '@testing-library/react'; | ||
import { render } from 'utils/testRenderer'; | ||
import { testServerRoute, testServerSetup } from 'utils/testServer'; | ||
import { FreeTextInput } from './FreeTextInput'; | ||
|
||
const server = testServerSetup(); | ||
|
||
const LIMIT = 3; | ||
|
||
const setupApi = () => { | ||
testServerRoute(server, '/api/admin/ui-config', { | ||
flags: { | ||
resourceLimits: true, | ||
}, | ||
resourceLimits: { | ||
constraintValues: LIMIT, | ||
}, | ||
}); | ||
}; | ||
|
||
test('should set error when new constraint values exceed the limit', async () => { | ||
setupApi(); | ||
const values: string[] = []; | ||
const errors: string[] = []; | ||
render( | ||
<FreeTextInput | ||
error='' | ||
values={[]} | ||
setValues={(newValues) => { | ||
values.push(...newValues); | ||
}} | ||
setError={(newError: string) => { | ||
errors.push(newError); | ||
}} | ||
removeValue={() => {}} | ||
/>, | ||
); | ||
|
||
await waitFor(async () => { | ||
const button = await screen.findByText('Add values'); | ||
expect(button).not.toBeDisabled(); | ||
}); | ||
|
||
const input = await screen.findByLabelText('Values'); | ||
fireEvent.change(input, { | ||
target: { value: '1, 2, 3, 4' }, | ||
}); | ||
const button = await screen.findByText('Add values'); | ||
fireEvent.click(button); | ||
|
||
expect(errors).toEqual(['constraints cannot have more than 3 values']); | ||
expect(values).toEqual([]); | ||
}); | ||
|
||
test('should set error when old and new constraint values exceed the limit', async () => { | ||
setupApi(); | ||
const values: string[] = []; | ||
const errors: string[] = []; | ||
render( | ||
<FreeTextInput | ||
error='' | ||
values={['1', '2']} | ||
setValues={(newValues) => { | ||
values.push(...newValues); | ||
}} | ||
setError={(newError: string) => { | ||
errors.push(newError); | ||
}} | ||
removeValue={() => {}} | ||
/>, | ||
); | ||
|
||
await waitFor(async () => { | ||
const button = await screen.findByText('Add values'); | ||
expect(button).not.toBeDisabled(); | ||
}); | ||
|
||
const input = await screen.findByLabelText('Values'); | ||
fireEvent.change(input, { | ||
target: { value: '3, 4' }, | ||
}); | ||
const button = await screen.findByText('Add values'); | ||
fireEvent.click(button); | ||
|
||
expect(errors).toEqual(['constraints cannot have more than 3 values']); | ||
expect(values).toEqual([]); | ||
}); | ||
|
||
test('should set values', async () => { | ||
setupApi(); | ||
const values: string[] = []; | ||
const errors: string[] = []; | ||
render( | ||
<FreeTextInput | ||
error='' | ||
values={['1', '2']} | ||
setValues={(newValues) => { | ||
values.push(...newValues); | ||
}} | ||
setError={(newError: string) => { | ||
errors.push(newError); | ||
}} | ||
removeValue={() => {}} | ||
/>, | ||
); | ||
|
||
await waitFor(async () => { | ||
const button = await screen.findByText('Add values'); | ||
expect(button).not.toBeDisabled(); | ||
}); | ||
|
||
const input = await screen.findByLabelText('Values'); | ||
fireEvent.change(input, { | ||
target: { value: '2, 3' }, | ||
}); | ||
const button = await screen.findByText('Add values'); | ||
fireEvent.click(button); | ||
|
||
expect(errors).toEqual(['']); | ||
expect(values).toEqual(['1', '2', '3']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters