@@ -2,9 +2,19 @@ import React from 'react';
22import { expect } from 'chai' ;
33import { shallow , mount , render } from 'enzyme' ;
44import Pic from '../lib/' ;
5- import { spy } from 'sinon' ;
5+ import sinon from 'sinon' ;
66
77describe ( 'Pic' , function ( ) {
8+ let sandbox ;
9+
10+ beforeEach ( function ( ) {
11+ sandbox = sinon . sandbox . create ( ) ;
12+ } ) ;
13+
14+ afterEach ( function ( ) {
15+ sandbox . restore ( ) ;
16+ } ) ;
17+
818 it ( 'should render the first image in images' , function ( ) {
919 const props = {
1020 alt : 'heart' ,
@@ -104,10 +114,30 @@ describe('Pic', function() {
104114 ]
105115 } ;
106116
107- spy ( Pic . prototype , 'inViewHandler' ) ;
117+ sandbox . spy ( Pic . prototype , 'inViewHandler' ) ;
108118 mount ( < Pic { ...props } /> ) ;
109119
110- expect ( Pic . prototype . inViewHandler ) . to . have . property ( 'callCount' , 1 ) ;
120+ expect ( Pic . prototype . inViewHandler . callCount ) . to . equal ( 1 ) ;
121+ } ) ;
122+
123+ it ( 'should call componentWillUnmount when unmounted' , function ( ) {
124+ const props = {
125+ images : [
126+ {
127+ width : 290 ,
128+ url : 'http://placehold.it/290?text=♥'
129+ } ,
130+ {
131+ width : 320 ,
132+ url : 'http://placehold.it/320?text=♥'
133+ }
134+ ]
135+ } ;
136+
137+ sandbox . spy ( Pic . prototype , 'componentWillUnmount' ) ;
138+ const wrapper = mount ( < Pic { ...props } /> ) ;
139+ wrapper . unmount ( ) ;
140+ expect ( Pic . prototype . componentWillUnmount . callCount ) . to . equal ( 1 ) ;
111141 } ) ;
112142
113143 it ( 'should set optimal image if renderOutOfView is true' , function ( ) {
@@ -125,10 +155,10 @@ describe('Pic', function() {
125155 ]
126156 } ;
127157
128- spy ( Pic . prototype , 'setResponsiveImage' ) ;
158+ sandbox . spy ( Pic . prototype , 'setResponsiveImage' ) ;
129159 mount ( < Pic { ...props } /> ) ;
130160
131- expect ( Pic . prototype . setResponsiveImage ) . to . have . property ( 'callCount' , 1 ) ;
161+ expect ( Pic . prototype . setResponsiveImage . callCount ) . to . equal ( 1 ) ;
132162 } ) ;
133163
134164 it ( 'should set blur to false by default' , function ( ) {
@@ -250,4 +280,56 @@ describe('Pic', function() {
250280 pic . find ( 'div' ) . first ( ) . attr ( 'style' )
251281 ) . to . contain ( `background-color:${ props . baseStyle . backgroundColor } ;` ) ;
252282 } ) ;
283+
284+ it ( 'should not render if a url in images is not a string' , function ( ) {
285+ const props = {
286+ alt : 'heart' ,
287+ images : [
288+ {
289+ width : 290 ,
290+ url : 'http://placehold.it/290?text=♥'
291+ } ,
292+ {
293+ width : 320 ,
294+ url : { }
295+ }
296+ ]
297+ } ;
298+
299+ const wrapper = shallow ( < Pic { ...props } /> ) ;
300+ expect ( wrapper . find ( 'img' ) ) . to . have . length ( 0 ) ;
301+
302+ } ) ;
303+
304+ it ( 'should not render if a width in images is not a number' , function ( ) {
305+ const props = {
306+ alt : 'heart' ,
307+ images : [
308+ {
309+ width : 290 ,
310+ url : 'http://placehold.it/290?text=♥'
311+ } ,
312+ {
313+ width : 'test' ,
314+ url : 'http://placehold.it/290?text=♥'
315+ }
316+ ]
317+ } ;
318+
319+ const wrapper = shallow ( < Pic { ...props } /> ) ;
320+ expect ( wrapper . find ( 'img' ) ) . to . have . length ( 0 ) ;
321+
322+ } ) ;
323+
324+ it ( 'should not render if no images passed through' , function ( ) {
325+ const props = {
326+ alt : 'heart' ,
327+ images : [ ]
328+ } ;
329+
330+ const wrapper = shallow ( < Pic { ...props } /> ) ;
331+ expect ( wrapper . find ( 'img' ) ) . to . have . length ( 0 ) ;
332+
333+ } ) ;
334+
253335} ) ;
0 commit comments