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(input-presentation, input): convert enzyme tests to RTL #6958

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tomdavies73
Copy link
Contributor

Proposed behaviour

Converts all enzyme tests to RTL.

Current behaviour

Currently, tests are written in 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

const inputPresentation = within(inputPresentationContainer).getByRole(
"presentation"
);
expect(inputPresentationContainer).toBeVisible();
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'd feel more comfortable if this test actually checked that the Children content is visible - either by using getByTextContent or using a toHaveText assertion alongside the toBeVisible one(s)

expect(inputPresentation).toBeVisible();
});

test("renders a passed node via the `positionedChildren` prop as a direct child of the presentation container", () => {
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 would prefer to reword this description to emphasise that the positionedChildren content is being rendered before the "normal" children, which seems to be the purpose of the prop and which you are definitely (and correctly) testing for here. Maybe something like renders a passed node via the positionedChildren prop as a child before the other children?

"send",
] as EnterKeyHintTypes[])(
"'enterKeyHint' is correctly passed to the input when prop value is %s",
(keyHints) => {
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
(keyHints) => {
(keyHint) => {

nitpick (non-blocking): although it's from an array of different hintS, this variable at any one time only represents a single keyhint, so I'd prefer not to give it a name that implies a plural.

const user = userEvent.setup();
const input = screen.getByRole("textbox");

await user.click(input);
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): maybe there are problems that I've overlooked, but I'd prefer to just do input.focus() here, given what we're supposed to be testing. The danger with just testing the click is that, although this will focus the element, it does other things too, and we can't guarantee that other ways of focusing the element will also call the onFocus prop. Eg (unlikely I know) what if a bug was introduced where onFocus didn't get called on focus but was still called on click? That would be a bug but the test as you have it here would still pass.

const user = userEvent.setup();
const input = screen.getByRole("textbox");

await user.click(input);
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): same as above

expect(input).toBeFocused();
});

test("triggers a passed function via the `onClick` prop from the input context provider, when the input is clicked", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

comment: this seems to be the test that should have gone under the previous description. The onClick here is provided directly to the component rather than via context, so this description needs a slightly different test.


input.selectionStart = 0;
input.selectionEnd = 0;
input.focus();
Copy link
Contributor

Choose a reason for hiding this comment

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

comment: the description says "when the first character within the input is clicked", but there is no click here, just a focus. I'm not sure whether the important thing to test is the click or the focus, but either the description or this line of the test should be changed so that the 2 match.


input.selectionStart = 5;
input.selectionEnd = 5;
input.focus();
Copy link
Contributor

Choose a reason for hiding this comment

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

comment: as in the previous comment


input.selectionStart = 2;
input.selectionEnd = 2;
input.focus();
Copy link
Contributor

Choose a reason for hiding this comment

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

comment: as above

jest.useRealTimers();
});

test("when the input is rendered with a type other than 'text', setSelectionRange is not triggered on focus", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

comment (non-blocking): this one seems to be correct as the description says "on focus", which is what the test does. Maybe this means all the tests should have "on focus" (rather than "on click") in the description?

await user.type(input, "test");

jest.advanceTimersByTime(500);
expect(onChangeDeferredProp).not.toHaveBeenCalled();
Copy link
Contributor

Choose a reason for hiding this comment

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

praise: good idea to check the prop isn't called prior to the timeout finishing! 👍🏼

const input = screen.getByRole("textbox");

await user.click(input);
expect(input).toBeFocused();
Copy link
Contributor

Choose a reason for hiding this comment

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

question: I noticed the title doesn't seem to match with assertions taking place. Just wanted to clarify if this was a potential oversight?

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.

3 participants