p-elements-core 1.2.32-rc1 → 1.2.32-rc11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,226 @@
1
+ /**
2
+ * Tests for CustomElementController
3
+ */
4
+
5
+ import { describe, it, expect } from 'vitest';
6
+ import './test-setup.js';
7
+ import { CustomElement } from './custom-element.js';
8
+ import { CustomElementController } from './custom-element-controller.js';
9
+ import { customElementConfig } from './decorators/custom-element-config.js';
10
+ import { generateUniqueTagName } from './test-setup.js';
11
+ import { waitForRender } from './test-utils.js';
12
+
13
+ describe('CustomElementController', () => {
14
+ describe('Controller lifecycle', () => {
15
+ it('should register controller with host element', async () => {
16
+ const tagName = generateUniqueTagName('controller-test');
17
+ let controller: TestController | null = null;
18
+
19
+ class TestController extends CustomElementController {
20
+ testMethod() {
21
+ return 'test';
22
+ }
23
+ }
24
+
25
+ @customElementConfig({ tagName })
26
+ class ControllerTest extends CustomElement {
27
+ controller: TestController;
28
+
29
+ init() {
30
+ this.controller = new TestController(this);
31
+ }
32
+
33
+ render() {
34
+ return { vnodeSelector: 'div', properties: {}, children: [], text: undefined, domNode: null };
35
+ }
36
+ }
37
+
38
+ const el = document.createElement(tagName) as ControllerTest;
39
+ document.body.appendChild(el);
40
+ await waitForRender(el);
41
+
42
+ expect(el.controller).toBeDefined();
43
+ expect(el.controller.hostElement).toBe(el);
44
+ expect(el.controller.testMethod()).toBe('test');
45
+
46
+ document.body.removeChild(el);
47
+ });
48
+
49
+ it('should call controller.connected() on element connection', async () => {
50
+ const tagName = generateUniqueTagName('controller-test');
51
+ let connectedCalled = false;
52
+
53
+ class TestController extends CustomElementController {
54
+ connected() {
55
+ connectedCalled = true;
56
+ }
57
+ }
58
+
59
+ @customElementConfig({ tagName })
60
+ class ControllerTest extends CustomElement {
61
+ init() {
62
+ new TestController(this);
63
+ }
64
+
65
+ render() {
66
+ return { vnodeSelector: 'div', properties: {}, children: [], text: undefined, domNode: null };
67
+ }
68
+ }
69
+
70
+ const el = document.createElement(tagName) as ControllerTest;
71
+ expect(connectedCalled).toBe(false);
72
+
73
+ document.body.appendChild(el);
74
+ await waitForRender(el);
75
+
76
+ expect(connectedCalled).toBe(true);
77
+ document.body.removeChild(el);
78
+ });
79
+
80
+ it('should call controller.disconnected() on element disconnection', async () => {
81
+ const tagName = generateUniqueTagName('controller-test');
82
+ let disconnectedCalled = false;
83
+
84
+ class TestController extends CustomElementController {
85
+ disconnected() {
86
+ disconnectedCalled = true;
87
+ }
88
+ }
89
+
90
+ @customElementConfig({ tagName })
91
+ class ControllerTest extends CustomElement {
92
+ static style = ':host { display: block; }';
93
+
94
+ init() {
95
+ new TestController(this);
96
+ }
97
+
98
+ render() {
99
+ return { vnodeSelector: 'div', properties: {}, children: [], text: undefined, domNode: null };
100
+ }
101
+ }
102
+
103
+ const el = document.createElement(tagName) as ControllerTest;
104
+ document.body.appendChild(el);
105
+ await waitForRender(el);
106
+
107
+ expect(disconnectedCalled).toBe(false);
108
+
109
+ document.body.removeChild(el);
110
+ expect(disconnectedCalled).toBe(true);
111
+ });
112
+
113
+ it('should trigger render when controller calls renderNow()', async () => {
114
+ const tagName = generateUniqueTagName('controller-test');
115
+ let renderCount = 0;
116
+
117
+ class TestController extends CustomElementController {
118
+ triggerRender() {
119
+ this.renderNow();
120
+ }
121
+ }
122
+
123
+ @customElementConfig({ tagName })
124
+ class ControllerTest extends CustomElement {
125
+ static style = ':host { display: block; }';
126
+ controller: TestController;
127
+
128
+ init() {
129
+ this.controller = new TestController(this);
130
+ }
131
+
132
+ render() {
133
+ renderCount++;
134
+ return { vnodeSelector: 'div', properties: {}, children: [], text: undefined, domNode: null };
135
+ }
136
+ }
137
+
138
+ const el = document.createElement(tagName) as ControllerTest;
139
+ document.body.appendChild(el);
140
+ await waitForRender(el);
141
+
142
+ const initialRenderCount = renderCount;
143
+ el.controller.triggerRender();
144
+ await waitForRender(el);
145
+
146
+ expect(renderCount).toBeGreaterThan(initialRenderCount);
147
+ document.body.removeChild(el);
148
+ });
149
+
150
+ it('should schedule render when controller calls scheduleRender()', async () => {
151
+ const tagName = generateUniqueTagName('controller-test');
152
+ let renderCount = 0;
153
+
154
+ class TestController extends CustomElementController {
155
+ triggerSchedule() {
156
+ this.scheduleRender();
157
+ }
158
+ }
159
+
160
+ @customElementConfig({ tagName })
161
+ class ControllerTest extends CustomElement {
162
+ static style = ':host { display: block; }';
163
+ controller: TestController;
164
+
165
+ init() {
166
+ this.controller = new TestController(this);
167
+ }
168
+
169
+ render() {
170
+ renderCount++;
171
+ return { vnodeSelector: 'div', properties: {}, children: [], text: undefined, domNode: null };
172
+ }
173
+ }
174
+
175
+ const el = document.createElement(tagName) as ControllerTest;
176
+ document.body.appendChild(el);
177
+ await waitForRender(el);
178
+
179
+ const initialRenderCount = renderCount;
180
+ el.controller.triggerSchedule();
181
+ await waitForRender(el);
182
+
183
+ expect(renderCount).toBeGreaterThan(initialRenderCount);
184
+ document.body.removeChild(el);
185
+ });
186
+
187
+ it('should call hostRenderStart and hostRenderDone hooks', async () => {
188
+ const tagName = generateUniqueTagName('controller-test');
189
+ let startCalled = false;
190
+ let doneCalled = false;
191
+
192
+ class TestController extends CustomElementController {
193
+ hostRenderStart() {
194
+ startCalled = true;
195
+ }
196
+
197
+ hostRenderDone() {
198
+ doneCalled = true;
199
+ }
200
+ }
201
+
202
+ @customElementConfig({ tagName })
203
+ class ControllerTest extends CustomElement {
204
+ static style = ':host { display: block; }';
205
+
206
+ constructor() {
207
+ super();
208
+ new TestController(this);
209
+ }
210
+
211
+ render() {
212
+ return { vnodeSelector: 'div', properties: {}, children: [], text: undefined, domNode: null };
213
+ }
214
+ }
215
+
216
+ const el = document.createElement(tagName) as ControllerTest;
217
+ document.body.appendChild(el);
218
+ await waitForRender(el);
219
+
220
+ expect(startCalled).toBe(true);
221
+ expect(doneCalled).toBe(true);
222
+
223
+ document.body.removeChild(el);
224
+ });
225
+ });
226
+ });