-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added more tests for utils (#114)
* test: added more tests for utils * test: fixed useSpinner test & added clamp test * chore: tsignore
- Loading branch information
1 parent
cf52991
commit d8dc0f8
Showing
5 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { clamp, isInRange, getDefaultOptimumValue } from "../helpers"; | ||
|
||
describe("Meter Helpers", () => { | ||
test("getDefaultOptimumValue", () => { | ||
expect(getDefaultOptimumValue(0, 100)).toBe(50); | ||
expect(getDefaultOptimumValue(100, 0)).toBe(100); | ||
expect(getDefaultOptimumValue(100, 500)).toBe(300); | ||
}); | ||
|
||
test("isInRange", () => { | ||
expect(isInRange(100, 0, 50)).toBe(false); | ||
expect(isInRange(50, 0, 50)).toBe(true); | ||
expect(isInRange(50, 0, 40)).toBe(false); | ||
expect(isInRange(50, 40, 0)).toBe(false); | ||
expect(isInRange(50, 100, 0)).toBe(false); | ||
}); | ||
|
||
test("clamp", () => { | ||
// @ts-ignore | ||
expect(clamp(null, 1, 2)).toBe(0); | ||
expect(clamp(5, 0, 2)).toBe(2); | ||
expect(clamp(-5, 0, 2)).toBe(0); | ||
expect(clamp(2, 5, 8)).toBe(5); | ||
expect(clamp(6, 5, 8)).toBe(6); | ||
expect(clamp(6, -5, -2)).toBe(-2); | ||
expect(clamp(-8, -5, -10)).toBe(-10); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React from "react"; | ||
import { cleanup } from "@testing-library/react"; | ||
import { renderHook } from "reakit-test-utils/hooks"; | ||
import { fireEvent, render } from "reakit-test-utils"; | ||
|
||
import { useSpinner } from "../__utils"; | ||
|
||
afterEach(cleanup); | ||
|
||
const noop = () => {}; | ||
function renderUseSpinner(increment = noop, decrement = noop) { | ||
return renderHook(() => useSpinner(increment, decrement)).result; | ||
} | ||
|
||
const Example = () => { | ||
const [value, setValue] = React.useState(0); | ||
const { up, down } = useSpinner( | ||
() => setValue(v => v + 1), | ||
() => setValue(v => v - 1), | ||
); | ||
|
||
return ( | ||
<div> | ||
<p data-testid="value">{value}</p> | ||
<button onClick={() => up()}>+</button> | ||
<button onClick={() => down()}>-</button> | ||
</div> | ||
); | ||
}; | ||
|
||
describe("useSpinner", () => { | ||
it("should render properly", () => { | ||
const inc = jest.fn(); | ||
const dec = jest.fn(); | ||
const { current } = renderUseSpinner(inc, dec); | ||
|
||
expect(current).toMatchInlineSnapshot(` | ||
Object { | ||
"down": [Function], | ||
"stop": [Function], | ||
"up": [Function], | ||
} | ||
`); | ||
}); | ||
|
||
it("press up", () => { | ||
const { getByTestId: testId, getByText: text } = render(<Example />); | ||
|
||
fireEvent.click(text("+")); | ||
expect(testId("value")).toHaveTextContent("1"); | ||
}); | ||
|
||
it("press down", () => { | ||
const { getByTestId: testId, getByText: text } = render(<Example />); | ||
|
||
fireEvent.click(text("-")); | ||
expect(testId("value")).toHaveTextContent("-1"); | ||
}); | ||
|
||
// TODO: Simulate mouse hold to check for timeout and intervals of increments | ||
}); |
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,22 @@ | ||
import { isFloatingPointNumericCharacter, getStepFactor } from "../__utils"; | ||
|
||
describe("NumberInput Utils", () => { | ||
test("isFloatingPointNumericCharacter", () => { | ||
expect(isFloatingPointNumericCharacter("0")).toBeTruthy(); | ||
expect(isFloatingPointNumericCharacter("1")).toBeTruthy(); | ||
expect(isFloatingPointNumericCharacter("-")).toBeTruthy(); | ||
expect(isFloatingPointNumericCharacter("+")).toBeTruthy(); | ||
expect(isFloatingPointNumericCharacter(".")).toBeTruthy(); | ||
expect(isFloatingPointNumericCharacter("e")).toBeTruthy(); | ||
expect(isFloatingPointNumericCharacter("E")).toBeTruthy(); | ||
expect(isFloatingPointNumericCharacter("Nope")).toBeFalsy(); | ||
expect(isFloatingPointNumericCharacter("abcd")).toBeFalsy(); | ||
}); | ||
|
||
test("getStepFactor", () => { | ||
expect(getStepFactor({} as React.KeyboardEvent)).toBe(1); | ||
expect(getStepFactor({ shiftKey: true } as React.KeyboardEvent)).toBe(10); | ||
expect(getStepFactor({ metaKey: true } as React.KeyboardEvent)).toBe(0.1); | ||
expect(getStepFactor({ ctrlKey: true } as React.KeyboardEvent)).toBe(0.1); | ||
}); | ||
}); |