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(tabs): convert tests to use react-testing-library #6988

Merged
merged 2 commits into from
Oct 3, 2024

Conversation

robinzigmond
Copy link
Contributor

Proposed behaviour

Tabs component, and all subcomponents, should have tests written using React Testing Library

Current behaviour

Tests use Enzyme

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
  • 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 provided StackBlitz sandbox/Storybook
  • Add new Playwright test coverage if required
  • Carbon implementation matches Design System/designs
  • UI Tests GitHub check reviewed if required

Additional context

Testing instructions

Requires 1 QA approval due to the addition of a Playwright test (which was previously a unit tests, with a "TODO" comment to make it a Playwright test instead - I have done this in the course of refactoring the tests)

});

// coverage
it("renders the correct styles when size is `large`, position is `left`, a warning is present and a custom layout is used", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick:

Suggested change
it("renders the correct styles when size is `large`, position is `left`, a warning is present and a custom layout is used", () => {
test("renders the correct styles when size is `large`, position is `left`, a warning is present and a custom layout is used", () => {

@@ -182,6 +182,8 @@ const Tabs = ({

/** Handles the changing of tabs with the mouse */
const handleTabClick = (ev: React.MouseEvent<HTMLElement>) => {
// istanbul ignore if
// (code doesn't seem to be ever reached, the keydown event on TabTitle calls stopPropagation)
Copy link
Contributor

Choose a reason for hiding this comment

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

question: if this line of code is unreachable, should we open a ticket to investigate if it's worth removing it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

now done, thanks (I won't link to Jira here but I've put the ticket number in the comment in my code changes). I also realised the explanation I'd given in this comment of why it's apparently unreachable wasn't correct, the ticket has a more detailed description of why (in my opinion) it can't be reached.

<TabTitle title="example title" onClick={() => {}} onKeyDown={() => {}} />
);

const tabTitle = screen.getByRole("tab");
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): you could combine the query and assertion a bit like below

expect(screen.getByRole("tab", {name: "example title"})).toBeVisible();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe, I don't hate this but I'm not convinced it's an improvement. An accessible name can come from various places (eg an aria-label attribute) but to check it renders the title prop as visibe text - which is what the test description says and imo I think it's worth testing - the toHaveTextContent assertion is needed.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's true but you aren't passing anything like aria-label here so it's an acceptable shortcut given how manny lines of code this file already has imo. I'm happy for it to be left as is as it's not critical

);

const tabTitle = screen.getByRole("tab");
expect(tabTitle).toHaveClass("class1");
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):

Suggested change
expect(tabTitle).toHaveClass("class1");
expect(tabTitle).toHaveClass("class1", "class2");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cool, thanks for the suggestion - I hadn't realised toHaveClass could take multiple arguments

expect(screen.getByRole("tab")).not.toHaveAttribute("aria-selected");
});

test("`siblings` are rendered after the title when `titlePosition` is `before`", () => {
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): As we can't assert the order (fine as we use chromatic anyway) maybe it's better to remove that part from the description for this and the two below

Suggested change
test("`siblings` are rendered after the title when `titlePosition` is `before`", () => {
test("`siblings` are rendered when `titlePosition` is `before`", () => {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pretty sure I did assert the order - by the combined (parent element) text content being "foobar" as opposed to "barfoo" (compare with the next test). So I don't agree with this - this is the important part of the test for me and if I were to change the test description in the way you suggest (which I'd rather not) I'd remove those particular assertions

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah so you did I skimmed the last line of the test my bad

src/components/tabs/tab/tab.test.tsx Show resolved Hide resolved
</Tabs>
);

expect(screen.getByTestId("tabs")).toHaveClass("custom-class-1");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
expect(screen.getByTestId("tabs")).toHaveClass("custom-class-1");
expect(screen.getByTestId("tabs")).toHaveClass("custom-class-1", "custom-class-2"));

@robinzigmond robinzigmond force-pushed the FE-6635-react-tabbing-library branch from 93d18b2 to 645fc9b Compare October 3, 2024 09:00
@DipperTheDan DipperTheDan marked this pull request as ready for review October 3, 2024 10:08
@DipperTheDan DipperTheDan requested review from a team as code owners October 3, 2024 10:08
@robinzigmond robinzigmond force-pushed the FE-6635-react-tabbing-library branch from 645fc9b to 15e9a2d Compare October 3, 2024 10:11
@robinzigmond robinzigmond merged commit f474823 into master Oct 3, 2024
24 checks passed
@robinzigmond robinzigmond deleted the FE-6635-react-tabbing-library branch October 3, 2024 10:57
@carbonci
Copy link
Collaborator

carbonci commented Oct 4, 2024

🎉 This PR is included in version 142.13.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