Skip to content

Commit d9af163

Browse files
committed
added missing utils test
1 parent fabe4a5 commit d9af163

File tree

7 files changed

+117
-1
lines changed

7 files changed

+117
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { describe, it, expect } from "vitest";
2+
import { hasOwnProperty } from "@/utils/hasOwnProperty";
3+
4+
describe("Test that hasOwnProperty()", () => {
5+
it("works", () => {
6+
expect(hasOwnProperty({ test: 13 }, "test")).toBe(true);
7+
expect(hasOwnProperty({ test: 13 }, "test2")).toBe(false);
8+
});
9+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, it, expect } from "vitest";
2+
import { isFunction } from "@/utils/isFunction";
3+
4+
describe("Test that isFunction()", () => {
5+
it("works", () => {
6+
expect(isFunction(undefined)).toBe(false);
7+
expect(isFunction(null)).toBe(false);
8+
expect(isFunction(true)).toBe(false);
9+
expect(isFunction("abc")).toBe(false);
10+
expect(isFunction(0)).toBe(false);
11+
expect(isFunction(1)).toBe(false);
12+
expect(isFunction({})).toBe(false);
13+
expect(isFunction([])).toBe(false);
14+
expect(isFunction(() => {})).toBe(true);
15+
});
16+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, it, expect } from "vitest";
2+
import { isObject } from "@/utils/isObject";
3+
4+
describe("Test that isObject()", () => {
5+
it("works", () => {
6+
expect(isObject(undefined)).toBe(false);
7+
expect(isObject(null)).toBe(false);
8+
expect(isObject(true)).toBe(false);
9+
expect(isObject("abc")).toBe(false);
10+
expect(isObject(0)).toBe(false);
11+
expect(isObject(1)).toBe(false);
12+
expect(isObject({})).toBe(true);
13+
expect(isObject([])).toBe(true);
14+
expect(isObject(() => {})).toBe(false);
15+
});
16+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, it, expect } from "vitest";
2+
import { isPromise } from "@/utils/isPromise";
3+
4+
describe("Test that isPromise()", () => {
5+
it("works", () => {
6+
expect(isPromise(undefined)).toBe(false);
7+
expect(isPromise(null)).toBe(false);
8+
expect(isPromise(true)).toBe(false);
9+
expect(isPromise("abc")).toBe(false);
10+
expect(isPromise(0)).toBe(false);
11+
expect(isPromise(1)).toBe(false);
12+
expect(isPromise({})).toBe(false);
13+
expect(isPromise([])).toBe(false);
14+
expect(isPromise(() => {})).toBe(false);
15+
expect(isPromise(Promise.resolve(true))).toBe(true);
16+
});
17+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, it, expect } from "vitest";
2+
import { isString } from "@/utils/isString";
3+
4+
describe("Test that isString()", () => {
5+
it("works", () => {
6+
expect(isString(undefined)).toBe(false);
7+
expect(isString(null)).toBe(false);
8+
expect(isString(true)).toBe(false);
9+
expect(isString("abc")).toBe(true);
10+
expect(isString(0)).toBe(false);
11+
expect(isString(1)).toBe(false);
12+
expect(isString({})).toBe(false);
13+
expect(isString([])).toBe(false);
14+
expect(isString(() => {})).toBe(false);
15+
});
16+
});

chartlets.js/packages/lib/src/utils/objPath.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { describe, it, expect } from "vitest";
22

3-
import { equalObjPaths, getValue, setValue, normalizeObjPath } from "./objPath";
3+
import {
4+
equalObjPaths,
5+
getValue,
6+
setValue,
7+
normalizeObjPath,
8+
formatObjPath,
9+
} from "./objPath";
410

511
describe("Test that getValue()", () => {
612
it("works on 0th level", () => {
@@ -20,6 +26,12 @@ describe("Test that getValue()", () => {
2026
expect(getValue(obj, ["a", 2])).toEqual(3);
2127
expect(getValue(obj, ["b", "c"])).toEqual("x");
2228
});
29+
30+
it("ignores missing props", () => {
31+
const obj = { a: [1, 2, 3], b: { c: "x" } };
32+
expect(getValue(obj, ["c", 6, "d"])).toBeUndefined();
33+
expect(getValue(obj, ["b", "c", "d", "e"])).toBeUndefined();
34+
});
2335
});
2436

2537
describe("Test that setValue()", () => {
@@ -99,6 +111,16 @@ describe("Test that normalizeObjPath()", () => {
99111
});
100112
});
101113

114+
describe("Test that formatObjPath()", () => {
115+
it("works", () => {
116+
expect(formatObjPath(undefined)).toEqual("");
117+
expect(formatObjPath([])).toEqual("");
118+
expect(formatObjPath(["a", 3, "c"])).toEqual("a.3.c");
119+
expect(formatObjPath("a.b.3")).toEqual("a.b.3");
120+
expect(formatObjPath(6)).toEqual("6");
121+
});
122+
});
123+
102124
describe("Test that equalObjPaths()", () => {
103125
it("works with equal paths", () => {
104126
expect(equalObjPaths("a", "a")).toBe(true);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, it, expect } from "vitest";
2+
import { updateArray } from "@/utils/updateArray";
3+
4+
describe("Test that updateArray()", () => {
5+
it("works", () => {
6+
const array0: Record<string, unknown>[] = [
7+
{ x: [0] },
8+
{ x: [1] },
9+
{ x: [2] },
10+
];
11+
const array1 = updateArray(array0, 0, { y: 12 });
12+
expect(array0).toEqual([{ x: [0] }, { x: [1] }, { x: [2] }]);
13+
expect(array1).toEqual([{ x: [0], y: 12 }, { x: [1] }, { x: [2] }]);
14+
expect(array1).not.toBe(array0);
15+
expect(array1[0]).not.toBe(array0[0]);
16+
expect(array1[0].x).toBe(array0[0].x);
17+
expect(array1[1]).toBe(array0[1]);
18+
expect(array1[2]).toBe(array0[2]);
19+
});
20+
});

0 commit comments

Comments
 (0)