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

fix: TimezoneSelector is not reliably testable #19148

Closed
wants to merge 1 commit into from

Conversation

suddjian
Copy link
Member

@suddjian suddjian commented Mar 15, 2022

SUMMARY

When DST switched, this test suite broke. This new version of the component and its tests should be functionally equivalent to the original, but with system time properly mocked and frozen for the duration of the test.

The test has some odd failures that I haven't yet figured out. All I know about them so far is that when I remove the line await userEvent.type(searchInput, 'mou', { delay: 10 }); they pass. That line seems important. Since the test passes when the line is removed, I would presume that the test is also not adequately asserting that something actually changed after the user input.

Help getting the tests passing would be appreciated.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API


const TimezoneSelector = ({ onTimezoneChange, timezone }: TimezoneProps) => {
const currentDate = useMemo(moment, []);
Copy link
Member Author

Choose a reason for hiding this comment

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

currentDate needs to be instantiated in the component instead of at the module root in order to pick up the system time mock. All the rest of the changes in this component stem from that requirement.

expect(onTimezoneChange).toHaveBeenCalledTimes(1);
expect(onTimezoneChange).toHaveBeenCalledWith('America/Nassau');
});
describe('TimezoneSelector', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I wasn't familiar with this pattern. I don't think I agree that never adding describe is desirable. These blocks are important when you want to do setup and cleanup reliably.

Copy link
Member

Choose a reason for hiding this comment

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

You can run beforeEach and afterEach even without the nesting.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, that makes more sense then. I still do feel that it can be useful sometimes for categorization or setting up different shared initial conditions, but I agree with preferring to avoid in most cases.

@ktmud
Copy link
Member

ktmud commented Mar 15, 2022

I think ideally we'd want to test both DST and non-DST so you'd need to mock both a January and June date.

To fix the tests, all you have to do is simply removing L85. It was broken because America/Adak uses daylight saving time therefore was ranked to a lower position when DST switched. The user input line was broken for you probably because other changes you made in this PR.

I'm not sure we need to change the component code itself. Let me see what I can do.

Comment on lines +32 to +33
jest.useFakeTimers('modern');
jest.setSystemTime(new Date('January 10 2022'));
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 sets/freezes the system time so that tests will be run under the same conditions every time.

@suddjian suddjian closed this Mar 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants