mod_test.ts
1 import { default as Tree, EventEmmiter } from "./mod.ts"; 2 import { assertEquals } from "@std/assert"; 3 import type { codec } from "@massmarket/utils"; 4 5 Deno.test("Tree Event Testings", async (t) => { 6 await t.step("basic eventemmiter, no tree", () => { 7 const emmiter = new EventEmmiter<string>(); 8 let val; 9 const func = (e: string) => { 10 val = e; 11 }; 12 emmiter.on(func); 13 emmiter.emit("test"); 14 assertEquals(val, "test", "Event should be dispatched"); 15 emmiter.off(func); 16 emmiter.emit("test2"); 17 assertEquals(val, "test", "Handler should be removed"); 18 }); 19 await t.step("Tree Events!", () => { 20 const tree = new Tree<codec.CodecValue>(0); 21 let event; 22 const handler = (e: codec.CodecValue) => { 23 event = e; 24 }; 25 // should be able to listen and unlisten 26 tree.on(handler, ["some", "event"]); 27 tree.off(handler, ["some", "event"]); 28 29 // // shouldn't get an event 30 const update = new Map([["some", new Map([["event", 1]])]]); 31 tree.emit("something"); 32 assertEquals(event, undefined, "Event should not be dispatched"); 33 34 tree.on(handler, ["some", "event"]); 35 36 let parentEvent; 37 let calls = 0; 38 tree.on((e) => { 39 calls++; 40 parentEvent = e; 41 }); 42 43 tree.emit(update); 44 assertEquals(event, 1, "Event should be dispatched"); 45 assertEquals(parentEvent, update, "Event should be dispatched"); 46 47 tree.emit(update); 48 assertEquals( 49 calls, 50 1, 51 "Event should not be dispatched again if the value is the same", 52 ); 53 tree.off(handler, ["some", "event"]); 54 const update2 = new Map([["some", new Map([["event", 2]])]]); 55 tree.once((e) => { 56 assertEquals(e, update2.get("some")); 57 }, ["some"]); 58 tree.emit(update2); 59 assertEquals(event, 1, "off should work"); 60 assertEquals(parentEvent, update2); 61 const update3 = new Map([["some", new Map([["event", 3]])]]); 62 // makes sure once work once 63 tree.emit(update3); 64 }); 65 66 // await t.step("meta: subscription observations", () => { 67 // const tree = new Tree<string>(); 68 // let subArray; 69 // tree.meta.on((e) => { 70 // subArray = e; 71 // }); 72 // let _val; 73 // const func = (e: string) => { 74 // _val = e; 75 // }; 76 // const _f = tree.on(["a", "b", "c", "d", "e", "f"], func); 77 // assertEquals(subArray, [{ 78 // subscribe: true, 79 // path: ["a", "b", "c", "d", "e", "f"], 80 // }]); 81 82 // tree.on(["a", "b"], func); 83 // assertEquals(subArray, [ 84 // { subscribe: true, path: ["a", "b"] }, 85 // { 86 // subscribe: false, 87 // path: ["a", "b", "c", "d", "e", "f"], 88 // }, 89 // ]); 90 91 // tree.off(["a", "b"], func); 92 93 // assertEquals(subArray, [ 94 // { subscribe: false, path: ["a", "b"] }, 95 // { 96 // subscribe: true, 97 // path: ["a", "b", "c", "d", "e", "f"], 98 // }, 99 // ]); 100 101 // f.off(func); 102 // assertEquals(subArray, [ 103 // { 104 // subscribe: false, 105 // path: ["a", "b", "c", "d", "e", "f"], 106 // }, 107 // ]); 108 109 // tree.emit(["a", "b", "c", "d", "e", "f"], "test"); 110 // assertEquals(val, undefined, "Event should not be dispatched"); 111 // }); 112 });