Warning, /firebird/firebird-ng/src/app/model/entry-component.spec.ts is written in an unsupported language. File is not indexed.
0001 // entry-component.spec.ts
0002
0003 import {
0004 EntryComponent,
0005 EntryComponentFactory,
0006 registerComponentFactory,
0007 getComponentFactory,
0008 _resetComponentRegistry,
0009 } from './entry-component';
0010
0011 describe('EntryComponent', () => {
0012 it('should not allow instantiation of abstract class', () => {
0013 // Attempting to instantiate an abstract class should result in a compile-time error.
0014 // This test ensures that the class is abstract by design.
0015
0016 // Uncommenting the following line should cause a TypeScript error.
0017 // const component = new EntryComponent('name', 'type');
0018
0019 // Since TypeScript prevents instantiation of abstract classes at compile time,
0020 // we can simulate this in the test by checking that an error is thrown.
0021
0022 expect(() => {
0023 // Force a runtime check by attempting to instantiate via casting.
0024 (EntryComponent as any).call(null, 'name', 'type');
0025 }).toThrowError();
0026 });
0027 });
0028
0029 describe('Component Registry', () => {
0030 // Define a TestComponentFactory for testing
0031 class TestComponentFactory implements EntryComponentFactory {
0032 type: string = 'TestType';
0033
0034 fromDexObject(obj: any): EntryComponent {
0035 return new TestComponent(obj['name'], obj['originType']);
0036 }
0037 }
0038
0039 // Define TestComponent class extending EntryComponent
0040 class TestComponent extends EntryComponent {
0041 constructor(name: string, originType?: string) {
0042 super(name, 'TestType', originType);
0043 }
0044
0045 toDexObject(): any {
0046 return {
0047 name: this.name,
0048 type: this.type,
0049 originType: this.originType,
0050 };
0051 }
0052 }
0053
0054 beforeEach(() => {
0055 // Reset the component registry before each test
0056 _resetComponentRegistry();
0057 });
0058
0059 it('should register and retrieve component factories correctly', () => {
0060 const factory = new TestComponentFactory();
0061
0062 // Register the factory
0063 registerComponentFactory(factory);
0064
0065 // Retrieve the factory
0066 const retrievedFactory = getComponentFactory('TestType');
0067
0068 expect(retrievedFactory).toBeDefined();
0069 expect(retrievedFactory).toBe(factory);
0070 });
0071
0072 it('should return undefined for unregistered component types', () => {
0073 const retrievedFactory = getComponentFactory('UnknownType');
0074
0075 expect(retrievedFactory).toBeUndefined();
0076 });
0077
0078 it('should overwrite existing factory when registering a factory with the same type', () => {
0079 const factory1 = new TestComponentFactory();
0080 const factory2 = new TestComponentFactory();
0081
0082 // Register the first factory
0083 registerComponentFactory(factory1);
0084
0085 // Register the second factory with the same type
0086 registerComponentFactory(factory2);
0087
0088 // Retrieve the factory
0089 const retrievedFactory = getComponentFactory('TestType');
0090
0091 expect(retrievedFactory).toBe(factory2);
0092 });
0093
0094 it('should use the correct factory to create component instances', () => {
0095 const factory = new TestComponentFactory();
0096 registerComponentFactory(factory);
0097
0098 const dexObject = {
0099 name: 'TestComponent',
0100 type: 'TestType',
0101 originType: 'TestOrigin',
0102 };
0103
0104 const retrievedFactory = getComponentFactory('TestType');
0105 expect(retrievedFactory).toBeDefined();
0106
0107 const component = retrievedFactory!.fromDexObject(dexObject);
0108 expect(component).toBeInstanceOf(EntryComponent);
0109 expect(component.name).toBe('TestComponent');
0110 expect(component.type).toBe('TestType');
0111 expect(component.originType).toBe('TestOrigin');
0112 });
0113 });