@@ -5,8 +5,10 @@ import {
5
5
createAnchoredLeft ,
6
6
createAnchoredRight ,
7
7
createEqualWidth ,
8
+ createHorizontalPosition ,
8
9
FixConfig ,
9
10
} from "../createHorizontalPosition" ;
11
+
10
12
import {
11
13
getCenterXCoord ,
12
14
getInnerLeftCoord ,
@@ -137,10 +139,10 @@ describe("createAnchoredLeft", () => {
137
139
} ) ;
138
140
139
141
it ( "should ignore the disableSwapping argument" , ( ) => {
140
- const updatedConfig1 = { ...config1 , disableSwapping : true } ;
141
- const updatedConfig2 = { ...config2 , disableSwapping : true } ;
142
- const updatedConfig3 = { ...config3 , disableSwapping : true } ;
143
- const updatedConfig4 = { ...config4 , disableSwapping : true } ;
142
+ const updatedConfig1 : FixConfig = { ...config1 , disableSwapping : true } ;
143
+ const updatedConfig2 : FixConfig = { ...config2 , disableSwapping : true } ;
144
+ const updatedConfig3 : FixConfig = { ...config3 , disableSwapping : true } ;
145
+ const updatedConfig4 : FixConfig = { ...config4 , disableSwapping : true } ;
144
146
145
147
expect ( createAnchoredLeft ( updatedConfig1 ) ) . toEqual ( {
146
148
left : getLeftCoord ( updatedConfig1 ) ,
@@ -161,10 +163,10 @@ describe("createAnchoredLeft", () => {
161
163
} ) ;
162
164
163
165
it ( "should return the vwMargin value as the left value if swapping is disabled" , ( ) => {
164
- const updatedConfig5 = { ...config5 , disableSwapping : true } ;
165
- const updatedConfig6 = { ...config6 , disableSwapping : true } ;
166
- const updatedConfig7 = { ...config7 , disableSwapping : true } ;
167
- const updatedConfig8 = { ...config8 , disableSwapping : true } ;
166
+ const updatedConfig5 : FixConfig = { ...config5 , disableSwapping : true } ;
167
+ const updatedConfig6 : FixConfig = { ...config6 , disableSwapping : true } ;
168
+ const updatedConfig7 : FixConfig = { ...config7 , disableSwapping : true } ;
169
+ const updatedConfig8 : FixConfig = { ...config8 , disableSwapping : true } ;
168
170
expect ( createAnchoredLeft ( updatedConfig5 ) ) . toEqual ( {
169
171
left : 0 ,
170
172
actualX : "left" ,
@@ -184,10 +186,10 @@ describe("createAnchoredLeft", () => {
184
186
} ) ;
185
187
186
188
it ( "should return the vwMargin value as the left value if swapping to the right will force it out of the viewport right bounds" , ( ) => {
187
- const updatedConfig5 = { ...config5 , screenRight : 200 } ;
188
- const updatedConfig6 = { ...config6 , screenRight : 200 } ;
189
- const updatedConfig7 = { ...config7 , screenRight : 200 } ;
190
- const updatedConfig8 = { ...config8 , screenRight : 200 } ;
189
+ const updatedConfig5 : FixConfig = { ...config5 , screenRight : 200 } ;
190
+ const updatedConfig6 : FixConfig = { ...config6 , screenRight : 200 } ;
191
+ const updatedConfig7 : FixConfig = { ...config7 , screenRight : 200 } ;
192
+ const updatedConfig8 : FixConfig = { ...config8 , screenRight : 200 } ;
191
193
expect ( createAnchoredLeft ( updatedConfig5 ) ) . toEqual ( {
192
194
left : 0 ,
193
195
actualX : "left" ,
@@ -246,31 +248,50 @@ describe("createAnchoredInnerLeft", () => {
246
248
} ) ;
247
249
} ) ;
248
250
249
- it ( "should return the vwMargin if swapping is disabled" , ( ) => {
250
- const updatedConfig1 = { ...rightBoundsConfig1 , disableSwapping : true } ;
251
- const updatedConfig2 = { ...rightBoundsConfig2 , disableSwapping : true } ;
251
+ it ( "should just update the position to be within the viewport if swapping is disabled" , ( ) => {
252
+ const leftOutOfBounds1 : FixConfig = {
253
+ ...leftBoundsConfig1 ,
254
+ disableSwapping : true ,
255
+ containerRect : {
256
+ ...leftBoundsConfig1 . containerRect ,
257
+ left : - 20 ,
258
+ } ,
259
+ } ;
260
+ const leftOutOfBounds2 : FixConfig = {
261
+ ...leftOutOfBounds1 ,
262
+ vwMargin : 8 ,
263
+ } ;
264
+
265
+ const tooWideConfig1 : FixConfig = {
266
+ ...rightBoundsConfig1 ,
267
+ disableSwapping : true ,
268
+ } ;
252
269
253
- expect ( createAnchoredInnerLeft ( updatedConfig1 ) ) . toEqual ( {
270
+ expect ( createAnchoredInnerLeft ( leftOutOfBounds1 ) ) . toEqual ( {
254
271
left : 0 ,
255
272
actualX : "inner-left" ,
256
273
} ) ;
257
- expect ( createAnchoredInnerLeft ( updatedConfig2 ) ) . toEqual ( {
274
+ expect ( createAnchoredInnerLeft ( leftOutOfBounds2 ) ) . toEqual ( {
258
275
left : 8 ,
259
276
actualX : "inner-left" ,
260
277
} ) ;
278
+ expect ( createAnchoredInnerLeft ( tooWideConfig1 ) ) . toEqual ( {
279
+ left : 25 ,
280
+ actualX : "inner-left" ,
281
+ } ) ;
261
282
} ) ;
262
283
263
284
it ( "should return the vwMargin if the swapped inner right values are less than the vwMargin" , ( ) => {
264
285
// need to move the container rect a bit more into the page so that the fixed element will
265
286
// no longer be within the viewport
266
- const updatedConfig1 = {
287
+ const updatedConfig1 : FixConfig = {
267
288
...leftBoundsConfig1 ,
268
289
containerRect : {
269
290
...leftBoundsConfig1 . containerRect ,
270
291
left : 40 ,
271
292
} ,
272
293
} ;
273
- const updatedConfig2 = {
294
+ const updatedConfig2 : FixConfig = {
274
295
...leftBoundsConfig2 ,
275
296
containerRect : {
276
297
...leftBoundsConfig2 . containerRect ,
@@ -296,6 +317,29 @@ describe("createAnchoredInnerLeft", () => {
296
317
left : getInnerRightCoord ( rightBoundsConfig2 ) ,
297
318
actualX : "inner-right" ,
298
319
} ) ;
320
+
321
+ const scrolledOffscreenRight : FixConfig = {
322
+ xMargin : 0 ,
323
+ vwMargin : 0 ,
324
+ elWidth : 100 ,
325
+ screenRight : vw ,
326
+ disableSwapping : false ,
327
+ containerRect : {
328
+ top : 0 ,
329
+ bottom : 0 ,
330
+ left : vw - 80 ,
331
+ right : vw - 40 ,
332
+ height : 40 ,
333
+ width : 120 ,
334
+ x : 0 ,
335
+ y : 0 ,
336
+ toJSON ( ) { } ,
337
+ } ,
338
+ } ;
339
+ expect ( createAnchoredInnerLeft ( scrolledOffscreenRight ) ) . toEqual ( {
340
+ left : vw - 100 ,
341
+ actualX : "inner-right" ,
342
+ } ) ;
299
343
} ) ;
300
344
} ) ;
301
345
@@ -379,42 +423,48 @@ describe("createAnchoredInnerRight", () => {
379
423
} ) ;
380
424
381
425
it ( "should return the screenRight minus the element's width as the left value if swapping is disabled" , ( ) => {
382
- const updatedConfig1 = { ...leftBoundsConfig1 , disableSwapping : true } ;
383
- const updatedConfig2 = { ...leftBoundsConfig2 , disableSwapping : true } ;
426
+ const updatedConfig1 : FixConfig = {
427
+ ...leftBoundsConfig1 ,
428
+ disableSwapping : true ,
429
+ } ;
430
+ const updatedConfig2 : FixConfig = {
431
+ ...leftBoundsConfig2 ,
432
+ disableSwapping : true ,
433
+ } ;
384
434
385
435
expect ( createAnchoredInnerRight ( updatedConfig1 ) ) . toEqual ( {
386
- left : 25 ,
436
+ left : 0 ,
387
437
actualX : "inner-right" ,
388
438
} ) ;
389
439
expect ( createAnchoredInnerRight ( updatedConfig2 ) ) . toEqual ( {
390
- left : 25 ,
440
+ left : 8 ,
391
441
actualX : "inner-right" ,
392
442
} ) ;
393
443
} ) ;
394
444
395
445
it ( "should return the screenRight minus the element's width as the left value if the swapped inner left values are less than the vwMargin" , ( ) => {
396
446
// need to move the container rect a bit more into the page so that the fixed element will
397
447
// no longer be within the viewport
398
- const updatedConfig1 = {
448
+ const updatedConfig1 : FixConfig = {
399
449
...leftBoundsConfig1 ,
400
450
containerRect : {
401
451
...leftBoundsConfig1 . containerRect ,
402
452
left : 40 ,
403
453
} ,
404
454
} ;
405
- const updatedConfig2 = {
455
+ const updatedConfig2 : FixConfig = {
406
456
...leftBoundsConfig2 ,
407
457
containerRect : {
408
458
...leftBoundsConfig2 . containerRect ,
409
459
left : 40 ,
410
460
} ,
411
461
} ;
412
462
expect ( createAnchoredInnerRight ( updatedConfig1 ) ) . toEqual ( {
413
- left : 25 ,
463
+ left : 0 ,
414
464
actualX : "inner-right" ,
415
465
} ) ;
416
466
expect ( createAnchoredInnerRight ( updatedConfig2 ) ) . toEqual ( {
417
- left : 25 ,
467
+ left : 8 ,
418
468
actualX : "inner-right" ,
419
469
} ) ;
420
470
} ) ;
@@ -425,7 +475,7 @@ describe("createAnchoredInnerRight", () => {
425
475
actualX : "inner-left" ,
426
476
} ) ;
427
477
expect ( createAnchoredInnerRight ( leftBoundsConfig2 ) ) . toEqual ( {
428
- left : getInnerLeftCoord ( leftBoundsConfig2 ) ,
478
+ left : 8 ,
429
479
actualX : "inner-left" ,
430
480
} ) ;
431
481
} ) ;
@@ -452,10 +502,10 @@ describe("createAnchoredRight", () => {
452
502
} ) ;
453
503
454
504
it ( "should ignore the disableSwapping argument" , ( ) => {
455
- const updatedConfig1 = { ...config1 , disableSwapping : true } ;
456
- const updatedConfig2 = { ...config2 , disableSwapping : true } ;
457
- const updatedConfig3 = { ...config3 , disableSwapping : true } ;
458
- const updatedConfig4 = { ...config4 , disableSwapping : true } ;
505
+ const updatedConfig1 : FixConfig = { ...config1 , disableSwapping : true } ;
506
+ const updatedConfig2 : FixConfig = { ...config2 , disableSwapping : true } ;
507
+ const updatedConfig3 : FixConfig = { ...config3 , disableSwapping : true } ;
508
+ const updatedConfig4 : FixConfig = { ...config4 , disableSwapping : true } ;
459
509
460
510
expect ( createAnchoredRight ( updatedConfig1 ) ) . toEqual ( {
461
511
left : getRightCoord ( updatedConfig1 ) ,
@@ -624,4 +674,98 @@ describe("createEqualWidth", () => {
624
674
actualX : "center" ,
625
675
} ) ;
626
676
} ) ;
677
+
678
+ it ( "should use the initialX value instead of the containerRect.left and xMargin if it was provided" , ( ) => {
679
+ expect ( createEqualWidth ( { ...options1 , initialX : 150 } ) ) . toEqual ( {
680
+ left : 150 ,
681
+ width : 400 ,
682
+ actualX : "center" ,
683
+ } ) ;
684
+ } ) ;
685
+ } ) ;
686
+
687
+ describe ( "createHorizontalPosition" , ( ) => {
688
+ it ( "should return the correct positions..." , ( ) => {
689
+ const equalWidthOptions = {
690
+ x : "inner-left" ,
691
+ vw : 500 ,
692
+ vwMargin : 0 ,
693
+ xMargin : 0 ,
694
+ width : "equal" ,
695
+ elWidth : 120 ,
696
+ initialX : undefined ,
697
+ containerRect : containerRect1 ,
698
+ disableSwapping : false ,
699
+ } as const ;
700
+
701
+ expect ( createHorizontalPosition ( equalWidthOptions ) ) . toEqual ( {
702
+ left : containerRect1 . left ,
703
+ width : containerRect1 . width ,
704
+ actualX : "inner-left" ,
705
+ } ) ;
706
+
707
+ const minWidthOptions = {
708
+ ...equalWidthOptions ,
709
+ width : "min" ,
710
+ } as const ;
711
+ expect ( createHorizontalPosition ( minWidthOptions ) ) . toEqual ( {
712
+ left : containerRect1 . left ,
713
+ minWidth : containerRect1 . width ,
714
+ actualX : "inner-left" ,
715
+ } ) ;
716
+
717
+ const tooLargeOptions = {
718
+ ...equalWidthOptions ,
719
+ elWidth : 560 ,
720
+ width : "auto" ,
721
+ } as const ;
722
+ expect ( createHorizontalPosition ( tooLargeOptions ) ) . toEqual ( {
723
+ left : 0 ,
724
+ right : 0 ,
725
+ actualX : "inner-left" ,
726
+ } ) ;
727
+
728
+ const simpleLeftOptions = {
729
+ ...equalWidthOptions ,
730
+ x : "left" ,
731
+ width : "auto" ,
732
+ } as const ;
733
+ const simpleInnerLeftOptions = {
734
+ ...simpleLeftOptions ,
735
+ x : "inner-left" ,
736
+ } as const ;
737
+ const simpleCenterOptions = {
738
+ ...simpleInnerLeftOptions ,
739
+ x : "center" ,
740
+ } as const ;
741
+ const simpleInnerRightOptions = {
742
+ ...simpleCenterOptions ,
743
+ x : "inner-right" ,
744
+ } as const ;
745
+ const simpleRightOptions = {
746
+ ...simpleInnerRightOptions ,
747
+ x : "right" ,
748
+ } as const ;
749
+
750
+ expect ( createHorizontalPosition ( simpleLeftOptions ) ) . toEqual ( {
751
+ left : 200 ,
752
+ actualX : "right" ,
753
+ } ) ;
754
+ expect ( createHorizontalPosition ( simpleInnerLeftOptions ) ) . toEqual ( {
755
+ left : 100 ,
756
+ actualX : "inner-left" ,
757
+ } ) ;
758
+ expect ( createHorizontalPosition ( simpleCenterOptions ) ) . toEqual ( {
759
+ left : 90 ,
760
+ actualX : "center" ,
761
+ } ) ;
762
+ expect ( createHorizontalPosition ( simpleInnerRightOptions ) ) . toEqual ( {
763
+ left : 80 ,
764
+ actualX : "inner-right" ,
765
+ } ) ;
766
+ expect ( createHorizontalPosition ( simpleRightOptions ) ) . toEqual ( {
767
+ left : 200 ,
768
+ actualX : "right" ,
769
+ } ) ;
770
+ } ) ;
627
771
} ) ;
0 commit comments