-
Notifications
You must be signed in to change notification settings - Fork 361
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
1 parent
129cc35
commit c275b5a
Showing
5 changed files
with
73 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { createRoot } from 'react-dom/client'; | ||
|
||
import { handleRoot, rootInstances } from './rootManager'; | ||
|
||
vi.mock('react-dom/client', () => ({ | ||
createRoot: vi.fn().mockImplementation((container) => ({ | ||
_internalRoot: container, // Mock implementation detail | ||
render: vi.fn(), | ||
})), | ||
})); | ||
|
||
describe('handleRoot', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
rootInstances.clear(); | ||
}); | ||
|
||
it('should create a new root for a new container', () => { | ||
const container = document.createElement('div'); | ||
const root = handleRoot(container); | ||
|
||
expect(createRoot).toHaveBeenCalledWith(container); | ||
expect(rootInstances.get(container)).toBe(root); | ||
expect(createRoot).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should return the existing root for an existing container', () => { | ||
const container = document.createElement('div'); | ||
// Call handleRoot twice with the same container | ||
const firstCallRoot = handleRoot(container); | ||
const secondCallRoot = handleRoot(container); | ||
|
||
// createRoot should only have been called once | ||
expect(createRoot).toHaveBeenCalledTimes(1); | ||
expect(firstCallRoot).toBe(secondCallRoot); | ||
expect(rootInstances.size).toBe(1); | ||
}); | ||
}); |
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,16 @@ | ||
import { createRoot } from 'react-dom/client'; | ||
|
||
import type { Root } from 'react-dom/client'; | ||
|
||
export const rootInstances = new Map<HTMLElement, Root>(); | ||
|
||
export function handleRoot(container: HTMLElement): Root { | ||
let root = rootInstances.get(container); | ||
|
||
if (!root) { | ||
root = createRoot(container); | ||
rootInstances.set(container, root); | ||
} | ||
|
||
return root; | ||
} |