|
1 | 1 | import ComponentRegistry from "../ComponentRegistry.ts"; |
2 | | -import { Body, PositionOnScreen } from "../fixtures.ts"; |
| 2 | +import { Body, Keyboard, PositionOnScreen } from "../fixtures.ts"; |
| 3 | +import { afterAll, afterEach, describe } from "vitest"; |
3 | 4 |
|
4 | 5 | describe("ComponentRegistry", () => { |
5 | | - it("constructor", () => { |
6 | | - const reg = ComponentRegistry.getInstance(); |
7 | | - expect(reg).toBeInstanceOf(ComponentRegistry); |
| 6 | + afterEach(() => { |
| 7 | + ComponentRegistry.getInstance().reset(); |
8 | 8 | }); |
9 | 9 |
|
10 | | - it("registerComponent", () => { |
11 | | - const reg = ComponentRegistry.getInstance(); |
12 | | - expect(reg).toBeInstanceOf(ComponentRegistry); |
| 10 | + describe("components", () => { |
| 11 | + it("constructor", () => { |
| 12 | + const reg = ComponentRegistry.getInstance(); |
| 13 | + expect(reg).toBeInstanceOf(ComponentRegistry); |
| 14 | + }); |
13 | 15 |
|
14 | | - reg.registerComponent(Body); |
15 | | - reg.registerComponent(PositionOnScreen); |
| 16 | + it("registerComponent", () => { |
| 17 | + const reg = ComponentRegistry.getInstance(); |
| 18 | + expect(reg).toBeInstanceOf(ComponentRegistry); |
16 | 19 |
|
17 | | - expect(reg.getLastBitmask()).toBe(4n); |
| 20 | + reg.registerComponent(Body); |
| 21 | + reg.registerComponent(PositionOnScreen); |
18 | 22 |
|
19 | | - const body1 = new Body({ width: 10, height: 20 }); |
20 | | - const body2 = new Body({ width: 30, height: 40 }); |
21 | | - const body3 = new Body({ width: 50, height: 60 }); |
| 23 | + expect(reg.getLastBitmask()).toBe(4n); |
22 | 24 |
|
23 | | - const position1 = new PositionOnScreen({ x: 1, y: 2 }); |
24 | | - const position2 = new PositionOnScreen({ x: 3, y: 4 }); |
25 | | - const position3 = new PositionOnScreen({ x: 5, y: 6 }); |
| 25 | + const body1 = new Body({ width: 10, height: 20 }); |
| 26 | + const body2 = new Body({ width: 30, height: 40 }); |
| 27 | + const body3 = new Body({ width: 50, height: 60 }); |
26 | 28 |
|
27 | | - expect(body1.bitmask).toBe(2n); |
28 | | - expect(body2.bitmask).toBe(2n); |
29 | | - expect(body3.bitmask).toBe(2n); |
| 29 | + const position1 = new PositionOnScreen({ x: 1, y: 2 }); |
| 30 | + const position2 = new PositionOnScreen({ x: 3, y: 4 }); |
| 31 | + const position3 = new PositionOnScreen({ x: 5, y: 6 }); |
30 | 32 |
|
31 | | - expect(position1.bitmask).toBe(4n); |
32 | | - expect(position2.bitmask).toBe(4n); |
33 | | - expect(position3.bitmask).toBe(4n); |
| 33 | + expect(body1.bitmask).toBe(2n); |
| 34 | + expect(body2.bitmask).toBe(2n); |
| 35 | + expect(body3.bitmask).toBe(2n); |
34 | 36 |
|
35 | | - // Extra safety check that the properties of the class were successfully set. |
36 | | - expect(body1.properties.width).toBe(10); |
37 | | - expect(body1.properties.height).toBe(20); |
| 37 | + expect(position1.bitmask).toBe(4n); |
| 38 | + expect(position2.bitmask).toBe(4n); |
| 39 | + expect(position3.bitmask).toBe(4n); |
38 | 40 |
|
39 | | - expect(body2.properties.width).toBe(30); |
40 | | - expect(body2.properties.height).toBe(40); |
| 41 | + // Extra safety check that the properties of the class were successfully set. |
| 42 | + expect(body1.properties.width).toBe(10); |
| 43 | + expect(body1.properties.height).toBe(20); |
41 | 44 |
|
42 | | - expect(body3.properties.width).toBe(50); |
43 | | - expect(body3.properties.height).toBe(60); |
| 45 | + expect(body2.properties.width).toBe(30); |
| 46 | + expect(body2.properties.height).toBe(40); |
44 | 47 |
|
45 | | - expect(position1.properties.x).toBe(1); |
46 | | - expect(position1.properties.y).toBe(2); |
| 48 | + expect(body3.properties.width).toBe(50); |
| 49 | + expect(body3.properties.height).toBe(60); |
47 | 50 |
|
48 | | - expect(position2.properties.x).toBe(3); |
49 | | - expect(position2.properties.y).toBe(4); |
| 51 | + expect(position1.properties.x).toBe(1); |
| 52 | + expect(position1.properties.y).toBe(2); |
50 | 53 |
|
51 | | - expect(position3.properties.x).toBe(5); |
52 | | - expect(position3.properties.y).toBe(6); |
53 | | - }); |
| 54 | + expect(position2.properties.x).toBe(3); |
| 55 | + expect(position2.properties.y).toBe(4); |
54 | 56 |
|
55 | | - it("registerComponents", () => { |
56 | | - const reg = ComponentRegistry.getInstance(); |
57 | | - reg.registerComponents([Body, PositionOnScreen]); |
| 57 | + expect(position3.properties.x).toBe(5); |
| 58 | + expect(position3.properties.y).toBe(6); |
| 59 | + }); |
58 | 60 |
|
59 | | - expect(reg.getComponent("Body")).toBe(Body); |
60 | | - expect(reg.getComponent("PositionOnScreen")).toBe(PositionOnScreen); |
61 | | - }); |
| 61 | + it("registerComponents", () => { |
| 62 | + const reg = ComponentRegistry.getInstance(); |
| 63 | + reg.registerComponents([Body, PositionOnScreen]); |
| 64 | + |
| 65 | + expect(reg.getComponent("Body")).toBe(Body); |
| 66 | + expect(reg.getComponent("PositionOnScreen")).toBe(PositionOnScreen); |
| 67 | + }); |
62 | 68 |
|
63 | | - it("getComponent", () => { |
64 | | - const reg = ComponentRegistry.getInstance(); |
65 | | - reg.registerComponent(Body); |
66 | | - reg.registerComponent(PositionOnScreen); |
| 69 | + it("getComponent", () => { |
| 70 | + const reg = ComponentRegistry.getInstance(); |
| 71 | + reg.registerComponent(Body); |
| 72 | + reg.registerComponent(PositionOnScreen); |
| 73 | + |
| 74 | + expect(reg.getComponent("Body")).toBe(Body); |
| 75 | + expect(reg.getComponent("PositionOnScreen")).toBe(PositionOnScreen); |
| 76 | + }); |
| 77 | + }); |
67 | 78 |
|
68 | | - expect(reg.getComponent("Body")).toBe(Body); |
69 | | - expect(reg.getComponent("PositionOnScreen")).toBe(PositionOnScreen); |
| 79 | + describe("component groups", () => { |
| 80 | + it("getComponentGroup", () => { |
| 81 | + const reg = ComponentRegistry.getInstance(); |
| 82 | + |
| 83 | + reg.registerComponent(Body); |
| 84 | + reg.registerComponent(PositionOnScreen); |
| 85 | + reg.registerComponent(Keyboard); |
| 86 | + |
| 87 | + reg.registerComponentGroup("group1", [Body, PositionOnScreen]); |
| 88 | + reg.registerComponentGroup("group2", [Keyboard]); |
| 89 | + |
| 90 | + expect(reg.getComponentGroup("group0")).toBeUndefined(); |
| 91 | + expect(reg.getComponentGroup("group1")).toEqual( |
| 92 | + expect.objectContaining({ |
| 93 | + bitmask: 6n, |
| 94 | + components: [Body, PositionOnScreen], |
| 95 | + }), |
| 96 | + ); |
| 97 | + expect(reg.getComponentGroup("group2")).toEqual( |
| 98 | + expect.objectContaining({ |
| 99 | + bitmask: 8n, |
| 100 | + components: [Keyboard], |
| 101 | + }), |
| 102 | + ); |
| 103 | + }); |
| 104 | + |
| 105 | + it("getComponentGroupName", () => { |
| 106 | + const reg = ComponentRegistry.getInstance(); |
| 107 | + |
| 108 | + reg.registerComponent(Body); |
| 109 | + reg.registerComponentGroup("group1", [Body]); |
| 110 | + |
| 111 | + const a = reg.getComponentGroup("group1"); |
| 112 | + |
| 113 | + expect(reg.getComponentGroupName(1n)).toBeUndefined(); |
| 114 | + expect(reg.getComponentGroupName(2n)).toEqual("group1"); |
| 115 | + }); |
70 | 116 | }); |
71 | 117 | }); |
0 commit comments