|
1 | 1 | import { expect } from 'chai'; |
2 | 2 | import 'mocha'; |
3 | | -import { ExtensionContext, Memento } from 'vscode'; |
| 3 | +import { ExtensionContext, Memento, window } from 'vscode'; |
4 | 4 | import * as yaml from 'js-yaml'; |
5 | 5 | import * as tmp from 'tmp'; |
6 | 6 | import * as path from 'path'; |
7 | 7 | import * as fs from 'fs-extra'; |
8 | 8 | import * as sinon from 'sinon'; |
9 | 9 |
|
10 | | -import { getInitialQueryContents, InvocationRateLimiter, isLikelyDbLanguageFolder } from '../../helpers'; |
| 10 | +import { getInitialQueryContents, InvocationRateLimiter, isLikelyDbLanguageFolder, showBinaryChoiceDialog, showBinaryChoiceWithUrlDialog, showInformationMessageWithAction } from '../../helpers'; |
11 | 11 | import { reportStreamProgress } from '../../commandRunner'; |
| 12 | +import Sinon = require('sinon'); |
| 13 | +import { fail } from 'assert'; |
12 | 14 |
|
13 | 15 | describe('helpers', () => { |
14 | 16 | let sandbox: sinon.SinonSandbox; |
@@ -225,4 +227,91 @@ describe('helpers', () => { |
225 | 227 | message: 'My prefix (Size unknown)', |
226 | 228 | }); |
227 | 229 | }); |
| 230 | + |
| 231 | + describe('open dialog', () => { |
| 232 | + let showInformationMessageSpy: Sinon.SinonStub; |
| 233 | + beforeEach(() => { |
| 234 | + showInformationMessageSpy = sandbox.stub(window, 'showInformationMessage'); |
| 235 | + }); |
| 236 | + |
| 237 | + it('should show a binary choice dialog and return `yes`', (done) => { |
| 238 | + // pretend user chooses 'yes' |
| 239 | + showInformationMessageSpy.onCall(0).resolvesArg(2); |
| 240 | + const res = showBinaryChoiceDialog('xxx'); |
| 241 | + res.then((val) => { |
| 242 | + expect(val).to.eq(true); |
| 243 | + done(); |
| 244 | + }).catch(e => fail(e)); |
| 245 | + }); |
| 246 | + |
| 247 | + it('should show a binary choice dialog and return `no`', (done) => { |
| 248 | + // pretend user chooses 'no' |
| 249 | + showInformationMessageSpy.onCall(0).resolvesArg(3); |
| 250 | + const res = showBinaryChoiceDialog('xxx'); |
| 251 | + res.then((val) => { |
| 252 | + expect(val).to.eq(false); |
| 253 | + done(); |
| 254 | + }).catch(e => fail(e)); |
| 255 | + }); |
| 256 | + |
| 257 | + it('should show an info dialog and confirm the action', (done) => { |
| 258 | + // pretend user chooses to run action |
| 259 | + showInformationMessageSpy.onCall(0).resolvesArg(1); |
| 260 | + const res = showInformationMessageWithAction('xxx', 'yyy'); |
| 261 | + res.then((val) => { |
| 262 | + expect(val).to.eq(true); |
| 263 | + done(); |
| 264 | + }).catch(e => fail(e)); |
| 265 | + }); |
| 266 | + |
| 267 | + it('should show an action dialog and avoid choosing the action', (done) => { |
| 268 | + // pretend user does not choose to run action |
| 269 | + showInformationMessageSpy.onCall(0).resolves(undefined); |
| 270 | + const res = showInformationMessageWithAction('xxx', 'yyy'); |
| 271 | + res.then((val) => { |
| 272 | + expect(val).to.eq(false); |
| 273 | + done(); |
| 274 | + }).catch(e => fail(e)); |
| 275 | + }); |
| 276 | + |
| 277 | + it('should show a binary choice dialog with a url and return `yes`', (done) => { |
| 278 | + // pretend user clicks on the url twice and then clicks 'yes' |
| 279 | + showInformationMessageSpy.onCall(0).resolvesArg(2); |
| 280 | + showInformationMessageSpy.onCall(1).resolvesArg(2); |
| 281 | + showInformationMessageSpy.onCall(2).resolvesArg(3); |
| 282 | + const res = showBinaryChoiceWithUrlDialog('xxx', 'invalid:url'); |
| 283 | + res.then((val) => { |
| 284 | + expect(val).to.eq(true); |
| 285 | + done(); |
| 286 | + }).catch(e => fail(e)); |
| 287 | + }); |
| 288 | + |
| 289 | + it('should show a binary choice dialog with a url and return `no`', (done) => { |
| 290 | + // pretend user clicks on the url twice and then clicks 'no' |
| 291 | + showInformationMessageSpy.onCall(0).resolvesArg(2); |
| 292 | + showInformationMessageSpy.onCall(1).resolvesArg(2); |
| 293 | + showInformationMessageSpy.onCall(2).resolvesArg(4); |
| 294 | + const res = showBinaryChoiceWithUrlDialog('xxx', 'invalid:url'); |
| 295 | + res.then((val) => { |
| 296 | + expect(val).to.eq(false); |
| 297 | + done(); |
| 298 | + }).catch(e => fail(e)); |
| 299 | + }); |
| 300 | + |
| 301 | + it('should show a binary choice dialog and exit after clcking `more info` 5 times', (done) => { |
| 302 | + // pretend user clicks on the url twice and then clicks 'no' |
| 303 | + showInformationMessageSpy.onCall(0).resolvesArg(2); |
| 304 | + showInformationMessageSpy.onCall(1).resolvesArg(2); |
| 305 | + showInformationMessageSpy.onCall(2).resolvesArg(2); |
| 306 | + showInformationMessageSpy.onCall(3).resolvesArg(2); |
| 307 | + showInformationMessageSpy.onCall(4).resolvesArg(2); |
| 308 | + const res = showBinaryChoiceWithUrlDialog('xxx', 'invalid:url'); |
| 309 | + res.then((val) => { |
| 310 | + // No choie was made |
| 311 | + expect(val).to.eq(undefined); |
| 312 | + expect(showInformationMessageSpy.getCalls().length).to.eq(5); |
| 313 | + done(); |
| 314 | + }).catch(e => fail(e)); |
| 315 | + }); |
| 316 | + }); |
228 | 317 | }); |
0 commit comments