1
1
var $ = require ( "jquery" ) ;
2
2
var assert = require ( "chai/chai" ) . assert ;
3
3
var TableOfContents = require ( "../toc-control" ) ;
4
+ var safeCustomElement = require ( "../safe-custom-element" ) ;
4
5
5
6
require ( "steal-mocha" ) ;
6
7
7
8
describe ( "TableOfContents" , function ( ) {
9
+ if ( ! safeCustomElement . supported ) {
10
+ return ;
11
+ }
8
12
var $el , $testArea ;
9
13
10
14
beforeEach ( function ( ) {
11
- $ ( "body" ) . append ( "<ul id=\"toc-test\"></ul>" ) ;
12
-
13
- $el = $ ( "#toc-test" ) ;
14
15
$testArea = $ ( "#test-area" ) ;
16
+
15
17
} ) ;
16
18
17
19
afterEach ( function ( ) {
18
- $el . remove ( ) ;
19
20
$testArea . empty ( ) ;
21
+
20
22
} ) ;
21
23
22
24
it ( "makes a flat list from headings" , function ( ) {
@@ -27,15 +29,16 @@ describe("TableOfContents", function() {
27
29
"<h2>Configure</h2>"
28
30
] ;
29
31
30
- $testArea . html ( headings . join ( "" ) ) ;
32
+ $testArea . html ( "<article id='article'>" + headings . join ( "" ) + "</article>" +
33
+ "<bit-toc child-tag='ul' headings-container-selector='#article' depth='1'>" ) ;
31
34
32
- new TableOfContents ( $el . get ( 0 ) , {
35
+ /* new TableOfContents($el.get(0), {
33
36
tagName: "ul",
34
37
depth: 1,
35
38
headingsContainerSelector: "#test-area"
36
- } ) ;
37
-
38
- assert . equal ( $el . html ( ) , [
39
+ });*/
40
+ $ ( 'bit-toc li' ) . removeAttr ( "class" ) ; // remove for nice html
41
+ assert . equal ( document . querySelector ( 'bit-toc ul' ) . innerHTML , [
39
42
'<li><a href="#usage">Usage</a></li>' ,
40
43
'<li><a href="#install">Install</a></li>' ,
41
44
'<li><a href="#configure">Configure</a></li>' ,
@@ -53,13 +56,11 @@ describe("TableOfContents", function() {
53
56
"<h2>Writing Modules</h2>" ,
54
57
] ;
55
58
56
- $testArea . html ( headings . join ( "" ) ) ;
57
59
58
- new TableOfContents ( $el . get ( 0 ) , {
59
- tagName : "ul" ,
60
- depth : 3 ,
61
- headingsContainerSelector : "#test-area"
62
- } ) ;
60
+ $testArea . html ( "<article id='article'>" + headings . join ( "" ) + "</article>" +
61
+ "<bit-toc child-tag='ul' headings-container-selector='#article' depth='3'>" ) ;
62
+
63
+ var $el = $ ( 'bit-toc>ul' ) ;
63
64
64
65
assert . equal ( $el . find ( ">li:eq(0) ul" ) . length , 1 , "bower has a nested list" ) ;
65
66
assert . equal ( $el . find ( ">li:eq(1) ul" ) . length , 2 , "npm two nested lists" ) ;
@@ -76,18 +77,79 @@ describe("TableOfContents", function() {
76
77
"<h2>Writing Modules</h2>"
77
78
] ;
78
79
79
- $testArea . html ( headings . join ( "" ) ) ;
80
+ $testArea . html ( "<article id='article'>" + headings . join ( "" ) + "</article>" +
81
+ "<bit-toc child-tag='ul' headings-container-selector='#article' depth='1'>" ) ;
80
82
81
- new TableOfContents ( $el . get ( 0 ) , {
82
- tagName : "ul" ,
83
- depth : 1 ,
84
- headingsContainerSelector : "#test-area"
85
- } ) ;
83
+ var $el = $ ( 'bit-toc>ul' ) ;
84
+
85
+ $ ( 'bit-toc li' ) . removeAttr ( "class" ) ; // remove for nice html
86
86
87
87
assert . equal ( $el . html ( ) , [
88
88
'<li><a href="#bower">Bower</a></li>' ,
89
89
'<li><a href="#npm">NPM</a></li>' ,
90
90
'<li><a href="#writing-modules">Writing Modules</a></li>'
91
91
] . join ( "" ) ) ;
92
92
} ) ;
93
+
94
+ it ( "highlights what has been completed" , function ( ) {
95
+ var headings = [
96
+ "<h2>Bower</h2>" ,
97
+ "<p>Install</p>" ,
98
+ "<h2>NPM</h2>" ,
99
+ "<p>Install</p>" ,
100
+ "<h2>Configure</h2>" ,
101
+ "<p>xyz</p>" ,
102
+ "<h2>Writing Modules</h2>" ,
103
+ "<p>writing modules</p>" ,
104
+ "<h2>Extra</h2>" ,
105
+ "<p>final</p>"
106
+ ] ;
107
+
108
+ $testArea . html ( "<article id='article'>" + headings . join ( "" ) + "</article>" +
109
+ "<bit-toc headings-container-selector='#article'></bit-toc>" ) ;
110
+
111
+ $ ( "article" ) . css ( {
112
+ position : "fixed" ,
113
+ top : 0 ,
114
+ height : 200 ,
115
+ left : 200 ,
116
+ width : 600 ,
117
+ backgroundColor : "gray" ,
118
+ overflowY : "auto"
119
+ } ) ;
120
+
121
+ $ ( "article p" ) . css ( {
122
+ height : "500px" ,
123
+ border : "solid 1px red"
124
+ } ) ;
125
+
126
+ $ ( "bit-toc" ) [ 0 ] . highlight ( ) ;
127
+
128
+ function getCompletedAndActive ( ) {
129
+ var result = { completed : [ ] , active : [ ] }
130
+ $ ( "bit-toc li" ) . each ( function ( i , node ) {
131
+ if ( node . classList . contains ( "completed" ) ) {
132
+ result . completed . push ( node . textContent )
133
+ }
134
+ if ( node . classList . contains ( "active" ) ) {
135
+ result . active . push ( node . textContent )
136
+ }
137
+ } ) ;
138
+ return result ;
139
+ }
140
+
141
+ assert . deepEqual ( getCompletedAndActive ( ) , {
142
+ active : [ "Bower" ] ,
143
+ completed : [ ]
144
+ } , "initialized correctly" ) ;
145
+
146
+ $ ( "#article" ) . scrollTop ( 600 ) ;
147
+ $ ( "bit-toc" ) [ 0 ] . highlight ( ) ; // so we don't have to wait for the throttling
148
+
149
+ assert . deepEqual ( getCompletedAndActive ( ) , {
150
+ active : [ "NPM" ] ,
151
+ completed : [ "Bower" ]
152
+ } , "initialized correctly" ) ;
153
+
154
+ } ) ;
93
155
} ) ;
0 commit comments