|
1 | | -import { describe, it, expect, vi } from 'vitest'; |
2 | | -import { Base, BaseConfig, BaseProps, getInstanceFromElement } from '@studiometa/js-toolkit'; |
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { |
| 3 | + Base, |
| 4 | + BaseConfig, |
| 5 | + BaseProps, |
| 6 | + getInstanceFromElement, |
| 7 | + withExtraConfig, |
| 8 | + withName, |
| 9 | +} from '@studiometa/js-toolkit'; |
3 | 10 | import { h } from '#test-utils'; |
4 | 11 |
|
5 | 12 | let mockedIsDev = true; |
@@ -36,6 +43,10 @@ async function getContext() { |
36 | 43 | return { Foo, element, foo }; |
37 | 44 | } |
38 | 45 |
|
| 46 | +beforeEach(() => { |
| 47 | + globalThis['__JS_TOOLKIT_REGISTRY__'] = new Map(); |
| 48 | +}); |
| 49 | + |
39 | 50 | describe('The abstract Base class', () => { |
40 | 51 | it('must be extended', () => { |
41 | 52 | expect(() => { |
@@ -349,6 +360,44 @@ describe('A Base instance methods', () => { |
349 | 360 | expect(fn).toHaveBeenNthCalledWith(2, 'method'); |
350 | 361 | }); |
351 | 362 |
|
| 363 | + it('should register itself on instantiation', async () => { |
| 364 | + const TestComponent = withName(Base, 'TestComponent'); |
| 365 | + const registry = globalThis.__JS_TOOLKIT_REGISTRY__; |
| 366 | + const component = new TestComponent(h('div')); |
| 367 | + |
| 368 | + expect(registry.has('TestComponent')).toBe(true); |
| 369 | + expect(registry.get('TestComponent')).toBe(TestComponent); |
| 370 | + |
| 371 | + registry.delete('TestComponent'); |
| 372 | + }); |
| 373 | + |
| 374 | + it('should register sync configured children components on instantiation', async () => { |
| 375 | + const Child = withName(Base, 'Child'); |
| 376 | + const Parent = withExtraConfig(Base, { |
| 377 | + name: 'Parent', |
| 378 | + components: { |
| 379 | + Child, |
| 380 | + div: Child, |
| 381 | + AsyncChild: () => new Promise(() => {}), |
| 382 | + }, |
| 383 | + }); |
| 384 | + |
| 385 | + const registry = globalThis.__JS_TOOLKIT_REGISTRY__; |
| 386 | + const component = new Parent(h('div')); |
| 387 | + |
| 388 | + // Check that it's in the registry |
| 389 | + expect(registry.has('Parent')).toBe(true); |
| 390 | + expect(registry.get('Parent')).toBe(Parent); |
| 391 | + |
| 392 | + expect(registry.has('Child')).toBe(true); |
| 393 | + expect(registry.get('Child')).toBe(Child); |
| 394 | + |
| 395 | + expect(registry.has('div')).toBe(true); |
| 396 | + expect(registry.get('div')).toBe(Child); |
| 397 | + |
| 398 | + expect(registry.has('AsyncChild')).toBe(false); |
| 399 | + }); |
| 400 | + |
352 | 401 | it('should not find children if none provided', async () => { |
353 | 402 | const { foo } = await getContext(); |
354 | 403 |
|
|
0 commit comments