Skip to content

Commit b9c3bf6

Browse files
refactor: move fmt related tests into separate file (anuraghazra#4569)
Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
1 parent 5df2e63 commit b9c3bf6

2 files changed

Lines changed: 67 additions & 64 deletions

File tree

tests/fmt.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { describe, expect, it } from "@jest/globals";
2+
import { formatBytes, kFormatter } from "../src/common/fmt.js";
3+
4+
describe("Test fmt.js", () => {
5+
it("should test kFormatter default behavior", () => {
6+
expect(kFormatter(1)).toBe(1);
7+
expect(kFormatter(-1)).toBe(-1);
8+
expect(kFormatter(500)).toBe(500);
9+
expect(kFormatter(1000)).toBe("1k");
10+
expect(kFormatter(1200)).toBe("1.2k");
11+
expect(kFormatter(10000)).toBe("10k");
12+
expect(kFormatter(12345)).toBe("12.3k");
13+
expect(kFormatter(99900)).toBe("99.9k");
14+
expect(kFormatter(9900000)).toBe("9900k");
15+
});
16+
17+
it("should test kFormatter with 0 decimal precision", () => {
18+
expect(kFormatter(1, 0)).toBe("0k");
19+
expect(kFormatter(-1, 0)).toBe("-0k");
20+
expect(kFormatter(500, 0)).toBe("1k");
21+
expect(kFormatter(1000, 0)).toBe("1k");
22+
expect(kFormatter(1200, 0)).toBe("1k");
23+
expect(kFormatter(10000, 0)).toBe("10k");
24+
expect(kFormatter(12345, 0)).toBe("12k");
25+
expect(kFormatter(99000, 0)).toBe("99k");
26+
expect(kFormatter(99900, 0)).toBe("100k");
27+
expect(kFormatter(9900000, 0)).toBe("9900k");
28+
});
29+
30+
it("should test kFormatter with 1 decimal precision", () => {
31+
expect(kFormatter(1, 1)).toBe("0.0k");
32+
expect(kFormatter(-1, 1)).toBe("-0.0k");
33+
expect(kFormatter(500, 1)).toBe("0.5k");
34+
expect(kFormatter(1000, 1)).toBe("1.0k");
35+
expect(kFormatter(1200, 1)).toBe("1.2k");
36+
expect(kFormatter(10000, 1)).toBe("10.0k");
37+
expect(kFormatter(12345, 1)).toBe("12.3k");
38+
expect(kFormatter(99900, 1)).toBe("99.9k");
39+
expect(kFormatter(9900000, 1)).toBe("9900.0k");
40+
});
41+
42+
it("should test kFormatter with 2 decimal precision", () => {
43+
expect(kFormatter(1, 2)).toBe("0.00k");
44+
expect(kFormatter(-1, 2)).toBe("-0.00k");
45+
expect(kFormatter(500, 2)).toBe("0.50k");
46+
expect(kFormatter(1000, 2)).toBe("1.00k");
47+
expect(kFormatter(1200, 2)).toBe("1.20k");
48+
expect(kFormatter(10000, 2)).toBe("10.00k");
49+
expect(kFormatter(12345, 2)).toBe("12.35k");
50+
expect(kFormatter(99900, 2)).toBe("99.90k");
51+
expect(kFormatter(9900000, 2)).toBe("9900.00k");
52+
});
53+
54+
it("formatBytes: should return expected values", () => {
55+
expect(formatBytes(0)).toBe("0 B");
56+
expect(formatBytes(100)).toBe("100.0 B");
57+
expect(formatBytes(1024)).toBe("1.0 KB");
58+
expect(formatBytes(1024 * 1024)).toBe("1.0 MB");
59+
expect(formatBytes(1024 * 1024 * 1024)).toBe("1.0 GB");
60+
expect(formatBytes(1024 * 1024 * 1024 * 1024)).toBe("1.0 TB");
61+
expect(formatBytes(1024 * 1024 * 1024 * 1024 * 1024)).toBe("1.0 PB");
62+
expect(formatBytes(1024 * 1024 * 1024 * 1024 * 1024 * 1024)).toBe("1.0 EB");
63+
64+
expect(formatBytes(1234 * 1024)).toBe("1.2 MB");
65+
expect(formatBytes(123.4 * 1024)).toBe("123.4 KB");
66+
});
67+
});

tests/utils.test.js

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,58 +9,8 @@ import {
99
renderError,
1010
wrapTextMultiline,
1111
} from "../src/common/utils.js";
12-
import { formatBytes, kFormatter } from "../src/common/fmt.js";
1312

1413
describe("Test utils.js", () => {
15-
it("should test kFormatter default behavior", () => {
16-
expect(kFormatter(1)).toBe(1);
17-
expect(kFormatter(-1)).toBe(-1);
18-
expect(kFormatter(500)).toBe(500);
19-
expect(kFormatter(1000)).toBe("1k");
20-
expect(kFormatter(1200)).toBe("1.2k");
21-
expect(kFormatter(10000)).toBe("10k");
22-
expect(kFormatter(12345)).toBe("12.3k");
23-
expect(kFormatter(99900)).toBe("99.9k");
24-
expect(kFormatter(9900000)).toBe("9900k");
25-
});
26-
27-
it("should test kFormatter with 0 decimal precision", () => {
28-
expect(kFormatter(1, 0)).toBe("0k");
29-
expect(kFormatter(-1, 0)).toBe("-0k");
30-
expect(kFormatter(500, 0)).toBe("1k");
31-
expect(kFormatter(1000, 0)).toBe("1k");
32-
expect(kFormatter(1200, 0)).toBe("1k");
33-
expect(kFormatter(10000, 0)).toBe("10k");
34-
expect(kFormatter(12345, 0)).toBe("12k");
35-
expect(kFormatter(99000, 0)).toBe("99k");
36-
expect(kFormatter(99900, 0)).toBe("100k");
37-
expect(kFormatter(9900000, 0)).toBe("9900k");
38-
});
39-
40-
it("should test kFormatter with 1 decimal precision", () => {
41-
expect(kFormatter(1, 1)).toBe("0.0k");
42-
expect(kFormatter(-1, 1)).toBe("-0.0k");
43-
expect(kFormatter(500, 1)).toBe("0.5k");
44-
expect(kFormatter(1000, 1)).toBe("1.0k");
45-
expect(kFormatter(1200, 1)).toBe("1.2k");
46-
expect(kFormatter(10000, 1)).toBe("10.0k");
47-
expect(kFormatter(12345, 1)).toBe("12.3k");
48-
expect(kFormatter(99900, 1)).toBe("99.9k");
49-
expect(kFormatter(9900000, 1)).toBe("9900.0k");
50-
});
51-
52-
it("should test kFormatter with 2 decimal precision", () => {
53-
expect(kFormatter(1, 2)).toBe("0.00k");
54-
expect(kFormatter(-1, 2)).toBe("-0.00k");
55-
expect(kFormatter(500, 2)).toBe("0.50k");
56-
expect(kFormatter(1000, 2)).toBe("1.00k");
57-
expect(kFormatter(1200, 2)).toBe("1.20k");
58-
expect(kFormatter(10000, 2)).toBe("10.00k");
59-
expect(kFormatter(12345, 2)).toBe("12.35k");
60-
expect(kFormatter(99900, 2)).toBe("99.90k");
61-
expect(kFormatter(9900000, 2)).toBe("9900.00k");
62-
});
63-
6414
it("should test parseBoolean", () => {
6515
expect(parseBoolean(true)).toBe(true);
6616
expect(parseBoolean(false)).toBe(false);
@@ -102,20 +52,6 @@ describe("Test utils.js", () => {
10252
queryByTestId(document.body, "message").children[1],
10353
).toHaveTextContent(/Secondary Message/gim);
10454
});
105-
106-
it("formatBytes: should return expected values", () => {
107-
expect(formatBytes(0)).toBe("0 B");
108-
expect(formatBytes(100)).toBe("100.0 B");
109-
expect(formatBytes(1024)).toBe("1.0 KB");
110-
expect(formatBytes(1024 * 1024)).toBe("1.0 MB");
111-
expect(formatBytes(1024 * 1024 * 1024)).toBe("1.0 GB");
112-
expect(formatBytes(1024 * 1024 * 1024 * 1024)).toBe("1.0 TB");
113-
expect(formatBytes(1024 * 1024 * 1024 * 1024 * 1024)).toBe("1.0 PB");
114-
expect(formatBytes(1024 * 1024 * 1024 * 1024 * 1024 * 1024)).toBe("1.0 EB");
115-
116-
expect(formatBytes(1234 * 1024)).toBe("1.2 MB");
117-
expect(formatBytes(123.4 * 1024)).toBe("123.4 KB");
118-
});
11955
});
12056

12157
describe("wrapTextMultiline", () => {

0 commit comments

Comments
 (0)