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: use utility to ensure test-specific data attributes are removed in prod builds #8171

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe("calcite-flow-item", () => {

await page.$eval("calcite-flow-item", (flowItem: HTMLCalciteFlowItemElement) => {
const panel = flowItem.shadowRoot.querySelector("calcite-panel");
const toggleButton = panel.shadowRoot.querySelector("[data-test=collapse]") as HTMLCalciteActionElement;
const toggleButton = panel.shadowRoot.querySelector("[data-test-id=collapse]") as HTMLCalciteActionElement;
toggleButton.click();
});

Expand Down
6 changes: 3 additions & 3 deletions packages/calcite-components/src/components/panel/panel.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe("calcite-panel", () => {

const element = await page.find("calcite-panel");
const container = await page.find(`calcite-panel >>> .${CSS.contentWrapper}`);
const collapseButtonSelector = `calcite-panel >>> [data-test="collapse"]`;
const collapseButtonSelector = `calcite-panel >>> [data-test-id="collapse"]`;
expect(await page.find(collapseButtonSelector)).toBeNull();

await page.waitForChanges();
Expand All @@ -107,7 +107,7 @@ describe("calcite-panel", () => {

const calcitePanelClose = await page.spyOnEvent("calcitePanelClose", "window");

const closeButton = await page.find("calcite-panel >>> calcite-action[data-test=close]");
const closeButton = await page.find("calcite-panel >>> calcite-action[data-test-id=close]");

await closeButton.click();

Expand All @@ -121,7 +121,7 @@ describe("calcite-panel", () => {

const calcitePanelToggle = await page.spyOnEvent("calcitePanelToggle", "window");

const toggleButton = await page.find("calcite-panel >>> [data-test=collapse]");
const toggleButton = await page.find("calcite-panel >>> [data-test-id=collapse]");

await toggleButton.click();

Expand Down
5 changes: 3 additions & 2 deletions packages/calcite-components/src/components/panel/panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
updateMessages,
} from "../../utils/t9n";
import { PanelMessages } from "./assets/panel/t9n";
import { toTestObject } from "../../utils/component";

/**
* @slot - A slot for adding custom content.
Expand Down Expand Up @@ -428,22 +429,22 @@ export class Panel
<calcite-action
aria-expanded={toAriaBoolean(!collapsed)}
aria-label={collapse}
data-test="collapse"
icon={collapsed ? icons[0] : icons[1]}
onClick={this.collapse}
text={collapse}
title={collapsed ? expand : collapse}
{...toTestObject("id", "collapse")}
/>
) : null;

const closeNode = closable ? (
<calcite-action
aria-label={close}
data-test="close"
icon={ICONS.close}
onClick={this.close}
text={close}
title={close}
{...toTestObject("id", "close")}
/>
) : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { CSS_UTILITY } from "../../utils/resources";
import { FlipContext, Scale, SelectionMode } from "../interfaces";
import { TreeItemSelectDetail } from "./interfaces";
import { CSS, ICONS, SLOTS } from "./resources";
import { getIconScale } from "../../utils/component";
import { getIconScale, toTestObject } from "../../utils/component";

/**
* @slot - A slot for adding text.
Expand Down Expand Up @@ -214,10 +214,10 @@ export class TreeItem implements ConditionalSlotComponent, InteractiveComponent
[CSS.chevron]: true,
[CSS_UTILITY.rtl]: rtl,
}}
data-test-id="icon"
icon={ICONS.chevronRight}
onClick={this.iconClickHandler}
scale={getIconScale(this.scale)}
{...toTestObject("id", "icon")}
/>
) : null;
const defaultSlotNode: VNode = <slot key="default-slot" />;
Expand All @@ -228,10 +228,10 @@ export class TreeItem implements ConditionalSlotComponent, InteractiveComponent
<calcite-checkbox
checked={this.selected}
class={CSS.checkbox}
data-test-id="checkbox"
indeterminate={this.hasChildren && this.indeterminate}
scale={this.scale}
tabIndex={-1}
{...toTestObject("id", "checkbox")}
/>
{defaultSlotNode}
</label>
Expand Down Expand Up @@ -313,9 +313,9 @@ export class TreeItem implements ConditionalSlotComponent, InteractiveComponent
[CSS.childrenContainer]: true,
[CSS_UTILITY.rtl]: rtl,
}}
data-test-id="calcite-tree-children"
onClick={this.childrenClickHandler}
role={this.hasChildren ? "group" : undefined}
{...toTestObject("id", "calcite-tree-children")}
>
<slot name={SLOTS.children} />
</div>
Expand Down
32 changes: 32 additions & 0 deletions packages/calcite-components/src/utils/component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
import { Scale } from "../components/interfaces";
import { Build } from "@stencil/core";

export function getIconScale(componentScale: Scale): Extract<Scale, "s" | "m"> {
return componentScale === "l" ? "m" : "s";
}

/**
* This util is used to create an object to spread data attributes used in E2E or unit tests.
*
* Attributes used for testing will get removed when the components are built for production.
*
* @example
*
* <calcite-icon
* class={{
* [CSS.chevron]: true,
* [CSS_UTILITY.rtl]: rtl,
* }}
* icon={ICONS.chevronRight}
* onClick={this.iconClickHandler}
* scale={getIconScale(this.scale)}
* {...toTestObject("id", "icon")}
* />
*
* @param id – the id to use for the data-test attribute
* @param value – the value to use for the data-test attribute
*/
export function toTestObject(id: string, value: string): object {
if (!Build.isTesting) {
return null;
}

return {
[`data-test-${id}`]: value,
};
}
Loading