fix(tests): avoid using instance() in tests

This commit is contained in:
Łukasz Mierzwa
2020-04-28 17:09:24 +01:00
committed by Łukasz Mierzwa
parent 0fce699894
commit 55463e9ef5
3 changed files with 23 additions and 20 deletions

View File

@@ -260,17 +260,18 @@ describe("<Fetcher />", () => {
expect(fetchSpy).toHaveBeenCalledWith("", true, "", "", "");
});
it("internal timer is armed after render", () => {
const tree = MountedFetcher();
const instance = tree.instance();
expect(instance.timer).toBeGreaterThanOrEqual(0);
});
it("internal timer is null after unmount", () => {
const tree = MountedFetcher();
const instance = tree.instance();
instance.componentWillUnmount();
expect(instance.timer).toBeNull();
expect(fetchSpy).toHaveBeenCalledTimes(1);
tree.unmount();
expect(fetchSpy).toHaveBeenCalledTimes(1);
settingsStore.gridConfig.config.reverseSort = !settingsStore.gridConfig
.config.reverseSort;
expect(fetchSpy).toHaveBeenCalledTimes(1);
jest.runOnlyPendingTimers();
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
it("doesn't fetch on mount when paused", () => {
@@ -281,16 +282,18 @@ describe("<Fetcher />", () => {
it("doesn't fetch on update when paused", () => {
alertStore.status.pause();
const tree = MountedFetcher();
tree.instance().componentDidUpdate();
MountedFetcher();
settingsStore.gridConfig.config.reverseSort = !settingsStore.gridConfig
.config.reverseSort;
expect(fetchSpy).toHaveBeenCalledTimes(0);
});
it("fetches on update when resumed", () => {
alertStore.status.pause();
const tree = MountedFetcher();
MountedFetcher();
alertStore.status.resume();
tree.instance().componentDidUpdate();
settingsStore.gridConfig.config.reverseSort = !settingsStore.gridConfig
.config.reverseSort;
expect(fetchSpy).toHaveBeenCalledTimes(1);
});

View File

@@ -35,11 +35,9 @@ describe("<ReloadNeeded />", () => {
expect(reloadSpy).toBeCalled();
});
it("timer is cleared on unmount", () => {
it("unmounts cleanly", () => {
const tree = mount(<ReloadNeeded reloadAfter={100000000} />);
const instance = tree.instance();
instance.componentWillUnmount();
expect(instance.timer).toBeNull();
tree.unmount();
jest.runOnlyPendingTimers();
});
});

View File

@@ -40,7 +40,8 @@ describe("<Modal />", () => {
document.body.classList.toggle("modal-open", true);
const tree = MountedModal(false);
expect(document.body.className.split(" ")).toContain("modal-open");
tree.instance().componentDidUpdate();
// force update
tree.setProps({ style: {} });
expect(document.body.className.split(" ")).toContain("modal-open");
});
@@ -51,7 +52,8 @@ describe("<Modal />", () => {
expect(document.body.className.split(" ")).toContain("modal-open");
isOpen = false;
tree.instance().componentDidUpdate();
// force update
tree.setProps({ style: {} });
expect(document.body.className.split(" ")).toContain("modal-open");
});