From 88c837f956f38f211b07e667a1908785ad979d48 Mon Sep 17 00:00:00 2001 From: FBerthelot Date: Sat, 7 Dec 2019 16:48:03 +0100 Subject: [PATCH] :sparkles: add react contexts externals option --- .../src/__tests__/react-context.spec.js | 21 +++++++++++ .../src/dispatcher/index.js | 6 +++- .../src/dispatcher/index.spec.js | 35 ++++++++++++++++++- website/docs/api-react.md | 29 ++++++++++++++- 4 files changed, 88 insertions(+), 3 deletions(-) diff --git a/packages/component-test-utils-react/src/__tests__/react-context.spec.js b/packages/component-test-utils-react/src/__tests__/react-context.spec.js index 30cdd7f..e898a81 100644 --- a/packages/component-test-utils-react/src/__tests__/react-context.spec.js +++ b/packages/component-test-utils-react/src/__tests__/react-context.spec.js @@ -73,4 +73,25 @@ describe('shallow - react context', () => { '
light
' ); }); + + it('should overide the context when givin a specific param to the shallow', () => { + const ThemeContext = React.createContext('light'); + + const ThemedButton = () => { + const context = React.useContext(ThemeContext); + return
{context}
; + }; + + const cmp = shallow(, { + externals: { + contexts: [ + {id: ThemeContext, value: 'dark'} + ] + } + }); + + expect(cmp.html()).toBe( + '
dark
' + ); + }); }); diff --git a/packages/component-test-utils-react/src/dispatcher/index.js b/packages/component-test-utils-react/src/dispatcher/index.js index 4cd6867..6688e01 100644 --- a/packages/component-test-utils-react/src/dispatcher/index.js +++ b/packages/component-test-utils-react/src/dispatcher/index.js @@ -56,7 +56,11 @@ class Dispatcher { } useContext(context) { - return context._currentValue; + const contextOveride = this._shallowedComponent._config.externals && + this._shallowedComponent._config.externals.contexts && + this._shallowedComponent._config.externals.contexts.find(c => c.id === context); + + return contextOveride ? contextOveride.value : context._currentValue; } useReducer(reducer, initialState) { diff --git a/packages/component-test-utils-react/src/dispatcher/index.spec.js b/packages/component-test-utils-react/src/dispatcher/index.spec.js index 442b65f..2ca4d97 100644 --- a/packages/component-test-utils-react/src/dispatcher/index.spec.js +++ b/packages/component-test-utils-react/src/dispatcher/index.spec.js @@ -6,7 +6,8 @@ describe('dispatcher', () => { beforeEach(() => { component = { - _render: jest.fn() + _render: jest.fn(), + _config: {} }; dispatcher = createDispatcher(component); }); @@ -77,6 +78,38 @@ describe('dispatcher', () => { const contextValue = dispatcher.useContext({_currentValue: 42}); expect(contextValue).toBe(42); }); + + it('should return the context overide when there is one', () => { + const contextTest = {_currentValue: 42}; + component._config = { + externals: { + contexts: [{id: contextTest, value: 0}] + } + }; + + const contextValue = dispatcher.useContext(contextTest); + expect(contextValue).toBe(0); + }); + + it('should should return actualValue when context is not found', () => { + const contextTest = {_currentValue: 42}; + component._config = { + externals: { + contexts: [{id: {}, value: 0}] + } + }; + const contextValue = dispatcher.useContext(contextTest); + expect(contextValue).toBe(42); + }); + + it('should should return actualValue when no contexts found', () => { + const contextTest = {_currentValue: 42}; + component._config = { + externals: {} + }; + const contextValue = dispatcher.useContext(contextTest); + expect(contextValue).toBe(42); + }); }); describe('useReducer', () => { diff --git a/website/docs/api-react.md b/website/docs/api-react.md index 233b92a..cb41cb5 100644 --- a/website/docs/api-react.md +++ b/website/docs/api-react.md @@ -1,12 +1,39 @@ ## React specialty +### React context + +When you want to test component that use a context, you can set while shallow the context using `externals` shallow constructor option. + +### Example with jest + +```jsx +const ThemeContext = React.createContext('light'); + +const ThemedButton = () => { + const context = React.useContext(ThemeContext); + return
{context}
; +}; + +const cmp = shallow(, { + externals: { + contexts: [ + {id: ThemeContext, value: 'dark'} + ] + } +}); + +expect(cmp.html()).toBe( + '
dark
' +); +``` + ### useDebugValue() hook If you want to test that your component use this hook, you can set the debug option to true. If the option is at true, useDebugValue will call `console.debug` function. -#### example with jest : +#### Example with jest ```jsx it('should call console.debug', () => {