|
| 1 | +import { BasePattern } from "./basepattern"; |
| 2 | +import registry from "./registry"; |
| 3 | +import utils from "./utils"; |
| 4 | +import { jest } from "@jest/globals"; |
| 5 | + |
| 6 | +describe("Basepattern class tests", function () { |
| 7 | + const patterns = registry.patterns; |
| 8 | + |
| 9 | + beforeEach(function () { |
| 10 | + registry.clear(); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(function () { |
| 14 | + registry.patterns = patterns; |
| 15 | + jest.restoreAllMocks(); |
| 16 | + }); |
| 17 | + |
| 18 | + it("1 - Trigger and name are statically available on the class.", async function () { |
| 19 | + class Pat extends BasePattern { |
| 20 | + static name = "example"; |
| 21 | + static trigger = ".example"; |
| 22 | + } |
| 23 | + |
| 24 | + // trigger and name are static and available on the class itself |
| 25 | + expect(Pat.trigger).toBe(".example"); |
| 26 | + expect(Pat.name).toBe("example"); |
| 27 | + |
| 28 | + const el = document.createElement("div"); |
| 29 | + |
| 30 | + const pat = new Pat(el); |
| 31 | + await utils.timeout(1); |
| 32 | + |
| 33 | + expect(pat.name).toEqual("example"); |
| 34 | + expect(pat.trigger).toEqual(".example"); |
| 35 | + }); |
| 36 | + |
| 37 | + it("2 - Base pattern is class based and does inheritance, polymorphism, encapsulation, ... pt1", async function () { |
| 38 | + class Pat1 extends BasePattern { |
| 39 | + some = "thing"; |
| 40 | + } |
| 41 | + class Pat2 extends Pat1 { |
| 42 | + init() { |
| 43 | + this.extra(); |
| 44 | + } |
| 45 | + extra() { |
| 46 | + this.some = "other"; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + const el1 = document.createElement("div"); |
| 51 | + const el2 = document.createElement("div"); |
| 52 | + |
| 53 | + const pat1 = new Pat1(el1); |
| 54 | + await utils.timeout(1); |
| 55 | + |
| 56 | + const pat2 = new Pat2(el2); |
| 57 | + await utils.timeout(1); |
| 58 | + |
| 59 | + expect(pat1 instanceof Pat1).toBe(true); |
| 60 | + expect(pat1 instanceof Pat2).toBe(false); |
| 61 | + expect(pat1.el).toEqual(el1); |
| 62 | + expect(pat1.some).toEqual("thing"); |
| 63 | + |
| 64 | + expect(pat2 instanceof Pat1).toBe(true); |
| 65 | + expect(pat2 instanceof Pat2).toBe(true); |
| 66 | + expect(pat2.el).toEqual(el2); |
| 67 | + expect(pat2.some).toEqual("other"); |
| 68 | + }); |
| 69 | + |
| 70 | + it("3 - can be extended multiple times", async function () { |
| 71 | + class Pat1 extends BasePattern { |
| 72 | + static name = "example1"; |
| 73 | + something = "else"; |
| 74 | + init() {} |
| 75 | + } |
| 76 | + class Pat2 extends Pat1 { |
| 77 | + static name = "example2"; |
| 78 | + some = "thing2"; |
| 79 | + init() {} |
| 80 | + } |
| 81 | + class Pat3 extends Pat2 { |
| 82 | + static name = "example3"; |
| 83 | + some = "thing3"; |
| 84 | + init() {} |
| 85 | + } |
| 86 | + |
| 87 | + const el = document.createElement("div"); |
| 88 | + const pat1 = new Pat1(el); |
| 89 | + await utils.timeout(1); |
| 90 | + const pat2 = new Pat2(el); |
| 91 | + await utils.timeout(1); |
| 92 | + const pat3 = new Pat3(el); |
| 93 | + await utils.timeout(1); |
| 94 | + |
| 95 | + expect(pat1.name).toEqual("example1"); |
| 96 | + expect(pat1.something).toEqual("else"); |
| 97 | + expect(pat1.some).toEqual(undefined); |
| 98 | + expect(pat1 instanceof BasePattern).toBeTruthy(); |
| 99 | + expect(pat1 instanceof Pat2).toBeFalsy(); |
| 100 | + expect(pat1 instanceof Pat3).toBeFalsy(); |
| 101 | + |
| 102 | + expect(pat2.name).toEqual("example2"); |
| 103 | + expect(pat2.something).toEqual("else"); |
| 104 | + expect(pat2.some).toEqual("thing2"); |
| 105 | + expect(pat2 instanceof BasePattern).toBeTruthy(); |
| 106 | + expect(pat2 instanceof Pat1).toBeTruthy(); |
| 107 | + expect(pat2 instanceof Pat3).toBeFalsy(); |
| 108 | + |
| 109 | + expect(pat3.name).toEqual("example3"); |
| 110 | + expect(pat3.something).toEqual("else"); |
| 111 | + expect(pat3.some).toEqual("thing3"); |
| 112 | + expect(pat3 instanceof BasePattern).toBeTruthy(); |
| 113 | + expect(pat3 instanceof Pat1).toBeTruthy(); |
| 114 | + expect(pat3 instanceof Pat2).toBeTruthy(); |
| 115 | + }); |
| 116 | + |
| 117 | + it("4 - The pattern instance is stored on the element itself.", async function () { |
| 118 | + class Pat extends BasePattern { |
| 119 | + static name = "example"; |
| 120 | + } |
| 121 | + |
| 122 | + const el = document.createElement("div"); |
| 123 | + const pat = new Pat(el); |
| 124 | + await utils.timeout(1); |
| 125 | + |
| 126 | + expect(el["pattern-example"]).toEqual(pat); |
| 127 | + }); |
| 128 | + |
| 129 | + it("5 - Registers with the registry.", async function () { |
| 130 | + class Pat extends BasePattern { |
| 131 | + static name = "example"; |
| 132 | + static trigger = ".example"; |
| 133 | + } |
| 134 | + |
| 135 | + registry.register(Pat); |
| 136 | + |
| 137 | + const el = document.createElement("div"); |
| 138 | + el.classList.add("example"); |
| 139 | + |
| 140 | + registry.scan(el); |
| 141 | + await utils.timeout(1); |
| 142 | + |
| 143 | + // gh-copilot wrote this line. |
| 144 | + expect(el["pattern-example"]).toBeInstanceOf(Pat); |
| 145 | + }); |
| 146 | +}); |
0 commit comments