-
Notifications
You must be signed in to change notification settings - Fork 0
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
Test: Add interaction test for use async tracker #89
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { userEvent, waitFor, within } from '@storybook/testing-library'; | ||
import { expect } from '@storybook/jest'; | ||
import { UseAsyncTracker } from './UseAsyncTracker'; | ||
import { sleep } from './sleep'; | ||
|
||
const meta = { | ||
component: UseAsyncTracker, | ||
} satisfies Meta<typeof UseAsyncTracker>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
play: async ({ canvasElement }) => { | ||
await sleep(); | ||
const canvas = within(canvasElement); | ||
|
||
expect(canvas.getByTestId('global-count').textContent).toBe( | ||
'Global count: 0' | ||
); | ||
expect(canvas.getByTestId('other-count').textContent).toBe( | ||
'Other count: 0' | ||
); | ||
|
||
await userEvent.click(canvas.getByText('load other 2')); | ||
await waitFor(() => { | ||
expect(canvas.getByTestId('other-count').textContent).toBe( | ||
'Other count: 1' | ||
); | ||
expect(canvas.getByText('loading...')).toBeDisabled(); | ||
}); | ||
await waitFor( | ||
() => { | ||
expect(canvas.getByTestId('other-count').textContent).toBe( | ||
'Other count: 0' | ||
); | ||
}, | ||
{ timeout: 5000 } | ||
); | ||
|
||
await userEvent.click(canvas.getByText('load global')); | ||
await waitFor(() => { | ||
expect(canvas.getByTestId('global-count').textContent).toBe( | ||
'Global count: 1' | ||
); | ||
expect(canvas.getByText('loading...')).toBeDisabled(); | ||
}); | ||
await waitFor( | ||
() => { | ||
expect(canvas.getByTestId('global-count').textContent).toBe( | ||
'Global count: 0' | ||
); | ||
}, | ||
{ timeout: 5000 } | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React from 'react'; | ||
import createContext from '@gen/use-async-tracker'; | ||
|
||
const { useAsyncTracker, Provider } = createContext({ global: 0, other: 0 }); | ||
|
||
const wait = async (ms = 1000): Promise<void> => | ||
new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
const Second: React.FC = () => { | ||
const [count, trackAsync] = useAsyncTracker((store) => store.other); | ||
|
||
const handleClick = async (): Promise<void> => { | ||
await trackAsync(wait(), 'other'); | ||
}; | ||
return ( | ||
<div className="bg-slate-200"> | ||
<div data-testid="other-count">Other count: {count}</div> | ||
<button disabled={count > 0} onClick={() => void handleClick()}> | ||
{count > 0 ? 'loading...' : 'load other'} | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
const First: React.FC = () => { | ||
const [count, trackAsync] = useAsyncTracker((store) => store.global); | ||
|
||
const handleClick = async (): Promise<void> => { | ||
await trackAsync(wait(), 'global'); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div data-testid="global-count">Global count: {count}</div> | ||
<div> | ||
<button disabled={count > 0} onClick={() => void handleClick()}> | ||
{count > 0 ? 'loading...' : 'load global'} | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
const Buttons: React.FC = () => { | ||
const [, trackAsync] = useAsyncTracker((store) => store.global); | ||
|
||
const handleClick: React.MouseEventHandler<HTMLButtonElement> = (e) => { | ||
const { value } = e.currentTarget; | ||
if (value !== 'global' && value !== 'other') return; | ||
void trackAsync(wait(), value); | ||
}; | ||
|
||
return ( | ||
<div className="flex flex-col items-start"> | ||
<button value="global" onClick={handleClick}> | ||
load global 2 | ||
</button> | ||
<button value="other" onClick={handleClick}> | ||
load other 2 | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
export const UseAsyncTracker: React.FC = () => { | ||
return ( | ||
<Provider> | ||
<First /> | ||
<Second /> | ||
<Buttons /> | ||
</Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Sleep for a given number of milliseconds. | ||
* Sometimes play function plays too fast that userEvent doesn't seem to work as intended. | ||
* https://github.com/storybookjs/storybook/issues/18258 | ||
* | ||
* @param ms milliseconds to sleep | ||
* @returns Promise<void> | ||
*/ | ||
export const sleep = (ms = 100): Promise<void> => | ||
new Promise((resolve) => setTimeout(resolve, ms)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Play function was failing without sleep.
I can see the button
onClick
handler is being called viauseEvent.click
but maybe it was clicked too fast? Nothing seems to happen on the click event.This could be an issue with storybook.
storybookjs/storybook#18258