-
-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jsx/dom): support getServerSnapshot in useSyncExternalStore (#2646)
* feat(jsx/dom): support getServerSnapshot in useSyncExternalStore * chore: denoify * test: add tests for hooks with `.toString()` * test: fix typo. * refactor: set the flag to false at the end of top level render * chore: denoify
- Loading branch information
Showing
6 changed files
with
126 additions
and
30 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 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 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,40 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { jsx, useState, useSyncExternalStore } from '..' | ||
|
||
describe('useState', () => { | ||
it('should be rendered with initial state', () => { | ||
const Component = () => { | ||
const [state] = useState('hello') | ||
return <span>{state}</span> | ||
} | ||
const template = <Component /> | ||
expect(template.toString()).toBe('<span>hello</span>') | ||
}) | ||
}) | ||
|
||
describe('useSyncExternalStore', () => { | ||
it('should be rendered with result of getServerSnapshot()', () => { | ||
const unsubscribe = vi.fn() | ||
const subscribe = vi.fn(() => unsubscribe) | ||
const getSnapshot = vi.fn() | ||
const getServerSnapshot = vi.fn(() => 100) | ||
const App = () => { | ||
const count = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot) | ||
return <div>{count}</div> | ||
} | ||
const template = <App /> | ||
expect(template.toString()).toBe('<div>100</div>') | ||
expect(unsubscribe).not.toBeCalled() | ||
expect(subscribe).not.toBeCalled() | ||
expect(getSnapshot).not.toBeCalled() | ||
}) | ||
|
||
it('should raise an error if getServerShot() is not provided', () => { | ||
const App = () => { | ||
const count = useSyncExternalStore(vi.fn(), vi.fn()) | ||
return <div>{count}</div> | ||
} | ||
const template = <App /> | ||
expect(() => template.toString()).toThrowError() | ||
}) | ||
}) |