All files / platform/core/src/classes CommandsManager.test.js

0% Statements 0/105
100% Branches 0/0
0% Functions 0/27
0% Lines 0/105

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192                                                                                                                                                                                                                                                                                                                                                                                               
import CommandsManager from './CommandsManager';
import log from './../log.js';
 
jest.mock('./../log.js');
 
describe('CommandsManager', () => {
  let commandsManager,
    contextName = 'VTK',
    command = {
      commandFn: jest.fn().mockReturnValue(true),
      options: { passMeToCommandFn: ':wave:' },
    },
    commandsManagerConfig = {
      getAppState: () => {
        return {
          viewers: 'Test',
        };
      },
    };
 
  beforeEach(() => {
    commandsManager = new CommandsManager(commandsManagerConfig);
    commandsManager.createContext('VIEWER');
    commandsManager.createContext('ACTIVE_VIEWER::CORNERSTONE');
    jest.clearAllMocks();
  });
 
  it('has a contexts property', () => {
    const localCommandsManager = new CommandsManager(commandsManagerConfig);
 
    expect(localCommandsManager).toHaveProperty('contexts');
    expect(localCommandsManager.contexts).toEqual({});
  });
 
  describe('createContext()', () => {
    it('creates a context', () => {
      commandsManager.createContext(contextName);
 
      expect(commandsManager.contexts).toHaveProperty(contextName);
    });
 
    it('clears the context if it already exists', () => {
      commandsManager.createContext(contextName);
      commandsManager.registerCommand(contextName, 'TestCommand', command);
      commandsManager.registerCommand(contextName, 'TestCommand2', command);
      commandsManager.createContext(contextName);
 
      const registeredCommands = commandsManager.getContext(contextName);
 
      expect(registeredCommands).toEqual({});
    });
  });
 
  describe('getContext()', () => {
    it('returns all registered commands for a context', () => {
      commandsManager.createContext(contextName);
      commandsManager.registerCommand(contextName, 'TestCommand', command);
      const registeredCommands = commandsManager.getContext(contextName);
 
      expect(registeredCommands).toHaveProperty('TestCommand');
      expect(registeredCommands['TestCommand']).toEqual(command);
    });
    it('returns undefined if the context does not exist', () => {
      const registeredCommands = commandsManager.getContext(contextName);
 
      expect(registeredCommands).toBe(undefined);
    });
  });
 
  describe('clearContext()', () => {
    it('clears all registered commands for a context', () => {
      commandsManager.createContext(contextName);
      commandsManager.registerCommand(contextName, 'TestCommand', command);
      commandsManager.registerCommand(contextName, 'TestCommand2', command);
      commandsManager.clearContext(contextName);
 
      const registeredCommands = commandsManager.getContext(contextName);
 
      expect(registeredCommands).toEqual({});
    });
  });
 
  describe('registerCommand()', () => {
    it('registers commands to a context', () => {
      commandsManager.createContext(contextName);
      commandsManager.registerCommand(contextName, 'TestCommand', command);
      const registeredCommands = commandsManager.getContext(contextName);
 
      expect(registeredCommands).toHaveProperty('TestCommand');
      expect(registeredCommands['TestCommand']).toEqual(command);
    });
  });
 
  describe('getCommand()', () => {
    it('returns undefined if context does not exist', () => {
      const result = commandsManager.getCommand('TestCommand', 'NonExistentContext');
 
      expect(result).toBe(undefined);
    });
    it('returns undefined if command does not exist in context', () => {
      commandsManager.createContext(contextName);
      const result = commandsManager.getCommand('TestCommand', contextName);
 
      expect(result).toBe(undefined);
    });
    it('uses contextName param to get command', () => {
      commandsManager.createContext('GLOBAL');
      commandsManager.registerCommand('GLOBAL', 'TestCommand', command);
      const foundCommand = commandsManager.getCommand('TestCommand', 'GLOBAL');
 
      expect(foundCommand).toBe(command);
    });
    it('uses activeContexts, if contextName is not provided, to get command', () => {
      commandsManager.registerCommand('VIEWER', 'TestCommand', command);
      const foundCommand = commandsManager.getCommand('TestCommand');
 
      expect(foundCommand).toBe(command);
    });
    it('returns the expected command', () => {
      commandsManager.createContext(contextName);
      commandsManager.registerCommand(contextName, 'TestCommand', command);
      const result = commandsManager.getCommand('TestCommand', contextName);
 
      expect(result).toEqual(command);
    });
  });
 
  describe('runCommand()', () => {
    it('Logs a warning if commandName not found in context', () => {
      const result = commandsManager.runCommand('CommandThatDoesNotExistInAnyContext');
 
      expect(result).toBe(undefined);
      expect(log.warn.mock.calls[0][0]).toEqual(
        'Command "CommandThatDoesNotExistInAnyContext" not found in current context'
      );
    });
 
    it('Logs a warning if command definition does not have a commandFn', () => {
      const commandWithNoCommmandFn = {
        commandFn: undefined,
        options: {},
      };
 
      commandsManager.createContext(contextName);
      commandsManager.registerCommand(contextName, 'TestCommand', commandWithNoCommmandFn);
      const result = commandsManager.runCommand('TestCommand', null, contextName);
 
      expect(result).toBe(undefined);
      expect(log.warn.mock.calls[0][0]).toEqual(
        'No commandFn was defined for command "TestCommand"'
      );
    });
 
    it('Calls commandFn', () => {
      commandsManager.registerCommand('VIEWER', 'TestCommand', command);
      commandsManager.runCommand('TestCommand', {}, 'VIEWER');
 
      expect(command.commandFn.mock.calls.length).toBe(1);
    });
 
    it('Calls commandFn w/ command definition options', () => {
      commandsManager.registerCommand('VIEWER', 'TestCommand', command);
      commandsManager.runCommand('TestCommand', {}, 'VIEWER');
 
      expect(command.commandFn.mock.calls.length).toBe(1);
      expect(command.commandFn.mock.calls[0][0].passMeToCommandFn).toEqual(
        command.options.passMeToCommandFn
      );
    });
 
    it('Calls commandFn w/ runCommand "options" parameter', () => {
      const runCommandOptions = {
        test: ':+1:',
      };
 
      commandsManager.registerCommand('VIEWER', 'TestCommand', command);
      commandsManager.runCommand('TestCommand', runCommandOptions, 'VIEWER');
 
      expect(command.commandFn.mock.calls.length).toBe(1);
      expect(command.commandFn.mock.calls[0][0].test).toEqual(runCommandOptions.test);
    });
 
    it('Returns the result of commandFn', () => {
      commandsManager.registerCommand('VIEWER', 'TestCommand', command);
      const result = commandsManager.runCommand('TestCommand', {}, 'VIEWER');
 
      expect(command.commandFn.mock.calls.length).toBe(1);
      expect(result).toBe(true);
    });
  });
});