-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#86 added global configuration object and enforceProvider rule
- Loading branch information
Showing
9 changed files
with
128 additions
and
43 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,17 @@ | ||
import { ModalConfig } from './types'; | ||
|
||
const config: ModalConfig = { | ||
/** | ||
* If set to `true` you will get an error when trying to access | ||
* a context without the `ModalProvider` declared above. | ||
*/ | ||
enforceProvider: false, | ||
}; | ||
|
||
export function setModalConfig(newConfig: Partial<ModalConfig>) { | ||
Object.assign(config, newConfig); | ||
} | ||
|
||
export function getModalConfig() { | ||
return config; | ||
} |
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 |
---|---|---|
@@ -1,47 +1,85 @@ | ||
import React from 'react'; | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import { ModalProviderWrapper as wrapper } from './test-utils'; | ||
import useModalContext from './use-modal-context'; | ||
import { | ||
act, | ||
renderHook, | ||
WrapperComponent, | ||
} from '@testing-library/react-hooks'; | ||
import { ModalProviderWrapper } from './test-utils'; | ||
import useModal from './use-modal'; | ||
import { ModalContextState } from './modal-context'; | ||
|
||
describe('ModalContext', () => { | ||
const rootId = '123'; | ||
const modalId = '321'; | ||
|
||
test('should be initialized with correct state', () => { | ||
const { result } = renderHook(() => useModalContext(), { | ||
wrapper, | ||
}); | ||
const testHook = (wrapper?: WrapperComponent<any>) => { | ||
const { result } = renderHook(() => useModal(), { wrapper }); | ||
const ctx = result.current as ModalContextState; | ||
|
||
expect(result.current).toMatchObject({ | ||
destroyModal: expect.any(Function), | ||
destroyModalsByRootId: expect.any(Function), | ||
hideModal: expect.any(Function), | ||
showModal: expect.any(Function), | ||
state: {}, | ||
updateModal: expect.any(Function), | ||
}); | ||
const performModalActions = (modal: any) => { | ||
act(() => { | ||
modal.update({}); | ||
modal.hide(); | ||
modal.destroy(); | ||
}); | ||
}; | ||
|
||
return { ctx, performModalActions }; | ||
}; | ||
|
||
const runTests = ( | ||
context: ModalContextState, | ||
wrapper?: WrapperComponent<any> | ||
) => { | ||
const { performModalActions } = testHook(wrapper); | ||
|
||
act(() => { | ||
const modal = result.current.showModal(() => <div>test</div>); | ||
modal.update({}); | ||
modal.hide(); | ||
modal.destroy(); | ||
const modal = context.showModal(() => <div>test</div>); | ||
performModalActions(modal); | ||
}); | ||
|
||
act(() => { | ||
result.current.updateModal(modalId, {}); | ||
context.updateModal(modalId, {}); | ||
}); | ||
|
||
act(() => { | ||
result.current.hideModal(modalId); | ||
context.hideModal(modalId); | ||
}); | ||
|
||
act(() => { | ||
result.current.destroyModal(modalId); | ||
context.destroyModal(modalId); | ||
}); | ||
|
||
act(() => { | ||
result.current.destroyModalsByRootId(rootId); | ||
context.destroyModalsByRootId(rootId); | ||
}); | ||
}; | ||
|
||
test('should be initialized with correct state', () => { | ||
const { ctx } = testHook(ModalProviderWrapper); | ||
expect(ctx).toMatchObject({ | ||
destroyModal: expect.any(Function), | ||
destroyModalsByRootId: expect.any(Function), | ||
hideModal: expect.any(Function), | ||
showModal: expect.any(Function), | ||
state: {}, | ||
updateModal: expect.any(Function), | ||
}); | ||
|
||
runTests(ctx, ModalProviderWrapper); | ||
}); | ||
|
||
test('should be initialized without context provider and return fallback', () => { | ||
const { ctx } = testHook(undefined); | ||
expect(ctx).toMatchObject({ | ||
destroyModal: expect.any(Function), | ||
destroyModalsByRootId: expect.any(Function), | ||
hideModal: expect.any(Function), | ||
showModal: expect.any(Function), | ||
state: {}, | ||
updateModal: expect.any(Function), | ||
}); | ||
|
||
runTests(ctx); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
import { useContext } from 'react'; | ||
import ModalContext from './modal-context'; | ||
import ModalContext, { modalContextFallback } from './modal-context'; | ||
import { getModalConfig } from './modal-config'; | ||
|
||
export default function useModalContext() { | ||
const context = useContext(ModalContext); | ||
const { enforceProvider } = getModalConfig(); | ||
|
||
if (context === undefined) { | ||
if (enforceProvider && context === undefined) { | ||
throw new Error('useModalContext must be used within a ModalProvider'); | ||
} | ||
|
||
return context; | ||
return context || modalContextFallback; | ||
} |
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