aboutsummaryrefslogtreecommitdiff
path: root/src/lib/__tests__
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/__tests__')
-rw-r--r--src/lib/__tests__/charts.test.ts39
-rw-r--r--src/lib/__tests__/detect.test.ts22
-rw-r--r--src/lib/__tests__/format.test.ts38
3 files changed, 99 insertions, 0 deletions
diff --git a/src/lib/__tests__/charts.test.ts b/src/lib/__tests__/charts.test.ts
new file mode 100644
index 0000000..e81be16
--- /dev/null
+++ b/src/lib/__tests__/charts.test.ts
@@ -0,0 +1,39 @@
+import { renderNumberLabels } from '../charts';
+
+// test for renderNumberLabels
+
+describe('renderNumberLabels', () => {
+ test.each([
+ ['1000000', '1.0m'],
+ ['2500000', '2.5m'],
+ ])("formats numbers ≥ 1 million as 'Xm' (%s → %s)", (input, expected) => {
+ expect(renderNumberLabels(input)).toBe(expected);
+ });
+
+ test.each([['150000', '150k']])("formats numbers ≥ 100K as 'Xk' (%s → %s)", (input, expected) => {
+ expect(renderNumberLabels(input)).toBe(expected);
+ });
+
+ test.each([
+ ['12500', '12.5k'],
+ ])("formats numbers ≥ 10K as 'X.Xk' (%s → %s)", (input, expected) => {
+ expect(renderNumberLabels(input)).toBe(expected);
+ });
+
+ test.each([['1500', '1.50k']])("formats numbers ≥ 1K as 'X.XXk' (%s → %s)", (input, expected) => {
+ expect(renderNumberLabels(input)).toBe(expected);
+ });
+
+ test.each([
+ ['999', '999'],
+ ])('calls formatNumber for values < 1000 (%s → %s)', (input, expected) => {
+ expect(renderNumberLabels(input)).toBe(expected);
+ });
+
+ test.each([
+ ['0', '0'],
+ ['-5000', '-5000'],
+ ])('handles edge cases correctly (%s → %s)', (input, expected) => {
+ expect(renderNumberLabels(input)).toBe(expected);
+ });
+});
diff --git a/src/lib/__tests__/detect.test.ts b/src/lib/__tests__/detect.test.ts
new file mode 100644
index 0000000..0395aef
--- /dev/null
+++ b/src/lib/__tests__/detect.test.ts
@@ -0,0 +1,22 @@
+import { getIpAddress } from '../ip';
+
+const IP = '127.0.0.1';
+const BAD_IP = '127.127.127.127';
+
+test('getIpAddress: Custom header', () => {
+ process.env.CLIENT_IP_HEADER = 'x-custom-ip-header';
+
+ expect(getIpAddress(new Headers({ 'x-custom-ip-header': IP }))).toEqual(IP);
+});
+
+test('getIpAddress: CloudFlare header', () => {
+ expect(getIpAddress(new Headers({ 'cf-connecting-ip': IP }))).toEqual(IP);
+});
+
+test('getIpAddress: Standard header', () => {
+ expect(getIpAddress(new Headers({ 'x-forwarded-for': IP }))).toEqual(IP);
+});
+
+test('getIpAddress: No header', () => {
+ expect(getIpAddress(new Headers())).toEqual(null);
+});
diff --git a/src/lib/__tests__/format.test.ts b/src/lib/__tests__/format.test.ts
new file mode 100644
index 0000000..6e1b319
--- /dev/null
+++ b/src/lib/__tests__/format.test.ts
@@ -0,0 +1,38 @@
+import * as format from '../format';
+
+test('parseTime', () => {
+ expect(format.parseTime(86400 + 3600 + 60 + 1)).toEqual({
+ days: 1,
+ hours: 1,
+ minutes: 1,
+ seconds: 1,
+ ms: 0,
+ });
+});
+
+test('formatTime', () => {
+ expect(format.formatTime(3600 + 60 + 1)).toBe('1:01:01');
+});
+
+test('formatShortTime', () => {
+ expect(format.formatShortTime(3600 + 60 + 1)).toBe('1m1s');
+
+ expect(format.formatShortTime(3600 + 60 + 1, ['h', 'm', 's'])).toBe('1h1m1s');
+});
+
+test('formatNumber', () => {
+ expect(format.formatNumber('10.2')).toBe('10');
+ expect(format.formatNumber('10.5')).toBe('11');
+});
+
+test('formatLongNumber', () => {
+ expect(format.formatLongNumber(1200000)).toBe('1.2m');
+ expect(format.formatLongNumber(575000)).toBe('575k');
+ expect(format.formatLongNumber(10500)).toBe('10.5k');
+ expect(format.formatLongNumber(1200)).toBe('1.20k');
+});
+
+test('stringToColor', () => {
+ expect(format.stringToColor('hello')).toBe('#d218e9');
+ expect(format.stringToColor('goodbye')).toBe('#11e956');
+});