-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
101 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { render } from '@testing-library/react'; | ||
import * as React from 'react'; | ||
import { renderToString } from 'react-dom/server'; | ||
import useId, { resetUuid } from '../src/hooks/useId'; | ||
|
||
jest.mock('react', () => { | ||
const react = jest.requireActual('react'); | ||
|
||
const clone = { ...react }; | ||
|
||
Object.defineProperty(clone, 'useId', { | ||
get: () => null, | ||
}); | ||
|
||
return clone; | ||
}); | ||
|
||
describe('hooks-17', () => { | ||
describe('useId', () => { | ||
const Demo = ({ id } = {}) => { | ||
const mergedId = useId(id); | ||
return <div id={mergedId} className="target" />; | ||
}; | ||
|
||
function matchId(container, id) { | ||
const ele = container.querySelector('.target'); | ||
return expect(ele.id).toEqual(id); | ||
} | ||
|
||
it('fallback of React 17 or lower', () => { | ||
const errorSpy = jest.spyOn(console, 'error'); | ||
const originEnv = process.env.NODE_ENV; | ||
process.env.NODE_ENV = 'development'; | ||
|
||
// SSR | ||
const content = renderToString( | ||
<React.StrictMode> | ||
<Demo /> | ||
</React.StrictMode>, | ||
); | ||
expect(content).toContain('ssr-id'); | ||
|
||
// Hydrate | ||
resetUuid(); | ||
const holder = document.createElement('div'); | ||
holder.innerHTML = content; | ||
const { container } = render( | ||
<React.StrictMode> | ||
<Demo /> | ||
</React.StrictMode>, | ||
{ | ||
hydrate: true, | ||
container: holder, | ||
}, | ||
); | ||
|
||
matchId(container, 'rc_unique_1'); | ||
|
||
errorSpy.mockRestore(); | ||
process.env.NODE_ENV = originEnv; | ||
}); | ||
}); | ||
}); |
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