Skip to content

Commit

Permalink
Add test block and change testid for buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
stephl3 committed Feb 27, 2024
1 parent 93deee5 commit 1f0a150
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/number-input/src/Arrows/Arrow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const Arrow = ({
type="button"
tabIndex={-1} // Mimicking native behavior; you cannot focus on an arrow.
disabled={disabled}
data-testid={`lg-number_input-${direction}-button`}
data-testid={`lg-number_input-${direction}_button`}
>
<Icon
className={cx({
Expand Down
34 changes: 29 additions & 5 deletions packages/number-input/src/NumberInput/NumberInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const label = 'This is the label text';
const description = 'This is the description text';
const errorMessage = 'error message';
const arrowTestId = {
up: 'lg-number_input-increment-button',
down: 'lg-number_input-decrement-button',
up: 'lg-number_input-increment_button',
down: 'lg-number_input-decrement_button',
};

const defaultProps = {
Expand Down Expand Up @@ -214,7 +214,7 @@ describe('packages/number-input', () => {

const upArrow = getByTestId(arrowTestId.up);

userEvent.click(upArrow as HTMLButtonElement);
userEvent.click(upArrow);
expect((numberInput as HTMLInputElement).value).toBe('1');
});

Expand All @@ -226,7 +226,7 @@ describe('packages/number-input', () => {

const downArrow = getByTestId(arrowTestId.down);

userEvent.click(downArrow as HTMLButtonElement);
userEvent.click(downArrow);
expect((numberInput as HTMLInputElement).value).toBe('-1');
});

Expand Down Expand Up @@ -256,12 +256,36 @@ describe('packages/number-input', () => {

const upArrow = getByTestId(arrowTestId.up);

userEvent.click(upArrow as HTMLButtonElement); // focus
userEvent.click(upArrow); // focus
expect(upArrow).toHaveFocus();

userEvent.tab(); // blur
expect(onBlur).toHaveBeenCalledTimes(1);
});

test('callback does not trigger when focus changes to adjacent elements within number input', () => {
const onBlur = jest.fn();
const { getByTestId, numberInput } = renderNumberInput({
label,
onBlur,
...defaultProps,
});

const upArrow = getByTestId(arrowTestId.up);
const downArrow = getByTestId(arrowTestId.down);

userEvent.tab(); // focus
expect(numberInput).toHaveFocus();

userEvent.click(upArrow); // focus on up arrow
expect(upArrow).toHaveFocus();

userEvent.click(downArrow); // focus on down arrow
expect(downArrow).toHaveFocus();

userEvent.tab(); //blur
expect(onBlur).toHaveBeenCalledTimes(1);
});
});

test(`is disabled when disabled is passed`, () => {
Expand Down

0 comments on commit 1f0a150

Please sign in to comment.