Increase coverage of hotkey code

pull/1402/head
Rick Carlino 2019-09-03 14:55:56 -05:00
parent 761c6b0444
commit c4ac2e1485
2 changed files with 45 additions and 2 deletions

View File

@ -0,0 +1,43 @@
jest.mock("../history",
() => ({ push: jest.fn() }));
const mockSyncThunk = jest.fn();
jest.mock("../devices/actions",
() => ({ sync: () => mockSyncThunk }));
import { HotKeys } from "../hotkeys";
import { betterCompact } from "../util";
import { push } from "../history";
import { sync } from "../devices/actions";
describe("hotkeys", () => {
it("has key bindings", () => {
const dispatch = jest.fn();
const e = {} as KeyboardEvent;
const comp = new HotKeys({ dispatch });
const hmm = comp.hotkeys(dispatch, "whatever");
const fns = betterCompact(hmm.map(item => item.onKeyDown));
expect(fns.length).toBe(7);
fns[0](e);
expect(dispatch).toHaveBeenCalledWith(sync());
fns[1](e);
expect(push).toHaveBeenCalledWith("/app/designer");
fns[2](e);
expect(push).toHaveBeenCalledWith("/app/messages");
fns[3](e);
expect(push).toHaveBeenCalledWith("/app/designer/plants/crop_search");
fns[4](e);
expect(push).toHaveBeenCalledWith("/app/designer/events/add");
fns[5](e);
expect(push).toHaveBeenCalledWith("/app/designer/plants");
comp.toggle = jest.fn(() => () => { });
fns[6](e);
expect(comp.toggle).toHaveBeenCalledWith("guideOpen");
});
});

View File

@ -55,10 +55,10 @@ export class HotKeys extends React.Component<Props, Partial<State>> {
</Overlay>;
}
private toggle = (property: keyof State) => () =>
toggle = (property: keyof State) => () =>
this.setState({ [property]: !this.state[property] });
private hotkeys(dispatch: Function, slug: string) {
hotkeys(dispatch: Function, slug: string) {
const links = getLinks();
const idx = findIndex(links, { slug });
const right = "/app/" + (links[idx + 1] || links[0]).slug;