1
- import " raf/polyfill" ;
2
- import React from " react" ;
3
- import { configure , mount } from " enzyme" ;
4
- import Adapter from " enzyme-adapter-react-16" ;
5
- import ProgressiveImage from " ../src" ;
1
+ import ' raf/polyfill' ;
2
+ import React from ' react' ;
3
+ import { configure , mount } from ' enzyme' ;
4
+ import Adapter from ' enzyme-adapter-react-16' ;
5
+ import ProgressiveImage from ' ../src' ;
6
6
7
7
configure ( { adapter : new Adapter ( ) } ) ;
8
8
9
- const src = " https://image.xyz/source" ;
10
- const placeholder = " https://image.xyz/placeholder" ;
9
+ const src = ' https://image.xyz/source' ;
10
+ const placeholder = ' https://image.xyz/placeholder' ;
11
11
const srcSetData = {
12
- srcSet : " srcSet" ,
13
- sizes : " sizes"
12
+ srcSet : ' srcSet' ,
13
+ sizes : ' sizes'
14
14
} ;
15
15
16
- const mountProgressiveImage = renderFn => {
16
+ const mountProgressiveImage = ( renderFn , delay ) => {
17
17
const defaultRender = image => {
18
18
return < img src = { image } /> ;
19
19
} ;
20
20
const render = renderFn || defaultRender ;
21
21
return mount (
22
22
< ProgressiveImage
23
+ delay = { delay }
23
24
src = { src }
24
25
placeholder = { placeholder }
25
26
srcSetData = { srcSetData }
@@ -29,14 +30,14 @@ const mountProgressiveImage = renderFn => {
29
30
) ;
30
31
} ;
31
32
32
- describe ( " react-progressive-image" , ( ) => {
33
+ describe ( ' react-progressive-image' , ( ) => {
33
34
beforeEach ( ( ) => {
34
35
global . Image = Image ;
35
36
} ) ;
36
- it ( " exports a React component" , ( ) => {
37
- expect ( typeof ProgressiveImage ) . toBe ( " function" ) ;
37
+ it ( ' exports a React component' , ( ) => {
38
+ expect ( typeof ProgressiveImage ) . toBe ( ' function' ) ;
38
39
} ) ;
39
- it ( " throws if not provided a function as a child" , ( ) => {
40
+ it ( ' throws if not provided a function as a child' , ( ) => {
40
41
/* eslint-disable no-console */
41
42
const _error = console . error ;
42
43
console . error = jest . fn ( ( ) => { } ) ;
@@ -53,54 +54,72 @@ describe("react-progressive-image", () => {
53
54
}
54
55
/* eslint-enable no-console */
55
56
} ) ;
56
- it ( " creates an instance of Image when mounted" , ( ) => {
57
+ it ( ' creates an instance of Image when mounted' , ( ) => {
57
58
const wrapper = mountProgressiveImage ( ) ;
58
59
const instance = wrapper . instance ( ) ;
59
60
expect ( instance . image . constructor ) . toBe ( HTMLImageElement ) ;
60
61
} ) ;
61
- it ( " sets the onload property on the Image instance" , ( ) => {
62
+ it ( ' sets the onload property on the Image instance' , ( ) => {
62
63
const wrapper = mountProgressiveImage ( ) ;
63
64
const instance = wrapper . instance ( ) ;
64
65
expect ( instance . image . onload ) . toEqual ( instance . onLoad ) ;
65
66
} ) ;
66
- it ( " sets the onerror property on the Image instance" , ( ) => {
67
+ it ( ' sets the onerror property on the Image instance' , ( ) => {
67
68
const wrapper = mountProgressiveImage ( ) ;
68
69
const instance = wrapper . instance ( ) ;
69
70
expect ( instance . image . onerror ) . toEqual ( instance . onError ) ;
70
71
} ) ;
71
- it ( " sets the src property on the Image instance" , ( ) => {
72
+ it ( ' sets the src property on the Image instance' , ( ) => {
72
73
const wrapper = mountProgressiveImage ( ) ;
73
74
const instance = wrapper . instance ( ) ;
74
75
expect ( instance . image . src ) . toEqual ( src ) ;
75
76
} ) ;
76
- it ( " sets the srcSet property on the Image instance" , ( ) => {
77
+ it ( ' sets the srcSet property on the Image instance' , ( ) => {
77
78
const wrapper = mountProgressiveImage ( ) ;
78
79
const instance = wrapper . instance ( ) ;
79
80
expect ( instance . image . srcset ) . toEqual ( srcSetData . srcSet ) ;
80
81
} ) ;
81
- it ( " sets the sizes property on the Image instance" , ( ) => {
82
+ it ( ' sets the sizes property on the Image instance' , ( ) => {
82
83
const wrapper = mountProgressiveImage ( ) ;
83
84
const instance = wrapper . instance ( ) ;
84
85
expect ( instance . image . sizes ) . toEqual ( srcSetData . sizes ) ;
85
86
} ) ;
86
- it ( " renders placeholder image on initial render" , ( ) => {
87
+ it ( ' renders placeholder image on initial render' , ( ) => {
87
88
const render = jest . fn ( src => < img src = { src } alt = "an image" /> ) ;
88
89
const wrapper = mountProgressiveImage ( render ) ;
89
90
expect ( render . mock . calls [ 0 ] [ 0 ] ) . toEqual ( placeholder ) ;
90
91
} ) ;
91
- it ( " renders src image on second render" , ( ) => {
92
+ it ( ' renders src image on second render' , ( ) => {
92
93
const render = jest . fn ( src => < img src = { src } alt = "an image" /> ) ;
93
94
const wrapper = mountProgressiveImage ( render ) ;
94
95
wrapper . instance ( ) . loadImage ( src ) ;
95
96
wrapper . instance ( ) . onLoad ( ) ;
96
97
expect ( render . mock . calls [ 1 ] [ 0 ] ) . toEqual ( src ) ;
97
98
} ) ;
98
- it ( " sets loading to false after src image is loaded" , ( ) => {
99
+ it ( ' sets loading to false after src image is loaded' , ( ) => {
99
100
const render = jest . fn ( src => < img src = { src } alt = "an image" /> ) ;
100
101
const wrapper = mountProgressiveImage ( render ) ;
101
102
expect ( render . mock . calls [ 0 ] [ 1 ] ) . toEqual ( true ) ;
102
103
wrapper . instance ( ) . loadImage ( src ) ;
103
104
wrapper . instance ( ) . onLoad ( ) ;
104
105
expect ( render . mock . calls [ 1 ] [ 1 ] ) . toEqual ( false ) ;
105
106
} ) ;
107
+ it ( 'does not immediately set image if delay prop exists' , ( ) => {
108
+ const delay = 3000 ;
109
+ const render = jest . fn ( src => < img src = { src } alt = "an image" /> ) ;
110
+ const wrapper = mountProgressiveImage ( render , delay ) ;
111
+ wrapper . instance ( ) . loadImage ( src ) ;
112
+ wrapper . instance ( ) . onLoad ( ) ;
113
+ expect ( wrapper . instance ( ) . state . image ) . toEqual ( placeholder ) ;
114
+ } ) ;
115
+ it ( 'sets image after delay passes if delay prop exists' , ( ) => {
116
+ const delay = 3000 ;
117
+ const render = jest . fn ( src => < img src = { src } alt = "an image" /> ) ;
118
+ const wrapper = mountProgressiveImage ( render , delay ) ;
119
+ wrapper . instance ( ) . loadImage ( src ) ;
120
+ wrapper . instance ( ) . onLoad ( ) ;
121
+ setTimeout ( ( ) => {
122
+ expect ( wrapper . instance ( ) . state . image ) . toEqual ( src ) ;
123
+ } , delay + 1 ) ;
124
+ } ) ;
106
125
} ) ;
0 commit comments