11/* eslint-env jest */
2- import { parseRootMargin } from '../src/utils' ;
2+ import { parseRootMargin , shallowCompareOptions } from '../src/utils' ;
33
44describe ( 'parseRootMargin' , ( ) => {
55 test ( 'throws when using wrong units' , ( ) => {
@@ -13,3 +13,38 @@ describe('parseRootMargin', () => {
1313 expect ( parseRootMargin ( '10px 5px 0%' ) ) . toBe ( '10px 5px 0% 5px' ) ;
1414 } ) ;
1515} ) ;
16+
17+ describe ( 'shallowCompareOptions' , ( ) => {
18+ const comparerFn = ( nextProps , prevProps ) =>
19+ [ 'disabled' , 'root' , 'rootMargin' , 'threshold' ] . some ( option =>
20+ shallowCompareOptions ( nextProps [ option ] , prevProps [ option ] )
21+ ) ;
22+
23+ test ( 'should return true if threshold array length is not the same' , ( ) => {
24+ const nextProps = { threshold : [ 0.25 , 0.5 ] } ;
25+ const prevProps = { threshold : [ 0.25 , 0.5 , 0.75 ] } ;
26+
27+ expect ( comparerFn ( nextProps , prevProps ) ) . toBeTruthy ( ) ;
28+ } ) ;
29+
30+ test ( 'should return true if threshold array length is the same but not equal' , ( ) => {
31+ const nextProps = { threshold : [ 0.25 , 0.75 , 0.5 ] } ;
32+ const prevProps = { threshold : [ 0.25 , 0.5 , 0.75 ] } ;
33+
34+ expect ( comparerFn ( nextProps , prevProps ) ) . toBeTruthy ( ) ;
35+ } ) ;
36+
37+ test ( 'should return false if options are equal' , ( ) => {
38+ const nextProps = { disabled : true , root : 1 , rootMargin : 2 , threshold : [ 0.25 , 0.75 , 0.5 ] } ;
39+ const prevProps = { ...nextProps } ;
40+
41+ expect ( comparerFn ( nextProps , prevProps ) ) . toBeFalsy ( ) ;
42+ } ) ;
43+
44+ test ( 'should return true if options are different' , ( ) => {
45+ const nextProps = { disabled : true , root : 1 , rootMargin : 2 , threshold : [ 0.25 , 0.75 , 0.5 ] } ;
46+ const prevProps = { ...nextProps , threshold : 1 } ;
47+
48+ expect ( comparerFn ( nextProps , prevProps ) ) . toBeTruthy ( ) ;
49+ } ) ;
50+ } ) ;
0 commit comments