148 lines
4.9 KiB
TypeScript
148 lines
4.9 KiB
TypeScript
jest.mock("../../history", () => ({
|
|
push: jest.fn(),
|
|
history: { getCurrentLocation: () => ({ pathname: "" }) }
|
|
}));
|
|
|
|
jest.mock("../set_active_sequence_by_name", () => ({
|
|
setActiveSequenceByName: jest.fn()
|
|
}));
|
|
|
|
jest.mock("../../api/crud", () => ({
|
|
init: jest.fn(),
|
|
destroy: jest.fn(),
|
|
}));
|
|
|
|
let mockDev = false;
|
|
jest.mock("../../account/dev/dev_support", () => ({
|
|
DevSettings: { quickDeleteEnabled: () => mockDev, }
|
|
}));
|
|
|
|
import * as React from "react";
|
|
import { mount, shallow } from "enzyme";
|
|
import { SequencesList } from "../sequences_list";
|
|
import { fakeSequence } from "../../__test_support__/fake_state/resources";
|
|
import { SequencesListProps } from "../interfaces";
|
|
import { Actions } from "../../constants";
|
|
import { init, destroy } from "../../api/crud";
|
|
import { push } from "../../history";
|
|
import { resourceUsageList } from "../../resources/in_use";
|
|
import { buildResourceIndex } from "../../__test_support__/resource_index_builder";
|
|
import { resourceReducer } from "../../resources/reducer";
|
|
import { resourceReady } from "../../sync/actions";
|
|
import { setActiveSequenceByName } from "../set_active_sequence_by_name";
|
|
import { inputEvent } from "../../__test_support__/fake_input_event";
|
|
import { Link } from "../../link";
|
|
|
|
describe("<SequencesList />", () => {
|
|
const fakeSequences = () => {
|
|
const fakeSequence1 = fakeSequence();
|
|
fakeSequence1.body.name = "Sequence 1";
|
|
const fakeSequence2 = fakeSequence();
|
|
fakeSequence2.body.name = "Sequence 2";
|
|
return [fakeSequence1, fakeSequence2];
|
|
};
|
|
|
|
const fakeProps = (sequences = fakeSequences()): SequencesListProps => {
|
|
const { inUse } = resourceReducer(buildResourceIndex(sequences),
|
|
resourceReady("Sequence", sequences)).index;
|
|
return {
|
|
resourceUsage: resourceUsageList(inUse),
|
|
dispatch: jest.fn(),
|
|
sequence: undefined,
|
|
sequences,
|
|
sequenceMetas: {},
|
|
};
|
|
};
|
|
|
|
it("renders list", () => {
|
|
const p = fakeProps();
|
|
const wrapper = mount(<SequencesList {...p} />);
|
|
expect(wrapper.find("input").first().props().placeholder)
|
|
.toContain("Search Sequences");
|
|
["Sequence 1", "Sequence 2"].map(string =>
|
|
expect(wrapper.text()).toContain(string));
|
|
});
|
|
|
|
it("has correct drag data", () => {
|
|
const p = fakeProps();
|
|
const wrapper = mount(<SequencesList {...p} />);
|
|
const seq = wrapper.find("div").last();
|
|
expect(seq.text()).toEqual("Sequence 2");
|
|
seq.simulate("dragStart", { dataTransfer: { setData: jest.fn() } });
|
|
expect(p.dispatch).toHaveBeenCalledWith({
|
|
type: Actions.PUT_DATA_XFER,
|
|
payload: expect.objectContaining({
|
|
value: {
|
|
kind: "execute",
|
|
args: { sequence_id: p.sequences[1].body.id },
|
|
}
|
|
})
|
|
});
|
|
});
|
|
|
|
it("shows in-use indicator", () => {
|
|
const fakeSequence1 = fakeSequence();
|
|
fakeSequence1.body.name = "Sequence 1";
|
|
fakeSequence1.body.id = 1;
|
|
|
|
const fakeSequence2 = fakeSequence();
|
|
fakeSequence2.body.name = "Sequence 2";
|
|
fakeSequence2.body.id = 2;
|
|
fakeSequence2.body.body =
|
|
[{ kind: "execute", args: { sequence_id: fakeSequence1.body.id } }];
|
|
|
|
const p = fakeProps([fakeSequence1, fakeSequence2]);
|
|
|
|
const wrapper = mount(<SequencesList {...p} />);
|
|
expect(wrapper.find(".in-use").length).toEqual(1);
|
|
});
|
|
|
|
it("doesn't show in-use indicator", () => {
|
|
const p = fakeProps();
|
|
const wrapper = mount(<SequencesList {...p} />);
|
|
expect(wrapper.find(".in-use").length).toEqual(0);
|
|
});
|
|
|
|
it("adds new sequence", () => {
|
|
const wrapper = mount(<SequencesList {...fakeProps()} />);
|
|
wrapper.find("button").first().simulate("click");
|
|
expect(init).toHaveBeenCalledWith("Sequence",
|
|
expect.objectContaining({ body: [] }));
|
|
expect(push).toHaveBeenCalledWith("/app/sequences/new_sequence_2");
|
|
});
|
|
|
|
it("sets search term", () => {
|
|
const wrapper = shallow<SequencesList>(<SequencesList {...fakeProps()} />);
|
|
expect(wrapper.state().searchTerm).toEqual("");
|
|
wrapper.instance().onChange(inputEvent("search this"));
|
|
expect(wrapper.instance().state.searchTerm).toEqual("search this");
|
|
});
|
|
|
|
it("opens sequence", () => {
|
|
const p = fakeProps();
|
|
const wrapper = mount(<SequencesList {...p} />);
|
|
wrapper.find(Link).first().simulate("click");
|
|
expect(setActiveSequenceByName).toHaveBeenCalled();
|
|
});
|
|
|
|
it("doesn't delete sequence", () => {
|
|
mockDev = false;
|
|
const p = fakeProps([fakeSequence()]);
|
|
const wrapper = mount(<SequencesList {...p} />);
|
|
const button = wrapper.find("button").last();
|
|
expect(button.hasClass("quick-del")).toBeFalsy();
|
|
button.simulate("click");
|
|
expect(destroy).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("deletes sequence", () => {
|
|
mockDev = true;
|
|
const p = fakeProps([fakeSequence()]);
|
|
const wrapper = mount(<SequencesList {...p} />);
|
|
const button = wrapper.find("button").last();
|
|
expect(button.hasClass("quick-del")).toBeTruthy();
|
|
button.simulate("click");
|
|
expect(destroy).toHaveBeenCalledWith(p.sequences[0].uuid);
|
|
});
|
|
});
|