react-native-toast-message 2.1.5 → 2.2.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/src/Toast.d.ts +1 -1
- package/lib/src/Toast.js +5 -3
- package/lib/src/ToastUI.js +1 -1
- package/lib/src/__helpers__/PanResponder.d.ts +3 -0
- package/lib/src/__helpers__/PanResponder.js +26 -0
- package/lib/src/__helpers__/testing-library/react-hooks.d.ts +9 -0
- package/lib/src/__helpers__/testing-library/react-hooks.js +13 -0
- package/lib/src/__helpers__/testing-library/react-native.d.ts +9 -0
- package/lib/src/__helpers__/testing-library/react-native.js +13 -0
- package/lib/src/__mocks__/EnvironmentProviderMock.d.ts +7 -0
- package/lib/src/__mocks__/EnvironmentProviderMock.js +9 -0
- package/lib/src/__tests__/Toast.test.d.ts +1 -0
- package/lib/src/__tests__/Toast.test.js +190 -0
- package/lib/src/__tests__/ToastUI.test.d.ts +1 -0
- package/lib/src/__tests__/ToastUI.test.js +52 -0
- package/lib/src/__tests__/useToast.test.d.ts +1 -0
- package/lib/src/__tests__/useToast.test.js +142 -0
- package/lib/src/components/AnimatedContainer.js +2 -2
- package/lib/src/components/BaseToast.js +2 -2
- package/lib/src/components/__tests__/AnimatedContainer.test.d.ts +1 -0
- package/lib/src/components/__tests__/AnimatedContainer.test.js +147 -0
- package/lib/src/components/__tests__/BaseToast.test.d.ts +1 -0
- package/lib/src/components/__tests__/BaseToast.test.js +81 -0
- package/lib/src/components/__tests__/ErrorToast.test.d.ts +1 -0
- package/lib/src/components/__tests__/ErrorToast.test.js +20 -0
- package/lib/src/components/__tests__/InfoToast.test.d.ts +1 -0
- package/lib/src/components/__tests__/InfoToast.test.js +20 -0
- package/lib/src/components/__tests__/SuccessToast.test.d.ts +1 -0
- package/lib/src/components/__tests__/SuccessToast.test.js +20 -0
- package/lib/src/contexts/EnvironmentContext.d.ts +8 -0
- package/lib/src/contexts/EnvironmentContext.js +22 -0
- package/lib/src/contexts/__tests__/EnvironmentContext.test.d.ts +1 -0
- package/lib/src/contexts/__tests__/EnvironmentContext.test.js +29 -0
- package/lib/src/contexts/__tests__/LoggerContext.test.d.ts +1 -0
- package/lib/src/contexts/__tests__/LoggerContext.test.js +35 -0
- package/lib/src/contexts/index.d.ts +4 -1
- package/lib/src/contexts/index.js +2 -1
- package/lib/src/hooks/__tests__/useKeyboard.test.d.ts +1 -0
- package/lib/src/hooks/__tests__/useKeyboard.test.js +60 -0
- package/lib/src/hooks/__tests__/usePanResponder.test.d.ts +1 -0
- package/lib/src/hooks/__tests__/usePanResponder.test.js +112 -0
- package/lib/src/hooks/__tests__/useSlideAnimation.test.d.ts +1 -0
- package/lib/src/hooks/__tests__/useSlideAnimation.test.js +88 -0
- package/lib/src/hooks/__tests__/useTimeout.test.d.ts +1 -0
- package/lib/src/hooks/__tests__/useTimeout.test.js +54 -0
- package/lib/src/hooks/__tests__/useViewDimensions.test.d.ts +1 -0
- package/lib/src/hooks/__tests__/useViewDimensions.test.js +51 -0
- package/lib/src/hooks/useSlideAnimation.d.ts +6 -2
- package/lib/src/hooks/useSlideAnimation.js +35 -10
- package/lib/src/types/index.d.ts +9 -0
- package/lib/src/utils/__tests__/number.test.d.ts +1 -0
- package/lib/src/utils/__tests__/number.test.js +15 -0
- package/lib/src/utils/__tests__/obj.test.d.ts +1 -0
- package/lib/src/utils/__tests__/obj.test.js +21 -0
- package/package.json +21 -11
- package/CHANGELOG.md +0 -311
- package/lib/src/utils/array.d.ts +0 -2
- package/lib/src/utils/array.js +0 -4
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/* eslint-env jest */
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { Dimensions, View } from 'react-native';
|
|
4
|
+
import { render, waitFor } from '~/__helpers__/testing-library/react-native';
|
|
5
|
+
import { mockGestureValues, mockPanResponder } from '../../__helpers__/PanResponder';
|
|
6
|
+
import { AnimatedContainer, animatedValueFor, dampingFor } from '../AnimatedContainer';
|
|
7
|
+
const setup = (props) => {
|
|
8
|
+
const onHide = jest.fn();
|
|
9
|
+
const defaultProps = {
|
|
10
|
+
isVisible: false,
|
|
11
|
+
position: 'top',
|
|
12
|
+
topOffset: 40,
|
|
13
|
+
bottomOffset: 40,
|
|
14
|
+
keyboardOffset: 10,
|
|
15
|
+
onHide
|
|
16
|
+
};
|
|
17
|
+
const utils = render(<AnimatedContainer {...defaultProps} {...props}>
|
|
18
|
+
<View testID='childView'/>
|
|
19
|
+
</AnimatedContainer>);
|
|
20
|
+
return {
|
|
21
|
+
...utils
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
const defaultStyles = {
|
|
25
|
+
position: 'absolute',
|
|
26
|
+
left: 0,
|
|
27
|
+
right: 0,
|
|
28
|
+
alignItems: 'center',
|
|
29
|
+
justifyContent: 'center',
|
|
30
|
+
top: 0
|
|
31
|
+
};
|
|
32
|
+
describe('test AnimatedContainer component', () => {
|
|
33
|
+
it('is hidden by default', () => {
|
|
34
|
+
const { queryByTestId } = setup();
|
|
35
|
+
expect(queryByTestId('toastAnimatedContainer')).toHaveStyle({
|
|
36
|
+
...defaultStyles,
|
|
37
|
+
opacity: 0
|
|
38
|
+
});
|
|
39
|
+
expect(queryByTestId('childView')).not.toBe(null);
|
|
40
|
+
});
|
|
41
|
+
it('shows when isVisible is true', async () => {
|
|
42
|
+
const { queryByTestId } = setup({
|
|
43
|
+
isVisible: true
|
|
44
|
+
});
|
|
45
|
+
await waitFor(() => expect(queryByTestId('toastAnimatedContainer')).toHaveStyle({
|
|
46
|
+
...defaultStyles,
|
|
47
|
+
opacity: 1
|
|
48
|
+
}));
|
|
49
|
+
expect(queryByTestId('childView')).not.toBe(null);
|
|
50
|
+
});
|
|
51
|
+
it('restores toast position on pan (if gesture is higher than threshold)', async () => {
|
|
52
|
+
mockPanResponder();
|
|
53
|
+
const onRestorePosition = jest.fn();
|
|
54
|
+
const { queryByTestId } = setup({
|
|
55
|
+
isVisible: true,
|
|
56
|
+
onRestorePosition
|
|
57
|
+
});
|
|
58
|
+
await waitFor(() => expect(queryByTestId('toastAnimatedContainer')).toHaveStyle({
|
|
59
|
+
...defaultStyles,
|
|
60
|
+
opacity: 1
|
|
61
|
+
}));
|
|
62
|
+
const panHandler = queryByTestId('toastAnimatedContainer');
|
|
63
|
+
const gesture = {
|
|
64
|
+
...mockGestureValues,
|
|
65
|
+
moveY: 100,
|
|
66
|
+
dy: 10
|
|
67
|
+
};
|
|
68
|
+
panHandler?.props.onResponderMove(undefined, gesture);
|
|
69
|
+
panHandler?.props.onResponderRelease(undefined, gesture);
|
|
70
|
+
expect(onRestorePosition).toHaveBeenCalled();
|
|
71
|
+
});
|
|
72
|
+
it('dismisses toast on pan (if gesture is lower than threshold)', async () => {
|
|
73
|
+
mockPanResponder();
|
|
74
|
+
const onHide = jest.fn();
|
|
75
|
+
const { queryByTestId } = setup({
|
|
76
|
+
isVisible: true,
|
|
77
|
+
onHide
|
|
78
|
+
});
|
|
79
|
+
await waitFor(() => expect(queryByTestId('toastAnimatedContainer')).toHaveStyle({
|
|
80
|
+
...defaultStyles,
|
|
81
|
+
opacity: 1
|
|
82
|
+
}));
|
|
83
|
+
const panHandler = queryByTestId('toastAnimatedContainer');
|
|
84
|
+
const gesture = {
|
|
85
|
+
...mockGestureValues,
|
|
86
|
+
moveY: 5,
|
|
87
|
+
dy: -78
|
|
88
|
+
};
|
|
89
|
+
panHandler?.props.onResponderMove(undefined, gesture);
|
|
90
|
+
panHandler?.props.onResponderRelease(undefined, gesture);
|
|
91
|
+
expect(onHide).toHaveBeenCalled();
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
jest.spyOn(Dimensions, 'get').mockImplementation(() => ({
|
|
95
|
+
height: 600,
|
|
96
|
+
width: 200,
|
|
97
|
+
scale: 1,
|
|
98
|
+
fontScale: 1
|
|
99
|
+
}));
|
|
100
|
+
describe('test dampingFor function', () => {
|
|
101
|
+
const gesture = {
|
|
102
|
+
...mockGestureValues,
|
|
103
|
+
moveY: 100
|
|
104
|
+
};
|
|
105
|
+
it('returns damping for position: bottom', () => {
|
|
106
|
+
const position = 'bottom';
|
|
107
|
+
expect(dampingFor(gesture, position)).toBe(500);
|
|
108
|
+
});
|
|
109
|
+
it('returns damping for position: top', () => {
|
|
110
|
+
const position = 'top';
|
|
111
|
+
expect(dampingFor(gesture, position)).toBe(100);
|
|
112
|
+
});
|
|
113
|
+
it('throws if position is not implemented', () => {
|
|
114
|
+
try {
|
|
115
|
+
dampingFor(gesture, 'foo');
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
expect(err).toBeDefined();
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
describe('test animatedValueFor function', () => {
|
|
123
|
+
const gesture = {
|
|
124
|
+
...mockGestureValues,
|
|
125
|
+
dy: 10
|
|
126
|
+
};
|
|
127
|
+
it('returns animated value for position: bottom', () => {
|
|
128
|
+
const position = 'bottom';
|
|
129
|
+
const damping = 100;
|
|
130
|
+
expect(animatedValueFor(gesture, position, damping)).toBe(0.9);
|
|
131
|
+
});
|
|
132
|
+
it('returns animated value for position: top', () => {
|
|
133
|
+
const position = 'top';
|
|
134
|
+
const damping = 100;
|
|
135
|
+
expect(animatedValueFor(gesture, position, damping)).toBe(1.1);
|
|
136
|
+
});
|
|
137
|
+
it('throws if position is not implemented', () => {
|
|
138
|
+
try {
|
|
139
|
+
const position = 'foo';
|
|
140
|
+
const damping = 100;
|
|
141
|
+
animatedValueFor(gesture, position, damping);
|
|
142
|
+
}
|
|
143
|
+
catch (err) {
|
|
144
|
+
expect(err).toBeDefined();
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-native';
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/* eslint-env jest */
|
|
2
|
+
import '@testing-library/jest-native';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import { Image } from 'react-native';
|
|
5
|
+
import { fireEvent, render } from '~/__helpers__/testing-library/react-native';
|
|
6
|
+
import { BaseToast } from '../BaseToast';
|
|
7
|
+
const setup = (props) => {
|
|
8
|
+
const utils = render(<BaseToast {...props}/>);
|
|
9
|
+
return {
|
|
10
|
+
...utils
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
describe('test BaseToast component', () => {
|
|
14
|
+
it('renders defaults', () => {
|
|
15
|
+
const { queryByTestId } = setup();
|
|
16
|
+
expect(queryByTestId('toastTouchableContainer')).not.toBe(null);
|
|
17
|
+
expect(queryByTestId('toastContentContainer')).not.toBe(null);
|
|
18
|
+
expect(queryByTestId('toastText1')).toBe(null);
|
|
19
|
+
expect(queryByTestId('toastText2')).toBe(null);
|
|
20
|
+
});
|
|
21
|
+
it('renders text1 and text2', () => {
|
|
22
|
+
const { queryByTestId, queryByText } = setup({
|
|
23
|
+
text1: 'Hello',
|
|
24
|
+
text2: 'World'
|
|
25
|
+
});
|
|
26
|
+
expect(queryByTestId('toastText1')).not.toBe(null);
|
|
27
|
+
expect(queryByTestId('toastText2')).not.toBe(null);
|
|
28
|
+
expect(queryByText('Hello')).not.toBe(null);
|
|
29
|
+
expect(queryByText('World')).not.toBe(null);
|
|
30
|
+
});
|
|
31
|
+
it('renders only text1', () => {
|
|
32
|
+
const { queryByTestId, queryByText } = setup({
|
|
33
|
+
text1: 'Hello'
|
|
34
|
+
});
|
|
35
|
+
expect(queryByTestId('toastText1')).not.toBe(null);
|
|
36
|
+
expect(queryByTestId('toastText2')).toBe(null);
|
|
37
|
+
expect(queryByText('Hello')).not.toBe(null);
|
|
38
|
+
});
|
|
39
|
+
it('renders only text2', () => {
|
|
40
|
+
const { queryByTestId, queryByText } = setup({
|
|
41
|
+
text2: 'World'
|
|
42
|
+
});
|
|
43
|
+
expect(queryByTestId('toastText1')).toBe(null);
|
|
44
|
+
expect(queryByTestId('toastText2')).not.toBe(null);
|
|
45
|
+
expect(queryByText('World')).not.toBe(null);
|
|
46
|
+
});
|
|
47
|
+
it('fires onPress', () => {
|
|
48
|
+
const onPress = jest.fn();
|
|
49
|
+
const { queryByTestId } = setup({
|
|
50
|
+
onPress
|
|
51
|
+
});
|
|
52
|
+
const touchableContainer = queryByTestId('toastTouchableContainer');
|
|
53
|
+
expect(touchableContainer).not.toBe(null);
|
|
54
|
+
fireEvent.press(touchableContainer);
|
|
55
|
+
expect(onPress).toHaveBeenCalled();
|
|
56
|
+
});
|
|
57
|
+
it('adds custom style to toastTouchableContainer', () => {
|
|
58
|
+
const { queryByTestId } = setup({
|
|
59
|
+
style: {
|
|
60
|
+
borderRadius: 20
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
const touchableContainer = queryByTestId('toastTouchableContainer');
|
|
64
|
+
expect(touchableContainer).not.toBe(null);
|
|
65
|
+
expect(touchableContainer).toHaveStyle({
|
|
66
|
+
borderRadius: 20
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
it('renders leading icon', () => {
|
|
70
|
+
const { queryByTestId } = setup({
|
|
71
|
+
renderLeadingIcon: () => (<Image source={{ uri: 'testUri' }} testID='leadingIcon'/>)
|
|
72
|
+
});
|
|
73
|
+
expect(queryByTestId('leadingIcon')).not.toBe(null);
|
|
74
|
+
});
|
|
75
|
+
it('renders trailing icon', () => {
|
|
76
|
+
const { queryByTestId } = setup({
|
|
77
|
+
renderTrailingIcon: () => (<Image source={{ uri: 'testUri' }} testID='trailingIcon'/>)
|
|
78
|
+
});
|
|
79
|
+
expect(queryByTestId('trailingIcon')).not.toBe(null);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* eslint-env jest */
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { render } from '~/__helpers__/testing-library/react-native';
|
|
4
|
+
import { ErrorToast } from '../ErrorToast';
|
|
5
|
+
const setup = (props) => {
|
|
6
|
+
const utils = render(<ErrorToast {...props}/>);
|
|
7
|
+
return {
|
|
8
|
+
...utils
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
describe('test ErrorToast component', () => {
|
|
12
|
+
it('renders default style', () => {
|
|
13
|
+
const { queryByTestId } = setup();
|
|
14
|
+
const touchableContainer = queryByTestId('toastTouchableContainer');
|
|
15
|
+
expect(touchableContainer).not.toBe(null);
|
|
16
|
+
expect(touchableContainer).toHaveStyle({
|
|
17
|
+
borderLeftColor: '#FE6301'
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* eslint-env jest */
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { render } from '~/__helpers__/testing-library/react-native';
|
|
4
|
+
import { InfoToast } from '../InfoToast';
|
|
5
|
+
const setup = (props) => {
|
|
6
|
+
const utils = render(<InfoToast {...props}/>);
|
|
7
|
+
return {
|
|
8
|
+
...utils
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
describe('test ErrorToast component', () => {
|
|
12
|
+
it('renders default style', () => {
|
|
13
|
+
const { queryByTestId } = setup();
|
|
14
|
+
const touchableContainer = queryByTestId('toastTouchableContainer');
|
|
15
|
+
expect(touchableContainer).not.toBe(null);
|
|
16
|
+
expect(touchableContainer).toHaveStyle({
|
|
17
|
+
borderLeftColor: '#87CEFA'
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/* eslint-env jest */
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { render } from '~/__helpers__/testing-library/react-native';
|
|
4
|
+
import { SuccessToast } from '../SuccessToast';
|
|
5
|
+
const setup = (props) => {
|
|
6
|
+
const utils = render(<SuccessToast {...props}/>);
|
|
7
|
+
return {
|
|
8
|
+
...utils
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
describe('test SuccessToast component', () => {
|
|
12
|
+
it('renders default style', () => {
|
|
13
|
+
const { queryByTestId } = setup();
|
|
14
|
+
const touchableContainer = queryByTestId('toastTouchableContainer');
|
|
15
|
+
expect(touchableContainer).not.toBe(null);
|
|
16
|
+
expect(touchableContainer).toHaveStyle({
|
|
17
|
+
borderLeftColor: '#69C779'
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ToastProps } from '~/types';
|
|
3
|
+
export declare type EnvironmentContextType = {
|
|
4
|
+
useNativeDriver?: ToastProps['useNativeDriver'];
|
|
5
|
+
};
|
|
6
|
+
declare const EnvironmentProvider: React.FC<EnvironmentContextType>;
|
|
7
|
+
declare function useEnvironmentContext(): EnvironmentContextType;
|
|
8
|
+
export { EnvironmentProvider, useEnvironmentContext };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Platform } from 'react-native';
|
|
3
|
+
const EnvironmentContext = React.createContext(null);
|
|
4
|
+
const EnvironmentProvider = ({ children, useNativeDriver = Platform.select({
|
|
5
|
+
default: true,
|
|
6
|
+
web: false
|
|
7
|
+
}) }) => {
|
|
8
|
+
const value = {
|
|
9
|
+
useNativeDriver
|
|
10
|
+
};
|
|
11
|
+
return (<EnvironmentContext.Provider value={value}>
|
|
12
|
+
{children}
|
|
13
|
+
</EnvironmentContext.Provider>);
|
|
14
|
+
};
|
|
15
|
+
function useEnvironmentContext() {
|
|
16
|
+
const ctx = React.useContext(EnvironmentContext);
|
|
17
|
+
if (!ctx) {
|
|
18
|
+
throw new Error(`useEnvironment() must be called within EnvironmentProvider context`);
|
|
19
|
+
}
|
|
20
|
+
return ctx;
|
|
21
|
+
}
|
|
22
|
+
export { EnvironmentProvider, useEnvironmentContext };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/* eslint-env jest */
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { renderHook } from '~/__helpers__/testing-library/react-hooks';
|
|
4
|
+
import { EnvironmentProvider, useEnvironmentContext } from '../EnvironmentContext';
|
|
5
|
+
describe('test useEnvironmentContext hook', () => {
|
|
6
|
+
const setup = (props) => {
|
|
7
|
+
const utils = renderHook(useEnvironmentContext, {
|
|
8
|
+
wrapper: ({ children }) => (<EnvironmentProvider {...props}>{children}</EnvironmentProvider>)
|
|
9
|
+
});
|
|
10
|
+
return {
|
|
11
|
+
...utils
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
it('returns environment dependencies / props', () => {
|
|
15
|
+
const { result } = setup({
|
|
16
|
+
useNativeDriver: true
|
|
17
|
+
});
|
|
18
|
+
expect(result.current.useNativeDriver).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
it('throws if there is no EnvironmentProvider', () => {
|
|
21
|
+
expect(() => {
|
|
22
|
+
const { result } = renderHook(useEnvironmentContext, {
|
|
23
|
+
wrapper: ({ children }) => <>{children}</>
|
|
24
|
+
});
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
26
|
+
result.current;
|
|
27
|
+
}).toThrow(new Error('useEnvironment() must be called within EnvironmentProvider context'));
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/* eslint-env jest */
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { renderHook } from '~/__helpers__/testing-library/react-hooks';
|
|
4
|
+
import { LoggerProvider, useLogger } from '../LoggerContext';
|
|
5
|
+
const setup = (props) => {
|
|
6
|
+
const wrapper = ({ children }) => (<LoggerProvider {...props}>{children}</LoggerProvider>);
|
|
7
|
+
const utils = renderHook(useLogger, {
|
|
8
|
+
wrapper
|
|
9
|
+
});
|
|
10
|
+
return {
|
|
11
|
+
...utils
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
describe('test Logger context', () => {
|
|
15
|
+
const spy = jest.spyOn(console, 'log');
|
|
16
|
+
it('injects the log function', () => {
|
|
17
|
+
const { result } = setup();
|
|
18
|
+
expect(result.current.log).toBeDefined();
|
|
19
|
+
});
|
|
20
|
+
it('it calls console.log when enableLogs: true', () => {
|
|
21
|
+
const { result } = setup({
|
|
22
|
+
enableLogs: true
|
|
23
|
+
});
|
|
24
|
+
const args = ['answer', 'is', 42];
|
|
25
|
+
result.current.log(...args);
|
|
26
|
+
expect(spy).toHaveBeenCalledWith('Toast:', ...args);
|
|
27
|
+
});
|
|
28
|
+
it('it does not call console.log when enableLogs: false', () => {
|
|
29
|
+
const { result } = setup({
|
|
30
|
+
enableLogs: false
|
|
31
|
+
});
|
|
32
|
+
result.current.log('test');
|
|
33
|
+
expect(spy).not.toHaveBeenCalledWith('test');
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -1 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { LoggerProvider, useLogger } from './LoggerContext';
|
|
2
|
+
export type { LoggerContextType } from './LoggerContext';
|
|
3
|
+
export { EnvironmentProvider, useEnvironmentContext } from './EnvironmentContext';
|
|
4
|
+
export type { EnvironmentContextType } from './EnvironmentContext';
|
|
@@ -1 +1,2 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { LoggerProvider, useLogger } from './LoggerContext';
|
|
2
|
+
export { EnvironmentProvider, useEnvironmentContext } from './EnvironmentContext';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/* eslint-env jest */
|
|
2
|
+
import { DeviceEventEmitter } from 'react-native';
|
|
3
|
+
import { act, renderHook } from '~/__helpers__/testing-library/react-hooks';
|
|
4
|
+
import { isIOS } from '../../utils/platform';
|
|
5
|
+
import { useKeyboard } from '../useKeyboard';
|
|
6
|
+
jest.mock('../../utils/platform');
|
|
7
|
+
const mockIsIOS = isIOS;
|
|
8
|
+
const setup = (platform = 'ios') => {
|
|
9
|
+
mockIsIOS.mockReturnValue(platform === 'ios');
|
|
10
|
+
const utils = renderHook(useKeyboard);
|
|
11
|
+
return {
|
|
12
|
+
...utils
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
describe('test useKeyboard hook', () => {
|
|
16
|
+
it('returns defaults', () => {
|
|
17
|
+
const { result } = setup();
|
|
18
|
+
expect(result.current.isKeyboardVisible).toBe(false);
|
|
19
|
+
expect(result.current.keyboardHeight).toBe(0);
|
|
20
|
+
});
|
|
21
|
+
it('updates keyboard height on show', () => {
|
|
22
|
+
const { result } = setup();
|
|
23
|
+
expect(result.current.isKeyboardVisible).toBe(false);
|
|
24
|
+
expect(result.current.keyboardHeight).toBe(0);
|
|
25
|
+
act(() => {
|
|
26
|
+
DeviceEventEmitter.emit('keyboardDidShow', {
|
|
27
|
+
endCoordinates: {
|
|
28
|
+
height: 425
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
expect(result.current.isKeyboardVisible).toBe(true);
|
|
33
|
+
expect(result.current.keyboardHeight).toBe(425);
|
|
34
|
+
});
|
|
35
|
+
it('updates keyboard height on hide', () => {
|
|
36
|
+
const { result } = setup();
|
|
37
|
+
expect(result.current.isKeyboardVisible).toBe(false);
|
|
38
|
+
expect(result.current.keyboardHeight).toBe(0);
|
|
39
|
+
act(() => {
|
|
40
|
+
DeviceEventEmitter.emit('keyboardDidShow', {
|
|
41
|
+
endCoordinates: {
|
|
42
|
+
height: 425
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
expect(result.current.isKeyboardVisible).toBe(true);
|
|
47
|
+
expect(result.current.keyboardHeight).toBe(425);
|
|
48
|
+
act(() => {
|
|
49
|
+
DeviceEventEmitter.emit('keyboardDidHide');
|
|
50
|
+
});
|
|
51
|
+
expect(result.current.isKeyboardVisible).toBe(false);
|
|
52
|
+
expect(result.current.keyboardHeight).toBe(0);
|
|
53
|
+
});
|
|
54
|
+
it('does nothing on Android', () => {
|
|
55
|
+
mockIsIOS.mockReturnValue(false);
|
|
56
|
+
const { result } = setup('android');
|
|
57
|
+
expect(result.current.isKeyboardVisible).toBe(false);
|
|
58
|
+
expect(result.current.keyboardHeight).toBe(0);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/* eslint-env jest */
|
|
2
|
+
import { Animated } from 'react-native';
|
|
3
|
+
import { renderHook } from '~/__helpers__/testing-library/react-hooks';
|
|
4
|
+
import { mockGestureValues } from '../../__helpers__/PanResponder';
|
|
5
|
+
import { usePanResponder } from '../usePanResponder';
|
|
6
|
+
import { shouldSetPanResponder } from '..';
|
|
7
|
+
const setup = ({ newAnimatedValueForGesture = 0 } = {}) => {
|
|
8
|
+
const animatedValue = {
|
|
9
|
+
current: new Animated.Value(0)
|
|
10
|
+
};
|
|
11
|
+
const computeNewAnimatedValueForGesture = jest.fn(() => newAnimatedValueForGesture);
|
|
12
|
+
const onDismiss = jest.fn();
|
|
13
|
+
const onRestore = jest.fn();
|
|
14
|
+
const utils = renderHook(() => usePanResponder({
|
|
15
|
+
animatedValue,
|
|
16
|
+
computeNewAnimatedValueForGesture,
|
|
17
|
+
onDismiss,
|
|
18
|
+
onRestore
|
|
19
|
+
}));
|
|
20
|
+
return {
|
|
21
|
+
animatedValue,
|
|
22
|
+
computeNewAnimatedValueForGesture,
|
|
23
|
+
onDismiss,
|
|
24
|
+
onRestore,
|
|
25
|
+
...utils
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
describe('test usePanResponder hook', () => {
|
|
29
|
+
it('returns defaults', () => {
|
|
30
|
+
const { result } = setup();
|
|
31
|
+
expect(result.current.panResponder.panHandlers).toBeDefined();
|
|
32
|
+
expect(result.current.onMove).toBeDefined();
|
|
33
|
+
expect(result.current.onRelease).toBeDefined();
|
|
34
|
+
});
|
|
35
|
+
it('computes new animated value on move gesture', () => {
|
|
36
|
+
const { result, computeNewAnimatedValueForGesture } = setup({
|
|
37
|
+
newAnimatedValueForGesture: 1
|
|
38
|
+
});
|
|
39
|
+
result.current.onMove({}, mockGestureValues);
|
|
40
|
+
expect(computeNewAnimatedValueForGesture).toBeCalledWith(mockGestureValues);
|
|
41
|
+
});
|
|
42
|
+
it('computes new animated value on release gesture', () => {
|
|
43
|
+
const { result, computeNewAnimatedValueForGesture } = setup({
|
|
44
|
+
newAnimatedValueForGesture: 1
|
|
45
|
+
});
|
|
46
|
+
result.current.onRelease({}, mockGestureValues);
|
|
47
|
+
expect(computeNewAnimatedValueForGesture).toBeCalledWith(mockGestureValues);
|
|
48
|
+
});
|
|
49
|
+
it('calls onDismiss when swipe gesture value is below dismiss threshold', () => {
|
|
50
|
+
const { result, computeNewAnimatedValueForGesture, onDismiss } = setup({
|
|
51
|
+
newAnimatedValueForGesture: 0.65
|
|
52
|
+
});
|
|
53
|
+
result.current.onRelease({}, mockGestureValues);
|
|
54
|
+
expect(computeNewAnimatedValueForGesture).toBeCalledWith(mockGestureValues);
|
|
55
|
+
expect(onDismiss).toHaveBeenCalled();
|
|
56
|
+
});
|
|
57
|
+
it('calls onDismiss when swipe gesture vy (velocity) is over dismiss threshold', () => {
|
|
58
|
+
const { result, computeNewAnimatedValueForGesture, onDismiss } = setup({
|
|
59
|
+
newAnimatedValueForGesture: 1
|
|
60
|
+
});
|
|
61
|
+
const gesture = {
|
|
62
|
+
...mockGestureValues,
|
|
63
|
+
vy: -0.8,
|
|
64
|
+
dy: -1
|
|
65
|
+
};
|
|
66
|
+
result.current.onRelease({}, gesture);
|
|
67
|
+
expect(computeNewAnimatedValueForGesture).toBeCalledWith(gesture);
|
|
68
|
+
expect(onDismiss).toHaveBeenCalled();
|
|
69
|
+
});
|
|
70
|
+
it('calls onRestore when swipe gesture value is not below dismiss threshold', () => {
|
|
71
|
+
const { result, computeNewAnimatedValueForGesture, onRestore } = setup({
|
|
72
|
+
newAnimatedValueForGesture: 0.66
|
|
73
|
+
});
|
|
74
|
+
result.current.onRelease({}, mockGestureValues);
|
|
75
|
+
expect(computeNewAnimatedValueForGesture).toBeCalledWith(mockGestureValues);
|
|
76
|
+
expect(onRestore).toHaveBeenCalled();
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
describe('test shouldSetPanResponder function', () => {
|
|
80
|
+
it('is set when dx > offset', () => {
|
|
81
|
+
const gesture = {
|
|
82
|
+
...mockGestureValues,
|
|
83
|
+
dx: 2.1,
|
|
84
|
+
dy: 0
|
|
85
|
+
};
|
|
86
|
+
expect(shouldSetPanResponder({}, gesture)).toBe(true);
|
|
87
|
+
});
|
|
88
|
+
it('is set when dy > offset', () => {
|
|
89
|
+
const gesture = {
|
|
90
|
+
...mockGestureValues,
|
|
91
|
+
dx: 0,
|
|
92
|
+
dy: 2.1
|
|
93
|
+
};
|
|
94
|
+
expect(shouldSetPanResponder({}, gesture)).toBe(true);
|
|
95
|
+
});
|
|
96
|
+
it('is not set when dx <= offset', () => {
|
|
97
|
+
const gesture = {
|
|
98
|
+
...mockGestureValues,
|
|
99
|
+
dx: 2,
|
|
100
|
+
dy: 0
|
|
101
|
+
};
|
|
102
|
+
expect(shouldSetPanResponder({}, gesture)).toBe(false);
|
|
103
|
+
});
|
|
104
|
+
it('is not set when dy <= offset', () => {
|
|
105
|
+
const gesture = {
|
|
106
|
+
...mockGestureValues,
|
|
107
|
+
dx: 0,
|
|
108
|
+
dy: 2
|
|
109
|
+
};
|
|
110
|
+
expect(shouldSetPanResponder({}, gesture)).toBe(false);
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|