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

Make preferences reducer deterministic #5423

Merged
merged 1 commit into from
Mar 8, 2018

Conversation

noisysocks
Copy link
Member

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 to Date.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).

@noisysocks noisysocks requested a review from aduth March 6, 2018 01:37
@aduth
Copy link
Member

aduth commented Mar 6, 2018

It's certainly a fair bit more verbose 😬

Initial thoughts are whether we can / should...

  • Implement as a middleware augmenting all relevant actions?
  • Stop adding positional arguments to action creators, as they're starting to lose meaning, and instead introduce an options object?

Will give a closer look tomorrow.

@aduth aduth added the Framework Issues related to broader framework topics, especially as it relates to javascript label Mar 6, 2018
@noisysocks
Copy link
Member Author

noisysocks commented Mar 6, 2018

It's probably simpler to just avoid adding an extra argument altogether, so long as we don't mind using an expect.any to unit test insertBlocks and replaceBlocks, e.g.

// 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 ),
} );

@aduth
Copy link
Member

aduth commented Mar 6, 2018

I'd be fine with that.

@noisysocks
Copy link
Member Author

noisysocks commented Mar 6, 2018

Oh... 🤦‍♂️ but that would mean we can't use these action creators (e.g. replaceBlocks()) in our effect tests (e.g. describe( '.MERGE_BLCOCKS')). Not sure that I'm into changing the effect tests to assert against the raw action objects instead of calling action creators, since it's fragile and verbose.

Stop adding positional arguments to action creators, as they're starting to lose meaning, and instead introduce an options object?

I'd be into this.

Will give a closer look tomorrow.

👌

@aduth
Copy link
Member

aduth commented Mar 6, 2018

Not sure I'm into letting unit tests force our hand into how best to implement the code under test 😆

@noisysocks noisysocks force-pushed the fix/make-reducer-deterministic branch from c1aa1c1 to e4f1c1b Compare March 6, 2018 03:26
@noisysocks
Copy link
Member Author

Very true! I've updated the PR.

Copy link
Member

@aduth aduth left a comment

Choose a reason for hiding this comment

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

Nice 👍

attributes: { content: 'chicken ribs' },
} ] ) );
expect( dispatch ).toHaveBeenCalledWith( {
type: 'REPLACE_BLOCKS',
Copy link
Member

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 ),
} );

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's nicer 🙂 — updated.

Remove Date.now() from the preferences reducer, making it a pure
function.
@noisysocks noisysocks force-pushed the fix/make-reducer-deterministic branch from e4f1c1b to da00a9a Compare March 8, 2018 18:25
@noisysocks noisysocks merged commit 5706d51 into master Mar 8, 2018
@noisysocks noisysocks deleted the fix/make-reducer-deterministic branch March 8, 2018 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Framework Issues related to broader framework topics, especially as it relates to javascript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants