Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep track of number of references to global context and cleanup (#1543)
* keep track of number of references to global context and cleanup * refactor and add unit test * fix: run prettier on dndprovider, add some additional checks in test
- Loading branch information
1 parent
c090ee4
commit c5a9abc
Showing
3 changed files
with
104 additions
and
36 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
packages/core/react-dnd/src/__tests__/DndProvider.spec.tsx
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,59 @@ | ||
import { createDragDropManager } from 'dnd-core' | ||
import * as React from 'react' | ||
import { create } from 'react-test-renderer' | ||
import { DndContext, DndProvider } from '..' | ||
import TestBackend from 'react-dnd-test-backend' | ||
|
||
describe('DndProvider', () => { | ||
it('reuses DragDropManager provided to it', () => { | ||
let capturedManager | ||
const manager = createDragDropManager(TestBackend, {}, {}) | ||
|
||
create( | ||
<DndContext.Provider value={{ dragDropManager: manager }}> | ||
<DndContext.Consumer> | ||
{({ dragDropManager }) => { | ||
capturedManager = dragDropManager | ||
return null | ||
}} | ||
</DndContext.Consumer> | ||
</DndContext.Provider>, | ||
) | ||
|
||
expect(capturedManager).toBe(manager) | ||
}) | ||
|
||
it('stores DragDropManager in global context and cleans up on unmount', () => { | ||
let capturedManager | ||
|
||
const mountProvider = () => | ||
create( | ||
<DndProvider backend={TestBackend}> | ||
<DndContext.Consumer> | ||
{({ dragDropManager }) => { | ||
capturedManager = dragDropManager | ||
return null | ||
}} | ||
</DndContext.Consumer> | ||
</DndProvider>, | ||
) | ||
|
||
const globalInstance = () => | ||
global[Symbol.for('__REACT_DND_CONTEXT_INSTANCE__')] | ||
|
||
// Single mount & unmount works | ||
const root = mountProvider() | ||
expect(globalInstance().dragDropManager).toEqual(capturedManager) | ||
root.unmount() | ||
expect(globalInstance()).toEqual(null) | ||
|
||
// Two mounted components do a refcount | ||
const rootA = mountProvider() | ||
const rootB = mountProvider() | ||
expect(globalInstance().dragDropManager).toEqual(capturedManager) | ||
rootA.unmount() | ||
expect(globalInstance().dragDropManager).toEqual(capturedManager) | ||
rootB.unmount() | ||
expect(globalInstance()).toEqual(null) | ||
}) | ||
}) |
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