UNPKG

7.77 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;
22describe('Commands', () => {
23 beforeEach(() => {
24 commandRegistry = new command_1.CommandRegistry(new EmptyContributionProvider());
25 });
26 it('should register and execute a given command', async () => {
27 const concatId = 'concat';
28 const command = { id: concatId };
29 commandRegistry.registerCommand(command, new ConcatCommandHandler());
30 const result = await commandRegistry.executeCommand(concatId, 'a', 'b', 'c');
31 expect('abc').equals(result);
32 });
33 it('should add command to recently used', async () => {
34 const commandId = 'stub';
35 const command = { id: commandId };
36 commandRegistry.registerCommand(command, new StubCommandHandler());
37 commandRegistry.addRecentCommand(command);
38 expect(commandRegistry.recent.length).equal(1);
39 });
40 it('should add multiple commands to recently used in the order they were used', async () => {
41 const commandIds = ['a', 'b', 'c'];
42 const commands = [
43 { id: commandIds[0] },
44 { id: commandIds[1] },
45 { id: commandIds[2] },
46 ];
47 // Register each command.
48 commands.forEach((c) => {
49 commandRegistry.registerCommand(c, new StubCommandHandler());
50 });
51 // Execute order c, b, a.
52 commandRegistry.addRecentCommand(commands[2]);
53 commandRegistry.addRecentCommand(commands[1]);
54 commandRegistry.addRecentCommand(commands[0]);
55 // Expect recently used to be a, b, c.
56 const result = commandRegistry.recent;
57 expect(result.length).equal(3);
58 expect(result[0].id).equal(commandIds[0]);
59 expect(result[1].id).equal(commandIds[1]);
60 expect(result[2].id).equal(commandIds[2]);
61 });
62 it('should add a previously used command to the top of the most recently used', async () => {
63 const commandIds = ['a', 'b', 'c'];
64 const commands = [
65 { id: commandIds[0] },
66 { id: commandIds[1] },
67 { id: commandIds[2] },
68 ];
69 // Register each command.
70 commands.forEach((c) => {
71 commandRegistry.registerCommand(c, new StubCommandHandler());
72 });
73 // Execute order a, b, c, a.
74 commandRegistry.addRecentCommand(commands[0]);
75 commandRegistry.addRecentCommand(commands[1]);
76 commandRegistry.addRecentCommand(commands[2]);
77 commandRegistry.addRecentCommand(commands[0]);
78 // Expect recently used to be a, b, c.
79 const result = commandRegistry.recent;
80 expect(result.length).equal(3);
81 expect(result[0].id).equal(commandIds[0]);
82 expect(result[1].id).equal(commandIds[2]);
83 expect(result[2].id).equal(commandIds[1]);
84 });
85 it('should clear the recently used command history', async () => {
86 const commandIds = ['a', 'b', 'c'];
87 const commands = [
88 { id: commandIds[0] },
89 { id: commandIds[1] },
90 { id: commandIds[2] },
91 ];
92 // Register each command.
93 commands.forEach((c) => {
94 commandRegistry.registerCommand(c, new StubCommandHandler());
95 });
96 // Execute each command.
97 commandRegistry.addRecentCommand(commands[0]);
98 commandRegistry.addRecentCommand(commands[1]);
99 commandRegistry.addRecentCommand(commands[2]);
100 // Clear the list of recently used commands.
101 commandRegistry.clearCommandHistory();
102 expect(commandRegistry.recent.length).equal(0);
103 });
104 it('should return with an empty array of handlers if the command is not registered', () => {
105 expect(commandRegistry.getCommand('missing')).to.be.undefined;
106 expect(commandRegistry.getAllHandlers('missing')).to.be.empty;
107 });
108 it('should return with an empty array of handlers if the command has no registered handlers', () => {
109 commandRegistry.registerCommand({ id: 'id' });
110 expect(commandRegistry.getCommand('id')).to.be.not.undefined;
111 expect(commandRegistry.getAllHandlers('id')).to.be.empty;
112 });
113 it('should return all handlers including the non active ones', () => {
114 commandRegistry.registerCommand({ id: 'id' });
115 commandRegistry.registerHandler('id', new StubCommandHandler());
116 commandRegistry.registerHandler('id', new NeverActiveStubCommandHandler());
117 expect(commandRegistry.getAllHandlers('id').length).to.be.equal(2);
118 });
119 describe('compareCommands', () => {
120 it('should sort command \'a\' before command \'b\' with categories', () => {
121 const a = { id: 'a', category: 'a', label: 'a' };
122 const b = { id: 'b', category: 'b', label: 'b' };
123 expect(command_1.Command.compareCommands(a, b)).to.equal(-1);
124 expect(command_1.Command.compareCommands(b, a)).to.equal(1);
125 });
126 it('should sort command \'a\' before command \'b\' without categories', () => {
127 const a = { id: 'a', label: 'a' };
128 const b = { id: 'b', label: 'b' };
129 expect(command_1.Command.compareCommands(a, b)).to.equal(-1);
130 expect(command_1.Command.compareCommands(b, a)).to.equal(1);
131 });
132 it('should sort command \'a\' before command \'b\' with mix-match categories', () => {
133 const a = { id: 'a', category: 'a', label: 'a' };
134 const b = { id: 'b', label: 'a' };
135 expect(command_1.Command.compareCommands(a, b)).to.equal(1);
136 expect(command_1.Command.compareCommands(b, a)).to.equal(-1);
137 });
138 it('should sort irregardless of casing', () => {
139 const lowercase = { id: 'a', label: 'a' };
140 const uppercase = { id: 'a', label: 'A' };
141 expect(command_1.Command.compareCommands(lowercase, uppercase)).to.equal(0);
142 });
143 it('should not sort if commands are equal', () => {
144 const a = { id: 'a', label: 'a' };
145 expect(command_1.Command.compareCommands(a, a)).to.equal(0);
146 });
147 it('should not sort commands without labels', () => {
148 const a = { id: 'a' };
149 const b = { id: 'b' };
150 expect(command_1.Command.compareCommands(a, b)).to.equal(0);
151 });
152 });
153});
154class EmptyContributionProvider {
155 getContributions(recursive) {
156 return [];
157 }
158}
159class ConcatCommandHandler {
160 execute(...args) {
161 let concat = '';
162 args.forEach(element => {
163 concat += element;
164 });
165 return concat;
166 }
167}
168class StubCommandHandler {
169 execute(...args) { return undefined; }
170}
171class NeverActiveStubCommandHandler extends StubCommandHandler {
172 isEnabled() { return false; }
173}
174//# sourceMappingURL=command.spec.js.map
\No newline at end of file