Skip to content
This repository was archived by the owner on Aug 19, 2022. It is now read-only.

Commit 2dc86a8

Browse files
committed
Add tests for delay prop
1 parent 3acce81 commit 2dc86a8

File tree

4 files changed

+73
-41
lines changed

4 files changed

+73
-41
lines changed

__tests__/index.js

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
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';
66

77
configure({ adapter: new Adapter() });
88

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';
1111
const srcSetData = {
12-
srcSet: "srcSet",
13-
sizes: "sizes"
12+
srcSet: 'srcSet',
13+
sizes: 'sizes'
1414
};
1515

16-
const mountProgressiveImage = renderFn => {
16+
const mountProgressiveImage = (renderFn, delay) => {
1717
const defaultRender = image => {
1818
return <img src={image} />;
1919
};
2020
const render = renderFn || defaultRender;
2121
return mount(
2222
<ProgressiveImage
23+
delay={delay}
2324
src={src}
2425
placeholder={placeholder}
2526
srcSetData={srcSetData}
@@ -29,14 +30,14 @@ const mountProgressiveImage = renderFn => {
2930
);
3031
};
3132

32-
describe("react-progressive-image", () => {
33+
describe('react-progressive-image', () => {
3334
beforeEach(() => {
3435
global.Image = Image;
3536
});
36-
it("exports a React component", () => {
37-
expect(typeof ProgressiveImage).toBe("function");
37+
it('exports a React component', () => {
38+
expect(typeof ProgressiveImage).toBe('function');
3839
});
39-
it("throws if not provided a function as a child", () => {
40+
it('throws if not provided a function as a child', () => {
4041
/* eslint-disable no-console */
4142
const _error = console.error;
4243
console.error = jest.fn(() => {});
@@ -53,54 +54,72 @@ describe("react-progressive-image", () => {
5354
}
5455
/* eslint-enable no-console */
5556
});
56-
it("creates an instance of Image when mounted", () => {
57+
it('creates an instance of Image when mounted', () => {
5758
const wrapper = mountProgressiveImage();
5859
const instance = wrapper.instance();
5960
expect(instance.image.constructor).toBe(HTMLImageElement);
6061
});
61-
it("sets the onload property on the Image instance", () => {
62+
it('sets the onload property on the Image instance', () => {
6263
const wrapper = mountProgressiveImage();
6364
const instance = wrapper.instance();
6465
expect(instance.image.onload).toEqual(instance.onLoad);
6566
});
66-
it("sets the onerror property on the Image instance", () => {
67+
it('sets the onerror property on the Image instance', () => {
6768
const wrapper = mountProgressiveImage();
6869
const instance = wrapper.instance();
6970
expect(instance.image.onerror).toEqual(instance.onError);
7071
});
71-
it("sets the src property on the Image instance", () => {
72+
it('sets the src property on the Image instance', () => {
7273
const wrapper = mountProgressiveImage();
7374
const instance = wrapper.instance();
7475
expect(instance.image.src).toEqual(src);
7576
});
76-
it("sets the srcSet property on the Image instance", () => {
77+
it('sets the srcSet property on the Image instance', () => {
7778
const wrapper = mountProgressiveImage();
7879
const instance = wrapper.instance();
7980
expect(instance.image.srcset).toEqual(srcSetData.srcSet);
8081
});
81-
it("sets the sizes property on the Image instance", () => {
82+
it('sets the sizes property on the Image instance', () => {
8283
const wrapper = mountProgressiveImage();
8384
const instance = wrapper.instance();
8485
expect(instance.image.sizes).toEqual(srcSetData.sizes);
8586
});
86-
it("renders placeholder image on initial render", () => {
87+
it('renders placeholder image on initial render', () => {
8788
const render = jest.fn(src => <img src={src} alt="an image" />);
8889
const wrapper = mountProgressiveImage(render);
8990
expect(render.mock.calls[0][0]).toEqual(placeholder);
9091
});
91-
it("renders src image on second render", () => {
92+
it('renders src image on second render', () => {
9293
const render = jest.fn(src => <img src={src} alt="an image" />);
9394
const wrapper = mountProgressiveImage(render);
9495
wrapper.instance().loadImage(src);
9596
wrapper.instance().onLoad();
9697
expect(render.mock.calls[1][0]).toEqual(src);
9798
});
98-
it("sets loading to false after src image is loaded", () => {
99+
it('sets loading to false after src image is loaded', () => {
99100
const render = jest.fn(src => <img src={src} alt="an image" />);
100101
const wrapper = mountProgressiveImage(render);
101102
expect(render.mock.calls[0][1]).toEqual(true);
102103
wrapper.instance().loadImage(src);
103104
wrapper.instance().onLoad();
104105
expect(render.mock.calls[1][1]).toEqual(false);
105106
});
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+
});
106125
});

demo/inline.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

demo/webpack.config.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
module.exports = {
22
mode: 'development',
33
context: __dirname,
4-
entry: [
5-
'./app.js'
6-
],
4+
entry: ['./app.js'],
75
output: {
86
path: __dirname,
97
filename: 'bundle.js'
108
},
119
module: {
12-
rules: [{
13-
test: /\.js$/,
14-
exclude: /node_modules/,
15-
loader: 'babel-loader'
16-
}]
10+
rules: [
11+
{
12+
test: /\.js$/,
13+
exclude: /node_modules/,
14+
loader: 'babel-loader'
15+
}
16+
]
1717
}
1818
};

src/index.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,29 @@ export default class ProgressiveImage extends React.Component<Props, State> {
7979
// avoid the possibility of props being updated and the
8080
// new image loading before the new props are available as
8181
// this.props.
82+
83+
if (this.props.delay) {
84+
this.setImageWithDelay();
85+
} else {
86+
this.setImage();
87+
}
88+
};
89+
90+
setImageWithDelay = () => {
8291
setTimeout(() => {
83-
this.setState({
84-
image: this.image.src,
85-
loading: false,
86-
srcSetData: {
87-
srcSet: this.image.srcset || '',
88-
sizes: this.image.sizes || ''
89-
}
90-
});
91-
}, this.props.delay || 0);
92+
this.setImage();
93+
}, this.props.delay);
94+
};
95+
96+
setImage = () => {
97+
this.setState({
98+
image: this.image.src,
99+
loading: false,
100+
srcSetData: {
101+
srcSet: this.image.srcset || '',
102+
sizes: this.image.sizes || ''
103+
}
104+
});
92105
};
93106

94107
onError = (errorEvent: Event) => {

0 commit comments

Comments
 (0)