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(onStateChange): propagate change to middleware #4796

Merged
merged 4 commits into from
Jun 29, 2021

Conversation

Haroenv
Copy link
Contributor

@Haroenv Haroenv commented Jun 25, 2021

This wrong behaviour was originally introduced in #4699 by removing onInternalStateChange, since I assumed the helper state change event would call the middleware, but it doesn't happen in all cases.

This means that now onInternalStateChange gets called one more time in certain situations, but that's not really an issue, as it's deferred anyway.

I've checked and the integration test written by @tkrugg indeed starts failing on #4699

Fixes #4795

@Haroenv Haroenv requested review from eunjae-lee and tkrugg June 25, 2021 12:14
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 25, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 89ca17f:

Sandbox Source
InstantSearch.js Configuration

Copy link
Contributor

@tkrugg tkrugg left a comment

Choose a reason for hiding this comment

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

It looks good, thanks for fixing this so fast.
The regression was that onInternalStateChange was no longer called in setUiState. We should have a test checking this specifically (middleware.onStateChange not being called was just a side effect of the regression).

@Haroenv
Copy link
Contributor Author

Haroenv commented Jun 25, 2021

that's more of a side-effect of what actually was the wrong behaviour (middleware weren't called), the onInternalStateChange could change without breaking the behaviour @tkrugg, we shouldn't have a test on the solution, but on the problem it solves

@Haroenv Haroenv requested a review from tkrugg June 25, 2021 13:44
@tkrugg
Copy link
Contributor

tkrugg commented Jun 25, 2021

that's more of a side-effect of what actually was the wrong behaviour (middleware weren't called), the onInternalStateChange could change without breaking the behaviour @tkrugg, we shouldn't have a test on the solution, but on the problem it solves

I think we agree on the idea but disagree on what we call side-effect and behaviour.
I'm really speaking from the setUiState point of view (because that's where I think we should add a test).
The behaviour of this public function is "change uiState and notify everybody with uiState has changed".
The regression happened because the second part of this responsibility was removed, and there was nothing that could have prevented it. Such a test would have made it very clear.

middleware being notified is the side-effect. If at some point we decide middleware will be notified by another mean, setUiState test doesn't have to know about it, it's not its responsibility. I don't think we can ever go wrong by having a test clearly checking the behaviour of a public function is preserved, and here its behaviour is not "it should call middleware", it's "it should notify others of UI State update". That's why I think we're missing a test.

@Haroenv
Copy link
Contributor Author

Haroenv commented Jun 25, 2021

There's actually already two tests here, but they passed with the bug already, do you have any idea what's missing in the condition @tkrugg? https://github.com/algolia/instantsearch.js/blob/e8329aecb386755a039cf10850e394d0d71f29f4/src/lib/__tests__/InstantSearch-test.tsx#L1769-L1856

@eunjae-lee
Copy link
Contributor

The new 2 test cases make sense to me. Which scenario do you think we should test more? @tkrugg

@tkrugg
Copy link
Contributor

tkrugg commented Jun 25, 2021

I was thinking one that would check onInternalStateUpdate is called when setUIState is, because that's the "notify everybody state has changed" part of the implem.

@eunjae-lee
Copy link
Contributor

I was thinking one that would check onInternalStateUpdate is called when setUIState is, because that's the "notify everybody state has changed" part of the implem.

yeah, but also the implementation can change while the end result stays the same.

The contract we have is

when setUiState is called(A), the middlewares' onStateChange gets called(B).

It's not important what happens between (A) and (B). We trigger (A) and see if (B) is achieved. By testing this way, we have a room to refactor , even rename or do whatever with the internal implementations.

Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
@Haroenv Haroenv merged commit 57c32c0 into master Jun 29, 2021
@Haroenv Haroenv deleted the fix/routing-state-change branch June 29, 2021 08:57
Haroenv added a commit that referenced this pull request Jul 1, 2021
* chore: create a regression test showcasing onStateChange regression

* fix(onStateChange): still call internal state change

* simplify test

* better test titles

Co-authored-by: Youcef Mammar <youcef.mammar@algolia.com>
Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
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.

URL routing is not working while a custom onStateChange function is defined
4 participants