createCB tests

pull/1575/head
Rick Carlino 2019-11-19 09:31:04 -06:00
parent 22f90b08fa
commit 19c7ef2f36
2 changed files with 23 additions and 6 deletions

View File

@ -1,8 +1,9 @@
import * as React from "react";
import { testGrid } from "./generate_grid_test";
import { GridInput, InputCell, InputCellProps } from "../grid_input";
import { GridInput, InputCell, InputCellProps, createCB } from "../grid_input";
import { mount, shallow } from "enzyme";
import { BlurableInput } from "../../../../ui/blurable_input";
import { DeepPartial } from "redux";
describe("<GridInput/>", () => {
it("renders", () => {
@ -33,3 +34,18 @@ describe("<InputCell/>", () => {
expect(p.onChange).toHaveBeenCalledWith(p.gridKey, 6);
});
});
describe("createCB", () => {
it("creates a callback", () => {
type E = React.ChangeEvent<HTMLInputElement>;
const e: DeepPartial<E> = {
currentTarget: {
value: "7"
}
};
const dispatch = jest.fn();
const cb = createCB("numPlantsH", dispatch);
cb(e as E);
expect(dispatch).toHaveBeenCalledWith("numPlantsH", 7);
});
});

View File

@ -67,11 +67,12 @@ const LABELS: Record<PlantGridKey, LabelData> = {
},
};
const createCB = (key: PlantGridKey, cb: GridInputProps["onChange"]) =>
(x: React.ChangeEvent<HTMLInputElement>) => {
const number = parseInt(x.currentTarget.value, 10);
(!isNaN(number)) && cb(key, number);
};
export const createCB =
(key: PlantGridKey, cb: GridInputProps["onChange"]) =>
(x: React.ChangeEvent<HTMLInputElement>) => {
const number = parseInt(x.currentTarget.value, 10);
(!isNaN(number)) && cb(key, number);
};
export function InputCell({ gridKey, onChange, grid, xy_swap }: InputCellProps) {
const data = LABELS[gridKey];