From 90caad1129021d43f46144ca8454fc9d5ee6e865 Mon Sep 17 00:00:00 2001 From: Anurag Hazra Date: Thu, 22 Oct 2020 20:11:51 +0530 Subject: [PATCH] test: added tests for useSpinButton (#110) --- src/utils/useSpinButton.test.tsx | 150 +++++++++++++++++++++++++++++++ src/utils/useSpinButton.ts | 13 +-- 2 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 src/utils/useSpinButton.test.tsx diff --git a/src/utils/useSpinButton.test.tsx b/src/utils/useSpinButton.test.tsx new file mode 100644 index 000000000..42c3103c2 --- /dev/null +++ b/src/utils/useSpinButton.test.tsx @@ -0,0 +1,150 @@ +import React from "react"; +import { fireEvent, render } from "reakit-test-utils"; +import { SpinButtonProps, useSpinButton } from "./useSpinButton"; + +const SpinButtonComp: React.FC = props => { + const { spinButtonProps } = useSpinButton(props); + return ( +
+ {props.children} +
+ ); +}; + +describe("useSpinButton", () => { + it('should have role="spinbutton" and aria props', () => { + const { getByTestId: testId } = render( + , + ); + + const el = testId("test"); + expect(el).toHaveAttribute("role", "spinbutton"); + expect(el).toHaveAttribute("aria-valuenow", "2"); + expect(el).toHaveAttribute("aria-valuemin", "1"); + expect(el).toHaveAttribute("aria-valuemax", "3"); + expect(el).toHaveAttribute("aria-valuetext", "2 items"); + expect(el).not.toHaveAttribute("aria-disabled"); + expect(el).not.toHaveAttribute("aria-readonly"); + }); + + it("should have aria-disabled if isDisabled is set", () => { + const { getByTestId: testId } = render( + , + ); + + expect(testId("test")).toHaveAttribute("aria-disabled", "true"); + }); + + it("should have aria-readonly if isReadOnly is set", () => { + const { getByTestId: testId } = render( + , + ); + + expect(testId("test")).toHaveAttribute("aria-readonly", "true"); + }); + + it("should trigger onIncrement on arrow up", () => { + const onIncrement = jest.fn(); + const { getByTestId: testId } = render( + , + ); + + fireEvent.keyDown(testId("test"), { key: "ArrowUp" }); + expect(onIncrement).toHaveBeenCalledTimes(1); + }); + + it("should trigger onDecrement on arrow down", () => { + const onDecrement = jest.fn(); + const { getByTestId: testId } = render( + , + ); + + fireEvent.keyDown(testId("test"), { key: "ArrowDown" }); + expect(onDecrement).toHaveBeenCalledTimes(1); + }); + + it("should trigger onIncrementPage on page up", () => { + const onIncrementPage = jest.fn(); + const { getByTestId: testId } = render( + , + ); + + fireEvent.keyDown(testId("test"), { key: "PageUp" }); + expect(onIncrementPage).toHaveBeenCalledTimes(1); + }); + + it("should fall back to onIncrement on page up if onIncrementPage is not available", () => { + const onIncrement = jest.fn(); + const { getByTestId: testId } = render( + , + ); + + fireEvent.keyDown(testId("test"), { key: "PageUp" }); + expect(onIncrement).toHaveBeenCalledTimes(1); + }); + + it("should trigger onDecrementPage on page up", () => { + const onDecrementPage = jest.fn(); + const { getByTestId: testId } = render( + , + ); + + fireEvent.keyDown(testId("test"), { key: "PageDown" }); + expect(onDecrementPage).toHaveBeenCalledTimes(1); + }); + + it("should fall back to onDecrement on page up if onDecrementPage is not available", () => { + const onDecrement = jest.fn(); + const { getByTestId: testId } = render( + , + ); + + fireEvent.keyDown(testId("test"), { key: "PageDown" }); + expect(onDecrement).toHaveBeenCalledTimes(1); + }); + + it("should trigger onDecrementToMin on home key", () => { + const onDecrementToMin = jest.fn(); + const { getByTestId: testId } = render( + , + ); + + fireEvent.keyDown(testId("test"), { key: "Home" }); + expect(onDecrementToMin).toHaveBeenCalledTimes(1); + }); + + it("should trigger onIncrementToMax on end key", () => { + const onIncrementToMax = jest.fn(); + const { getByTestId: testId } = render( + , + ); + + fireEvent.keyDown(testId("test"), { key: "End" }); + expect(onIncrementToMax).toHaveBeenCalledTimes(1); + }); +}); diff --git a/src/utils/useSpinButton.ts b/src/utils/useSpinButton.ts index 030be9507..8e30f3070 100644 --- a/src/utils/useSpinButton.ts +++ b/src/utils/useSpinButton.ts @@ -10,7 +10,6 @@ * governing permissions and limitations under the License. */ -// import { announce } from "./storybook/"; import { ValueBase, InputBase, @@ -20,6 +19,8 @@ import { import { AriaButtonProps } from "@react-types/button"; import { HTMLAttributes, useCallback, useEffect, useRef } from "react"; +// import { announce } from "./LiveAnnouncer"; + export interface SpinButtonProps extends InputBase, Validation, @@ -124,11 +125,11 @@ export function useSpinButton(props: SpinButtonProps): SpinbuttonAria { isFocused.current = false; }; - // useEffect(() => { - // if (isFocused.current) { - // announce(textValue || `${value}`); - // } - // }, [textValue, value]); + // useEffect(() => { + // if (isFocused.current) { + // announce(textValue || `${value}`, "assertive"); + // } + // }, [textValue, value]); const onIncrementPressStart = useCallback( (initialStepDelay: number) => {