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 { EventGroup, EventGroupFactory, registerEventGroupFactory, getEventGroupFactory, _resetEventGroupRegistry, } from './event-group';
0004 
0005 describe('EventGroup', () => {
0006     it('should be defined as an abstract class', () => {
0007         // EventGroup is an abstract class that serves as a base for all event components.
0008         // TypeScript prevents direct instantiation at compile time.
0009         // This test verifies the class is properly defined and exported.
0010         expect(EventGroup).toBeDefined();
0011         expect(typeof EventGroup).toBe('function');
0012     });
0013 });
0014 
0015 describe('Component Registry', () => {
0016     // Define a TestComponentFactory for testing
0017     class TestComponentFactory implements EventGroupFactory {
0018         type: string = 'TestType';
0019 
0020         fromDexObject(obj: any): EventGroup {
0021             return new TestEventGroup(obj['name'], obj['origin']);
0022         }
0023     }
0024 
0025     // Define TestEventGroup class extending EventGroup
0026     class TestEventGroup extends EventGroup {
0027         override get timeRange(): [
0028             number,
0029             number
0030         ] | null {
0031             return [0, 100];
0032         }
0033         constructor(name: string, origin?: string) {
0034             super(name, 'TestType', origin);
0035         }
0036 
0037         toDexObject(): any {
0038             return {
0039                 name: this.name,
0040                 type: this.type,
0041                 origin: this.origin,
0042             };
0043         }
0044     }
0045 
0046     beforeEach(() => {
0047         // Reset the component registry before each test
0048         _resetEventGroupRegistry();
0049     });
0050 
0051     it('should register and retrieve component factories correctly', () => {
0052         const factory = new TestComponentFactory();
0053 
0054         // Register the factory
0055         registerEventGroupFactory(factory);
0056 
0057         // Retrieve the factory
0058         const retrievedFactory = getEventGroupFactory('TestType');
0059 
0060         expect(retrievedFactory).toBeDefined();
0061         expect(retrievedFactory).toBe(factory);
0062     });
0063 
0064     it('should return undefined for unregistered component types', () => {
0065         const retrievedFactory = getEventGroupFactory('UnknownType');
0066 
0067         expect(retrievedFactory).toBeUndefined();
0068     });
0069 
0070     it('should overwrite existing factory when registering a factory with the same type', () => {
0071         const factory1 = new TestComponentFactory();
0072         const factory2 = new TestComponentFactory();
0073 
0074         // Register the first factory
0075         registerEventGroupFactory(factory1);
0076 
0077         // Register the second factory with the same type
0078         registerEventGroupFactory(factory2);
0079 
0080         // Retrieve the factory
0081         const retrievedFactory = getEventGroupFactory('TestType');
0082 
0083         expect(retrievedFactory).toBe(factory2);
0084     });
0085 
0086     it('should use the correct factory to create component instances', () => {
0087         const factory = new TestComponentFactory();
0088         registerEventGroupFactory(factory);
0089 
0090         const dexObject = {
0091             name: 'TestEventGroup',
0092             type: 'TestType',
0093             origin: 'TestOrigin',
0094         };
0095 
0096         const retrievedFactory = getEventGroupFactory('TestType');
0097         expect(retrievedFactory).toBeDefined();
0098 
0099         const group = retrievedFactory!.fromDexObject(dexObject);
0100         expect(group).toBeInstanceOf(EventGroup);
0101         expect(group.name).toBe('TestEventGroup');
0102         expect(group.type).toBe('TestType');
0103         expect(group.origin).toBe('TestOrigin');
0104     });
0105 });