Skip to content

Commit

Permalink
chore(meter-test): ✅ improve test with jest-in-case
Browse files Browse the repository at this point in the history
  • Loading branch information
navin-moorthy committed Sep 15, 2020
1 parent 5bc0c66 commit 826ec63
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 336 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@react-aria/link": "3.1.1",
"@react-aria/utils": "3.2.1",
"@react-stately/toggle": "3.2.0",
"@types/jest-in-case": "^1.0.2",
"emotion": "10.0.27",
"react-use-gesture": "7.0.16",
"reakit": "1.2.4",
Expand Down Expand Up @@ -78,6 +79,7 @@
"husky": "4.3.0",
"jest": "26.4.2",
"jest-axe": "4.0.0",
"jest-in-case": "^1.0.2",
"jest-matcher-utils": "26.4.2",
"lint-staged": "10.3.0",
"prettier": "2.1.1",
Expand Down
346 changes: 10 additions & 336 deletions src/meter/__examples__/__tests__/Meter.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as React from "react";
import cases from "jest-in-case";
import { render } from "reakit-test-utils";
import { renderHook } from "reakit-test-utils/hooks";
import { jestSerializerStripFunctions } from "reakit-test-utils/jestSerializerStripFunctions";

import { MeterComp } from "../index";
import { data } from "./statehook-test-data";
import { useMeterState, UseMeterProps } from "../../index";

expect.addSnapshotSerializer(jestSerializerStripFunctions);
Expand Down Expand Up @@ -100,340 +102,12 @@ describe("Meter", function () {
expect(meter).toBeInTheDocument();
});

it("meter state initial state", function () {
const result = renderMeterStateHook();

expect(result.current).toMatchObject({
high: 1,
low: 0,
max: 1,
min: 0,
optimum: 0.5,
percent: 0,
status: "safe",
value: 0,
});
});

it("meter state with value", function () {
const result = renderMeterStateHook({ value: 0.5 });

expect(result.current).toMatchObject({
high: 1,
low: 0,
max: 1,
min: 0,
optimum: 0.5,
percent: 50,
status: "safe",
value: 0.5,
});
});

it("meter state with custom props", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 5,
high: 7.5,
max: 10,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 5,
percent: 50,
status: "safe",
value: 5,
});
});

it("meter state with optimum < min", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 5,
high: 7.5,
max: 10,
optimum: -5,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 0,
percent: 50,
status: "caution",
value: 5,
});
});

it("meter state with optimum > max", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 5,
high: 7.5,
max: 10,
optimum: 15,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 10,
percent: 50,
status: "caution",
value: 5,
});
});

it("meter state with optimum between low & high and value at same range", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 5,
high: 7.5,
max: 10,
optimum: 5,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 5,
percent: 50,
status: "safe",
value: 5,
});
});

it("meter state with optimum between low & high and value below low", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 2,
high: 7.5,
max: 10,
optimum: 5,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 5,
percent: 20,
status: "caution",
value: 2,
});
});

it("meter state with optimum between low & high and value above high", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 8,
high: 7.5,
max: 10,
optimum: 5,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 5,
percent: 80,
status: "caution",
value: 8,
});
});

it("meter state with optimum at high", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 5,
high: 7.5,
max: 10,
optimum: 7.5,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 7.5,
percent: 50,
status: "safe",
value: 5,
});
});

it("meter state with optimum at low", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 5,
high: 7.5,
max: 10,
optimum: 2.5,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 2.5,
percent: 50,
status: "safe",
value: 5,
});
});

it("meter state with optimum < low & >= min and value at same range", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 0,
high: 7.5,
max: 10,
optimum: 2,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 2,
percent: 0,
status: "safe",
value: 0,
});
});

it("meter state with optimum < low & >= min and value > low", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 5,
high: 7.5,
max: 10,
optimum: 2,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 2,
percent: 50,
status: "caution",
value: 5,
});
});

it("meter state with optimum < low & >= min and value > high", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 8,
high: 7.5,
max: 10,
optimum: 2,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 2,
percent: 80,
status: "danger",
value: 8,
});
});

it("meter state with optimum <= max & >= high & value at same range", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 8,
high: 7.5,
max: 10,
optimum: 9,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 9,
percent: 80,
status: "safe",
value: 8,
});
});

it("meter state with optimum <= max & >= high & value < high", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 5,
high: 7.5,
max: 10,
optimum: 9,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 9,
percent: 50,
status: "caution",
value: 5,
});
});

it("meter state with optimum <= max & >= high & value < low", function () {
const result = renderMeterStateHook({
min: 0,
low: 2.5,
value: 2,
high: 7.5,
max: 10,
optimum: 9,
});

expect(result.current).toMatchObject({
high: 7.5,
low: 2.5,
max: 10,
min: 0,
optimum: 9,
percent: 20,
status: "danger",
value: 2,
});
});
cases(
"meter state hook tests",
(opts: any) => {
const result = renderMeterStateHook(opts.in);
expect(result.current).toMatchObject(opts.out);
},
data,
);
});
Loading

0 comments on commit 826ec63

Please sign in to comment.