1
1
import React from 'react' ;
2
- import { render , cleanup } from '@testing-library/react'
2
+ import { render , cleanup } from '@testing-library/react' ;
3
3
import InfiniteScroll from '../index' ;
4
4
5
5
describe ( 'React Infinite Scroll Component' , ( ) => {
6
-
7
- afterEach ( cleanup )
6
+ afterEach ( cleanup ) ;
8
7
9
8
it ( 'renders .infinite-scroll-component' , ( ) => {
10
9
const { container } = render (
11
- < InfiniteScroll
12
- dataLength = { 4 }
13
- loader = { " Loading..." }
10
+ < InfiniteScroll
11
+ dataLength = { 4 }
12
+ loader = { ' Loading...' }
14
13
hasMore = { false }
15
14
next = { ( ) => { } }
16
15
>
17
16
< div />
18
17
</ InfiniteScroll >
19
18
) ;
20
- expect ( container . querySelectorAll ( '.infinite-scroll-component' ) . length ) . toBe ( 1 )
19
+ expect (
20
+ container . querySelectorAll ( '.infinite-scroll-component' ) . length
21
+ ) . toBe ( 1 ) ;
21
22
} ) ;
22
23
23
24
it ( 'renders custom class' , ( ) => {
24
25
const { container } = render (
25
- < InfiniteScroll
26
- dataLength = { 4 }
27
- loader = { " Loading..." }
26
+ < InfiniteScroll
27
+ dataLength = { 4 }
28
+ loader = { ' Loading...' }
28
29
className = "custom-class"
29
30
hasMore = { false }
30
31
next = { ( ) => { } }
@@ -38,26 +39,26 @@ describe('React Infinite Scroll Component', () => {
38
39
it ( 'renders children when passed in' , ( ) => {
39
40
const { container } = render (
40
41
< InfiniteScroll
41
- dataLength = { 4 }
42
- loader = { " Loading..." }
42
+ dataLength = { 4 }
43
+ loader = { ' Loading...' }
43
44
hasMore = { false }
44
45
next = { ( ) => { } }
45
46
>
46
47
< div className = "child" />
47
48
</ InfiniteScroll >
48
49
) ;
49
- expect ( container . querySelectorAll ( '.child' ) . length ) . toBe ( 1 )
50
+ expect ( container . querySelectorAll ( '.child' ) . length ) . toBe ( 1 ) ;
50
51
} ) ;
51
52
52
53
it ( 'calls scroll handler if provided, when user scrolls' , ( ) => {
53
54
jest . useFakeTimers ( ) ;
54
55
const onScrollMock = jest . fn ( ) ;
55
56
56
57
const { container } = render (
57
- < InfiniteScroll
58
- onScroll = { onScrollMock }
59
- dataLength = { 4 }
60
- loader = { " Loading..." }
58
+ < InfiniteScroll
59
+ onScroll = { onScrollMock }
60
+ dataLength = { 4 }
61
+ loader = { ' Loading...' }
61
62
height = { 100 }
62
63
hasMore = { false }
63
64
next = { ( ) => { } }
@@ -67,7 +68,9 @@ describe('React Infinite Scroll Component', () => {
67
68
) ;
68
69
69
70
const scrollEvent = new Event ( 'scroll' ) ;
70
- const node = container . querySelector ( '.infinite-scroll-component' ) as HTMLElement ;
71
+ const node = container . querySelector (
72
+ '.infinite-scroll-component'
73
+ ) as HTMLElement ;
71
74
72
75
node . dispatchEvent ( scrollEvent ) ;
73
76
jest . runOnlyPendingTimers ( ) ;
@@ -76,12 +79,11 @@ describe('React Infinite Scroll Component', () => {
76
79
} ) ;
77
80
78
81
describe ( 'When user scrolls to the bottom' , ( ) => {
79
-
80
82
it ( 'does not show loader if hasMore is false' , ( ) => {
81
83
const { container, queryByText } = render (
82
- < InfiniteScroll
83
- dataLength = { 4 }
84
- loader = { " Loading..." }
84
+ < InfiniteScroll
85
+ dataLength = { 4 }
86
+ loader = { ' Loading...' }
85
87
hasMore = { false }
86
88
scrollThreshold = { 0 }
87
89
next = { ( ) => { } }
@@ -91,16 +93,18 @@ describe('React Infinite Scroll Component', () => {
91
93
) ;
92
94
93
95
const scrollEvent = new Event ( 'scroll' ) ;
94
- const node = container . querySelector ( '.infinite-scroll-component' ) as HTMLElement ;
96
+ const node = container . querySelector (
97
+ '.infinite-scroll-component'
98
+ ) as HTMLElement ;
95
99
node . dispatchEvent ( scrollEvent ) ;
96
100
expect ( queryByText ( 'Loading...' ) ) . toBeFalsy ( ) ;
97
101
} ) ;
98
102
99
103
it ( 'shows loader if hasMore is true' , ( ) => {
100
104
const { container, getByText } = render (
101
- < InfiniteScroll
102
- dataLength = { 4 }
103
- loader = { " Loading..." }
105
+ < InfiniteScroll
106
+ dataLength = { 4 }
107
+ loader = { ' Loading...' }
104
108
hasMore = { true }
105
109
scrollThreshold = { 0 }
106
110
next = { ( ) => { } }
@@ -111,19 +115,20 @@ describe('React Infinite Scroll Component', () => {
111
115
) ;
112
116
113
117
const scrollEvent = new Event ( 'scroll' ) ;
114
- const node = container . querySelector ( '.infinite-scroll-component' ) as HTMLElement ;
118
+ const node = container . querySelector (
119
+ '.infinite-scroll-component'
120
+ ) as HTMLElement ;
115
121
node . dispatchEvent ( scrollEvent ) ;
116
122
expect ( getByText ( 'Loading...' ) ) . toBeTruthy ( ) ;
117
123
} ) ;
118
-
119
124
} ) ;
120
125
121
126
it ( 'shows end message' , ( ) => {
122
127
const { queryByText } = render (
123
128
< InfiniteScroll
124
- dataLength = { 4 }
125
- loader = { " Loading..." }
126
- endMessage = { " Reached end." }
129
+ dataLength = { 4 }
130
+ loader = { ' Loading...' }
131
+ endMessage = { ' Reached end.' }
127
132
hasMore = { false }
128
133
next = { ( ) => { } }
129
134
>
@@ -132,4 +137,4 @@ describe('React Infinite Scroll Component', () => {
132
137
) ;
133
138
expect ( queryByText ( 'Reached end.' ) ) . toBeTruthy ( ) ;
134
139
} ) ;
135
- } ) ;
140
+ } ) ;
0 commit comments