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 586c254
Show file tree
Hide file tree
Showing 34 changed files with 1,321 additions and 192 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
54 changes: 5 additions & 49 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}
/>
<LoggerProvider enableLogs={false}>
<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
})
);
}
75 changes: 75 additions & 0 deletions src/__tests__/ToastUI.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/* eslint-env jest */

import { render } from '@testing-library/react-native';
import React from 'react';

import { ToastUI, ToastUIProps } from '../ToastUI';
import { DEFAULT_OPTIONS } from '../useToast';

const setup = (props?: Partial<ToastUIProps>) => {
const show = jest.fn();
const hide = jest.fn();

const utils = render(
<ToastUI
isVisible
options={DEFAULT_OPTIONS}
data={{
text1: 'text1',
text2: 'text2'
}}
show={show}
hide={hide}
{...props}
/>
);
return {
...utils
};
};

describe('test ToastUI component', () => {
it('renders defaults', () => {
const { queryByText } = setup();
expect(queryByText('text1')).toBeDefined();
expect(queryByText('text2')).toBeDefined();
});

it('renders error type Toast', () => {
const { queryByText } = setup({
options: {
...DEFAULT_OPTIONS,
type: 'error'
}
});
expect(queryByText('text1')).toBeDefined();
expect(queryByText('text2')).toBeDefined();
});

it('renders info type Toast', () => {
const { queryByText } = setup({
options: {
...DEFAULT_OPTIONS,
type: 'info'
}
});
expect(queryByText('text1')).toBeDefined();
expect(queryByText('text2')).toBeDefined();
});

it('throws when trying to render an undefined Toast type', () => {
const type = 'mock';
expect(() =>
setup({
options: {
...DEFAULT_OPTIONS,
type
}
})
).toThrow(
new Error(
`Toast type: '${type}' does not exist. You can add it via the 'config' prop on the Toast instance. Learn more: https://github.com/calintamas/react-native-toast-message/blob/master/README.md`
)
);
});
});
Loading

0 comments on commit 586c254

Please sign in to comment.