-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(auth): read login error param synchronously to close a render race #3034
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
Open
fra-shipper
wants to merge
2
commits into
Chainlit:main
Choose a base branch
from
fra-shipper:fix/oauth-login-error-render-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+109
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,103 @@ | ||
| import { i18nSetupLocalization } from '@/i18n'; | ||
| import { flushSync } from 'react-dom'; | ||
| import { createRoot } from 'react-dom/client'; | ||
| import { MemoryRouter } from 'react-router-dom'; | ||
| import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import Login from '@/pages/Login'; | ||
|
|
||
| // Real i18next init (no backend plugin, so it resolves synchronously) -- | ||
| // LoginForm's <Translator> calls i18n.exists() at render time and throws | ||
| // if no instance was ever initialized. | ||
| i18nSetupLocalization(); | ||
|
|
||
| vi.mock('client-types/*', async () => { | ||
| const { createContext } = await import('react'); | ||
| const mockApiClient = { | ||
| buildEndpoint: (path: string) => `http://localhost:8000${path}`, | ||
| getOAuthEndpoint: (provider: string) => | ||
| `http://localhost:8000/auth/oauth/${provider}` | ||
| }; | ||
| return { | ||
| ChainlitContext: createContext(mockApiClient), | ||
| useAuth: () => ({ | ||
| data: { requireLogin: true, oauthProviders: [] }, | ||
| user: null, | ||
| setUserFromAPI: vi.fn() | ||
| }) | ||
| }; | ||
| }); | ||
|
|
||
| // Logo pulls useConfig() from @chainlit/react-client, which needs a | ||
| // RecoilRoot ancestor. It is unrelated to the error-render race under test, | ||
| // so stub it out rather than wiring up Recoil for this test. | ||
| vi.mock('@/components/Logo', () => ({ | ||
| Logo: () => null | ||
| })); | ||
|
|
||
| // jsdom does not implement matchMedia, and Login -> useTheme() calls it | ||
| // synchronously during render (not from an effect), so stub it the same way | ||
| // FavoriteButton.spec.tsx stubs ResizeObserver. | ||
| window.matchMedia = | ||
| window.matchMedia || | ||
| vi.fn().mockImplementation((query: string) => ({ | ||
| matches: false, | ||
| media: query, | ||
| addEventListener: vi.fn(), | ||
| removeEventListener: vi.fn(), | ||
| dispatchEvent: vi.fn() | ||
| })); | ||
|
|
||
| describe('Login', () => { | ||
| let container: HTMLDivElement | null = null; | ||
| let root: ReturnType<typeof createRoot> | null = null; | ||
|
|
||
| afterEach(() => { | ||
| if (root) { | ||
| root.unmount(); | ||
| root = null; | ||
| } | ||
| if (container) { | ||
| document.body.removeChild(container); | ||
| container = null; | ||
| } | ||
| }); | ||
|
|
||
| // Regression test for the render race fixed by reading the ?error= query | ||
| // param via a lazy useState initializer instead of an effect. Uses | ||
| // createRoot + flushSync directly instead of Testing Library's render(), | ||
| // because render() wraps in act(), which flushes passive effects | ||
| // synchronously and hides the gap this test is pinning: what is in the DOM | ||
| // on the very first commit, before any useEffect has run. | ||
| it('renders the [role="alert"] error message on the first synchronous commit', () => { | ||
| container = document.createElement('div'); | ||
| document.body.appendChild(container); | ||
| root = createRoot(container); | ||
|
|
||
| flushSync(() => { | ||
| root.render( | ||
| <MemoryRouter initialEntries={['/login?error=some-error']}> | ||
| <Login /> | ||
| </MemoryRouter> | ||
| ); | ||
| }); | ||
|
|
||
| expect(container.querySelector('[role="alert"]')).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('renders no [role="alert"] on the first synchronous commit when there is no error param', () => { | ||
| container = document.createElement('div'); | ||
| document.body.appendChild(container); | ||
| root = createRoot(container); | ||
|
|
||
| flushSync(() => { | ||
| root.render( | ||
| <MemoryRouter initialEntries={['/login']}> | ||
| <Login /> | ||
| </MemoryRouter> | ||
| ); | ||
| }); | ||
|
|
||
| expect(container.querySelector('[role="alert"]')).toBeNull(); | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.