Skip to content

Commit

Permalink
update test to correctly test override
Browse files Browse the repository at this point in the history
  • Loading branch information
Flirre committed Feb 14, 2024
1 parent 3271d6d commit 48280de
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions __tests__/atomWithLocation_spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useAtom } from 'jotai';
import React, { StrictMode } from 'react';
import { render } from '@testing-library/react';
import { act, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { atomWithLocation } from '../src/index';

Expand Down Expand Up @@ -126,15 +126,18 @@ describe('atomWithLocation', () => {
<button type="button" onClick={() => window.history.back()}>
back
</button>
<button type="button" onClick={() => setLocation({ pathname: '/1' })}>
<button
type="button"
onClick={() => setLocation({ pathname: '/123' })}
>
button1
</button>
<button
type="button"
onClick={() =>
setLocation(
{
pathname: '/2',
pathname: '/234',
},
{ replace: true },
)
Expand All @@ -154,33 +157,42 @@ describe('atomWithLocation', () => {

const previousTestHistoryLength = 3;

await act(async () => {
window.history.pushState(null, '', '/');
});

await findByText('current pathname in atomWithLocation: /');
expect(window.location.pathname).toEqual('/');
expect(window.history.length).toEqual(previousTestHistoryLength);

await userEvent.click(getByText('button1')); // TODO: Why doesn't it increment the history length?
await userEvent.click(getByText('button1'));

await findByText('current pathname in atomWithLocation: /1');
expect(window.location.pathname).toEqual('/1');
expect(window.history.length).toEqual(previousTestHistoryLength);
await findByText('current pathname in atomWithLocation: /123');
expect(window.location.pathname).toEqual('/123');
expect(window.history.length).toEqual(previousTestHistoryLength + 1);

await userEvent.click(getByText('button2'));

await findByText('current pathname in atomWithLocation: /2');
expect(window.location.pathname).toEqual('/2');
expect(window.history.length).toEqual(previousTestHistoryLength);
await findByText('current pathname in atomWithLocation: /234');
expect(window.location.pathname).toEqual('/234');
expect(window.history.length).toEqual(previousTestHistoryLength + 1);

await userEvent.click(getByText('back'));

await findByText('current pathname in atomWithLocation: /');
expect(window.location.pathname).toEqual('/');
expect(window.history.length).toEqual(previousTestHistoryLength);
expect(window.history.length).toEqual(previousTestHistoryLength + 1);

// The first click overwrites the history entry we
// went back from. The history length remains the same.
await userEvent.click(getByText('button1'));

await findByText('current pathname in atomWithLocation: /1');
expect(window.location.pathname).toEqual('/1');
expect(window.history.length).toEqual(previousTestHistoryLength);
// The second click adds a new history entry, which now increments the history length.
await userEvent.click(getByText('button1'));

await findByText('current pathname in atomWithLocation: /123');
expect(window.location.pathname).toEqual('/123');
expect(window.history.length).toEqual(previousTestHistoryLength + 2);
});
});

Expand Down

0 comments on commit 48280de

Please sign in to comment.