Debian Codegraph @

packages/popper/tests/unit/getRoundedOffsets.js

raw
1 import getRoundedOffsets from '../../src/utils/getRoundedOffsets';
2 import placements from '../../src/methods/placements';
3
4 const TOP = 200;
5 const BOTTOM = 300;
6 const EVEN_SIZE = { width: 20, height: 20, top: TOP, bottom: BOTTOM };
7 const ODD_SIZE = { width: 21, height: 21, top: TOP, bottom: BOTTOM };
8 const ROUNDS_UP = { left: 18.57127, right: 38.57127 };
9 const ROUNDED_UP = { left: 19, right: 39 };
10 const ROUNDED_DOWN = { left: 18, right: 38 };
11 const ALL_SIZE_COMBINATIONS = [
12 [EVEN_SIZE, EVEN_SIZE],
13 [EVEN_SIZE, ODD_SIZE],
14 [ODD_SIZE, EVEN_SIZE],
15 [ODD_SIZE, ODD_SIZE],
16 ];
17 const variationPlacements = placements.filter(
18 placement => placement.indexOf('-') !== -1
19 );
20
21 describe('utils/getRoundedOffsets', () => {
22 it('Math.round()s when both popper and reference have even width', () => {
23 const offsets = getRoundedOffsets({
24 placement: 'bottom',
25 offsets: {
26 popper: { ...EVEN_SIZE, ...ROUNDS_UP },
27 reference: EVEN_SIZE,
28 },
29 }, true);
30 expect(offsets.left).toBe(ROUNDED_UP.left);
31 expect(offsets.right).toBe(ROUNDED_UP.right);
32 expect(offsets.top).toBe(TOP);
33 expect(offsets.bottom).toBe(BOTTOM);
34 });
35
36 it('Math.floor()s when popper and reference have a difference in width oddness', () => {
37 const offsets1 = getRoundedOffsets({
38 placement: 'bottom',
39 offsets: {
40 popper: { ...EVEN_SIZE, ...ROUNDS_UP },
41 reference: ODD_SIZE,
42 },
43 }, true);
44 const offsets2 = getRoundedOffsets({
45 placement: 'bottom',
46 offsets: {
47 popper: { ...ODD_SIZE, ...ROUNDS_UP },
48 reference: EVEN_SIZE,
49 },
50 }, true);
51 [offsets1, offsets2].forEach(offsets => {
52 expect(offsets.left).toBe(ROUNDED_DOWN.left);
53 expect(offsets.right).toBe(ROUNDED_DOWN.right);
54 expect(offsets.top).toBe(TOP);
55 expect(offsets.bottom).toBe(BOTTOM);
56 })
57 });
58
59 it('Math.rounds()s and subtracts 1 from left offset if both are odd in width', () => {
60 const offsets = getRoundedOffsets({
61 placement: 'bottom',
62 offsets: {
63 popper: { ...ODD_SIZE, ...ROUNDS_UP },
64 reference: ODD_SIZE,
65 },
66 }, true);
67 expect(offsets.left).toBe(ROUNDED_UP.left - 1);
68 expect(offsets.right).toBe(ROUNDED_UP.right);
69 expect(offsets.top).toBe(TOP);
70 expect(offsets.bottom).toBe(BOTTOM);
71 });
72
73 it('always Math.round()s variation placements', () => {
74 variationPlacements.forEach(placement => {
75 ALL_SIZE_COMBINATIONS.forEach(([popperSize, referenceSize]) => {
76 const offsets = getRoundedOffsets({
77 placement,
78 offsets: {
79 popper: { ...popperSize, ...ROUNDS_UP },
80 reference: referenceSize,
81 },
82 }, true);
83 expect(offsets.left).toBe(ROUNDED_UP.left);
84 expect(offsets.right).toBe(ROUNDED_UP.right);
85 expect(offsets.top).toBe(TOP);
86 expect(offsets.bottom).toBe(BOTTOM);
87 });
88 });
89 });
90
91 it('always Math.round()s vertical offsets', () => {
92 ALL_SIZE_COMBINATIONS.forEach(([popperSize, referenceSize]) => {
93 const offsets = getRoundedOffsets({
94 placement: 'bottom',
95 offsets: {
96 popper: { ...popperSize, ...ROUNDS_UP, top: 218.6, bottom: 318.6 },
97 reference: referenceSize,
98 },
99 }, true);
100 expect(offsets.top).toBe(219);
101 expect(offsets.bottom).toBe(319);
102 });
103 });
104
105 it('does not integerize the offsets if second argument is `false`', () => {
106 ALL_SIZE_COMBINATIONS.forEach(([popperSize, referenceSize]) => {
107 const offsets = getRoundedOffsets({
108 placement: 'bottom',
109 offsets: {
110 popper: { ...popperSize, ...ROUNDS_UP, top: 218.6, bottom: 318.6 },
111 reference: referenceSize,
112 },
113 }, false);
114 expect(offsets.left).toBe(ROUNDS_UP.left);
115 expect(offsets.right).toBe(ROUNDS_UP.right);
116 expect(offsets.top).toBe(218.6);
117 expect(offsets.bottom).toBe(318.6);
118 });
119 });
120 });