@@ -63,178 +63,182 @@ describe('TocComponent', () => {
6363 expect ( tocComponent . type ) . toEqual ( 'None' ) ;
6464 } ) ;
6565
66- it ( 'should not display anything when no h2 or h3 TocItems' , ( ) => {
67- tocService . tocList . next ( [ tocItem ( 'H1' , 'h1' ) ] ) ;
68- fixture . detectChanges ( ) ;
69- expect ( tocComponentDe . children . length ) . toEqual ( 0 ) ;
70- } ) ;
71-
72- it ( 'should update when the TocItems are updated' , ( ) => {
73- tocService . tocList . next ( [ tocItem ( 'Heading A' ) ] ) ;
74- fixture . detectChanges ( ) ;
75- expect ( tocComponentDe . queryAll ( By . css ( 'li' ) ) . length ) . toBe ( 1 ) ;
76-
77- tocService . tocList . next ( [ tocItem ( 'Heading A' ) , tocItem ( 'Heading B' ) , tocItem ( 'Heading C' ) ] ) ;
78- fixture . detectChanges ( ) ;
79- expect ( tocComponentDe . queryAll ( By . css ( 'li' ) ) . length ) . toBe ( 3 ) ;
80- } ) ;
81-
82- it ( 'should only display H2 and H3 TocItems' , ( ) => {
83- tocService . tocList . next ( [ tocItem ( 'Heading A' , 'h1' ) , tocItem ( 'Heading B' ) , tocItem ( 'Heading C' , 'h3' ) ] ) ;
84- fixture . detectChanges ( ) ;
85-
86- const tocItems = tocComponentDe . queryAll ( By . css ( 'li' ) ) ;
87- const textContents = tocItems . map ( item => item . nativeNode . textContent . trim ( ) ) ;
66+ describe ( '(once the lifecycle hooks have run)' , ( ) => {
67+ beforeEach ( ( ) => fixture . detectChanges ( ) ) ;
8868
89- expect ( tocItems . length ) . toBe ( 2 ) ;
90- expect ( textContents . find ( text => text === 'Heading A' ) ) . toBeFalsy ( ) ;
91- expect ( textContents . find ( text => text === 'Heading B' ) ) . toBeTruthy ( ) ;
92- expect ( textContents . find ( text => text === 'Heading C' ) ) . toBeTruthy ( ) ;
93- expect ( setPage ( ) . tocH1Heading ) . toBeFalsy ( ) ;
94- } ) ;
95-
96- it ( 'should stop listening for TocItems once destroyed' , ( ) => {
97- tocService . tocList . next ( [ tocItem ( 'Heading A' ) ] ) ;
98- fixture . detectChanges ( ) ;
99- expect ( tocComponentDe . queryAll ( By . css ( 'li' ) ) . length ) . toBe ( 1 ) ;
100-
101- tocComponent . ngOnDestroy ( ) ;
102- tocService . tocList . next ( [ tocItem ( 'Heading A' , 'h1' ) , tocItem ( 'Heading B' ) , tocItem ( 'Heading C' ) ] ) ;
103- fixture . detectChanges ( ) ;
104- expect ( tocComponentDe . queryAll ( By . css ( 'li' ) ) . length ) . toBe ( 1 ) ;
105- } ) ;
106-
107- describe ( 'when fewer than `maxPrimary` TocItems' , ( ) => {
108-
109- beforeEach ( ( ) => {
110- tocService . tocList . next ( [ tocItem ( 'Heading A' ) , tocItem ( 'Heading B' ) , tocItem ( 'Heading C' ) , tocItem ( 'Heading D' ) ] ) ;
69+ it ( 'should not display anything when no h2 or h3 TocItems' , ( ) => {
70+ tocService . tocList . next ( [ tocItem ( 'H1' , 'h1' ) ] ) ;
11171 fixture . detectChanges ( ) ;
112- page = setPage ( ) ;
72+ expect ( tocComponentDe . children . length ) . toEqual ( 0 ) ;
11373 } ) ;
11474
115- it ( 'should have four displayed items' , ( ) => {
116- expect ( page . listItems . length ) . toEqual ( 4 ) ;
117- } ) ;
118-
119- it ( 'should not have secondary items' , ( ) => {
120- expect ( tocComponent . type ) . toEqual ( 'EmbeddedSimple' ) ;
121- const aSecond = page . listItems . find ( item => item . classes . secondary ) ;
122- expect ( aSecond ) . toBeFalsy ( 'should not find a secondary' ) ;
123- } ) ;
124-
125- it ( 'should not display expando buttons' , ( ) => {
126- expect ( page . tocHeadingButtonEmbedded ) . toBeFalsy ( 'top expand/collapse button' ) ;
127- expect ( page . tocMoreButton ) . toBeFalsy ( 'bottom more button' ) ;
128- } ) ;
129- } ) ;
130-
131- describe ( 'when many TocItems' , ( ) => {
132- let scrollToTopSpy : jasmine . Spy ;
133-
134- beforeEach ( ( ) => {
75+ it ( 'should update when the TocItems are updated' , ( ) => {
76+ tocService . tocList . next ( [ tocItem ( 'Heading A' ) ] ) ;
13577 fixture . detectChanges ( ) ;
136- page = setPage ( ) ;
137- scrollToTopSpy = TestBed . get ( ScrollService ) . scrollToTop ;
138- } ) ;
78+ expect ( tocComponentDe . queryAll ( By . css ( 'li' ) ) . length ) . toBe ( 1 ) ;
13979
140- it ( 'should have more than 4 displayed items' , ( ) => {
141- expect ( page . listItems . length ) . toBeGreaterThan ( 4 ) ;
80+ tocService . tocList . next ( [ tocItem ( 'Heading A' ) , tocItem ( 'Heading B' ) , tocItem ( 'Heading C' ) ] ) ;
81+ fixture . detectChanges ( ) ;
82+ expect ( tocComponentDe . queryAll ( By . css ( 'li' ) ) . length ) . toBe ( 3 ) ;
14283 } ) ;
14384
144- it ( 'should not display the h1 item ' , ( ) => {
145- expect ( page . listItems . find ( item => item . classes . h1 ) ) . toBeFalsy ( 'should not find h1 item' ) ;
146- } ) ;
85+ it ( 'should only display H2 and H3 TocItems ' , ( ) => {
86+ tocService . tocList . next ( [ tocItem ( 'Heading A' , 'h1' ) , tocItem ( 'Heading B' ) , tocItem ( 'Heading C' , 'h3' ) ] ) ;
87+ fixture . detectChanges ( ) ;
14788
148- it ( 'should be in "collapsed" (not expanded) state at the start' , ( ) => {
149- expect ( tocComponent . isCollapsed ) . toBeTruthy ( ) ;
150- } ) ;
89+ const tocItems = tocComponentDe . queryAll ( By . css ( 'li' ) ) ;
90+ const textContents = tocItems . map ( item => item . nativeNode . textContent . trim ( ) ) ;
15191
152- it ( 'should have "collapsed" class at the start' , ( ) => {
153- expect ( tocComponentDe . children [ 0 ] . classes . collapsed ) . toEqual ( true ) ;
92+ expect ( tocItems . length ) . toBe ( 2 ) ;
93+ expect ( textContents . find ( text => text === 'Heading A' ) ) . toBeFalsy ( ) ;
94+ expect ( textContents . find ( text => text === 'Heading B' ) ) . toBeTruthy ( ) ;
95+ expect ( textContents . find ( text => text === 'Heading C' ) ) . toBeTruthy ( ) ;
96+ expect ( setPage ( ) . tocH1Heading ) . toBeFalsy ( ) ;
15497 } ) ;
15598
156- it ( 'should display expando buttons' , ( ) => {
157- expect ( page . tocHeadingButtonEmbedded ) . toBeTruthy ( 'top expand/collapse button' ) ;
158- expect ( page . tocMoreButton ) . toBeTruthy ( 'bottom more button' ) ;
159- } ) ;
160-
161- it ( 'should have secondary items' , ( ) => {
162- expect ( tocComponent . type ) . toEqual ( 'EmbeddedExpandable' ) ;
163- } ) ;
99+ it ( 'should stop listening for TocItems once destroyed' , ( ) => {
100+ tocService . tocList . next ( [ tocItem ( 'Heading A' ) ] ) ;
101+ fixture . detectChanges ( ) ;
102+ expect ( tocComponentDe . queryAll ( By . css ( 'li' ) ) . length ) . toBe ( 1 ) ;
164103
165- // CSS will hide items with the secondary class when collapsed
166- it ( 'should have secondary item with a secondary class' , ( ) => {
167- const aSecondary = page . listItems . find ( item => item . classes . secondary ) ;
168- expect ( aSecondary ) . toBeTruthy ( 'should find a secondary' ) ;
104+ tocComponent . ngOnDestroy ( ) ;
105+ tocService . tocList . next ( [ tocItem ( 'Heading A' , 'h1' ) , tocItem ( 'Heading B' ) , tocItem ( 'Heading C' ) ] ) ;
106+ fixture . detectChanges ( ) ;
107+ expect ( tocComponentDe . queryAll ( By . css ( 'li' ) ) . length ) . toBe ( 1 ) ;
169108 } ) ;
170109
171- describe ( 'after click tocHeading button ' , ( ) => {
110+ describe ( 'when fewer than `maxPrimary` TocItems ' , ( ) => {
172111
173112 beforeEach ( ( ) => {
174- page . tocHeadingButtonEmbedded . nativeElement . click ( ) ;
113+ tocService . tocList . next ( [ tocItem ( 'Heading A' ) , tocItem ( 'Heading B' ) , tocItem ( 'Heading C' ) , tocItem ( 'Heading D' ) ] ) ;
175114 fixture . detectChanges ( ) ;
115+ page = setPage ( ) ;
176116 } ) ;
177117
178- it ( 'should not be "collapsed" ' , ( ) => {
179- expect ( tocComponent . isCollapsed ) . toEqual ( false ) ;
118+ it ( 'should have four displayed items ' , ( ) => {
119+ expect ( page . listItems . length ) . toEqual ( 4 ) ;
180120 } ) ;
181121
182- it ( 'should not have "collapsed" class' , ( ) => {
183- expect ( tocComponentDe . children [ 0 ] . classes . collapsed ) . toBeFalsy ( ) ;
122+ it ( 'should not have secondary items' , ( ) => {
123+ expect ( tocComponent . type ) . toEqual ( 'EmbeddedSimple' ) ;
124+ const aSecond = page . listItems . find ( item => item . classes . secondary ) ;
125+ expect ( aSecond ) . toBeFalsy ( 'should not find a secondary' ) ;
184126 } ) ;
185127
186- it ( 'should not scroll' , ( ) => {
187- expect ( scrollToTopSpy ) . not . toHaveBeenCalled ( ) ;
128+ it ( 'should not display expando buttons' , ( ) => {
129+ expect ( page . tocHeadingButtonEmbedded ) . toBeFalsy ( 'top expand/collapse button' ) ;
130+ expect ( page . tocMoreButton ) . toBeFalsy ( 'bottom more button' ) ;
188131 } ) ;
132+ } ) ;
133+
134+ describe ( 'when many TocItems' , ( ) => {
135+ let scrollToTopSpy : jasmine . Spy ;
189136
190- it ( 'should be "collapsed" after clicking again' , ( ) => {
191- page . tocHeadingButtonEmbedded . nativeElement . click ( ) ;
137+ beforeEach ( ( ) => {
192138 fixture . detectChanges ( ) ;
193- expect ( tocComponent . isCollapsed ) . toEqual ( true ) ;
139+ page = setPage ( ) ;
140+ scrollToTopSpy = TestBed . get ( ScrollService ) . scrollToTop ;
194141 } ) ;
195142
196- it ( 'should not scroll after clicking again' , ( ) => {
197- page . tocHeadingButtonEmbedded . nativeElement . click ( ) ;
198- fixture . detectChanges ( ) ;
199- expect ( scrollToTopSpy ) . not . toHaveBeenCalled ( ) ;
143+ it ( 'should have more than 4 displayed items' , ( ) => {
144+ expect ( page . listItems . length ) . toBeGreaterThan ( 4 ) ;
200145 } ) ;
201- } ) ;
202146
203- describe ( 'after click tocMore button' , ( ) => {
147+ it ( 'should not display the h1 item' , ( ) => {
148+ expect ( page . listItems . find ( item => item . classes . h1 ) ) . toBeFalsy ( 'should not find h1 item' ) ;
149+ } ) ;
204150
205- beforeEach ( ( ) => {
206- page . tocMoreButton . nativeElement . click ( ) ;
207- fixture . detectChanges ( ) ;
151+ it ( 'should be in "collapsed" (not expanded) state at the start' , ( ) => {
152+ expect ( tocComponent . isCollapsed ) . toBeTruthy ( ) ;
208153 } ) ;
209154
210- it ( 'should not be "collapsed"' , ( ) => {
211- expect ( tocComponent . isCollapsed ) . toEqual ( false ) ;
155+ it ( 'should have "collapsed" class at the start ' , ( ) => {
156+ expect ( tocComponentDe . children [ 0 ] . classes . collapsed ) . toEqual ( true ) ;
212157 } ) ;
213158
214- it ( 'should not have "collapsed" class' , ( ) => {
215- expect ( tocComponentDe . children [ 0 ] . classes . collapsed ) . toBeFalsy ( ) ;
159+ it ( 'should display expando buttons' , ( ) => {
160+ expect ( page . tocHeadingButtonEmbedded ) . toBeTruthy ( 'top expand/collapse button' ) ;
161+ expect ( page . tocMoreButton ) . toBeTruthy ( 'bottom more button' ) ;
216162 } ) ;
217163
218- it ( 'should not scroll ' , ( ) => {
219- expect ( scrollToTopSpy ) . not . toHaveBeenCalled ( ) ;
164+ it ( 'should have secondary items ' , ( ) => {
165+ expect ( tocComponent . type ) . toEqual ( 'EmbeddedExpandable' ) ;
220166 } ) ;
221167
222- it ( 'should be "collapsed" after clicking again' , ( ) => {
223- page . tocMoreButton . nativeElement . click ( ) ;
224- fixture . detectChanges ( ) ;
225- expect ( tocComponent . isCollapsed ) . toEqual ( true ) ;
168+ // CSS will hide items with the secondary class when collapsed
169+ it ( 'should have secondary item with a secondary class' , ( ) => {
170+ const aSecondary = page . listItems . find ( item => item . classes . secondary ) ;
171+ expect ( aSecondary ) . toBeTruthy ( 'should find a secondary' ) ;
226172 } ) ;
227173
228- it ( 'should be "collapsed" after clicking tocHeadingButton' , ( ) => {
229- page . tocMoreButton . nativeElement . click ( ) ;
230- fixture . detectChanges ( ) ;
231- expect ( tocComponent . isCollapsed ) . toEqual ( true ) ;
174+ describe ( 'after click tocHeading button' , ( ) => {
175+
176+ beforeEach ( ( ) => {
177+ page . tocHeadingButtonEmbedded . nativeElement . click ( ) ;
178+ fixture . detectChanges ( ) ;
179+ } ) ;
180+
181+ it ( 'should not be "collapsed"' , ( ) => {
182+ expect ( tocComponent . isCollapsed ) . toEqual ( false ) ;
183+ } ) ;
184+
185+ it ( 'should not have "collapsed" class' , ( ) => {
186+ expect ( tocComponentDe . children [ 0 ] . classes . collapsed ) . toBeFalsy ( ) ;
187+ } ) ;
188+
189+ it ( 'should not scroll' , ( ) => {
190+ expect ( scrollToTopSpy ) . not . toHaveBeenCalled ( ) ;
191+ } ) ;
192+
193+ it ( 'should be "collapsed" after clicking again' , ( ) => {
194+ page . tocHeadingButtonEmbedded . nativeElement . click ( ) ;
195+ fixture . detectChanges ( ) ;
196+ expect ( tocComponent . isCollapsed ) . toEqual ( true ) ;
197+ } ) ;
198+
199+ it ( 'should not scroll after clicking again' , ( ) => {
200+ page . tocHeadingButtonEmbedded . nativeElement . click ( ) ;
201+ fixture . detectChanges ( ) ;
202+ expect ( scrollToTopSpy ) . not . toHaveBeenCalled ( ) ;
203+ } ) ;
232204 } ) ;
233205
234- it ( 'should scroll after clicking again' , ( ) => {
235- page . tocMoreButton . nativeElement . click ( ) ;
236- fixture . detectChanges ( ) ;
237- expect ( scrollToTopSpy ) . toHaveBeenCalled ( ) ;
206+ describe ( 'after click tocMore button' , ( ) => {
207+
208+ beforeEach ( ( ) => {
209+ page . tocMoreButton . nativeElement . click ( ) ;
210+ fixture . detectChanges ( ) ;
211+ } ) ;
212+
213+ it ( 'should not be "collapsed"' , ( ) => {
214+ expect ( tocComponent . isCollapsed ) . toEqual ( false ) ;
215+ } ) ;
216+
217+ it ( 'should not have "collapsed" class' , ( ) => {
218+ expect ( tocComponentDe . children [ 0 ] . classes . collapsed ) . toBeFalsy ( ) ;
219+ } ) ;
220+
221+ it ( 'should not scroll' , ( ) => {
222+ expect ( scrollToTopSpy ) . not . toHaveBeenCalled ( ) ;
223+ } ) ;
224+
225+ it ( 'should be "collapsed" after clicking again' , ( ) => {
226+ page . tocMoreButton . nativeElement . click ( ) ;
227+ fixture . detectChanges ( ) ;
228+ expect ( tocComponent . isCollapsed ) . toEqual ( true ) ;
229+ } ) ;
230+
231+ it ( 'should be "collapsed" after clicking tocHeadingButton' , ( ) => {
232+ page . tocMoreButton . nativeElement . click ( ) ;
233+ fixture . detectChanges ( ) ;
234+ expect ( tocComponent . isCollapsed ) . toEqual ( true ) ;
235+ } ) ;
236+
237+ it ( 'should scroll after clicking again' , ( ) => {
238+ page . tocMoreButton . nativeElement . click ( ) ;
239+ fixture . detectChanges ( ) ;
240+ expect ( scrollToTopSpy ) . toHaveBeenCalled ( ) ;
241+ } ) ;
238242 } ) ;
239243 } ) ;
240244 } ) ;
0 commit comments