MCPcopy Index your code
hub / github.com/OneNoteDev/WebClipper / tests

Method tests

src/tests/communicator/smartValue_tests.ts:10–277  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

8 }
9
10 protected tests() {
11 test("Test that SmartValue subscriptions are triggered with the appropriate values", () => {
12 let smartValue = new SmartValue<string>("originalValue");
13
14 let counter = 0;
15 smartValue.subscribe((newValue) => {
16 counter++;
17
18 if (counter === 1) {
19 strictEqual(newValue, "originalValue", "Subscribers should be called immediately after being created with the existing value");
20 strictEqual(smartValue.get(), "originalValue", "Subscribers should be called immediately after being created, and the get method should return the existing value");
21 } else if (counter === 2) {
22 strictEqual(newValue, "setTwice", "When a new value is set, the subscriber should be called with the new value as a parameter");
23 strictEqual(smartValue.get(), "setTwice", "When a new value is set, the subscriber should be called, and the get method should return the new value");
24 } else if (counter === 3) {
25 if (newValue === "setTwice") {
26 ok(false, "If the value is set to the exact same value as it already was, we shouldn't notify the subscribers");
27 } else {
28 strictEqual(newValue, "finalValue", "When a new value is set, the subscriber should be called with the new value as a parameter");
29 strictEqual(smartValue.get(), "finalValue", "When a new value is set, the subscriber should be called, and the get method should return the new value");
30 }
31 } else {
32 ok(false, "The subscribed method was called more times than expected!");
33 }
34 });
35
36 smartValue.set("setTwice");
37 smartValue.set("setTwice");
38 smartValue.set("finalValue");
39
40 strictEqual(smartValue.get(), "finalValue", "The get method should return the updated value");
41 });
42
43 test("Test SmartValue.Subscribe for multiple smart values", () => {
44 let smartValue1 = new SmartValue<string>("initialValue1");
45 let smartValue2 = new SmartValue<string>("initialValue2");
46
47 let counter = 0;
48 SmartValue.subscribe([smartValue1, smartValue2], (newValue1, newValue2) => {
49 counter++;
50
51 if (counter === 1) {
52 strictEqual(newValue1, "initialValue1", "Check the initialValues");
53 strictEqual(newValue2, "initialValue2", "Check the initialValues");
54 } else if (counter === 2) {
55 strictEqual(newValue1, "initialValue1", "We expect this to fire twice");
56 strictEqual(newValue2, "initialValue2", "We expect this to fire twice");
57 } else if (counter === 3) {
58 strictEqual(newValue1, "updatedValue1", "Value should be updated");
59 strictEqual(newValue2, "initialValue2", "Value should be the original");
60 } else if (counter === 4) {
61 strictEqual(newValue1, "updatedValue1", "Value should be updated");
62 strictEqual(newValue2, "updatedValue2", "Value should be updated");
63 } else if (counter === 5) {
64 ok(false, "The subscribed method was called more times than expected!");
65 }
66 });
67

Callers

nothing calls this directly

Calls 4

subscribeMethod · 0.95
getMethod · 0.95
setMethod · 0.95
unsubscribeMethod · 0.95

Tested by

no test coverage detected