-
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
Make preferences reducer deterministic #5423
Conversation
It's certainly a fair bit more verbose 😬 Initial thoughts are whether we can / should...
Will give a closer look tomorrow. |
It's probably simpler to just avoid adding an extra argument altogether, so long as we don't mind using an // In editor/store/actions.js:
export function insertBlocks( blocks, index, rootUID ) {
return {
type: 'INSERT_BLOCKS',
blocks: castArray( blocks ),
index,
rootUID,
time: Date.now(),
};
}
// In editor/store/test/actions.js:
expect( insertBlocks( blocks, index ) ).toEqual( {
type: 'INSERT_BLOCKS',
blocks,
index,
time: expect.any( Number ),
} ); |
I'd be fine with that. |
Oh... 🤦♂️ but that would mean we can't use these action creators (e.g.
I'd be into this.
👌 |
Not sure I'm into letting unit tests force our hand into how best to implement the code under test 😆 |
c1aa1c1
to
e4f1c1b
Compare
Very true! I've updated the 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.
Nice 👍
editor/store/test/effects.js
Outdated
attributes: { content: 'chicken ribs' }, | ||
} ] ) ); | ||
expect( dispatch ).toHaveBeenCalledWith( { | ||
type: 'REPLACE_BLOCKS', |
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.
Alternatively, we could do something like:
expect( dispatch ).toHaveBeenCalledWith( {
...replaceBlocks( /* ... */ ),
time: expect.any( Number ),
} );
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.
Ah, that's nicer 🙂 — updated.
Remove Date.now() from the preferences reducer, making it a pure function.
e4f1c1b
to
da00a9a
Compare
Calling
Date.now()
from the preferences reducer makes the reducer impure, which will bite us if we use any of the time travelling features that Redux enables. To fix this, I've moved the call toDate.now()
to the action creator as an optional argument.This is a small mistake introduced in #5342, and pointed out by @aduth in #5342 (comment).