diff --git a/packages/core/src/commands/index.ts b/packages/core/src/commands/index.ts index dfd919958..f82143223 100644 --- a/packages/core/src/commands/index.ts +++ b/packages/core/src/commands/index.ts @@ -279,7 +279,6 @@ export default class CommandsModule extends Module { describe('Main', () => { @@ -170,6 +171,48 @@ describe('Commands', () => { result = obj.run(commName, customOptions); expect(result).toEqual({ ...customOptions, ...defaultOptions }); }); + + test('Command constructor aliases keep independent ids and events', () => { + class SharedCommand extends CommandAbstract { + run() { + return commResultRun; + } + + stop() { + return commResultStop; + } + } + + const runSpy = jest.fn(); + const stopSpy = jest.fn(); + + obj.add('core:test', SharedCommand); + obj.add('test', SharedCommand); + + expect(obj.get('core:test')?.id).toBe('core:test'); + expect(obj.get('test')?.id).toBe('test'); + + em.on('command:run:core:test', runSpy); + em.on('command:stop:core:test', stopSpy); + + obj.run('core:test'); + expect(obj.isActive('core:test')).toBe(true); + expect(obj.isActive('test')).toBe(false); + expect(runSpy).toHaveBeenCalledTimes(1); + + obj.stop('core:test'); + expect(obj.isActive('core:test')).toBe(false); + expect(stopSpy).toHaveBeenCalledTimes(1); + }); + + test('Default command aliases keep their registered ids', () => { + expect(obj.get('core:preview')?.id).toBe('core:preview'); + expect(obj.get('preview')?.id).toBe('preview'); + expect(obj.get('core:fullscreen')?.id).toBe('core:fullscreen'); + expect(obj.get('fullscreen')?.id).toBe('fullscreen'); + expect(obj.get('core:component-outline')?.id).toBe('core:component-outline'); + expect(obj.get('sw-visibility')?.id).toBe('sw-visibility'); + }); }); });