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

chore(ui-react): lint test files #3306

Merged
merged 1 commit into from
Jan 13, 2023
Merged

Conversation

calebpollman
Copy link
Member

Description of changes

Begin the process of linting all the test files in @aws-amplify/ui-react:

  • fix all TS issues (tests will not run with TS issues once linting is turned on)
  • run yarn react lint --fix to autofix issues
  • manually update the following files:
    • SwitchField.test.tsx - general clean up, remove extraneous tests
    • FileUploader.test.tsx - general clean up
    • useBreakpoint.test.ts - refactor to use jest idiomatic it.each

Before (with linting turned on for test files):
Screen Shot 2023-01-13 at 2 40 09 PM

After (with linting turned on for test files):
Screen Shot 2023-01-13 at 2 41 17 PM

Issue #, if available

NA

Description of how you validated changes

yarn react lint && yarn react test

Checklist

  • PR description included
  • yarn test passes
  • Tests are updated
  • No side effects or sideEffects field updated
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@calebpollman calebpollman requested a review from a team as a code owner January 13, 2023 23:13
@changeset-bot
Copy link

changeset-bot bot commented Jan 13, 2023

⚠️ No Changeset found

Latest commit: db56892

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Comment on lines -109 to -124
let updateControlledValue;
const ControlledSwitch = () => {
const [isChecked, setIsChecked] = React.useState(true);
const changeFunction = (e) => {
setIsChecked(e.target.checked);
};
updateControlledValue = setIsChecked;

return (
<SwitchField
label={label}
isChecked={isChecked}
onChange={changeFunction}
/>
);
};
Copy link
Member Author

Choose a reason for hiding this comment

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

This test component was extraneous, the desired behavior can be achieved using the rerender function returned from calling render

);

const field = container.getElementsByTagName('input')[0];
expect(field).toBeChecked();
});

it('should fire the onChange function with a checkbox change event', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed this test as it was really only testing the behavior of the SwitchFieldControlledExample contained inside, not the functionality of the SwitchField. To note, doublechecked that it had no impact on test coverage

);

if (breakpoint === 'base') {
// eslint-disable-next-line jest/no-conditional-expect
Copy link
Member Author

Choose a reason for hiding this comment

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

Disabled jest/no-conditional-expect here (and below on lines 70, 92, and 95) to avoid refactoring further, which most likely require moving away from the current approach of using the iterative it.each

let matchMedia: MatchMediaMock;
let mediaQueries = getMediaQueries({ breakpoints });

const testBreakpoints = (m) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious why linting didn't like sharing this function. I thought it was pretty clever ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

Eslint does not care abt cleverness 😭

Copy link
Member Author

Choose a reason for hiding this comment

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

All jokes aside, maybe it kind of breaks jest idioms. Could see an argument for requiring the tests to be inside an it for readability?

mediaQueries.reverse().forEach(testBreakpoints);
it.each(mediaQueries)(
`should return the $breakpoint breakpoint as expected`,
({ query, breakpoint }) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't this be the shared function here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Tried that but it gives a linting error in the it blocks because eslint thinks there are no tests inside. We could just disable that lint rule but thought it was probably okay to be more verbose since it's a test file

Copy link
Contributor

@reesscot reesscot left a comment

Choose a reason for hiding this comment

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

Had a question on useMediaQueries test, but not a blocker

@calebpollman calebpollman temporarily deployed to ci January 13, 2023 23:30 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 13, 2023 23:30 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 13, 2023 23:30 — with GitHub Actions Inactive
@calebpollman calebpollman temporarily deployed to ci January 13, 2023 23:30 — with GitHub Actions Inactive
@calebpollman calebpollman merged commit ed2892f into main Jan 13, 2023
@calebpollman calebpollman deleted the ui-react/lint-test-files branch January 13, 2023 23:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants