Skip to content

Commit

Permalink
fix(web): admin settings number input validation (#9470)
Browse files Browse the repository at this point in the history
* fix(web): admin settings number input validation

* fix import by creating *.ts file

* just ignore import error
  • Loading branch information
michelheusschen authored May 14, 2024
1 parent 4d7aa7e commit acc611a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
14 changes: 14 additions & 0 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@sveltejs/vite-plugin-svelte": "^3.0.2",
"@testing-library/jest-dom": "^6.4.2",
"@testing-library/svelte": "^5.0.0",
"@testing-library/user-event": "^14.5.2",
"@types/dom-to-image": "^2.6.7",
"@types/justified-layout": "^4.1.4",
"@types/lodash-es": "^4.17.12",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { render } from '@testing-library/svelte';
import userEvent from '@testing-library/user-event';
// @ts-expect-error the import works but tsc check errors
import SettingInputField, { SettingInputFieldType } from './setting-input-field.svelte';

describe('SettingInputField component', () => {
it('validates number input on blur', async () => {
const { getByRole } = render(SettingInputField, {
props: {
label: 'test-number-input',
inputType: SettingInputFieldType.NUMBER,
value: 0,
min: 0,
max: 100,
step: '0.1',
},
});
const user = userEvent.setup();

const numberInput = getByRole('spinbutton') as HTMLInputElement;
expect(numberInput.value).toEqual('0');

await user.click(numberInput);
await user.keyboard('100.1');
expect(numberInput.value).toEqual('100.1');

await user.click(document.body);
expect(numberInput.value).toEqual('100');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
export let isEdited = false;
export let passwordAutocomplete: string = 'current-password';
const handleInput = (e: Event) => {
value = (e.target as HTMLInputElement).value;
const validateInput = () => {
if (inputType === SettingInputFieldType.NUMBER) {
let newValue = Number(value) || 0;
if (newValue < min) {
Expand Down Expand Up @@ -79,7 +77,8 @@
{step}
{required}
{value}
on:input={handleInput}
on:input={(e) => (value = e.currentTarget.value)}
on:blur={validateInput}
{disabled}
{title}
/>
Expand Down

0 comments on commit acc611a

Please sign in to comment.