Skip to content
This repository was archived by the owner on Jan 1, 2025. It is now read-only.

fix: Reset atoms when param is removed from URL when queryParams with param is specified #1976

Conversation

AkifumiSato
Copy link
Contributor

sumary

resolve: #1900
If queryParams and param are specified and there are no parameters in the URL, the update process may not be performed.
The reason is that null is assigned here, and the branch is not passed.

note

I was going to write a test, but the current test relies on RecoilSync_MockURLSerialization.js, so it is difficult to write a reproducible test for this bug.

- when `queryParams` with `param` is specified, and there is no parameter in the URL
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 19, 2022
@drarmstr drarmstr added bug Something isn't working Component: recoil-sync Related to recoil-sync labels Aug 19, 2022
@drarmstr
Copy link
Contributor

@AkifumiSato - That mock URL serialization doesn't need to necessarily be used by all tests or can be extended. There is also a browser interface abstraction that may be useful for testing as well.

@AkifumiSato
Copy link
Contributor Author

@drarmstr Yes, I tried to write a test without mock, but it did not work.
This is the diff when I tried it on another branch.
AkifumiSato@a58eb75#diff-74f521e2004fc6640581dbfffa78e3cf816c4b8b55c81ab8f58d21873af6981cR193

This is what I found when I tried this test case.

  • pushState after rendering in a test case does not change the rendering(fail L.197).
  • await flushPromisesAndTimers causes URL to revert back to before push (fail L.196).

For these reasons, I gave up implementing this tests.
Maybe I am not doing it right, but I could not figure out the cause.
Would you advise?

@facebook-github-bot
Copy link
Contributor

@drarmstr has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@drarmstr
Copy link
Contributor

I think that's just because actions like history.pushState() don't trigger a popstate browser event. (See #1975). I've added the following updated test to the PR (No need for you to update the PR)

test('Remove parameter', async () => {
  const loc = {part: 'queryParams', param: 'param'};

  const atomA = atom({
    key: 'recoil-url-sync remove param',
    default: 'DEFAULT',
    effects: [syncEffect({itemKey: 'item', refine: string()})],
  });

  const container = renderElements(
    <TestURLSync location={loc}>
      <ReadsAtom atom={atomA} />
    </TestURLSync>,
  );
  expect(container.textContent).toBe('"DEFAULT"');

  history.replaceState(null, '', encodeURL([[loc, {item: 'SET'}]]));
  history.pushState(null, 'void');
  history.back();
  await flushPromisesAndTimers();
  expect(container.textContent).toBe('"SET"');

  // clear all query params from the URL to confirm it resets the atoms.
  history.replaceState(null, '', location.origin);
  history.pushState(null, 'void');
  history.back();
  await flushPromisesAndTimers();
  expect(container.textContent).toBe('"DEFAULT"');
});

@drarmstr drarmstr changed the title fix: Update may not be call when queryParams with param is specified fix: Reset atoms when param is removed from URL when queryParams with param is specified Aug 23, 2022
@mondaychen
Copy link
Contributor

This is strange. Why does this PR has no CI results?

@drarmstr
Copy link
Contributor

This is strange. Why does this PR has no CI results?

Because it was an external submission which I then extended with tests and test fixes in www, but because it originated with the external PR I can't export those edits back out to the PR again to run the CI. Seems like a limitation in our syncing mechanism.

@drarmstr drarmstr self-assigned this Sep 16, 2022
snipershooter0701 pushed a commit to snipershooter0701/Recoil that referenced this pull request Mar 5, 2023
…ied (#1976)

Summary:
## sumary
resolve: facebookexperimental/Recoil#1900
If `queryParams` and `param` are specified and there are no parameters in the URL, the update process may not be performed.
The reason is that null is assigned [here](https://github.com/facebookexperimental/Recoil/blob/0.7.5/packages/recoil-sync/RecoilSync_URL.js#L75), and [the branch](https://github.com/facebookexperimental/Recoil/blob/0.7.5/packages/recoil-sync/RecoilSync_URL.js#L248-L250) is not passed.

## note
I was going to write a test, but the current test relies on [RecoilSync_MockURLSerialization.js](https://github.com/facebookexperimental/Recoil/blob/0.7.5/packages/recoil-sync/__test_utils__/RecoilSync_MockURLSerialization.js#L92), so it is difficult to write a reproducible test for this bug.

Pull Request resolved: facebookexperimental/Recoil#1976

Reviewed By: butlersrepos

Differential Revision: D38922526

Pulled By: drarmstr

fbshipit-source-id: 40908fc2cc035790fe7750f13a5f8e2e26ef8b76
eagle2722 added a commit to eagle2722/Recoil that referenced this pull request Sep 21, 2024
…ied (#1976)

Summary:
## sumary
resolve: facebookexperimental/Recoil#1900
If `queryParams` and `param` are specified and there are no parameters in the URL, the update process may not be performed.
The reason is that null is assigned [here](https://github.com/facebookexperimental/Recoil/blob/0.7.5/packages/recoil-sync/RecoilSync_URL.js#L75), and [the branch](https://github.com/facebookexperimental/Recoil/blob/0.7.5/packages/recoil-sync/RecoilSync_URL.js#L248-L250) is not passed.

## note
I was going to write a test, but the current test relies on [RecoilSync_MockURLSerialization.js](https://github.com/facebookexperimental/Recoil/blob/0.7.5/packages/recoil-sync/__test_utils__/RecoilSync_MockURLSerialization.js#L92), so it is difficult to write a reproducible test for this bug.

Pull Request resolved: facebookexperimental/Recoil#1976

Reviewed By: butlersrepos

Differential Revision: D38922526

Pulled By: drarmstr

fbshipit-source-id: 40908fc2cc035790fe7750f13a5f8e2e26ef8b76
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Component: recoil-sync Related to recoil-sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

<RecoilURLSync> doesn't reset state if param is not present
4 participants