Skip to content

Commit

Permalink
test: added more tests for utils (#114)
Browse files Browse the repository at this point in the history
* test: added more tests for utils

* test: fixed useSpinner test & added clamp test

* chore: tsignore
  • Loading branch information
anuraghazra authored Oct 23, 2020
1 parent cf52991 commit d8dc0f8
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/datepicker/__utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { isString } from "@chakra-ui/utils";

export function setTime(date: Date, time: Date) {
if (!date || !time) {
return;
Expand Down
28 changes: 28 additions & 0 deletions src/meter/__tests__/helpers.test.ts
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);
});
});
2 changes: 1 addition & 1 deletion src/meter/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ export const calculateStatus = (props: CalculateStatusProps) => {
return "safe";
};

const isInRange = (value: number, min: number, max: number) =>
export const isInRange = (value: number, min: number, max: number) =>
value >= min && value <= max;
61 changes: 61 additions & 0 deletions src/number-input/__test__/useSpinner.test.tsx
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
});
22 changes: 22 additions & 0 deletions src/number-input/__test__/utils.test.ts
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);
});
});

0 comments on commit d8dc0f8

Please sign in to comment.