Skip to content

Commit

Permalink
Interaction tests (#582)
Browse files Browse the repository at this point in the history
* Interaction tests

* Interaction tests

* Interaction tests

* Interaction tests - test failure

* Interaction tests
  • Loading branch information
MosheZemah authored Mar 13, 2022
1 parent 468f3fd commit caaf9b4
Show file tree
Hide file tree
Showing 12 changed files with 25,416 additions and 24,729 deletions.
5 changes: 3 additions & 2 deletions .storybook/addons.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "@storybook/addon-docs/register";
import "@storybook/addon-interactions/register";
import "@storybook/addon-actions/register";
import "@storybook/addon-links/register";
import { create } from "@storybook/theming";
Expand All @@ -9,8 +10,8 @@ const theme = create({
base: "light",
brandImage: logo,
brandUrl: "https://monday.com",
barSelectedColor: '#5034ff',
brandTitle: 'Vibe Design',
barSelectedColor: "#5034ff",
brandTitle: "Vibe Design",
background: {
hoverable: "rgba(80, 52, 255, 0.1)"
},
Expand Down
4 changes: 4 additions & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ module.exports = {
webpackFinal: async config => {
return buildConfig(config);
},
features: {
interactionsDebugger: true
},
addons: [
"@storybook/addon-controls",
"storybook-addon-themes",
"@storybook/addon-a11y",
"storybook-addon-performance/register",
"@storybook/addon-docs",
"@storybook/addon-interactions",
"@storybook/addon-actions"
],
core: {
Expand Down
49,607 changes: 25,020 additions & 24,587 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
"@popperjs/core": "^2.11.0",
"@react-hook/merged-ref": "1.3.2",
"@react-stately/toggle": "3.2.3",
"@storybook/addon-interactions": "^6.4.19",
"@storybook/jest": "^0.0.9",
"@storybook/testing-library": "^0.0.9",
"autosize": "^5.0.1",
"classnames": "^2.3.1",
"detect-browser": "^5.3.0",
Expand Down
62 changes: 62 additions & 0 deletions src/__tests__/interactions-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { within, userEvent } from "@storybook/testing-library";
import { getTestId, ELEMENT_TYPES as types } from "../utils/test-utils";

export const ELEMENT_TYPES = types;

function getWithin(canvasOrValidTestElement) {
if (canvasOrValidTestElement.getByRole) return canvasOrValidTestElement;
return within(canvasOrValidTestElement);
}

export const testFunctionWrapper = testFunc => {
return async ({ canvasElement }) => {
// Starts querying the component from its root element
const canvas = getWithin(canvasElement);
return testFunc(canvas);
};
};

export const getByTestId = (rootElement, elementType, id = "") => {
const dataTestId = getTestId(elementType, id);
return getWithin(rootElement).getByTestId(dataTestId);
};

export const getByText = (rootElement, text) => {
return getWithin(rootElement).getByText(text);
};

export const getByPlaceholderText = (rootElement, text) => {
return getWithin(rootElement).getByPlaceholderText(text);
};

export const getByClassName = className => {
return document.getElementsByClassName(className)[0];
};

export const getByRole = (rootElement, role) => {
return getWithin(rootElement).getByRole(role);
};

export const getByLabelText = (rootElement, text) => {
return getWithin(rootElement).getByLabelText(text);
};

export const clickElement = element => {
return userEvent.click(element);
};

export const typeText = async (element, text, waitForDebounceMs = 250) => {
let promise = userEvent.type(element, text, {
delay: 50
});
const result = await promise;
await delay(waitForDebounceMs);
return result;
};

function delay(timeout) {
return new Promise(resolve => {
if (!timeout) return resolve();
setTimeout(resolve, timeout);
});
}
2 changes: 2 additions & 0 deletions src/components/Combobox/Combobox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from "prop-types";
import isFunction from "lodash/isFunction";
import NOOP from "lodash/noop";
import cx from "classnames";
import { getTestId, ELEMENT_TYPES } from "../../utils/test-utils";
import useMergeRefs from "../../hooks/useMergeRefs";
import Search from "../Search/Search";
import { SIZES } from "../../constants/sizes";
Expand Down Expand Up @@ -233,6 +234,7 @@ const Combobox = forwardRef(
"sticky-category": stickyCategories
})}
id={id}
data-testid={getTestId(ELEMENT_TYPES.COMBOBOX, id)}
>
<div className="combobox--wrapper-list" style={{ maxHeight: optionsListHeight }} role="listbox">
<Search
Expand Down
5 changes: 4 additions & 1 deletion src/components/Combobox/__stories__/combobox.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import person1 from "./assets/person1.png";
import person2 from "./assets/person2.png";
import person3 from "./assets/person3.png";
import { ArgsTable, Story, Canvas, Meta } from "@storybook/addon-docs";
import { onSelectClearsFilterTest } from "./interactions";
import DialogContentContainer from "../../DialogContentContainer/DialogContentContainer.jsx";
import Button from "../../Button/Button.jsx";
import Dialog from "../../Dialog/Dialog";
Expand Down Expand Up @@ -56,8 +57,10 @@ Combo box allowing users to filter longer lists to only the selections matching
{ id: "2", label: "Option 2" },
{ id: "3", label: "Option 3" }
],
placeholder: "Placeholder text here"
placeholder: "Placeholder text here",
clearFilterOnSelection: true
}}
play={onSelectClearsFilterTest}
>
{comboboxTemplate.bind({})}
</Story>
Expand Down
19 changes: 19 additions & 0 deletions src/components/Combobox/__stories__/interactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect } from "@storybook/jest";
import {
ELEMENT_TYPES,
getByLabelText,
getByTestId,
getByText,
clickElement,
testFunctionWrapper,
typeText
} from "../../../__tests__/interactions-helper";

export const onSelectClearsFilterTest = testFunctionWrapper(async canvas => {
const comboboxElement = await getByTestId(canvas, ELEMENT_TYPES.COMBOBOX);
const filterInput = await getByLabelText(comboboxElement, "Search for content");
await typeText(filterInput, "Option");
const optionToClick = getByText(comboboxElement, "Option 1");
await clickElement(optionToClick);
expect(filterInput).toHaveValue("");
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports[`Combobox renders correctly when disabled 1`] = `
<div
className="combobox--wrapper size-medium empty"
data-testid="combobox_"
id=""
>
<div
Expand Down Expand Up @@ -108,6 +109,7 @@ exports[`Combobox renders correctly when disabled 1`] = `
exports[`Combobox renders correctly with another noResultsMessage 1`] = `
<div
className="combobox--wrapper size-medium empty"
data-testid="combobox_"
id=""
>
<div
Expand Down Expand Up @@ -213,6 +215,7 @@ exports[`Combobox renders correctly with another noResultsMessage 1`] = `
exports[`Combobox renders correctly with autoFocus 1`] = `
<div
className="combobox--wrapper size-medium empty"
data-testid="combobox_"
id=""
>
<div
Expand Down Expand Up @@ -318,6 +321,7 @@ exports[`Combobox renders correctly with autoFocus 1`] = `
exports[`Combobox renders correctly with className 1`] = `
<div
className="combobox--wrapper test size-medium empty"
data-testid="combobox_"
id=""
>
<div
Expand Down Expand Up @@ -423,6 +427,7 @@ exports[`Combobox renders correctly with className 1`] = `
exports[`Combobox renders correctly with divider 1`] = `
<div
className="combobox--wrapper size-medium"
data-testid="combobox_"
id=""
>
<div
Expand Down Expand Up @@ -605,6 +610,7 @@ exports[`Combobox renders correctly with divider 1`] = `
exports[`Combobox renders correctly with empty props 1`] = `
<div
className="combobox--wrapper size-medium empty"
data-testid="combobox_"
id=""
>
<div
Expand Down Expand Up @@ -710,6 +716,7 @@ exports[`Combobox renders correctly with empty props 1`] = `
exports[`Combobox renders correctly with id 1`] = `
<div
className="combobox--wrapper size-medium empty"
data-testid="combobox_test"
id="test"
>
<div
Expand Down Expand Up @@ -815,6 +822,7 @@ exports[`Combobox renders correctly with id 1`] = `
exports[`Combobox renders correctly with loading 1`] = `
<div
className="combobox--wrapper size-medium empty"
data-testid="combobox_"
id=""
>
<div
Expand Down Expand Up @@ -947,6 +955,7 @@ exports[`Combobox renders correctly with loading 1`] = `
exports[`Combobox renders correctly with optionLineHeight 1`] = `
<div
className="combobox--wrapper size-medium empty"
data-testid="combobox_"
id=""
>
<div
Expand Down Expand Up @@ -1052,6 +1061,7 @@ exports[`Combobox renders correctly with optionLineHeight 1`] = `
exports[`Combobox renders correctly with optionRenderer 1`] = `
<div
className="combobox--wrapper size-medium"
data-testid="combobox_"
id=""
>
<div
Expand Down Expand Up @@ -1201,6 +1211,7 @@ exports[`Combobox renders correctly with optionRenderer 1`] = `
exports[`Combobox renders correctly with optionsListHeight 1`] = `
<div
className="combobox--wrapper size-medium empty"
data-testid="combobox_"
id=""
>
<div
Expand Down Expand Up @@ -1306,6 +1317,7 @@ exports[`Combobox renders correctly with optionsListHeight 1`] = `
exports[`Combobox renders correctly with placeholder 1`] = `
<div
className="combobox--wrapper size-medium empty"
data-testid="combobox_"
id=""
>
<div
Expand Down Expand Up @@ -1411,6 +1423,7 @@ exports[`Combobox renders correctly with placeholder 1`] = `
exports[`Combobox renders correctly with size 1`] = `
<div
className="combobox--wrapper size-large empty"
data-testid="combobox_"
id=""
>
<div
Expand Down
Loading

0 comments on commit caaf9b4

Please sign in to comment.