-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathTestContextWrapper.js
35 lines (33 loc) · 1023 Bytes
/
TestContextWrapper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import React from 'react';
import { View } from 'react-native';
import {
GlobalStateContext,
DispatchContext,
reducer,
DEFAULT_STATE,
} from './StateManager';
// To be used as a wrapper component for components which need context in tests
const TestContextWrapper = ({ child, childProps, passContextToChildren }) => {
const [ globalState, dispatch ] = React.useReducer(reducer, DEFAULT_STATE);
globalState.theme = {};
return (
<DispatchContext.Provider value={dispatch}>
<GlobalStateContext.Provider value={globalState}>
<View
_globalState={globalState}
_dispatch={dispatch}
>
{
passContextToChildren ?
React.createElement(child, { ...globalState, dispatch, ...childProps }) :
React.createElement(child, childProps)
}
</View>
</GlobalStateContext.Provider>
</DispatchContext.Provider>
);
}
TestContextWrapper.defaultProps = {
childProps: {},
}
export default TestContextWrapper;