Skip to content

Commit f9139a7

Browse files
committed
Added more tests, updated README and #8
1 parent b17628d commit f9139a7

File tree

8 files changed

+193
-73
lines changed

8 files changed

+193
-73
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
[![npm version](https://badge.fury.io/js/react-native-rooster.svg)](https://badge.fury.io/js/react-native-rooster)
55
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
66
![npm bundle size](https://img.shields.io/bundlephobia/min/react-native-rooster)
7+
![snyk](https://img.shields.io/snyk/vulnerabilities/npm/react-native-rooster)
8+
![website](https://img.shields.io/website?url=https%3A%2F%2Fmcodex.dev%2Freact-native-rooster)
79

810
# react-native-rooster
911

@@ -85,7 +87,7 @@ For example:
8587
```javascript
8688
import { setToastConfig } from 'react-native-rooster';
8789

88-
addToast({
90+
setToastConfig({
8991
bgColor: {
9092
success: 'olive',
9193
},

example/yalc.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"version": "v1",
33
"packages": {
44
"react-native-rooster": {
5-
"signature": "7e82ec7ac183ad6321c085bf0463484e",
5+
"signature": "b8dbe210d1043df049fdd413d1c5e0f2",
66
"file": true
77
}
88
}

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@
4040
"@rollup/plugin-json": "^4.1.0",
4141
"@rollup/plugin-node-resolve": "^8.4.0",
4242
"@testing-library/jest-native": "^3.2.0",
43+
"@testing-library/react-hooks": "^3.4.1",
4344
"@types/jest": "^26.0.7",
4445
"@types/react": "^16.9.43",
4546
"@types/react-native": "^0.63.2",
4647
"@types/react-test-renderer": "^16.9.2",
4748
"@types/styled-components": "^5.1.1",
48-
"@typescript-eslint/eslint-plugin": "^3.7.0",
49-
"@typescript-eslint/parser": "^3.7.0",
49+
"@typescript-eslint/eslint-plugin": "^3.7.1",
50+
"@typescript-eslint/parser": "^3.7.1",
5051
"babel-jest": "^26.1.0",
5152
"eslint": "^7.5.0",
5253
"eslint-config-airbnb": "^18.2.0",
@@ -55,7 +56,7 @@
5556
"eslint-plugin-import": "^2.22.0",
5657
"eslint-plugin-jsx-a11y": "^6.3.1",
5758
"eslint-plugin-prettier": "^3.1.4",
58-
"eslint-plugin-react": "^7.20.3",
59+
"eslint-plugin-react": "^7.20.4",
5960
"eslint-plugin-react-hooks": "^4.0.8",
6061
"jest": "^26.1.0",
6162
"lodash": "^4.17.19",
@@ -69,7 +70,7 @@
6970
"rollup-plugin-peer-deps-external": "^2.2.3",
7071
"rollup-plugin-typescript2": "^0.27.1",
7172
"styled-components": "^5.1.1",
72-
"ts-jest": "^26.1.3",
73+
"ts-jest": "^26.1.4",
7374
"typescript": "^3.9.7",
7475
"yalc": "^1.0.0-pre.40"
7576
},

src/components/ToastContainer/__tests__/ToastContainer.spec.tsx

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import React from 'react';
2-
import { render } from 'react-native-testing-library';
2+
import { render, fireEvent } from 'react-native-testing-library';
3+
import { renderHook } from '@testing-library/react-hooks';
4+
5+
import ToastProvider from '../../../providers/ToastProvider';
6+
7+
import ToastContext from '../../../contexts/ToastContext';
8+
9+
import useToast from '../../../hooks/useToast';
310

411
import ToastContainer from '..';
512

@@ -33,29 +40,6 @@ describe('ToastContainer', () => {
3340
expect(getByText('Toast success message')).toBeTruthy();
3441
});
3542

36-
// it('should be able to automatically dismiss toast with setTimeout fn', () => {
37-
// const messages = [
38-
// {
39-
// id: '1',
40-
// message: 'Testing toast timeout',
41-
// },
42-
// ];
43-
44-
// const { getByText } = render(
45-
// <ToastContainer messages={messages} toastConfig={defaultConfig} />,
46-
// {
47-
// wrapper: ToastProvider,
48-
// },
49-
// );
50-
51-
// expect(setTimeout).toHaveBeenCalledTimes(1);
52-
// expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 3000);
53-
// setTimeout(() => {
54-
// expect(getByText('Testing toast timeout')).toBeFalsy();
55-
// }, 3000);
56-
// jest.runAllTimers();
57-
// });
58-
5943
it('should be able to render a toast on screen with title', () => {
6044
const messages = [
6145
{
@@ -72,4 +56,75 @@ describe('ToastContainer', () => {
7256
expect(getByText('My Title')).toBeTruthy();
7357
expect(getByText('Toast success message')).toBeTruthy();
7458
});
59+
60+
it('should be able to automatically dismiss toast with setTimeout fn', () => {
61+
const messages = [
62+
{
63+
id: '1',
64+
message: 'Testing toast timeout',
65+
},
66+
];
67+
68+
const { result } = renderHook(() => useToast(), {
69+
wrapper: ToastProvider,
70+
});
71+
72+
const CustomProvider: React.FC = ({ children }) => (
73+
<ToastContext.Provider value={result.current}>
74+
{children}
75+
</ToastContext.Provider>
76+
);
77+
78+
const { getByText } = render(
79+
<ToastContainer messages={messages} toastConfig={defaultConfig} />,
80+
{
81+
wrapper: CustomProvider,
82+
},
83+
);
84+
85+
expect(setTimeout).toHaveBeenCalledTimes(1);
86+
setTimeout(() => {
87+
expect(getByText('Testing toast timeout')).toBeFalsy();
88+
}, 3000);
89+
90+
jest.useFakeTimers();
91+
});
92+
93+
it('should be able to render a toast on screen and close by clicking on it', async () => {
94+
const messages = [
95+
{
96+
id: '1',
97+
message: 'An error ocurred',
98+
},
99+
];
100+
101+
const { result } = renderHook(() => useToast(), {
102+
wrapper: ToastProvider,
103+
});
104+
105+
const removeToastHookSpy = jest
106+
.spyOn(result.current, 'removeToast')
107+
.mockImplementationOnce(() => jest.fn());
108+
109+
const CustomProvider: React.FC = ({ children }) => (
110+
<ToastContext.Provider value={result.current}>
111+
{children}
112+
</ToastContext.Provider>
113+
);
114+
115+
const { getByText } = render(
116+
<ToastContainer messages={messages} toastConfig={defaultConfig} />,
117+
{
118+
wrapper: CustomProvider,
119+
},
120+
);
121+
122+
const toast = getByText('An error ocurred');
123+
124+
expect(toast).toBeTruthy();
125+
126+
fireEvent.press(toast);
127+
128+
expect(removeToastHookSpy).toHaveBeenCalledTimes(1);
129+
});
75130
});

src/components/ToastContainer/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const Toast: React.FC<IToastComponent> = (props) => {
4747
bottom={handleBottomPadding}
4848
type={type}
4949
bgColor={bgColor}
50+
onPress={() => removeToast(id)}
5051
>
5152
{title && <Title>{title}</Title>}
5253
<Message>{message}</Message>

src/components/ToastContainer/styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface ContainerProps {
1111
};
1212
}
1313

14-
export const Container = styled.View<ContainerProps>`
14+
export const Container = styled.TouchableOpacity<ContainerProps>`
1515
align-self: center;
1616
position: absolute;
1717
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from 'react';
2+
import { waitFor } from 'react-native-testing-library';
3+
import { renderHook, act } from '@testing-library/react-hooks';
4+
5+
import ToastProvider from '../ToastProvider';
6+
7+
import useToast from '../../hooks/useToast';
8+
9+
describe('ToastProvider', () => {
10+
it('should be able to add a toast using addToast', async () => {
11+
const setState = jest.fn();
12+
13+
const useStateMock: any = (initState: any) => [initState, setState];
14+
15+
jest.spyOn(React, 'useState').mockImplementation(useStateMock);
16+
17+
const { result } = renderHook(() => useToast(), {
18+
wrapper: ToastProvider,
19+
});
20+
21+
await waitFor(() => {
22+
result.current.addToast({
23+
title: 'hello',
24+
message: 'New Message',
25+
});
26+
});
27+
28+
// should write the assertions
29+
});
30+
31+
it("should be able to change toast's global config", () => {
32+
const { result } = renderHook(() => useToast(), {
33+
wrapper: ToastProvider,
34+
});
35+
36+
act(() => {
37+
result.current.setToastConfig({
38+
bgColor: {
39+
success: 'olive',
40+
},
41+
});
42+
});
43+
44+
// should write the assertions
45+
});
46+
});

0 commit comments

Comments
 (0)