Skip to content

Commit

Permalink
style: simplify test assertions (#6983)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvandercar-vt authored Sep 18, 2024
1 parent 66a4ca4 commit dbb1adb
Show file tree
Hide file tree
Showing 31 changed files with 160 additions and 163 deletions.
4 changes: 2 additions & 2 deletions packages/core/test/alert/alertTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ describe("<Alert>", () => {
wrapper.find(Button).simulate("click");
assert.isTrue(onConfirm.calledOnce);
assert.isTrue(onClose.calledOnce);
assert.strictEqual(onClose.args[0][0], true);
assert.isTrue(onClose.args[0][0]);
});
});

Expand Down Expand Up @@ -178,7 +178,7 @@ describe("<Alert>", () => {
cancelButton.simulate("click");
assert.isTrue(onCancel.calledOnce);
assert.isTrue(onClose.calledOnce);
assert.strictEqual(onClose.args[0][0], false);
assert.isFalse(onClose.args[0][0]);
});

it("canEscapeKeyCancel enables escape key", () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/core/test/buttons/buttonTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ function buttonTestSuite(component: React.FC<any>, tagName: string) {
it("wraps string children in spans", () => {
// so text can be hidden when loading
const wrapper = button({}, "raw string", <em>not a string</em>);
assert.equal(wrapper.find("span").length, 1, "span not found");
assert.equal(wrapper.find("em").length, 1, "em not found");
assert.lengthOf(wrapper.find("span"), 1, "span not found");
assert.lengthOf(wrapper.find("em"), 1, "em not found");
});

it("renders span if text={0}", () => {
Expand All @@ -76,12 +76,12 @@ function buttonTestSuite(component: React.FC<any>, tagName: string) {

it('doesn\'t render a text span if children=""', () => {
const wrapper = button({}, "");
assert.equal(wrapper.find("span").length, 0);
assert.lengthOf(wrapper.find("span"), 0);
});

it('doesn\'t render a text span if text=""', () => {
const wrapper = button({ text: "" });
assert.equal(wrapper.find("span").length, 0);
assert.lengthOf(wrapper.find("span"), 0);
});

it("accepts textClassName prop", () => {
Expand Down Expand Up @@ -193,8 +193,8 @@ function buttonTestSuite(component: React.FC<any>, tagName: string) {

// check that the callback was invoked with modifier key flags included
assert.equal(callback.callCount, 1);
assert.equal(callback.firstCall.args[0].shiftKey, true);
assert.equal(callback.firstCall.args[0].metaKey, true);
assert.isTrue(callback.firstCall.args[0].shiftKey);
assert.isTrue(callback.firstCall.args[0].metaKey);
}
});
}
2 changes: 1 addition & 1 deletion packages/core/test/forms/formGroupTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("<FormGroup>", () => {

it("hides label when falsy", () => {
const label = shallow(<FormGroup />).find("label");
assert.strictEqual(label.length, 0);
assert.lengthOf(label, 0);
});

it("labelInfo=JSX renders JSX content in label", () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/test/menu/menuItemTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ describe("MenuItem", () => {
it("can set roleStructure to change role prop structure to that of a listbox or select item", () => {
const wrapper = mount(<MenuItem text="Roles" roleStructure="listoption" />);
assert.equal(wrapper.find("li").prop("role"), "option");
assert.equal(wrapper.find("a").prop("role"), undefined);
assert.isUndefined(wrapper.find("a").prop("role"));
});

it("can set roleStructure to change role prop structure to that of a list item", () => {
const wrapper = mount(<MenuItem text="Roles" roleStructure="listitem" />);
assert.equal(wrapper.find("li").prop("role"), undefined);
assert.equal(wrapper.find("a").prop("role"), undefined);
assert.isUndefined(wrapper.find("li").prop("role"));
assert.isUndefined(wrapper.find("a").prop("role"));
});

it('can set roleStructure to change role prop structure to void li role (set role="none")', () => {
const wrapper = mount(<MenuItem text="Roles" roleStructure="none" />);
assert.equal(wrapper.find("li").prop("role"), "none");
assert.equal(wrapper.find("a").prop("role"), undefined);
assert.isUndefined(wrapper.find("a").prop("role"));
});

it("disabled MenuItem will not show its submenu", () => {
Expand Down
48 changes: 24 additions & 24 deletions packages/core/test/multistep-dialog/multistepDialogTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ describe("<MultistepDialog>", () => {
);
assert.strictEqual(dialog.state("selectedIndex"), 0);
const steps = dialog.find(`.${Classes.DIALOG_STEP_CONTAINER}`);
assert.strictEqual(steps.at(0).find(`.${Classes.ACTIVE}`).length, 1);
assert.strictEqual(steps.at(1).find(`.${Classes.ACTIVE}`).length, 0);
assert.lengthOf(steps.at(0).find(`.${Classes.ACTIVE}`), 1);
assert.lengthOf(steps.at(1).find(`.${Classes.ACTIVE}`), 0);
dialog.unmount();
});

Expand All @@ -73,8 +73,8 @@ describe("<MultistepDialog>", () => {
findButtonWithText(dialog, "Next").simulate("click");
assert.strictEqual(dialog.state("selectedIndex"), 1);
const steps = dialog.find(`.${Classes.DIALOG_STEP_CONTAINER}`);
assert.strictEqual(steps.at(0).find(`.${Classes.DIALOG_STEP_VIEWED}`).length, 1);
assert.strictEqual(steps.at(1).find(`.${Classes.ACTIVE}`).length, 1);
assert.lengthOf(steps.at(0).find(`.${Classes.DIALOG_STEP_VIEWED}`), 1);
assert.lengthOf(steps.at(1).find(`.${Classes.ACTIVE}`), 1);
dialog.unmount();
});

Expand All @@ -89,14 +89,14 @@ describe("<MultistepDialog>", () => {
findButtonWithText(dialog, "Next").simulate("click");
assert.strictEqual(dialog.state("selectedIndex"), 1);
const steps = dialog.find(`.${Classes.DIALOG_STEP_CONTAINER}`);
assert.strictEqual(steps.at(0).find(`.${Classes.DIALOG_STEP_VIEWED}`).length, 1);
assert.strictEqual(steps.at(1).find(`.${Classes.ACTIVE}`).length, 1);
assert.lengthOf(steps.at(0).find(`.${Classes.DIALOG_STEP_VIEWED}`), 1);
assert.lengthOf(steps.at(1).find(`.${Classes.ACTIVE}`), 1);

findButtonWithText(dialog, "Back").simulate("click");
const newSteps = dialog.find(`.${Classes.DIALOG_STEP_CONTAINER}`);
assert.strictEqual(dialog.state("selectedIndex"), 0);
assert.strictEqual(newSteps.at(0).find(`.${Classes.ACTIVE}`).length, 1);
assert.strictEqual(newSteps.at(1).find(`.${Classes.DIALOG_STEP_VIEWED}`).length, 1);
assert.lengthOf(newSteps.at(0).find(`.${Classes.ACTIVE}`), 1);
assert.lengthOf(newSteps.at(1).find(`.${Classes.DIALOG_STEP_VIEWED}`), 1);
dialog.unmount();
});

Expand All @@ -109,9 +109,9 @@ describe("<MultistepDialog>", () => {
);
findButtonWithText(dialog, "Next").simulate("click");
assert.strictEqual(dialog.state("selectedIndex"), 1);
assert.strictEqual(findButtonWithText(dialog, "Back").length, 1);
assert.strictEqual(findButtonWithText(dialog, "Next").length, 0);
assert.strictEqual(findButtonWithText(dialog, "Submit").length, 1);
assert.lengthOf(findButtonWithText(dialog, "Back"), 1);
assert.lengthOf(findButtonWithText(dialog, "Next"), 0);
assert.lengthOf(findButtonWithText(dialog, "Submit"), 1);
dialog.unmount();
});

Expand All @@ -124,9 +124,9 @@ describe("<MultistepDialog>", () => {
);

assert.strictEqual(dialog.state("selectedIndex"), 0);
assert.strictEqual(findButtonWithText(dialog, "Back").length, 0);
assert.strictEqual(findButtonWithText(dialog, "Next").length, 1);
assert.strictEqual(findButtonWithText(dialog, "Submit").length, 0);
assert.lengthOf(findButtonWithText(dialog, "Back"), 0);
assert.lengthOf(findButtonWithText(dialog, "Next"), 1);
assert.lengthOf(findButtonWithText(dialog, "Submit"), 0);
dialog.unmount();
});

Expand All @@ -138,9 +138,9 @@ describe("<MultistepDialog>", () => {
);

assert.strictEqual(dialog.state("selectedIndex"), 0);
assert.strictEqual(findButtonWithText(dialog, "Back").length, 0);
assert.strictEqual(findButtonWithText(dialog, "Next").length, 0);
assert.strictEqual(findButtonWithText(dialog, "Submit").length, 1);
assert.lengthOf(findButtonWithText(dialog, "Back"), 0);
assert.lengthOf(findButtonWithText(dialog, "Next"), 0);
assert.lengthOf(findButtonWithText(dialog, "Submit"), 1);
dialog.unmount();
});

Expand All @@ -158,8 +158,8 @@ describe("<MultistepDialog>", () => {
step.at(0).simulate("click");
const steps = dialog.find(`.${Classes.DIALOG_STEP_CONTAINER}`);
assert.strictEqual(dialog.state("selectedIndex"), 0);
assert.strictEqual(steps.at(0).find(`.${Classes.ACTIVE}`).length, 1);
assert.strictEqual(steps.at(1).find(`.${Classes.DIALOG_STEP_VIEWED}`).length, 1);
assert.lengthOf(steps.at(0).find(`.${Classes.ACTIVE}`), 1);
assert.lengthOf(steps.at(1).find(`.${Classes.DIALOG_STEP_VIEWED}`), 1);
dialog.unmount();
});

Expand Down Expand Up @@ -212,7 +212,7 @@ describe("<MultistepDialog>", () => {
<DialogStep id="two" title="Step 2" panel={<Panel />} />
</MultistepDialog>,
);
assert.strictEqual(findButtonWithText(dialog, "Next").prop("disabled"), undefined);
assert.isUndefined(findButtonWithText(dialog, "Next").prop("disabled"));
dialog.unmount();
});

Expand All @@ -223,7 +223,7 @@ describe("<MultistepDialog>", () => {
<DialogStep id="two" title="Step 2" panel={<Panel />} />
</MultistepDialog>,
);
assert.strictEqual(findButtonWithText(dialog, "Next").prop("disabled"), true);
assert.isTrue(findButtonWithText(dialog, "Next").prop("disabled"));
dialog.unmount();
});

Expand All @@ -237,10 +237,10 @@ describe("<MultistepDialog>", () => {
);

assert.strictEqual(dialog.state("selectedIndex"), 0);
assert.strictEqual(findButtonWithText(dialog, "Next").prop("disabled"), undefined);
assert.isUndefined(findButtonWithText(dialog, "Next").prop("disabled"));
findButtonWithText(dialog, "Next").simulate("click");
assert.strictEqual(dialog.state("selectedIndex"), 1);
assert.strictEqual(findButtonWithText(dialog, "Next").prop("disabled"), true);
assert.isTrue(findButtonWithText(dialog, "Next").prop("disabled"));
findButtonWithText(dialog, "Next").simulate("click");
assert.strictEqual(dialog.state("selectedIndex"), 1);
dialog.unmount();
Expand All @@ -258,7 +258,7 @@ describe("<MultistepDialog>", () => {
assert.strictEqual(dialog.state("selectedIndex"), 0);
findButtonWithText(dialog, "Next").simulate("click");
assert.strictEqual(dialog.state("selectedIndex"), 1);
assert.strictEqual(findButtonWithText(dialog, "Back").prop("disabled"), true);
assert.isTrue(findButtonWithText(dialog, "Back").prop("disabled"));
findButtonWithText(dialog, "Back").simulate("click");
assert.strictEqual(dialog.state("selectedIndex"), 1);
dialog.unmount();
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/panel-stack/panelStackTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe("<PanelStack>", () => {
it("does not have the back button when only a single panel is on the stack", () => {
panelStackWrapper = renderPanelStack({ initialPanel });
const backButton = panelStackWrapper.findClass(Classes.PANEL_STACK_HEADER_BACK);
assert.equal(backButton.length, 0);
assert.lengthOf(backButton, 0);
});

it("assigns the class to TransitionGroup", () => {
Expand Down Expand Up @@ -252,7 +252,7 @@ describe("<PanelStack>", () => {

const panelHeaders = panelStackWrapper.findClass(Classes.HEADING);
assert.exists(panelHeaders);
assert.equal(panelHeaders.length, 1);
assert.lengthOf(panelHeaders, 1);
assert.equal(panelHeaders.at(0).text(), stack[1].title);
});

Expand All @@ -265,7 +265,7 @@ describe("<PanelStack>", () => {

const panelHeaders = panelStackWrapper.findClass(Classes.HEADING);
assert.exists(panelHeaders);
assert.equal(panelHeaders.length, 2);
assert.lengthOf(panelHeaders, 2);
assert.equal(panelHeaders.at(0).text(), stack[0].title);
assert.equal(panelHeaders.at(1).text(), stack[1].title);
});
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/panel-stack2/panelStack2Tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe("<PanelStack2>", () => {
it("does not have the back button when only a single panel is on the stack", () => {
panelStackWrapper = renderPanelStack({ initialPanel });
const backButton = panelStackWrapper.findClass(Classes.PANEL_STACK2_HEADER_BACK);
assert.equal(backButton.length, 0);
assert.lengthOf(backButton, 0);
});

it("assigns the class to TransitionGroup", () => {
Expand Down Expand Up @@ -252,7 +252,7 @@ describe("<PanelStack2>", () => {

const panelHeaders = panelStackWrapper.findClass(Classes.HEADING);
assert.exists(panelHeaders);
assert.equal(panelHeaders.length, 1);
assert.lengthOf(panelHeaders, 1);
assert.equal(panelHeaders.at(0).text(), stack[1].title);
});

Expand All @@ -266,7 +266,7 @@ describe("<PanelStack2>", () => {

const panelHeaders = panelStackWrapper.findClass(Classes.HEADING);
assert.exists(panelHeaders);
assert.equal(panelHeaders.length, 2);
assert.lengthOf(panelHeaders, 2);
assert.equal(panelHeaders.at(0).text(), stack[0].title);
assert.equal(panelHeaders.at(1).text(), stack[1].title);
});
Expand Down
9 changes: 3 additions & 6 deletions packages/core/test/popover/popoverTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ describe("<Popover>", () => {
it("moves focus to overlay when opened", done => {
function handleOpened() {
assert.notEqual(document.activeElement, document.body, "body element should not have focus");
assert.isTrue(
document.activeElement?.closest(`.${Classes.OVERLAY}`) !== null,
assert.isNotNull(
document.activeElement?.closest(`.${Classes.OVERLAY}`),
"focus should be inside overlay",
);
done();
Expand All @@ -271,10 +271,7 @@ describe("<Popover>", () => {
function handleClosed(wrapper2: PopoverWrapper) {
wrapper2.assertIsOpen(false);
assert.notEqual(document.activeElement, document.body, "body element should not have focus");
assert.isTrue(
document.activeElement?.closest(`.${targetClassName}`) != null,
"focus should be on target",
);
assert.isNotNull(document.activeElement?.closest(`.${targetClassName}`), "focus should be on target");
}

wrapper = renderPopover(commonProps);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/text/textTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe("<Text>", () => {
});
wrapper = wrapper.update();
const actualTitle = wrapper.find(`.${Classes.TEXT_OVERFLOW_ELLIPSIS}`).prop("title");
assert.strictEqual(actualTitle, undefined, "title should be undefined");
assert.isUndefined(actualTitle, "title should be undefined");
});

it("uses given title even if text overflows", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/tree/treeTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ describe("<Tree>", () => {
const tree = renderTree({ contents });
const disabledTreeNode = tree.find(`.${Classes.TREE_NODE}.c0.${Classes.DISABLED}`);

assert.equal(disabledTreeNode.length, 1);
assert.lengthOf(disabledTreeNode, 1);
});

it("icons are rendered correctly if present", () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/datetime/test/components/dateInputTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe("<DateInput>", () => {
focusInput(wrapper);

const input = wrapper.find(InputGroup);
assert.strictEqual(input.prop("fill"), true);
assert.isTrue(input.prop("fill"));
assert.strictEqual(input.prop("leftIcon"), "star");
assert.isTrue(input.prop("required"));
assert.isTrue(inputRef.called, "inputRef not invoked");
Expand All @@ -189,7 +189,7 @@ describe("<DateInput>", () => {

const popover = wrapper.find(Popover).first();
assert.strictEqual(popover.prop("placement"), "top");
assert.strictEqual(popover.prop("usePortal"), false);
assert.isFalse(popover.prop("usePortal"));
assert.isTrue(onOpening.calledOnce);
});

Expand Down
4 changes: 2 additions & 2 deletions packages/datetime2/test/components/dateInput3Tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe("<DateInput3>", () => {
focusInput(wrapper);

const input = wrapper.find(InputGroup);
assert.strictEqual(input.prop("fill"), true);
assert.isTrue(input.prop("fill"));
assert.strictEqual(input.prop("leftIcon"), "star");
assert.isTrue(input.prop("required"));
assert.isTrue(inputRef.called, "inputRef not invoked");
Expand All @@ -189,7 +189,7 @@ describe("<DateInput3>", () => {

const popover = wrapper.find(Popover).first();
assert.strictEqual(popover.prop("placement"), "top");
assert.strictEqual(popover.prop("usePortal"), false);
assert.isFalse(popover.prop("usePortal"));
assert.isTrue(onOpening.calledOnce);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe("<DateRangeInput3>", () => {

it("renders with two InputGroup children", () => {
const component = mount(<DateRangeInput3 {...DATE_FORMAT} />);
expect(component.find(InputGroup).length).to.equal(2);
expect(component.find(InputGroup)).to.have.lengthOf(2);
});

it("passes custom classNames to popover wrapper", () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/select/test/multiSelectTests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe("<MultiSelect>", () => {
selectedItems: [TOP_100_FILMS[0]],
tagRenderer: film => <strong>{film.title}</strong>,
});
assert.equal(wrapper.find(Tag).find("strong").length, 1);
assert.lengthOf(wrapper.find(Tag).find("strong"), 1);
});

it("only triggers QueryList key up events when focus is on TagInput's <input>", () => {
Expand Down Expand Up @@ -116,10 +116,10 @@ describe("<MultiSelect>", () => {
popoverProps: { usePortal: false },
});

assert.strictEqual(wrapper.find(Popover).prop("isOpen"), false);
assert.isFalse(wrapper.find(Popover).prop("isOpen"));
findTargetButton(wrapper).simulate("click");

assert.strictEqual(wrapper.find(Popover).prop("isOpen"), true);
assert.isTrue(wrapper.find(Popover).prop("isOpen"));
});

it("allows searching within popover content when custom target provided", async () => {
Expand Down
Loading

1 comment on commit dbb1adb

@svc-palantir-github
Copy link

Choose a reason for hiding this comment

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

style: simplify test assertions (#6983)

Build artifact links for this commit: documentation | landing | table | demo

This is an automated comment from the deploy-preview CircleCI job.

Please sign in to comment.