-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add DatePicker
tests using React Testing Library
#40754
Conversation
Size Change: +183 B (0%) Total Size: 1.23 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wonderful, thanks for improving the tests!
Thanks @mirka. I added a story comment and switched to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @noisysocks 👍
✅ Storybook example looks good until we can migrate Knobs to Controls
✅ Unit tests pass and make sense to me
❓ I also had the same question regarding whether we could assert date selection based off of accessibility properties.
I'm not very familiar with react-dates
and didn't find anything to solve that. Perhaps you'll have more ideas.
).toBe( true ); | ||
} ); | ||
it( 'should call onMonthPreviewed and onChange when a day in a different month is selected', async () => { | ||
const user = userEvent.setup( { delay: null } ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind making a few changes to these tests in order to align to how we use @testing-library/user-event
in other components?
You'd need to rebase on top of trunk
to include this version bump to user-event
(don't forget to reinstall node_modules
), and then switch to use jest fake timers + the advanceTimers
option from userEvent
Example
diff --git a/packages/components/src/date-time/test/date.js b/packages/components/src/date-time/test/date.js
index f204dff80f..4cf66697f4 100644
--- a/packages/components/src/date-time/test/date.js
+++ b/packages/components/src/date-time/test/date.js
@@ -11,7 +11,20 @@ import 'react-dates/initialize';
*/
import DatePicker from '../date';
+const user = userEvent.setup( {
+ advanceTimers: jest.advanceTimersByTime,
+} );
+
describe( 'DatePicker', () => {
+ beforeEach( () => {
+ jest.useFakeTimers();
+ } );
+
+ afterEach( () => {
+ jest.runOnlyPendingTimers();
+ jest.useRealTimers();
+ } );
+
it( 'should highlight the current date', () => {
render( <DatePicker currentDate="2022-05-02T11:00:00" /> );
@@ -34,8 +47,6 @@ describe( 'DatePicker', () => {
} );
it( 'should call onChange when a day is selected', async () => {
- const user = userEvent.setup( { delay: null } );
-
const onChange = jest.fn();
render(
@@ -51,8 +62,6 @@ describe( 'DatePicker', () => {
} );
it( 'should call onMonthPreviewed and onChange when a day in a different month is selected', async () => {
- const user = userEvent.setup( { delay: null } );
-
const onMonthPreviewed = jest.fn();
const onChange = jest.fn();
@@ -99,8 +108,6 @@ describe( 'DatePicker', () => {
} );
it( 'should not allow invalid date to be selected', async () => {
- const user = userEvent.setup( { delay: null } );
-
const onChange = jest.fn();
render(
I just merged a PR with similar changes today (#40790).
Also happy if you want to carry these changes in a follow-up !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened a follow-up for this: #40800
Unfortunately the change causes a weird error in react-dates
. Let's discuss in the other PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with the points made by @kevin940726 and @aaronrobertshaw .
Would it be also possible to add an entry to the CHANGELOG (this one could be under Internal
)
7728df6
to
fce95e2
Compare
I've added a |
DatePicker
tests using React Testing Library
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Just a small nitpicking left here, but overall I think this is good enough 💯 .
What?
Rewrites the
DatePicker
tests to use React Testing Library and to test the component's basic functionality instead of simply thatreact-dates
is called correctly.Why?
DatePicker
works. (I'm about to refactor it.)DatePicker
's sister component,TimePicker
.How?
Wrote new tests using React Testing Library. I focused on ensuring coverage for all of the public props.
I didn't touch
TimePicker
's tests as they already look pretty good.I also added a new story that demonstrates the
isInvalidDate
prop which was missing.Testing Instructions
npm run test