-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_base.ts
142 lines (123 loc) · 4.14 KB
/
test_base.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import {
assertEquals,
} from "https://deno.land/std/testing/asserts.ts";
import Observe from "./Observe.ts";
export function testBaseClass() {
Deno.test("Initial value populated", (): void => {
let val = new Observe("initial value");
assertEquals(val.getValue(), "initial value");
});
Deno.test("Value change is reflected", (): void => {
let val = new Observe("initial value");
val.setValue("new value");
assertEquals(val.getValue(), "new value");
});
Deno.test("History is populated", (): void => {
let val = new Observe("initial value");
val.setValue("new value");
assertEquals(val.getHistory().length, 2);
});
Deno.test("Callback is invoked (binding)", (): void => {
let val = new Observe("initial value");
val.bind((data) => {
assertEquals(data, "new value");
});
val.setValue("new value");
});
Deno.test("Unbind removes listener", (): void => {
let val = new Observe("initial value");
let temp = "init";
let func = val.bind(() => temp = "changed!!"); // attempt to change the temp variable
val.unBind(func); // removing the listener.. func variable should not trigger
val.setValue("new value");
assertEquals(temp, "init");
});
Deno.test("Stopping prevents continue", (): void => {
let val = new Observe("initial value");
let temp = "init";
val.bind(() => val.stop()); // halt the observer
val.bind(() => temp = "changed!!"); // should not get here (see above)
val.setValue("new value");
assertEquals(temp, "init");
});
Deno.test("Type is reflected", (): void => {
let val = new Observe<number>(2);
val.bind((data) => {
assertEquals(typeof data, "number");
});
val.setValue(10);
});
Deno.test("Same value does nothing", (): void => {
let val = new Observe<number>(100);
let temp = 100;
val.bind((data) => temp = data); // should not be called
val.setValue(100);
assertEquals(temp, 100);
});
Deno.test("History is limited on large change count", (): void => {
let val = new Observe("init");
val.maxHistorySize = 4;
val.setValue("SHOULD_BE_REMOVED");
for (let i = 0; i < val.maxHistorySize + 1; i++) {
val.setValue(
`str ${val.setValue(((Math.random() * 10) + 1).toString())}`,
);
}
assertEquals(val.getHistory().includes("SHOULD_BE_REMOVED"), false);
});
Deno.test("Reset restores the original value", (): void => {
let val = new Observe("init");
val.maxHistorySize = 4;
val.setValue("lorem");
val.setValue("ipsum");
val.setValue("some stuff");
val.reset();
assertEquals(val.getValue(), "init");
});
Deno.test("Nesting observables should un-sub in case setValue is called", (): void => {
let nested = new Observe(100);
let obs = new Observe(nested);
let temp = 0;
obs.bind((data) => {
temp = data.getValue();
});
let new_nest = new Observe(1);
obs.setValue(new_nest); // should UNBIND the prev value here (nested should be unbound)
nested.setValue(98); // should not be reached (unbound)
new_nest.setValue(200); // reached
assertEquals(temp, 200);
});
Deno.test("setValue returns the value that was set", (): void => {
let val = new Observe("init");
val.maxHistorySize = 2;
assertEquals(val.setValue("lorem"), "lorem");
});
Deno.test("setValue emits all var-args", (): void => {
let val = new Observe("init");
let saw: string[] = [];
val.bind((str) => {
saw.push(str);
});
val.setValue("lorem", "ipsum", "dolor");
assertEquals(saw, ["lorem", "ipsum", "dolor"]);
});
Deno.test("Reset returns the instance", (): void => {
let val = new Observe("init");
val.setValue("lorem");
assertEquals(val.reset(), val);
});
Deno.test("Stop returns the instance", (): void => {
let val = new Observe("init");
val.setValue("lorem");
assertEquals(val.stop(), val);
});
Deno.test("unbinding All removes all listeners", (): void => {
let val = new Observe("init");
let t = 0;
val.bind(()=>t++);
val.bind(()=>t++);
val.unBindAll();
assertEquals(t, 0);
});
console.log("\n[+] -- DONE: Testing main class -- \n");
}