react-native-toast-message 2.2.0-beta.1 → 2.2.0

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.
Files changed (61) hide show
  1. package/README.md +3 -3
  2. package/lib/src/Toast.d.ts +1 -1
  3. package/lib/src/Toast.js +3 -5
  4. package/lib/src/ToastUI.js +6 -4
  5. package/lib/src/components/AnimatedContainer.d.ts +2 -1
  6. package/lib/src/components/AnimatedContainer.js +6 -5
  7. package/lib/src/contexts/index.d.ts +1 -4
  8. package/lib/src/contexts/index.js +1 -2
  9. package/lib/src/hooks/usePanResponder.d.ts +2 -1
  10. package/lib/src/hooks/usePanResponder.js +9 -3
  11. package/lib/src/hooks/useSlideAnimation.d.ts +2 -6
  12. package/lib/src/hooks/useSlideAnimation.js +10 -35
  13. package/lib/src/types/index.d.ts +18 -12
  14. package/lib/src/useToast.js +7 -1
  15. package/lib/src/utils/array.d.ts +2 -0
  16. package/lib/src/utils/array.js +4 -0
  17. package/package.json +9 -19
  18. package/lib/src/__helpers__/PanResponder.d.ts +0 -3
  19. package/lib/src/__helpers__/PanResponder.js +0 -26
  20. package/lib/src/__helpers__/testing-library/react-hooks.d.ts +0 -9
  21. package/lib/src/__helpers__/testing-library/react-hooks.js +0 -13
  22. package/lib/src/__helpers__/testing-library/react-native.d.ts +0 -9
  23. package/lib/src/__helpers__/testing-library/react-native.js +0 -13
  24. package/lib/src/__mocks__/EnvironmentProviderMock.d.ts +0 -7
  25. package/lib/src/__mocks__/EnvironmentProviderMock.js +0 -9
  26. package/lib/src/__tests__/Toast.test.d.ts +0 -1
  27. package/lib/src/__tests__/Toast.test.js +0 -190
  28. package/lib/src/__tests__/ToastUI.test.d.ts +0 -1
  29. package/lib/src/__tests__/ToastUI.test.js +0 -52
  30. package/lib/src/__tests__/useToast.test.d.ts +0 -1
  31. package/lib/src/__tests__/useToast.test.js +0 -142
  32. package/lib/src/components/__tests__/AnimatedContainer.test.d.ts +0 -1
  33. package/lib/src/components/__tests__/AnimatedContainer.test.js +0 -147
  34. package/lib/src/components/__tests__/BaseToast.test.d.ts +0 -1
  35. package/lib/src/components/__tests__/BaseToast.test.js +0 -81
  36. package/lib/src/components/__tests__/ErrorToast.test.d.ts +0 -1
  37. package/lib/src/components/__tests__/ErrorToast.test.js +0 -20
  38. package/lib/src/components/__tests__/InfoToast.test.d.ts +0 -1
  39. package/lib/src/components/__tests__/InfoToast.test.js +0 -20
  40. package/lib/src/components/__tests__/SuccessToast.test.d.ts +0 -1
  41. package/lib/src/components/__tests__/SuccessToast.test.js +0 -20
  42. package/lib/src/contexts/EnvironmentContext.d.ts +0 -8
  43. package/lib/src/contexts/EnvironmentContext.js +0 -22
  44. package/lib/src/contexts/__tests__/EnvironmentContext.test.d.ts +0 -1
  45. package/lib/src/contexts/__tests__/EnvironmentContext.test.js +0 -29
  46. package/lib/src/contexts/__tests__/LoggerContext.test.d.ts +0 -1
  47. package/lib/src/contexts/__tests__/LoggerContext.test.js +0 -35
  48. package/lib/src/hooks/__tests__/useKeyboard.test.d.ts +0 -1
  49. package/lib/src/hooks/__tests__/useKeyboard.test.js +0 -60
  50. package/lib/src/hooks/__tests__/usePanResponder.test.d.ts +0 -1
  51. package/lib/src/hooks/__tests__/usePanResponder.test.js +0 -112
  52. package/lib/src/hooks/__tests__/useSlideAnimation.test.d.ts +0 -1
  53. package/lib/src/hooks/__tests__/useSlideAnimation.test.js +0 -88
  54. package/lib/src/hooks/__tests__/useTimeout.test.d.ts +0 -1
  55. package/lib/src/hooks/__tests__/useTimeout.test.js +0 -54
  56. package/lib/src/hooks/__tests__/useViewDimensions.test.d.ts +0 -1
  57. package/lib/src/hooks/__tests__/useViewDimensions.test.js +0 -51
  58. package/lib/src/utils/__tests__/number.test.d.ts +0 -1
  59. package/lib/src/utils/__tests__/number.test.js +0 -15
  60. package/lib/src/utils/__tests__/obj.test.d.ts +0 -1
  61. package/lib/src/utils/__tests__/obj.test.js +0 -21
@@ -1,20 +0,0 @@
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
- });
@@ -1,8 +0,0 @@
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 };
@@ -1,22 +0,0 @@
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 };
@@ -1,29 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,35 +0,0 @@
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 +0,0 @@
1
- export {};
@@ -1,60 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,112 +0,0 @@
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
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,88 +0,0 @@
1
- /* eslint-env jest */
2
- import { Animated } from 'react-native';
3
- import { renderHook } from '~/__helpers__/testing-library/react-hooks';
4
- import { translateYOutputRangeFor, useSlideAnimation } from '../useSlideAnimation';
5
- const defaultOffsets = {
6
- topOffset: 40,
7
- bottomOffset: 60,
8
- keyboardOffset: 5
9
- };
10
- const setup = (props) => {
11
- const utils = renderHook(() => useSlideAnimation({
12
- position: 'top',
13
- height: 20,
14
- ...defaultOffsets,
15
- ...props
16
- }));
17
- return {
18
- ...utils
19
- };
20
- };
21
- describe('test useSlideAnimation hook', () => {
22
- it('returns defaults', () => {
23
- const { result } = setup();
24
- const { animatedValue, animate, animationStyles } = result.current;
25
- expect(animatedValue.current).toBeDefined();
26
- expect(animate).toBeDefined();
27
- expect(animationStyles.opacity).toBeDefined();
28
- expect(animationStyles.transform).toBeDefined();
29
- });
30
- it('animates to a new value', async () => {
31
- const spy = jest.spyOn(Animated, 'spring').mockImplementation(() => ({
32
- start: jest.fn(),
33
- stop: jest.fn(),
34
- reset: jest.fn()
35
- }));
36
- const { result } = setup();
37
- result.current.animate(1);
38
- expect(spy).toHaveBeenCalled();
39
- });
40
- it.each([
41
- ['top', -40],
42
- ['bottom', 40]
43
- ])('returns default styles for position: %s', (position, translateY) => {
44
- const { result } = setup({
45
- position
46
- });
47
- expect(result.current.defaultStyles).toEqual({
48
- transform: [
49
- {
50
- translateY
51
- }
52
- ]
53
- });
54
- });
55
- });
56
- describe('test translateYOutputRangeFor function', () => {
57
- it('returns output range for position: top', () => {
58
- expect(translateYOutputRangeFor({
59
- position: 'top',
60
- height: 20,
61
- keyboardHeight: 0,
62
- ...defaultOffsets
63
- })).toEqual([-40, 40]);
64
- });
65
- it('returns output range for position: bottom', () => {
66
- expect(translateYOutputRangeFor({
67
- position: 'bottom',
68
- height: 20,
69
- keyboardHeight: 0,
70
- ...defaultOffsets
71
- })).toEqual([40, -60]);
72
- });
73
- it('returns output range for position: bottom, with keyboard offset', () => {
74
- expect(translateYOutputRangeFor({
75
- position: 'bottom',
76
- height: 20,
77
- keyboardHeight: 400,
78
- ...defaultOffsets
79
- })).toEqual([40, -405]);
80
- });
81
- it('throws if position is not supported', () => {
82
- expect(() => translateYOutputRangeFor({
83
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
84
- // @ts-ignore
85
- position: 'left'
86
- })).toThrow(new Error(`Position 'left' not supported`));
87
- });
88
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,54 +0,0 @@
1
- /* eslint-env jest */
2
- import { act, renderHook } from '~/__helpers__/testing-library/react-hooks';
3
- import { useTimeout } from '../useTimeout';
4
- const setup = () => {
5
- const cb = jest.fn();
6
- const utils = renderHook(() => useTimeout(cb));
7
- return {
8
- ...utils,
9
- cb
10
- };
11
- };
12
- describe('test useTimeout hook', () => {
13
- beforeAll(() => {
14
- jest.useFakeTimers();
15
- });
16
- it('sets timeout', () => {
17
- const { cb, result } = setup();
18
- act(() => {
19
- result.current.startTimer();
20
- });
21
- expect(cb).not.toHaveBeenCalled();
22
- act(() => {
23
- jest.runAllTimers();
24
- });
25
- expect(cb).toHaveBeenCalled();
26
- });
27
- it('clears timeout before running', () => {
28
- const { cb, result } = setup();
29
- act(() => {
30
- result.current.startTimer();
31
- });
32
- expect(cb).not.toHaveBeenCalled();
33
- act(() => {
34
- result.current.clearTimer();
35
- });
36
- expect(cb).not.toHaveBeenCalled();
37
- act(() => {
38
- jest.runAllTimers();
39
- });
40
- expect(cb).not.toHaveBeenCalled();
41
- });
42
- it('clears timeout when unmounting', () => {
43
- const { cb, result, unmount } = setup();
44
- act(() => {
45
- result.current.startTimer();
46
- });
47
- expect(cb).not.toHaveBeenCalled();
48
- act(() => {
49
- unmount();
50
- jest.runAllTimers();
51
- });
52
- expect(cb).not.toHaveBeenCalled();
53
- });
54
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,51 +0,0 @@
1
- /* eslint-env jest */
2
- import { act } from 'react-test-renderer';
3
- import { renderHook } from '~/__helpers__/testing-library/react-hooks';
4
- import { useViewDimensions } from '../useViewDimensions';
5
- const setup = (offsets) => {
6
- const layoutChangeEventMock = {
7
- nativeEvent: {
8
- layout: {
9
- height: 250,
10
- width: 320
11
- }
12
- }
13
- };
14
- const utils = renderHook(() => useViewDimensions(offsets));
15
- return {
16
- ...utils,
17
- layoutChangeEventMock
18
- };
19
- };
20
- describe('test useViewDimensions hook', () => {
21
- it('computes dimensions correctly', () => {
22
- const { result, layoutChangeEventMock } = setup();
23
- const { computeViewDimensions } = result.current;
24
- act(() => {
25
- computeViewDimensions(layoutChangeEventMock);
26
- });
27
- expect(result.current.height).toBe(250);
28
- expect(result.current.width).toBe(320);
29
- });
30
- it('computes dimensions with offsets correctly', () => {
31
- const offsets = {
32
- heightOffset: -40,
33
- widthOffset: 120
34
- };
35
- const { result, layoutChangeEventMock } = setup(offsets);
36
- const { computeViewDimensions } = result.current;
37
- act(() => {
38
- computeViewDimensions(layoutChangeEventMock);
39
- });
40
- expect(result.current.height).toBe(250 + offsets.heightOffset);
41
- expect(result.current.width).toBe(320 + offsets.widthOffset);
42
- });
43
- it('falls back to 0 when View dimensions are missing', () => {
44
- const { result } = setup();
45
- act(() => {
46
- result.current.computeViewDimensions({});
47
- });
48
- expect(result.current.height).toBe(0);
49
- expect(result.current.width).toBe(0);
50
- });
51
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,15 +0,0 @@
1
- /* eslint-env jest */
2
- import { bound, lowerBound, upperBound } from '../number';
3
- describe('test bound functions', () => {
4
- it('ensures lower bound', () => {
5
- expect(lowerBound(10, 2)).toBe(10);
6
- expect(lowerBound(0, 2)).toBe(2);
7
- });
8
- it('ensures upper bound', () => {
9
- expect(upperBound(10, 2)).toBe(2);
10
- expect(upperBound(0, 2)).toBe(0);
11
- });
12
- it('ensures both bounds', () => {
13
- expect(bound(10, 2, 5)).toBe(5);
14
- });
15
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,21 +0,0 @@
1
- /* eslint-env jest */
2
- import { mergeIfDefined } from '../obj';
3
- describe('test mergeIfDefined function', () => {
4
- it('merges defined values from obj2', () => {
5
- const obj1 = {
6
- foo: 1,
7
- bar: 2,
8
- baz: 3
9
- };
10
- const obj2 = {
11
- foo: null,
12
- bar: 20,
13
- baz: undefined
14
- };
15
- expect(mergeIfDefined(obj1, obj2)).toEqual({
16
- foo: 1,
17
- bar: 20,
18
- baz: 3
19
- });
20
- });
21
- });