1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | const command_1 = require("./command");
|
19 | const chai = require("chai");
|
20 | const expect = chai.expect;
|
21 | let commandRegistry;
|
22 |
|
23 | describe('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 |
|
49 | commands.forEach((c) => {
|
50 | commandRegistry.registerCommand(c, new StubCommandHandler());
|
51 | });
|
52 |
|
53 | commandRegistry.addRecentCommand(commands[2]);
|
54 | commandRegistry.addRecentCommand(commands[1]);
|
55 | commandRegistry.addRecentCommand(commands[0]);
|
56 |
|
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 |
|
71 | commands.forEach((c) => {
|
72 | commandRegistry.registerCommand(c, new StubCommandHandler());
|
73 | });
|
74 |
|
75 | commandRegistry.addRecentCommand(commands[0]);
|
76 | commandRegistry.addRecentCommand(commands[1]);
|
77 | commandRegistry.addRecentCommand(commands[2]);
|
78 | commandRegistry.addRecentCommand(commands[0]);
|
79 |
|
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 |
|
94 | commands.forEach((c) => {
|
95 | commandRegistry.registerCommand(c, new StubCommandHandler());
|
96 | });
|
97 |
|
98 | commandRegistry.addRecentCommand(commands[0]);
|
99 | commandRegistry.addRecentCommand(commands[1]);
|
100 | commandRegistry.addRecentCommand(commands[2]);
|
101 |
|
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 | });
|
155 | class EmptyContributionProvider {
|
156 | getContributions(recursive) {
|
157 | return [];
|
158 | }
|
159 | }
|
160 | class ConcatCommandHandler {
|
161 | execute(...args) {
|
162 | let concat = '';
|
163 | args.forEach(element => {
|
164 | concat += element;
|
165 | });
|
166 | return concat;
|
167 | }
|
168 | }
|
169 | class StubCommandHandler {
|
170 | execute(...args) { return undefined; }
|
171 | }
|
172 | class NeverActiveStubCommandHandler extends StubCommandHandler {
|
173 | isEnabled() { return false; }
|
174 | }
|
175 |
|
\ | No newline at end of file |