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

test(icon-button): add playwright tests #6369

Merged
merged 3 commits into from
Nov 1, 2023

Conversation

divyajindel
Copy link
Contributor

@divyajindel divyajindel commented Oct 12, 2023

Proposed behaviour

  • Refactor cypress tests to playwright

Current behaviour

  • Tests are currently using cypress

Checklist

  • Commits follow our style guide
  • Related issues linked in commit messages if required
  • Screenshots are included in the PR if useful
  • All themes are supported if required
  • Unit tests added or updated if required
  • Cypress automation tests added or updated if required
  • Playwright automation tests added or updated if required
  • Storybook added or updated if required
  • Translations added or updated (including creating or amending translation keys table in storybook) if required
  • Typescript d.ts file added or updated if required
  • Related docs have been updated if required

QA

  • Tested in CodeSandbox/storybook
  • Add new Cypress test coverage if required
  • Carbon implementation matches Design System/designs
  • UI Tests GitHub check reviewed if required

Additional context

N/A

Testing instructions

  • Run Icon-Button tests in Playwright Test Runner to check if the icon-button.pw.tsx file passed
  • Run all other tests in Playwright Test Runner to check none of the other *.pw.tsx files have regressed

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 12, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit dff1b79:

Sandbox Source
carbon-quickstart Configuration
carbon-quickstart-typescript Configuration

const element = await iconLocator;
await element.focus();
await expect(element).toHaveCSS(
"box-shadow",
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the line const element = await iconLocator; is necessary? The following should do the same:
const iconLocator = page.getByTestId("icon").locator("..");
await iconLocator.focus();
await expect(iconLocator).toHaveCSS(

import { HooksConfig } from "../../../playwright";
import { checkAccessibility } from "../../../playwright/support/helper";

test.describe("when focused", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This describe should probably be changed to something more meaningful like:
test.describe("check IconButton component focus outlines and border radius", () => {

);

const iconLocator = page.getByTestId("icon").locator("..");
const element = await iconLocator;
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as previous comment, const element = await iconLocator; is unnecessary.

"aria-label",
CHARACTERS.STANDARD
);
await iconLocator.isVisible();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think await expect(iconLocator).toBeVisible() is more reliable than this.

await mount(<IconButtonComponent />);

const iconLocator = page.getByTestId("icon");
await iconLocator.isVisible();
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as previous comment.


const iconLocator = page.getByTestId("icon").locator("..");
await iconLocator.isDisabled();
await iconLocator.getAttribute("disabled");
Copy link
Contributor

Choose a reason for hiding this comment

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

This would be preferable:
await expect(iconLocator).toBeDisabled()
await expect(iconLocator).toHaveAttribute("disabled", /.*/);

);
const iconLocator = page.getByTestId("icon").locator("..");
await iconLocator.focus();
await iconLocator.blur();
Copy link
Contributor

Choose a reason for hiding this comment

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

This line await iconLocator.blur(); is not needed.

});
});

test(`render with the expected border radius`, async ({ mount, page }) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could move this test to the first describe, with the border radius tests.

}) => {
await mount(<IconButtonComponent aria-label={CHARACTERS.STANDARD} />);

const iconLocator = page.getByTestId("icon").locator("..");
Copy link
Contributor

Choose a reason for hiding this comment

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

This const is used several times. Can it be declared once outside of the tests?

}
);

const iconLocator = page.getByTestId("icon").locator("..");
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, this const is used several times. Can it be declared once earlier then just iconLocator called where needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi Sian, IMO we can't do this as we are using page to create this locator inside the test.

}) => {
await mount(<IconButtonComponent aria-label={CHARACTERS.STANDARD} />);

const iconLocator = page.getByTestId("icon").locator("..");
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, this const is used several times.

}) => {
await mount(<IconButtonComponent disabled />);

const iconLocator = page.getByTestId("icon").locator("..");
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, this const is used several times.

}}
/>
);
const iconLocator = page.getByTestId("icon").locator("..");
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, this const is used several times.

}}
/>
);
const iconLocator = page.getByTestId("icon").locator("..");
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, this const is used several times.

}}
/>
);
const iconLocator = page.getByTestId("icon").locator("..");
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, this const is used several times.

}}
/>
);
const iconLocator = page.getByTestId("icon").locator("..");
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, this const is used several times.

}}
/>
);
const iconLocator = page.getByTestId("icon").locator("..");
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, this const is used several times.

}}
/>
);
const iconLocator = page.getByTestId("icon").locator("..");
Copy link
Contributor

Choose a reason for hiding this comment

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

As above, this const is used several times.

@divyajindel divyajindel force-pushed the icon-button-playwright-refactor branch from 21cd3ac to e7a227c Compare October 18, 2023 11:00
@divyajindel divyajindel force-pushed the icon-button-playwright-refactor branch from e7a227c to 4db9127 Compare October 19, 2023 09:57
stephenogorman
stephenogorman previously approved these changes Oct 23, 2023
@tomdavies73 tomdavies73 self-requested a review October 25, 2023 09:11
}) => {
await mount(<IconButtonComponent aria-label={CHARACTERS.STANDARD} />);

const iconLocator = page.getByTestId("icon").locator("..");
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: instead of page.getByTestId("icon").locator(".."); could we not just add a general button index/locator? Infact there already is one in locators.ts (line 18), you'd just have to import the index into the file and do iconButton = button(page) etc. I won't comment this on every instance you've used the above locator but if I were you i'd think about replacing it with this approach.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, as the new button(page) locator is fairly short and simple you can probably move away from the const approach and just do await button(page).focus() and then your two expects expect(button(page)...) if you do this test by test we can cut out a good fair few lines and reduce complexity

);

test.describe("check props for IconButton component", () => {
test(`should render IconButton with aria-label prop`, async ({
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: we've already informed future readers what component is being tested via the describe on the line above, I don't think it's necessary to include the component name on every test description within this describe block.

test.describe(
"check IconButton component focus outlines and border radius",
() => {
test(`should have the expected styling when the focusRedesignOptOut is false`, async ({
Copy link
Contributor

Choose a reason for hiding this comment

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

praise: I like that you've combined the focus/radius tests into one describe here, makes things a lot clearer

await checkAccessibility(page);
});

test(`should pass accessibility tests for children prop`, async ({
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (non-blocking): i know it was written like this in the cypress test, but i'd change this test description. There is no children prop and in the test there's only one child. Something like should pass accessibility tests when rendered with a child should do

await expect(iconLocator).toBeVisible();
});

test(`should render IconButton with children prop`, async ({
Copy link
Contributor

@tomdavies73 tomdavies73 Oct 26, 2023

Choose a reason for hiding this comment

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

suggestion (non-blocking): i know it was written like this in the cypress test, but i'd change this test description. There is no children prop and in the test there's only one child. Something like should render with a child should do


const iconLocator = page.getByTestId("icon").locator("..");
await expect(iconLocator).toBeDisabled();
await expect(iconLocator).toHaveAttribute("disabled", /.*/);
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: I think the expect above should be enough for this, instead of the attribute tests

mount,
page,
}) => {
await mount(<IconButtonComponent aria-label={CHARACTERS.STANDARD} />);
Copy link
Contributor

Choose a reason for hiding this comment

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

question (non-blocking): I can see we're passing an aria-label for these focus and radius tests, is there a specific reason for this, or can they be removed. I can see we do this in the cypress test too btw, so I understand why you've included them. Just could get rid if they're not neccessary

@@ -25,6 +26,10 @@ export const commonDataElementInputPreview = (page: Page) => {
return page.locator(COMMMON_DATA_ELEMENT_INPUT);
};

export const iconButton = (page: Page) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: this can just be called button instead of iconButton as the locator is just finding type="button" and has no overlap with the IconButton component, i'd rename this here at least 👍

import React from "react";
import { test, expect } from "@playwright/experimental-ct-react17";
import IconButtonComponent from "./component.test-pw";
import { iconButton } from "../../../playwright/components/index";
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: if you've changed the index/locator name from iconButton to button you can then do this for the import import { button as iconButton } from "../../../playwright/components/index". This way you can keep the name of the locator in your tests and ensure they're still clear what's being tested etc.

});

test.describe("check accessibility tests for IconButton component", () => {
test(`should render aria-label prop for accessibility`, async ({
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion: I think these test descriptions have been changed and i'm not 100% sure why, I think they should all follow something like should pass accessibility tests when rendered with an aria-label, should pass accessibility tests when rendered with a child, should pass accessibility tests when disabled etc.

@divyajindel divyajindel force-pushed the icon-button-playwright-refactor branch from 30da757 to 6dc0a93 Compare October 27, 2023 13:00
@divyajindel divyajindel marked this pull request as ready for review November 1, 2023 12:55
@divyajindel divyajindel marked this pull request as draft November 1, 2023 13:57
@divyajindel divyajindel marked this pull request as ready for review November 1, 2023 13:57
@divyajindel divyajindel merged commit aeb530d into master Nov 1, 2023
18 checks passed
@divyajindel divyajindel deleted the icon-button-playwright-refactor branch November 1, 2023 14:24
@carbonci
Copy link
Collaborator

carbonci commented Nov 2, 2023

🎉 This PR is included in version 123.2.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

5 participants