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 | describe('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 |
|
48 | commands.forEach((c) => {
|
49 | commandRegistry.registerCommand(c, new StubCommandHandler());
|
50 | });
|
51 |
|
52 | commandRegistry.addRecentCommand(commands[2]);
|
53 | commandRegistry.addRecentCommand(commands[1]);
|
54 | commandRegistry.addRecentCommand(commands[0]);
|
55 |
|
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 |
|
70 | commands.forEach((c) => {
|
71 | commandRegistry.registerCommand(c, new StubCommandHandler());
|
72 | });
|
73 |
|
74 | commandRegistry.addRecentCommand(commands[0]);
|
75 | commandRegistry.addRecentCommand(commands[1]);
|
76 | commandRegistry.addRecentCommand(commands[2]);
|
77 | commandRegistry.addRecentCommand(commands[0]);
|
78 |
|
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 |
|
93 | commands.forEach((c) => {
|
94 | commandRegistry.registerCommand(c, new StubCommandHandler());
|
95 | });
|
96 |
|
97 | commandRegistry.addRecentCommand(commands[0]);
|
98 | commandRegistry.addRecentCommand(commands[1]);
|
99 | commandRegistry.addRecentCommand(commands[2]);
|
100 |
|
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 | });
|
154 | class EmptyContributionProvider {
|
155 | getContributions(recursive) {
|
156 | return [];
|
157 | }
|
158 | }
|
159 | class ConcatCommandHandler {
|
160 | execute(...args) {
|
161 | let concat = '';
|
162 | args.forEach(element => {
|
163 | concat += element;
|
164 | });
|
165 | return concat;
|
166 | }
|
167 | }
|
168 | class StubCommandHandler {
|
169 | execute(...args) { return undefined; }
|
170 | }
|
171 | class NeverActiveStubCommandHandler extends StubCommandHandler {
|
172 | isEnabled() { return false; }
|
173 | }
|
174 |
|
\ | No newline at end of file |