@@ -75,25 +75,55 @@ class ComposedProviderSpec: QuickSpec {
7575 expect ( ( collectionView. subviews [ 0 ] as! UILabel ) . text) == " a "
7676 expect ( ( collectionView. subviews [ 1 ] as! UILabel ) . text) == " b "
7777
78- expect ( collectionView. subviews [ 0 ] . frame. origin) == CGPoint . zero
79- composer. layout = RowLayout ( justifyContent: . center)
80- collectionView. layoutIfNeeded ( )
81- expect ( collectionView. reloadCount) == 3
82- expect ( collectionView. subviews [ 0 ] . frame. origin) != CGPoint . zero
83-
8478 composer. animator = ScaleAnimator ( )
8579 collectionView. layoutIfNeeded ( )
86- expect ( collectionView. reloadCount) == 4
80+ expect ( collectionView. reloadCount) == 3
8781
8882 provider2. data = [ " b " ]
8983 collectionView. layoutIfNeeded ( )
90- expect ( collectionView. reloadCount) == 5
84+ expect ( collectionView. reloadCount) == 4
9185 expect ( collectionView. visibleCells. count) == 1
9286 expect ( ( collectionView. subviews [ 0 ] as! UILabel ) . text) == " b "
9387
9488 provider1. data = [ 3 , 4 , 5 ] // provider1 is not a section anymore, shouldnt trigger reload
9589 collectionView. layoutIfNeeded ( )
96- expect ( collectionView. reloadCount) == 5
90+ expect ( collectionView. reloadCount) == 4
91+ }
92+
93+ it ( " shouldn't reload when it doesn't need to " ) {
94+ let provider1 = SimpleTestProvider ( data: [ 1 , 2 , 3 , 4 ] )
95+ let provider2 = SimpleTestProvider ( data: [ " a " , " b " ] )
96+ let provider3 = SimpleTestProvider ( data: [ " hello " , " collectionKit " ] )
97+ let composer = ComposedProvider ( sections: [ provider1, provider2, provider3] )
98+ let collectionView = CollectionView ( provider: composer)
99+ collectionView. frame = CGRect ( x: 0 , y: 0 , width: 300 , height: 500 )
100+ collectionView. layoutIfNeeded ( )
101+ expect ( collectionView. reloadCount) == 1
102+
103+ expect ( collectionView. subviews [ 0 ] . frame. origin) == CGPoint . zero
104+ expect ( collectionView. subviews [ 1 ] . frame. origin) == CGPoint ( x: 50 , y: 0 )
105+ provider1. sizeSource = { _, _, _ in
106+ return CGSize ( width: 30 , height: 30 )
107+ }
108+ collectionView. layoutIfNeeded ( )
109+ expect ( collectionView. reloadCount) == 1
110+ expect ( collectionView. subviews [ 0 ] . frame. origin) == CGPoint . zero
111+ expect ( collectionView. subviews [ 1 ] . frame. origin) == CGPoint ( x: 30 , y: 0 )
112+
113+ // changing layout shouldn't reload, but will invalidate layout
114+ provider1. layout = FlowLayout ( justifyContent: . spaceBetween)
115+ collectionView. layoutIfNeeded ( )
116+ expect ( collectionView. reloadCount) == 1
117+ expect ( collectionView. subviews [ 0 ] . frame. origin) == CGPoint . zero
118+ expect ( collectionView. subviews [ 1 ] . frame. origin) != CGPoint ( x: 30 , y: 0 )
119+
120+ // changing layout shouldn't reload, but will invalidate layout
121+ provider1. layout = FlowLayout ( )
122+ composer. layout = FlowLayout ( justifyContent: . center)
123+ collectionView. layoutIfNeeded ( )
124+ print ( collectionView. subviews. map ( { $0. frame } ) )
125+ expect ( collectionView. reloadCount) == 1
126+ expect ( collectionView. subviews [ 0 ] . frame. origin) != CGPoint . zero
97127 }
98128
99129 it ( " supports nested update " ) {
0 commit comments