-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Elements initialization in React Strict/Concurrent mode
Refs cannot be mutated and used to update state in the same time in rendering phase. As this is side-effect, it can produce various bugs in concurrent mode. In StrictMode Elements doesn't do transition from null to valid stripe instance. As in StrictMode React renders twice, `final` ref becomes `true`, but `ctx` state isn't changed. References: https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects facebook/react#18003 facebook/react#18545
- Loading branch information
Showing
6 changed files
with
172 additions
and
116 deletions.
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,60 @@ | ||
import {renderHook, act} from '@testing-library/react-hooks'; | ||
import {usePromiseResolver} from './usePromiseResolver'; | ||
import { mockStripe } from '../../test/mocks'; | ||
|
||
const createImperivePromise = (): [Promise<unknown>, (value?: unknown) => Promise<void>, (reason?: any) => Promise<void>] => { | ||
let resolveFn: (value?: unknown) => Promise<void> = () => Promise.resolve() | ||
let rejectFn: (reason?: any) => Promise<void> = () => Promise.resolve() | ||
|
||
const promise = new Promise((resolve, reject) => { | ||
const createVoidPromise = () => promise.then(() => undefined, () => undefined) | ||
|
||
resolveFn = (value) => { | ||
resolve(value) | ||
return createVoidPromise() | ||
} | ||
|
||
rejectFn = (reason) => { | ||
reject(reason) | ||
return createVoidPromise() | ||
} | ||
}) | ||
|
||
return [promise, resolveFn, rejectFn] | ||
} | ||
|
||
describe('usePromiseResolver', () => { | ||
let stripe: ReturnType<typeof mockStripe>; | ||
|
||
beforeEach(() => { | ||
stripe = mockStripe(); | ||
}) | ||
|
||
it('returns resolved on mount when not promise given', () => { | ||
const {result} = renderHook(() => usePromiseResolver(stripe)); | ||
expect(result.current).toEqual([stripe, undefined, 'resolved']) | ||
}); | ||
|
||
it('returns pending on mount when promise given', () => { | ||
const [promise] = createImperivePromise() | ||
const {result} = renderHook(() => usePromiseResolver(promise)); | ||
expect(result.current).toEqual([undefined, undefined, 'pending']) | ||
}); | ||
|
||
it('returns resolved when given promise resolved', async () => { | ||
const [promise, resolve] = createImperivePromise() | ||
const {result} = renderHook(() => usePromiseResolver(promise)); | ||
|
||
await act(() => resolve(stripe)) | ||
expect(result.current).toEqual([stripe, undefined, 'resolved']) | ||
}); | ||
|
||
it('returns rejected when given promise rejected', async () => { | ||
const [promise,, reject] = createImperivePromise() | ||
const {result} = renderHook(() => usePromiseResolver(promise)); | ||
|
||
const error = new Error('Something went wrong') | ||
await act(() => reject(error)) | ||
expect(result.current).toEqual([undefined, error, 'rejected']) | ||
}); | ||
}); |
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,51 @@ | ||
import React from 'react'; | ||
import {isPromise} from '../utils/guards'; | ||
|
||
type PromisePending = [undefined, undefined, 'pending']; | ||
type PromiseResolved<T> = [T, undefined, 'resolved']; | ||
type PromiseRejected = [undefined, any, 'rejected']; | ||
type PromiseState<T> = PromisePending | PromiseResolved<T> | PromiseRejected; | ||
|
||
const createPending = (): PromisePending => [undefined, undefined, 'pending']; | ||
|
||
const createResolved = <T>(value: T): PromiseResolved<T> => [ | ||
value, | ||
undefined, | ||
'resolved', | ||
]; | ||
|
||
const createRejected = (reason: any): PromiseRejected => [ | ||
undefined, | ||
reason, | ||
'rejected', | ||
]; | ||
|
||
export const usePromiseResolver = <T>( | ||
mayBePromise: T | PromiseLike<T> | ||
): PromiseState<T> => { | ||
const [state, setState] = React.useState<PromiseState<T>>(() => | ||
isPromise(mayBePromise) ? createPending() : createResolved(mayBePromise) | ||
); | ||
|
||
React.useEffect(() => { | ||
if (!isPromise(mayBePromise)) return setState(createResolved(mayBePromise)); | ||
|
||
let isMounted = true; | ||
|
||
setState(createPending()); | ||
mayBePromise | ||
.then( | ||
(resolved) => createResolved(resolved), | ||
(error) => createRejected(error) | ||
) | ||
.then((nextState) => { | ||
if (isMounted) setState(nextState); | ||
}); | ||
|
||
return () => { | ||
isMounted = false; | ||
}; | ||
}, [mayBePromise]); | ||
|
||
return state; | ||
}; |