UNPKG

7.81 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 Red Hat and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18const command_1 = require("./command");
19const chai = require("chai");
20const expect = chai.expect;
21let commandRegistry;
22/* eslint-disable no-unused-expressions */
23describe('Commands', () => {
24 beforeEach(() => {
25 commandRegistry = new command_1.CommandRegistry(new EmptyContributionProvider());
26 });
27 it('should register and execute a given command', async () => {
28 const concatId = 'concat';
29 const command = { id: concatId };
30 commandRegistry.registerCommand(command, new ConcatCommandHandler());
31 const result = await commandRegistry.executeCommand(concatId, 'a', 'b', 'c');
32 expect('abc').equals(result);
33 });
34 it('should add command to recently used', async () => {
35 const commandId = 'stub';
36 const command = { id: commandId };
37 commandRegistry.registerCommand(command, new StubCommandHandler());
38 commandRegistry.addRecentCommand(command);
39 expect(commandRegistry.recent.length).equal(1);
40 });
41 it('should add multiple commands to recently used in the order they were used', async () => {
42 const commandIds = ['a', 'b', 'c'];
43 const commands = [
44 { id: commandIds[0] },
45 { id: commandIds[1] },
46 { id: commandIds[2] },
47 ];
48 // Register each command.
49 commands.forEach((c) => {
50 commandRegistry.registerCommand(c, new StubCommandHandler());
51 });
52 // Execute order c, b, a.
53 commandRegistry.addRecentCommand(commands[2]);
54 commandRegistry.addRecentCommand(commands[1]);
55 commandRegistry.addRecentCommand(commands[0]);
56 // Expect recently used to be a, b, c.
57 const result = commandRegistry.recent;
58 expect(result.length).equal(3);
59 expect(result[0].id).equal(commandIds[0]);
60 expect(result[1].id).equal(commandIds[1]);
61 expect(result[2].id).equal(commandIds[2]);
62 });
63 it('should add a previously used command to the top of the most recently used', async () => {
64 const commandIds = ['a', 'b', 'c'];
65 const commands = [
66 { id: commandIds[0] },
67 { id: commandIds[1] },
68 { id: commandIds[2] },
69 ];
70 // Register each command.
71 commands.forEach((c) => {
72 commandRegistry.registerCommand(c, new StubCommandHandler());
73 });
74 // Execute order a, b, c, a.
75 commandRegistry.addRecentCommand(commands[0]);
76 commandRegistry.addRecentCommand(commands[1]);
77 commandRegistry.addRecentCommand(commands[2]);
78 commandRegistry.addRecentCommand(commands[0]);
79 // Expect recently used to be a, b, c.
80 const result = commandRegistry.recent;
81 expect(result.length).equal(3);
82 expect(result[0].id).equal(commandIds[0]);
83 expect(result[1].id).equal(commandIds[2]);
84 expect(result[2].id).equal(commandIds[1]);
85 });
86 it('should clear the recently used command history', async () => {
87 const commandIds = ['a', 'b', 'c'];
88 const commands = [
89 { id: commandIds[0] },
90 { id: commandIds[1] },
91 { id: commandIds[2] },
92 ];
93 // Register each command.
94 commands.forEach((c) => {
95 commandRegistry.registerCommand(c, new StubCommandHandler());
96 });
97 // Execute each command.
98 commandRegistry.addRecentCommand(commands[0]);
99 commandRegistry.addRecentCommand(commands[1]);
100 commandRegistry.addRecentCommand(commands[2]);
101 // Clear the list of recently used commands.
102 commandRegistry.clearCommandHistory();
103 expect(commandRegistry.recent.length).equal(0);
104 });
105 it('should return with an empty array of handlers if the command is not registered', () => {
106 expect(commandRegistry.getCommand('missing')).to.be.undefined;
107 expect(commandRegistry.getAllHandlers('missing')).to.be.empty;
108 });
109 it('should return with an empty array of handlers if the command has no registered handlers', () => {
110 commandRegistry.registerCommand({ id: 'id' });
111 expect(commandRegistry.getCommand('id')).to.be.not.undefined;
112 expect(commandRegistry.getAllHandlers('id')).to.be.empty;
113 });
114 it('should return all handlers including the non active ones', () => {
115 commandRegistry.registerCommand({ id: 'id' });
116 commandRegistry.registerHandler('id', new StubCommandHandler());
117 commandRegistry.registerHandler('id', new NeverActiveStubCommandHandler());
118 expect(commandRegistry.getAllHandlers('id').length).to.be.equal(2);
119 });
120 describe('compareCommands', () => {
121 it('should sort command \'a\' before command \'b\' with categories', () => {
122 const a = { id: 'a', category: 'a', label: 'a' };
123 const b = { id: 'b', category: 'b', label: 'b' };
124 expect(command_1.Command.compareCommands(a, b)).to.equal(-1);
125 expect(command_1.Command.compareCommands(b, a)).to.equal(1);
126 });
127 it('should sort command \'a\' before command \'b\' without categories', () => {
128 const a = { id: 'a', label: 'a' };
129 const b = { id: 'b', label: 'b' };
130 expect(command_1.Command.compareCommands(a, b)).to.equal(-1);
131 expect(command_1.Command.compareCommands(b, a)).to.equal(1);
132 });
133 it('should sort command \'a\' before command \'b\' with mix-match categories', () => {
134 const a = { id: 'a', category: 'a', label: 'a' };
135 const b = { id: 'b', label: 'a' };
136 expect(command_1.Command.compareCommands(a, b)).to.equal(1);
137 expect(command_1.Command.compareCommands(b, a)).to.equal(-1);
138 });
139 it('should sort irregardless of casing', () => {
140 const lowercase = { id: 'a', label: 'a' };
141 const uppercase = { id: 'a', label: 'A' };
142 expect(command_1.Command.compareCommands(lowercase, uppercase)).to.equal(0);
143 });
144 it('should not sort if commands are equal', () => {
145 const a = { id: 'a', label: 'a' };
146 expect(command_1.Command.compareCommands(a, a)).to.equal(0);
147 });
148 it('should not sort commands without labels', () => {
149 const a = { id: 'a' };
150 const b = { id: 'b' };
151 expect(command_1.Command.compareCommands(a, b)).to.equal(0);
152 });
153 });
154});
155class EmptyContributionProvider {
156 getContributions(recursive) {
157 return [];
158 }
159}
160class ConcatCommandHandler {
161 execute(...args) {
162 let concat = '';
163 args.forEach(element => {
164 concat += element;
165 });
166 return concat;
167 }
168}
169class StubCommandHandler {
170 execute(...args) { return undefined; }
171}
172class NeverActiveStubCommandHandler extends StubCommandHandler {
173 isEnabled() { return false; }
174}
175//# sourceMappingURL=command.spec.js.map
\No newline at end of file