-
Notifications
You must be signed in to change notification settings - Fork 1
/
define.test.ts
77 lines (59 loc) · 2.46 KB
/
define.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import * as assert from "node:assert";
import { define } from "./index.ts";
import { _try } from "https://lib.deno.dev/x/ayonli_jsext@latest/index.ts";
describe("define", () => {
it("should set a non-enumerable and non-writable property", () => {
const obj: { foo?: string; } = {};
define(obj, "foo", "Hello, World!");
_try(() => {
obj.foo = "Hi, Ayon!";
});
assert.strictEqual(obj.foo, "Hello, World!");
assert.deepStrictEqual(Object.keys(obj), []);
});
it("should set a enumerable but non-writable property", () => {
const obj: { foo?: string; } = {};
define(obj, "foo", "Hello, World!", true);
_try(() => {
obj.foo = "Hi, Ayon!";
});
assert.strictEqual(obj.foo, "Hello, World!");
assert.deepStrictEqual(Object.keys(obj), ["foo"]);
});
it("should set a writable but non-enumerable property", () => {
const obj: { foo?: string; } = {};
define(obj, "foo", "Hello, World!", false, true);
obj.foo = "Hi, Ayon!";
assert.strictEqual(obj.foo, "Hi, Ayon!");
assert.deepStrictEqual(Object.keys(obj), []);
});
it("should set a enumerable and writable property", () => {
const obj: { foo?: string; } = {};
define(obj, "foo", "Hello, World!", true, true);
obj.foo = "Hi, Ayon!";
assert.strictEqual(obj.foo, "Hi, Ayon!");
assert.deepStrictEqual(Object.keys(obj), ["foo"]);
});
it("should set a getter property instead", () => {
const obj: { foo?: string; } = {};
define(obj, "foo", { get: () => "Hello, World!" });
assert.strictEqual(obj.foo, "Hello, World!");
});
it("should set a enumerable getter property instead", () => {
const obj: { foo?: string; } = {};
define(obj, "foo", { get: () => "Hello, World!" }, true);
assert.strictEqual(obj.foo, "Hello, World!");
assert.deepStrictEqual(Object.keys(obj), ["foo"]);
});
it("should set a getter and a setter property instead", () => {
const obj: { foo?: string; _foo?: string; } = {};
define(obj, "foo", {
get: () => obj["_foo"] === void 0 ? "Hello, World!" : obj["_foo"],
set: (v: any) => obj["_foo"] = v
});
assert.strictEqual(obj.foo, "Hello, World!");
obj.foo = "Hi, Ayon";
assert.strictEqual(obj.foo, "Hi, Ayon");
assert.strictEqual(obj["_foo"], obj.foo);
});
});