@@ -70,6 +70,9 @@ async function create_scroll_marker({
70
70
} ) ;
71
71
72
72
const instance = new Pattern ( el , options ) ;
73
+ jest . spyOn ( instance , "scroll_marker_callback" ) ;
74
+ jest . spyOn ( instance , "scroll_marker_current_callback" ) ;
75
+
73
76
await events . await_pattern_init ( instance ) ;
74
77
75
78
instance . scroll_marker_callback ( [
@@ -99,6 +102,24 @@ describe("pat-scroll-marker", () => {
99
102
document . body . innerHTML = "" ;
100
103
} ) ;
101
104
105
+ it ( "0: Inexistent targets do not break the runtime" , async ( ) => {
106
+ document . body . innerHTML = `
107
+ <nav class="pat-scroll-marker">
108
+ <a href="#id1">link 1</a>
109
+ <a href="#id2">link 2</a>
110
+ <a href="#id3">link 3</a>
111
+ </nav>
112
+ ` ;
113
+ const el = document . querySelector ( ".pat-scroll-marker" ) ;
114
+ const instance = new Pattern ( el ) ;
115
+ jest . spyOn ( instance , "scroll_marker_callback" ) ;
116
+ jest . spyOn ( instance , "scroll_marker_current_callback" ) ;
117
+ await events . await_pattern_init ( instance ) ;
118
+
119
+ expect ( instance . scroll_marker_callback ) . not . toHaveBeenCalled ( ) ;
120
+ expect ( instance . scroll_marker_current_callback ) . not . toHaveBeenCalled ( ) ;
121
+ } ) ;
122
+
102
123
describe ( "1: Test on window as scroll container" , ( ) => {
103
124
it ( "1.1: default values, id3 is current" , async ( ) => {
104
125
// With the default values the baseline is in the middle and the
@@ -113,6 +134,10 @@ describe("pat-scroll-marker", () => {
113
134
// container is the window object.
114
135
expect ( instance . scroll_container ) . toBe ( window ) ;
115
136
137
+ // Callbacks have been called.
138
+ expect ( instance . scroll_marker_callback ) . toHaveBeenCalled ( ) ;
139
+ expect ( instance . scroll_marker_current_callback ) . toHaveBeenCalled ( ) ;
140
+
116
141
expect ( nav_id1 . classList . contains ( "in-view" ) ) . toBe ( false ) ;
117
142
expect ( nav_id2 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
118
143
expect ( nav_id3 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
@@ -127,13 +152,17 @@ describe("pat-scroll-marker", () => {
127
152
} ) ;
128
153
129
154
it ( "1.2: distance 0, id2 is current" , async ( ) => {
130
- const { nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
155
+ const { instance , nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
131
156
await create_scroll_marker ( {
132
157
options : {
133
158
distance : 0 ,
134
159
} ,
135
160
} ) ;
136
161
162
+ // Callbacks have been called.
163
+ expect ( instance . scroll_marker_callback ) . toHaveBeenCalled ( ) ;
164
+ expect ( instance . scroll_marker_current_callback ) . toHaveBeenCalled ( ) ;
165
+
137
166
expect ( nav_id1 . classList . contains ( "in-view" ) ) . toBe ( false ) ;
138
167
expect ( nav_id2 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
139
168
expect ( nav_id3 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
@@ -148,14 +177,18 @@ describe("pat-scroll-marker", () => {
148
177
} ) ;
149
178
150
179
it ( "1.3: distance 50, side bottom, id2 is current" , async ( ) => {
151
- const { nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
180
+ const { instance , nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
152
181
await create_scroll_marker ( {
153
182
options : {
154
183
distance : "50%" ,
155
184
side : "bottom" ,
156
185
} ,
157
186
} ) ;
158
187
188
+ // Callbacks have been called.
189
+ expect ( instance . scroll_marker_callback ) . toHaveBeenCalled ( ) ;
190
+ expect ( instance . scroll_marker_current_callback ) . toHaveBeenCalled ( ) ;
191
+
159
192
expect ( nav_id1 . classList . contains ( "in-view" ) ) . toBe ( false ) ;
160
193
expect ( nav_id2 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
161
194
expect ( nav_id3 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
@@ -174,7 +207,7 @@ describe("pat-scroll-marker", () => {
174
207
// Only the visibility is set to most-visible, which means that id2 is
175
208
// the current one,
176
209
//
177
- const { nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
210
+ const { instance , nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
178
211
await create_scroll_marker ( {
179
212
options : {
180
213
distance : "50%" ,
@@ -183,6 +216,10 @@ describe("pat-scroll-marker", () => {
183
216
} ,
184
217
} ) ;
185
218
219
+ // Callbacks have been called.
220
+ expect ( instance . scroll_marker_callback ) . toHaveBeenCalled ( ) ;
221
+ expect ( instance . scroll_marker_current_callback ) . toHaveBeenCalled ( ) ;
222
+
186
223
expect ( nav_id1 . classList . contains ( "in-view" ) ) . toBe ( false ) ;
187
224
expect ( nav_id2 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
188
225
expect ( nav_id3 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
@@ -211,6 +248,10 @@ describe("pat-scroll-marker", () => {
211
248
// container is the window object.
212
249
expect ( instance . scroll_container ) . toBe ( main ) ;
213
250
251
+ // Callbacks have been called.
252
+ expect ( instance . scroll_marker_callback ) . toHaveBeenCalled ( ) ;
253
+ expect ( instance . scroll_marker_current_callback ) . toHaveBeenCalled ( ) ;
254
+
214
255
expect ( nav_id1 . classList . contains ( "in-view" ) ) . toBe ( false ) ;
215
256
expect ( nav_id2 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
216
257
expect ( nav_id3 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
@@ -225,14 +266,18 @@ describe("pat-scroll-marker", () => {
225
266
} ) ;
226
267
227
268
it ( "2.2: distance 0, id2 is current" , async ( ) => {
228
- const { nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
269
+ const { instance , nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
229
270
await create_scroll_marker ( {
230
271
options : {
231
272
distance : 0 ,
232
273
} ,
233
274
scroll_container_main : true ,
234
275
} ) ;
235
276
277
+ // Callbacks have been called.
278
+ expect ( instance . scroll_marker_callback ) . toHaveBeenCalled ( ) ;
279
+ expect ( instance . scroll_marker_current_callback ) . toHaveBeenCalled ( ) ;
280
+
236
281
expect ( nav_id1 . classList . contains ( "in-view" ) ) . toBe ( false ) ;
237
282
expect ( nav_id2 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
238
283
expect ( nav_id3 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
@@ -247,7 +292,7 @@ describe("pat-scroll-marker", () => {
247
292
} ) ;
248
293
249
294
it ( "2.3: distance 50, side bottom, id2 is current" , async ( ) => {
250
- const { nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
295
+ const { instance , nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
251
296
await create_scroll_marker ( {
252
297
options : {
253
298
distance : "50%" ,
@@ -256,6 +301,10 @@ describe("pat-scroll-marker", () => {
256
301
scroll_container_main : true ,
257
302
} ) ;
258
303
304
+ // Callbacks have been called.
305
+ expect ( instance . scroll_marker_callback ) . toHaveBeenCalled ( ) ;
306
+ expect ( instance . scroll_marker_current_callback ) . toHaveBeenCalled ( ) ;
307
+
259
308
expect ( nav_id1 . classList . contains ( "in-view" ) ) . toBe ( false ) ;
260
309
expect ( nav_id2 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
261
310
expect ( nav_id3 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
@@ -274,7 +323,7 @@ describe("pat-scroll-marker", () => {
274
323
// Only the visibility is set to most-visible, which means that id2 is
275
324
// the current one,
276
325
//
277
- const { nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
326
+ const { instance , nav_id1, nav_id2, nav_id3, id1, id2, id3 } =
278
327
await create_scroll_marker ( {
279
328
options : {
280
329
distance : "50%" ,
@@ -284,6 +333,10 @@ describe("pat-scroll-marker", () => {
284
333
scroll_container_main : true ,
285
334
} ) ;
286
335
336
+ // Callbacks have been called.
337
+ expect ( instance . scroll_marker_callback ) . toHaveBeenCalled ( ) ;
338
+ expect ( instance . scroll_marker_current_callback ) . toHaveBeenCalled ( ) ;
339
+
287
340
expect ( nav_id1 . classList . contains ( "in-view" ) ) . toBe ( false ) ;
288
341
expect ( nav_id2 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
289
342
expect ( nav_id3 . classList . contains ( "in-view" ) ) . toBe ( true ) ;
0 commit comments