Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️🧪 Refactor test cases with SafeElementWrapper querySelector chain to eliminate the unnecessary temporary variable #5062

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 57 additions & 50 deletions src/test/timepicker_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import React from "react";
import { formatDate, KeyType } from "../date_utils";
import DatePicker from "../index";

import { getKey, safeQuerySelector, safeQuerySelectorAll } from "./test_utils";
import {
getKey,
SafeElementWrapper,
safeQuerySelector,
safeQuerySelectorAll,
} from "./test_utils";

const MIN_TIME_LI_LEN = 2;

Expand Down Expand Up @@ -38,12 +43,11 @@ describe("TimePicker", () => {

fireEvent.focus(instance.input);

const time = safeQuerySelector(
datePicker,
".react-datepicker__time-container",
);
const lis = safeQuerySelectorAll(time, "li", MIN_TIME_LI_LEN);
fireEvent.click(lis[1]!);
const timeOption = new SafeElementWrapper(datePicker)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on your preferences and repo contribution guidelines (some folks have a strong preference for specs to be verbose rather than DRY), you could extract the common logic for this into a separate private function and re-use it throughout your tests, like:

describe("TimePicker", () => {
  let datePicker: HTMLDivElement;
  let div: HTMLDivElement;
  let onChangeMoment: Date | undefined;
  let instance: DatePicker | null = null;

  const timeOption: (datePicker: HTMLDivElement): SafeElementWrapper<HTMLDivElement> | undefined = (datePicker) => {
    return new SafeElementWrapper(datePicker)
      .safeQuerySelector(".react-datepicker__time-container')
      .safeQuerySelectorAll("li", MIN_TIME_LI_LEN)[1]? || undefined;
  }

  beforeEach(() => {
    div = document.createElement("div");
  });

  // ...

  it("should allow time changes after input change", () => {
    renderDatePicker("February 28, 2018 4:43 PM");
    setManually("February 28, 2018 4:45 PM");
    if (!instance?.input) {
      throw new Error("input is null/undefined");
    }
    fireEvent.focus(instance.input);
    fireEvent.click(timeOption(datePicker)); // can just use this in other tests within this `describe` block

    expect(getInputString()).toBe("February 28, 2018 12:30 AM");
  });

Note that I'm just writing this in a comment box, not in a live coding environment—you may have to check and massage the type signatures a bit 😅

🔹 Code Reuse (Nice to have)

Image of Dylan F Dylan F

.safeQuerySelector(".react-datepicker__time-container")
.safeQuerySelectorAll("li", MIN_TIME_LI_LEN)[1]!
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ESlint is complaining a bit about your usage of the non-null assertion (!) here.

Looking over the definition and types on SafeElementWrapper and its functions, I can see why you need the bang/!; there's no guarentee that safeQuerySelectorAll's returned array will have an element at index 1, so you do need to do something to make the types work.

Per https://typescript-eslint.io/rules/no-non-null-assertion/, you can either tell eslint to skip the rule for this line (it's a test, not user-facing code), or rewrite to satisfy the linter with something like:

const timeOption = new SafeElementWrapper(datePicker)
  .safeQuerySelector(".react-datepicker__time-container")
  .safeQuerySelectorAll("li", MIN_TIME_LI_LEN)[1]?
  .getElement() || undefined;
timeOption && fireEvent.click(timeOption);

You may have other preferences around how to handle this; note that passing undefined to fireEvent.click will at the least throw an error at runtime, and TS might throw a typeerror at it depending on what types fireEvent.click accepts.

This same comment applies to your other changes in this file as well, at #188, #203, !221, #258, #277, and #296.

🔸 Giving Information (Important)

Image of Dylan F Dylan F

.getElement();
fireEvent.click(timeOption);
expect(getInputString()).toBe("February 28, 2018 12:30 AM");
});

Expand Down Expand Up @@ -178,12 +182,13 @@ describe("TimePicker", () => {
throw new Error("input is null/undefined");
}
fireEvent.focus(instance.input);
const time = safeQuerySelector(
datePicker,
".react-datepicker__time-container",
);
const lis = safeQuerySelectorAll(time, "li", MIN_TIME_LI_LEN);
fireEvent.keyDown(lis[1]!, getKey(KeyType.Enter));

const timeOption = new SafeElementWrapper(datePicker)
.safeQuerySelector(".react-datepicker__time-container")
.safeQuerySelectorAll("li", MIN_TIME_LI_LEN)[1]!
.getElement();
fireEvent.keyDown(timeOption, getKey(KeyType.Enter));

expect(getInputString()).toBe("February 28, 2018 12:30 AM");
});

Expand All @@ -193,12 +198,11 @@ describe("TimePicker", () => {
throw new Error("input is null/undefined");
}
fireEvent.focus(instance.input);
const time = safeQuerySelector(
datePicker,
".react-datepicker__time-container",
);
const lis = safeQuerySelectorAll(time, "li", MIN_TIME_LI_LEN);
fireEvent.keyDown(lis[1]!, getKey(KeyType.Space));
const timeOption = new SafeElementWrapper(datePicker)
.safeQuerySelector(".react-datepicker__time-container")
.safeQuerySelectorAll("li", MIN_TIME_LI_LEN)[1]!
.getElement();
fireEvent.keyDown(timeOption, getKey(KeyType.Space));
expect(getInputString()).toBe("February 28, 2018 12:30 AM");
});

Expand All @@ -210,14 +214,13 @@ describe("TimePicker", () => {
}

const input = safeQuerySelector(datePicker, "input");

fireEvent.focus(instance.input);
const time = safeQuerySelector(
datePicker,
".react-datepicker__time-container",
);
const lis = safeQuerySelectorAll(time, "li", MIN_TIME_LI_LEN);
fireEvent.keyDown(lis[1]!, getKey(KeyType.Enter));

const timeOption = new SafeElementWrapper(datePicker)
.safeQuerySelector(".react-datepicker__time-container")
.safeQuerySelectorAll("li", MIN_TIME_LI_LEN)[1]!
.getElement();
fireEvent.keyDown(timeOption, getKey(KeyType.Enter));

await waitFor(() => {
expect(document.activeElement).toBe(input);
Expand All @@ -230,12 +233,13 @@ describe("TimePicker", () => {
throw new Error("input is null/undefined");
}
fireEvent.focus(instance.input);
const time = safeQuerySelector(
datePicker,
".react-datepicker__time-container",
);
const lis = safeQuerySelectorAll(time, "li", MIN_TIME_LI_LEN);
fireEvent.keyDown(lis[1]!, getKey(KeyType.Escape));

const timeOption = new SafeElementWrapper(datePicker)
.safeQuerySelector(".react-datepicker__time-container")
.safeQuerySelectorAll("li", MIN_TIME_LI_LEN)[1]!
.getElement();
fireEvent.keyDown(timeOption, getKey(KeyType.Escape));

expect(getInputString()).toBe("February 28, 2018 4:43 PM");
});

Expand All @@ -248,12 +252,13 @@ describe("TimePicker", () => {
throw new Error("input is null/undefined");
}
fireEvent.focus(instance.input);
const time = safeQuerySelector(
datePicker,
".react-datepicker__time-container",
);
const lis = safeQuerySelectorAll(time, "li", MIN_TIME_LI_LEN);
fireEvent.keyDown(lis[1]!, getKey(KeyType.Escape));

const timeOption = new SafeElementWrapper(datePicker)
.safeQuerySelector(".react-datepicker__time-container")
.safeQuerySelectorAll("li", MIN_TIME_LI_LEN)[1]!
.getElement();
fireEvent.keyDown(timeOption, getKey(KeyType.Escape));

expect(onKeyDownSpy).toHaveBeenCalledTimes(1);
});

Expand All @@ -266,12 +271,13 @@ describe("TimePicker", () => {
throw new Error("input is null/undefined");
}
fireEvent.focus(instance.input);
const time = safeQuerySelector(
datePicker,
".react-datepicker__time-container",
);
const lis = safeQuerySelectorAll(time, "li", MIN_TIME_LI_LEN);
fireEvent.keyDown(lis[1]!, getKey(KeyType.Enter));

const timeOption = new SafeElementWrapper(datePicker)
.safeQuerySelector(".react-datepicker__time-container")
.safeQuerySelectorAll("li", MIN_TIME_LI_LEN)[1]!
.getElement();
fireEvent.keyDown(timeOption, getKey(KeyType.Enter));

expect(onKeyDownSpy).toHaveBeenCalledTimes(1);
});

Expand All @@ -284,12 +290,13 @@ describe("TimePicker", () => {
throw new Error("input is null/undefined");
}
fireEvent.focus(instance.input);
const time = safeQuerySelector(
datePicker,
".react-datepicker__time-container",
);
const lis = safeQuerySelectorAll(time, "li", MIN_TIME_LI_LEN);
fireEvent.keyDown(lis[1]!, getKey(KeyType.Space));

const timeOption = new SafeElementWrapper(datePicker)
.safeQuerySelector(".react-datepicker__time-container")
.safeQuerySelectorAll("li", MIN_TIME_LI_LEN)[1]!
.getElement();
fireEvent.keyDown(timeOption, getKey(KeyType.Space));

expect(onKeyDownSpy).toHaveBeenCalledTimes(1);
});

Expand Down
20 changes: 9 additions & 11 deletions src/test/year_picker_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getKey,
gotoNextView,
openDateInput,
SafeElementWrapper,
safeQuerySelector,
safeQuerySelectorAll,
} from "./test_utils";
Expand Down Expand Up @@ -578,11 +579,10 @@ describe("YearPicker", () => {
const input = safeQuerySelector(container, "input");
fireEvent.focus(input);

const calendar = safeQuerySelector(container, ".react-datepicker");
const previousButton = safeQuerySelector(
calendar,
".react-datepicker__navigation--previous",
);
const previousButton = new SafeElementWrapper(container)
.safeQuerySelector(".react-datepicker")
.safeQuerySelector(".react-datepicker__navigation--previous")
.getElement();
fireEvent.click(previousButton);

const year = container.querySelector(".react-datepicker__year");
Expand Down Expand Up @@ -611,12 +611,10 @@ describe("YearPicker", () => {
const input = safeQuerySelector(container, "input");
fireEvent.focus(input);

const calendar = safeQuerySelector(container, ".react-datepicker");
const nextButton = safeQuerySelector(
calendar,
".react-datepicker__navigation--next",
);

const nextButton = new SafeElementWrapper(container)
.safeQuerySelector(".react-datepicker")
.safeQuerySelector(".react-datepicker__navigation--next")
.getElement();
fireEvent.click(nextButton);

const year = container.querySelector(".react-datepicker__year");
Expand Down
Loading