|
1 | 1 | import { expect, test } from "vitest"; |
2 | | -import { computePercentage } from "./computePercentage"; |
| 2 | +import { computePercentage, computePercentageOfY } from "./computePercentage"; |
| 3 | +import { describe } from "vitest"; |
3 | 4 |
|
4 | | -test("Return 0 if progressBarRef.current is undefined", () => { |
5 | | - expect( |
6 | | - computePercentage(new MouseEvent("mouseup", {}), { current: null }) |
7 | | - ).toBe(0); |
8 | | -}); |
| 5 | +describe("computePercentage", () => { |
| 6 | + test("Return 0 if progressBarRef.current is undefined", () => { |
| 7 | + expect( |
| 8 | + computePercentage(new MouseEvent("mouseup", {}), { current: null }) |
| 9 | + ).toBe(0); |
| 10 | + }); |
9 | 11 |
|
10 | | -test("Return 0 if progressBarRef.current is undefined", () => { |
11 | | - expect( |
12 | | - computePercentage(new MouseEvent("mousemove"), { current: null }) |
13 | | - ).toBe(0); |
14 | | -}); |
| 12 | + test("Return 0 if progressBarRef.current is undefined", () => { |
| 13 | + expect( |
| 14 | + computePercentage(new MouseEvent("mousemove"), { current: null }) |
| 15 | + ).toBe(0); |
| 16 | + }); |
| 17 | + |
| 18 | + test("Return 0 if progressBarRef.current is undefined", () => { |
| 19 | + expect( |
| 20 | + computePercentage(new MouseEvent("mousedown"), { |
| 21 | + current: null, |
| 22 | + }) |
| 23 | + ).toBe(0); |
| 24 | + }); |
| 25 | + |
| 26 | + describe("Given an valid percentage,when the mouse moves on the X axis", () => { |
| 27 | + /* MOCK DOM */ |
| 28 | + test("Return valid percentage,when input two valid Event Objet", () => { |
| 29 | + const container = document.createElement("div"); |
| 30 | + container.style.width = "200px"; |
| 31 | + container.style.height = "2px"; |
| 32 | + const mouseEvent = new MouseEvent("mousedown", { |
| 33 | + clientX: 50, |
| 34 | + clientY: 50, |
| 35 | + }); |
| 36 | + |
| 37 | + /* hack ! no value in the node environment , so overwrite they */ |
| 38 | + container.clientWidth = 200; |
| 39 | + container.getBoundingClientRect = () => ({ |
| 40 | + x: 10, |
| 41 | + y: 10, |
| 42 | + width: 300, |
| 43 | + height: 2, |
| 44 | + top: 10, |
| 45 | + right: 300, |
| 46 | + bottom: 10, |
| 47 | + left: 10, |
| 48 | + toJSON: function () { |
| 49 | + return ""; |
| 50 | + }, |
| 51 | + }); |
15 | 52 |
|
16 | | -test("Return 0 if progressBarRef.current is undefined", () => { |
17 | | - expect( |
18 | | - computePercentage(new MouseEvent("mousedown"), { |
19 | | - current: null, |
20 | | - }) |
21 | | - ).toBe(0); |
| 53 | + container.addEventListener("mousedown", function (e) { |
| 54 | + const val = computePercentage(e, { current: container }); |
| 55 | + expect(val).toBe(0.2); |
| 56 | + }); |
| 57 | + |
| 58 | + container.dispatchEvent(mouseEvent); |
| 59 | + }); |
| 60 | + }); |
22 | 61 | }); |
23 | 62 |
|
24 | | -/* MOCK DOM */ |
25 | | -test("Return percentage when mousedown event", () => { |
26 | | - const container = document.createElement("div"); |
27 | | - container.style.width = "200px"; |
28 | | - container.style.height = "2px"; |
29 | | - const mouseEvent = new MouseEvent("mousedown", { |
30 | | - clientX: 50, |
31 | | - clientY: 50, |
32 | | - }); |
33 | | - |
34 | | - /* hack ! no value in the node environment , so overwrite they */ |
35 | | - container.clientWidth = 200; |
36 | | - container.getBoundingClientRect = () => ({ |
37 | | - x: 10, |
38 | | - y: 10, |
39 | | - width: 300, |
40 | | - height: 2, |
41 | | - top: 10, |
42 | | - right: 300, |
43 | | - bottom: 10, |
44 | | - left: 10, |
45 | | - toJSON: function () { |
46 | | - return ""; |
47 | | - }, |
48 | | - }); |
49 | | - |
50 | | - container.addEventListener("mousedown", function (e) { |
51 | | - const val = computePercentage(e, { current: container }); |
52 | | - expect(val).toBe(0.2); |
53 | | - }); |
54 | | - |
55 | | - container.dispatchEvent(mouseEvent); |
| 63 | +describe("computePercentageOfY", () => { |
| 64 | + test("Return 0 if volumeBarRef.current is undefined", () => { |
| 65 | + expect( |
| 66 | + computePercentageOfY(new MouseEvent("mouseup"), { |
| 67 | + current: null, |
| 68 | + }) |
| 69 | + ).toBe(0); |
| 70 | + }); |
| 71 | + |
| 72 | + test("Return 0 if volumeBarRef.current is undefined", () => { |
| 73 | + expect( |
| 74 | + computePercentageOfY(new MouseEvent("mousemove"), { |
| 75 | + current: null, |
| 76 | + }) |
| 77 | + ).toBe(0); |
| 78 | + }); |
| 79 | + |
| 80 | + test("Return 0 if volumeBarRef.current is undefined", () => { |
| 81 | + expect( |
| 82 | + computePercentageOfY(new MouseEvent("mousedown"), { |
| 83 | + current: null, |
| 84 | + }) |
| 85 | + ).toBe(0); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("Given an valid percentage,when the mouse moves on the Y axis", () => { |
| 89 | + /* MOCK DOM */ |
| 90 | + test("Return valid percentage,when input two valid Event Objet", () => { |
| 91 | + const container = document.createElement("div"); |
| 92 | + container.style.width = "10px"; |
| 93 | + container.style.height = "300px"; |
| 94 | + const mouseEvent = new MouseEvent("mousedown", { |
| 95 | + clientX: 50, |
| 96 | + clientY: 100, |
| 97 | + }); |
| 98 | + |
| 99 | + /* hack ! no value in the node environment , so overwrite they */ |
| 100 | + container.clientHeight = 300; |
| 101 | + container.getBoundingClientRect = () => ({ |
| 102 | + x: 10, |
| 103 | + y: 10, |
| 104 | + width: 50, |
| 105 | + height: 300, |
| 106 | + top: 10, |
| 107 | + right: 300, |
| 108 | + bottom: 10, |
| 109 | + left: 10, |
| 110 | + toJSON: function () { |
| 111 | + return ""; |
| 112 | + }, |
| 113 | + }); |
| 114 | + |
| 115 | + container.addEventListener("mousedown", function (e) { |
| 116 | + const val = computePercentageOfY(e, { current: container }); |
| 117 | + expect(val).toBe(0.7); |
| 118 | + }); |
| 119 | + |
| 120 | + container.dispatchEvent(mouseEvent); |
| 121 | + }); |
| 122 | + }); |
56 | 123 | }); |
0 commit comments