|
1 | | -const { expect, sinon, stubWithArgs } = require('../../helpers') |
| 1 | +const { |
| 2 | + mockObject, |
| 3 | + expect, |
| 4 | + when, |
| 5 | + verify, |
| 6 | + argCaptor, |
| 7 | + matchers |
| 8 | +} = require('../../helpers') |
2 | 9 |
|
3 | 10 | const SelectText1Command = require('../../../lib/commands/save-text-1') |
4 | 11 |
|
5 | 12 | suite('SelectText1Command', () => { |
6 | 13 | test('it saves selected text', () => { |
7 | | - const selectionInfoBuilder = { |
8 | | - extract: stubWithArgs(['EDITOR'], { |
9 | | - text: 'SELECTED_TEXT', |
10 | | - fileName: 'FILENAME', |
11 | | - lineRanges: 'SELECTED_RANGE' |
12 | | - }) |
13 | | - } |
14 | | - const selectionInfoRegistry = { set: sinon.spy() } |
| 14 | + const selectionInfoBuilder = mockObject('extract') |
| 15 | + when(selectionInfoBuilder.extract('EDITOR')).thenReturn({ |
| 16 | + text: 'SELECTED_TEXT', |
| 17 | + fileName: 'FILENAME', |
| 18 | + lineRanges: 'SELECTED_RANGE' |
| 19 | + }) |
| 20 | + const selectionInfoRegistry = mockObject('set') |
15 | 21 | const command = new SelectText1Command({ |
16 | 22 | selectionInfoBuilder, |
17 | 23 | selectionInfoRegistry |
18 | 24 | }) |
19 | 25 | command.execute('EDITOR') |
20 | | - expect(selectionInfoRegistry.set).to.have.been.calledWith('reg1', { |
| 26 | + |
| 27 | + const arg1 = argCaptor() |
| 28 | + const arg2 = argCaptor() |
| 29 | + verify(selectionInfoRegistry.set(arg1.capture(), arg2.capture())) |
| 30 | + expect(arg1.values[0]).to.eql('reg1') |
| 31 | + expect(arg2.values[0]).to.eql({ |
21 | 32 | text: 'SELECTED_TEXT', |
22 | 33 | fileName: 'FILENAME', |
23 | 34 | lineRanges: 'SELECTED_RANGE' |
24 | 35 | }) |
25 | 36 | }) |
26 | 37 |
|
27 | | - test('it prints callstack if error occurred', () => { |
28 | | - const logger = { error: sinon.spy() } |
| 38 | + test('it prints callstack if error occurred', async () => { |
| 39 | + const logger = mockObject('error') |
29 | 40 | const command = new SelectText1Command({ logger }) |
30 | | - command.execute('EDITOR') |
31 | | - expect(logger.error).to.have.been.called |
| 41 | + |
| 42 | + await command.execute('EDITOR') |
| 43 | + |
| 44 | + verify(logger.error(), { times: 1, ignoreExtraArgs: true }) |
32 | 45 | }) |
33 | 46 |
|
34 | 47 | test('it prints callstack if saving text failed', () => { |
35 | | - const logger = { error: sinon.spy() } |
36 | | - const selectionInfoBuilder = { |
37 | | - extract: stubWithArgs(['EDITOR'], { |
38 | | - text: 'SELECTED_TEXT', |
39 | | - fileName: 'FILENAME', |
40 | | - lineRanges: 'SELECTED_RANGE' |
41 | | - }) |
42 | | - } |
43 | | - const selectionInfoRegistry = { |
44 | | - set: sinon.stub().throws(new Error('UNEXPECTED_ERROR')) |
45 | | - } |
| 48 | + const logger = mockObject('error') |
| 49 | + const selectionInfoBuilder = mockObject('extract') |
| 50 | + when(selectionInfoBuilder.extract('EDITOR')).thenReturn({ |
| 51 | + text: 'SELECTED_TEXT', |
| 52 | + fileName: 'FILENAME', |
| 53 | + lineRanges: 'SELECTED_RANGE' |
| 54 | + }) |
| 55 | + const selectionInfoRegistry = mockObject('set') |
| 56 | + when(selectionInfoRegistry.set(), { ignoreExtraArgs: true }).thenThrow( |
| 57 | + new Error('UNEXPECTED_ERROR') |
| 58 | + ) |
| 59 | + |
46 | 60 | const command = new SelectText1Command({ |
47 | 61 | logger, |
48 | 62 | selectionInfoBuilder, |
49 | 63 | selectionInfoRegistry |
50 | 64 | }) |
51 | 65 | command.execute('EDITOR') |
52 | | - expect(logger.error.args[0][0]).to.have.string('Error: UNEXPECTED_ERROR') |
| 66 | + |
| 67 | + verify(logger.error(matchers.contains('Error: UNEXPECTED_ERROR'))) |
53 | 68 | }) |
54 | 69 | }) |
0 commit comments