Skip to content

Commit

Permalink
feat: write tests
Browse files Browse the repository at this point in the history
  • Loading branch information
calintamas committed Oct 30, 2021
1 parent 0be39a8 commit 86f1ddf
Show file tree
Hide file tree
Showing 29 changed files with 989 additions and 118 deletions.
7 changes: 0 additions & 7 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,3 @@ updates:
open-pull-requests-limit: 10
reviewers:
- calintamas
ignore:
- dependency-name: husky
versions:
- '> 3.1.0'
- dependency-name: husky
versions:
- '>= 4.a, < 5'
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ jobs:

- name: Setup React Native environment
run: |
yarn add react-native@0.63.4
yarn add react@16.13.1
yarn add react@17.0.1
yarn add react-native@0.64.2
- name: Run tests
run: yarn test --coverage
Expand Down
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__tests__
**/__tests__/*
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Calin Tamas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
11 changes: 11 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
preset: 'react-native',
testEnvironment: 'node',
collectCoverage: true,
collectCoverageFrom: ['src/**/*.{ts,tsx}'],
setupFilesAfterEnv: [
'@testing-library/jest-native/extend-expect',
'./jest.setup.js'
],
testPathIgnorePatterns: ['/__helpers__/']
};
3 changes: 3 additions & 0 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint-env jest */

jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@
"type": "git",
"url": "git+https://github.com/calintamas/react-native-toast-message.git"
},
"keywords": [
"react-native",
"toast"
],
"scripts": {
"prepare": "husky install",
"build": "rm -rf ./lib && tsc",
"prettier": "./node_modules/.bin/prettier --write",
"lint": "./node_modules/.bin/eslint --fix",
"lint-staged": "./node_modules/.bin/lint-staged"
"lint-staged": "./node_modules/.bin/lint-staged",
"test": "./node_modules/.bin/jest"
},
"author": "Calin Tamas <calintamas2@gmail.com>",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.15.8",
"@testing-library/jest-native": "^4.0.2",
"@testing-library/react-native": "^7.2.0",
"@testing-library/react-hooks": "^7.0.2",
"@testing-library/react-native": "^8.0.0",
"@types/jest": "^27.0.1",
"eslint-config-backpacker-react-ts": "^0.1.0",
"husky": "^7.0.2",
Expand Down
52 changes: 4 additions & 48 deletions src/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,9 @@ import { ToastHideParams, ToastProps, ToastShowParams } from './types';
import { useToast } from './useToast';

const ToastRoot = React.forwardRef((props: ToastProps, ref) => {
const {
config,
type,
position,
visibilityTime,
topOffset,
bottomOffset,
keyboardOffset,
onShow,
onHide,
onPress
} = props;
const { config, ...defaultOptions } = props;
const { show, hide, isVisible, options, data } = useToast({
defaultOptions: {
type,
position,
visibilityTime,
topOffset,
bottomOffset,
keyboardOffset,
onShow,
onHide,
onPress
}
defaultOptions
});

React.useImperativeHandle(ref, () => ({
Expand All @@ -56,33 +35,10 @@ type ToastRef = {

const toastRef = React.createRef<ToastRef>();

export function Toast({
config,
type,
position,
visibilityTime,
topOffset,
bottomOffset,
keyboardOffset,
onShow,
onHide,
onPress
}: ToastProps) {
export function Toast(props: ToastProps) {
return (
<LoggerProvider enableLogs>
<ToastRoot
ref={toastRef}
config={config}
type={type}
position={position}
visibilityTime={visibilityTime}
topOffset={topOffset}
bottomOffset={bottomOffset}
keyboardOffset={keyboardOffset}
onShow={onShow}
onHide={onHide}
onPress={onPress}
/>
<ToastRoot ref={toastRef} {...props} />
</LoggerProvider>
);
}
Expand Down
40 changes: 40 additions & 0 deletions src/__helpers__/PanResponder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
GestureResponderHandlers,
PanResponder,
PanResponderCallbacks,
PanResponderGestureState
} from 'react-native';

export const mockGestureValues: PanResponderGestureState = {
moveY: 0,
moveX: 0,
y0: 0,
x0: 0,
dx: 0,
dy: 10,
stateID: 123,
vx: 0,
vy: 0,
numberActiveTouches: 1,
_accountsForMovesUpTo: 1
};

export function mockPanResponder() {
jest
.spyOn(PanResponder, 'create')
.mockImplementation(
({
onMoveShouldSetPanResponder,
onMoveShouldSetPanResponderCapture,
onPanResponderMove,
onPanResponderRelease
}: PanResponderCallbacks) => ({
panHandlers: {
onMoveShouldSetResponder: onMoveShouldSetPanResponder,
onMoveShouldSetResponderCapture: onMoveShouldSetPanResponderCapture,
onResponderMove: onPanResponderMove,
onResponderRelease: onPanResponderRelease
} as GestureResponderHandlers
})
);
}
16 changes: 11 additions & 5 deletions src/components/AnimatedContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ import {
useViewDimensions
} from '../hooks';
import { ReactChildren, ToastPosition } from '../types';
import { noop } from '../utils/func';
import { bound } from '../utils/number';
import { getTestId } from '../utils/test-id';
import { styles } from './AnimatedContainer.styles';

type AnimatedContainerProps = {
export type AnimatedContainerProps = {
children: ReactChildren;
isVisible: boolean;
position: ToastPosition;
topOffset: number;
bottomOffset: number;
keyboardOffset: number;
onHide: () => void;
onRestorePosition?: () => void;
};

function dampingFor(
export function dampingFor(
gesture: PanResponderGestureState,
position: ToastPosition
) {
Expand All @@ -39,7 +42,7 @@ function dampingFor(
}
}

function animatedValueFor(
export function animatedValueFor(
gesture: PanResponderGestureState,
position: ToastPosition,
damping: number
Expand All @@ -64,7 +67,8 @@ export function AnimatedContainer({
topOffset,
bottomOffset,
keyboardOffset,
onHide
onHide,
onRestorePosition = noop
}: AnimatedContainerProps) {
const { log } = useLogger();

Expand All @@ -87,7 +91,8 @@ export function AnimatedContainer({
const onRestore = React.useCallback(() => {
log('Swipe, restoring to original position');
animate(1);
}, [animate, log]);
onRestorePosition();
}, [animate, log, onRestorePosition]);

const computeNewAnimatedValueForGesture = React.useCallback(
(gesture: PanResponderGestureState) => {
Expand All @@ -111,6 +116,7 @@ export function AnimatedContainer({

return (
<Animated.View
testID={getTestId('AnimatedContainer')}
onLayout={computeViewDimensions}
style={[styles.base, styles[position], animationStyles]}
{...panResponder.panHandlers}>
Expand Down
5 changes: 1 addition & 4 deletions src/components/BaseToast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import React from 'react';
import { Text, View } from 'react-native';

import { BaseToastProps } from '../types';
import { getTestId } from '../utils/test-id';
import { styles } from './BaseToast.styles';
import { Touchable } from './Touchable';

function getTestId(elementName: string) {
return `toast${elementName}`;
}

export function BaseToast({
text1,
text2,
Expand Down
Loading

0 comments on commit 86f1ddf

Please sign in to comment.