Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/firebird-ng/src/app/model/event-group.spec.ts is written in an unsupported language. File is not indexed.

0001 // event-group.spec.ts
0002 
0003 import {
0004   EventGroup,
0005   EventGroupFactory,
0006   registerEventGroupFactory,
0007   getEventGroupFactory,
0008   _resetEventGroupRegistry,
0009 } from './event-group';
0010 
0011 describe('EventGroup', () => {
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 EventGroup('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       (EventGroup 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 EventGroupFactory {
0032     type: string = 'TestType';
0033 
0034     fromDexObject(obj: any): EventGroup {
0035       return new TestEventGroup(obj['name'], obj['origin']);
0036     }
0037   }
0038 
0039   // Define TestEventGroup class extending EventGroup
0040   class TestEventGroup extends EventGroup {
0041     override get timeRange(): [number, number] | null {
0042         return [0, 100]
0043     }
0044     constructor(name: string, origin?: string) {
0045       super(name, 'TestType', origin);
0046     }
0047 
0048     toDexObject(): any {
0049       return {
0050         name: this.name,
0051         type: this.type,
0052         origin: this.origin,
0053       };
0054     }
0055   }
0056 
0057   beforeEach(() => {
0058     // Reset the component registry before each test
0059     _resetEventGroupRegistry();
0060   });
0061 
0062   it('should register and retrieve component factories correctly', () => {
0063     const factory = new TestComponentFactory();
0064 
0065     // Register the factory
0066     registerEventGroupFactory(factory);
0067 
0068     // Retrieve the factory
0069     const retrievedFactory = getEventGroupFactory('TestType');
0070 
0071     expect(retrievedFactory).toBeDefined();
0072     expect(retrievedFactory).toBe(factory);
0073   });
0074 
0075   it('should return undefined for unregistered component types', () => {
0076     const retrievedFactory = getEventGroupFactory('UnknownType');
0077 
0078     expect(retrievedFactory).toBeUndefined();
0079   });
0080 
0081   it('should overwrite existing factory when registering a factory with the same type', () => {
0082     const factory1 = new TestComponentFactory();
0083     const factory2 = new TestComponentFactory();
0084 
0085     // Register the first factory
0086     registerEventGroupFactory(factory1);
0087 
0088     // Register the second factory with the same type
0089     registerEventGroupFactory(factory2);
0090 
0091     // Retrieve the factory
0092     const retrievedFactory = getEventGroupFactory('TestType');
0093 
0094     expect(retrievedFactory).toBe(factory2);
0095   });
0096 
0097   it('should use the correct factory to create component instances', () => {
0098     const factory = new TestComponentFactory();
0099     registerEventGroupFactory(factory);
0100 
0101     const dexObject = {
0102       name: 'TestEventGroup',
0103       type: 'TestType',
0104       origin: 'TestOrigin',
0105     };
0106 
0107     const retrievedFactory = getEventGroupFactory('TestType');
0108     expect(retrievedFactory).toBeDefined();
0109 
0110     const group = retrievedFactory!.fromDexObject(dexObject);
0111     expect(group).toBeInstanceOf(EventGroup);
0112     expect(group.name).toBe('TestEventGroup');
0113     expect(group.type).toBe('TestType');
0114     expect(group.origin).toBe('TestOrigin');
0115   });
0116 });