Skip to content

Commit 393d39c

Browse files
committed
Removing the direct call of mock libraries
1 parent 1e01d7f commit 393d39c

File tree

6 files changed

+116
-82
lines changed

6 files changed

+116
-82
lines changed

test/helpers.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
const sinon = require('sinon')
2+
const td = require('testdouble')
13
const chai = require('chai')
24
chai.use(require('sinon-chai'))
35

4-
const sinon = require('sinon')
5-
66
exports.expect = chai.expect
77
exports.sinon = sinon
88
exports.throwErrorIfCalled = () => {
@@ -17,3 +17,9 @@ exports.stubWithArgs = function (...args) {
1717
}
1818
return stub
1919
}
20+
21+
exports.mockObject = td.object
22+
exports.argCaptor = td.matchers.captor
23+
exports.verify = td.verify
24+
exports.when = td.when
25+
exports.matchers = td.matchers
Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
const { expect, sinon, stubWithArgs } = require('../../helpers')
1+
const { expect, verify, when, mockObject, argCaptor } = require('../../helpers')
22

33
const CompareSelectionWithClipboardCommand = require('../../../lib/commands/compare-selection-with-clipboard')
44

55
suite('CompareSelectionWithClipboardCommand', () => {
66
test('it compares selected text with clipboard text', async () => {
7-
const clipboard = { read: () => Promise.resolve('CLIPBOARD_TEXT') }
8-
const selectionInfoBuilder = {
9-
extract: stubWithArgs(['EDITOR'], {
10-
text: 'SELECTED_TEXT',
11-
fileName: 'FILENAME',
12-
lineRanges: 'SELECTED_RANGE'
13-
})
14-
}
15-
const selectionInfoRegistry = { set: sinon.spy() }
16-
const diffPresenter = { takeDiff: sinon.spy() }
7+
const clipboard = mockObject('read')
8+
when(clipboard.read()).thenResolve('CLIPBOARD_TEXT')
9+
10+
const selectionInfoBuilder = mockObject('extract')
11+
when(selectionInfoBuilder.extract('EDITOR')).thenReturn({
12+
text: 'SELECTED_TEXT',
13+
fileName: 'FILENAME',
14+
lineRanges: 'SELECTED_RANGE'
15+
})
16+
17+
const selectionInfoRegistry = mockObject('set')
18+
const diffPresenter = mockObject('takeDiff')
1719
const command = new CompareSelectionWithClipboardCommand({
1820
clipboard,
1921
diffPresenter,
@@ -23,24 +25,30 @@ suite('CompareSelectionWithClipboardCommand', () => {
2325

2426
await command.execute('EDITOR')
2527

26-
expect(selectionInfoRegistry.set).to.have.been.calledWith('clipboard', {
28+
const arg1 = argCaptor()
29+
const arg2 = argCaptor()
30+
verify(selectionInfoRegistry.set(arg1.capture(), arg2.capture()))
31+
expect(arg1.values[0]).to.eql('clipboard')
32+
expect(arg2.values[0]).to.eql({
2733
text: 'CLIPBOARD_TEXT',
2834
fileName: 'Clipboard'
2935
})
30-
expect(selectionInfoRegistry.set).to.have.been.calledWith('reg2', {
36+
expect(arg1.values[1]).to.eql('reg2')
37+
expect(arg2.values[1]).to.eql({
3138
text: 'SELECTED_TEXT',
3239
fileName: 'FILENAME',
3340
lineRanges: 'SELECTED_RANGE'
3441
})
35-
expect(diffPresenter.takeDiff).to.have.been.calledWith('clipboard', 'reg2')
42+
43+
verify(diffPresenter.takeDiff('clipboard', 'reg2'))
3644
})
3745

3846
test('it prints callstack if error occurred', async () => {
39-
const logger = { error: sinon.spy() }
47+
const logger = mockObject('error')
4048
const command = new CompareSelectionWithClipboardCommand({ logger })
4149

4250
await command.execute('EDITOR')
4351

44-
expect(logger.error).to.have.been.called
52+
verify(logger.error(), { times: 1, ignoreExtraArgs: true })
4553
})
4654
})
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
const { expect, sinon, stubWithArgs } = require('../../helpers')
1+
const { expect, verify, when, mockObject, argCaptor } = require('../../helpers')
22

33
const CompareSelectionWithText1 = require('../../../lib/commands/compare-selection-with-text1')
44

55
suite('CompareSelectionWithText1', () => {
66
test('it saves selected text and takes a diff of 2 texts', async () => {
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() }
15-
const diffPresenter = { takeDiff: sinon.spy() }
7+
const selectionInfoBuilder = mockObject('extract')
8+
when(selectionInfoBuilder.extract('EDITOR')).thenReturn({
9+
text: 'SELECTED_TEXT',
10+
fileName: 'FILENAME',
11+
lineRanges: 'SELECTED_RANGE'
12+
})
13+
14+
const selectionInfoRegistry = mockObject('set')
15+
const diffPresenter = mockObject('takeDiff')
16+
1617
const command = new CompareSelectionWithText1({
1718
diffPresenter,
1819
selectionInfoBuilder,
@@ -21,20 +22,24 @@ suite('CompareSelectionWithText1', () => {
2122

2223
await command.execute('EDITOR')
2324

24-
expect(selectionInfoRegistry.set).to.have.been.calledWith('reg2', {
25+
const arg1 = argCaptor()
26+
const arg2 = argCaptor()
27+
verify(selectionInfoRegistry.set(arg1.capture(), arg2.capture()))
28+
expect(arg1.values[0]).to.eql('reg2')
29+
expect(arg2.values[0]).to.eql({
2530
text: 'SELECTED_TEXT',
2631
fileName: 'FILENAME',
2732
lineRanges: 'SELECTED_RANGE'
2833
})
29-
expect(diffPresenter.takeDiff).to.have.been.calledWith('reg1', 'reg2')
34+
verify(diffPresenter.takeDiff('reg1', 'reg2'))
3035
})
3136

3237
test('it prints callstack if error occurred', async () => {
33-
const logger = { error: sinon.spy() }
38+
const logger = mockObject('error')
3439
const command = new CompareSelectionWithText1({ logger })
3540

3641
await command.execute('EDITOR')
3742

38-
expect(logger.error).to.have.been.called
43+
verify(logger.error(), { times: 1, ignoreExtraArgs: true })
3944
})
4045
})

test/lib/commands/compare-visible-editors.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const td = require('testdouble')
1+
const { mockObject, verify, when } = require('../../helpers')
22

33
const CompareVisibleEditorsCommand = require('../../../lib/commands/compare-visible-editors')
44

@@ -10,39 +10,39 @@ suite('CompareVisibleEditorsCommand', () => {
1010
const { command, deps } = createCommand([editor1, editor2])
1111
await command.execute()
1212

13-
td.verify(deps.selectionInfoRegistry.set('visible1', 'TEXT_INFO1'))
14-
td.verify(deps.selectionInfoRegistry.set('visible2', 'TEXT_INFO2'))
15-
td.verify(deps.diffPresenter.takeDiff('visible1', 'visible2'))
13+
verify(deps.selectionInfoRegistry.set('visible1', 'TEXT_INFO1'))
14+
verify(deps.selectionInfoRegistry.set('visible2', 'TEXT_INFO2'))
15+
verify(deps.diffPresenter.takeDiff('visible1', 'visible2'))
1616
})
1717

1818
test('it keeps the visual order of the editors when presents a diff', async () => {
1919
const { command, deps } = createCommand([editor2, editor1])
2020
await command.execute()
2121

22-
td.verify(deps.selectionInfoRegistry.set('visible1', 'TEXT_INFO1'))
23-
td.verify(deps.selectionInfoRegistry.set('visible2', 'TEXT_INFO2'))
22+
verify(deps.selectionInfoRegistry.set('visible1', 'TEXT_INFO1'))
23+
verify(deps.selectionInfoRegistry.set('visible2', 'TEXT_INFO2'))
2424
})
2525

2626
test('it tells you that it needs 2 visible editors', async () => {
2727
const { command, deps } = createCommand([editor1])
2828
await command.execute()
2929

30-
td.verify(
30+
verify(
3131
deps.messageBar.showInfo('Please first open 2 documents to compare.')
3232
)
3333
})
3434

3535
function createCommand (visibleTextEditors) {
36-
const selectionInfoBuilder = td.object(['extract'])
37-
td.when(selectionInfoBuilder.extract(editor1)).thenReturn('TEXT_INFO1')
38-
td.when(selectionInfoBuilder.extract(editor2)).thenReturn('TEXT_INFO2')
36+
const selectionInfoBuilder = mockObject('extract')
37+
when(selectionInfoBuilder.extract(editor1)).thenReturn('TEXT_INFO1')
38+
when(selectionInfoBuilder.extract(editor2)).thenReturn('TEXT_INFO2')
3939

4040
const dependencies = {
4141
editorWindow: { visibleTextEditors },
42-
diffPresenter: td.object(['takeDiff']),
43-
messageBar: td.object(['showInfo']),
42+
diffPresenter: mockObject('takeDiff'),
43+
messageBar: mockObject('showInfo'),
4444
selectionInfoBuilder,
45-
selectionInfoRegistry: td.object(['set'])
45+
selectionInfoRegistry: mockObject('set')
4646
}
4747
const command = new CompareVisibleEditorsCommand(dependencies)
4848
return { command, deps: dependencies }
Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,69 @@
1-
const { expect, sinon, stubWithArgs } = require('../../helpers')
1+
const {
2+
mockObject,
3+
expect,
4+
when,
5+
verify,
6+
argCaptor,
7+
matchers
8+
} = require('../../helpers')
29

310
const SelectText1Command = require('../../../lib/commands/save-text-1')
411

512
suite('SelectText1Command', () => {
613
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')
1521
const command = new SelectText1Command({
1622
selectionInfoBuilder,
1723
selectionInfoRegistry
1824
})
1925
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({
2132
text: 'SELECTED_TEXT',
2233
fileName: 'FILENAME',
2334
lineRanges: 'SELECTED_RANGE'
2435
})
2536
})
2637

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')
2940
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 })
3245
})
3346

3447
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+
4660
const command = new SelectText1Command({
4761
logger,
4862
selectionInfoBuilder,
4963
selectionInfoRegistry
5064
})
5165
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')))
5368
})
5469
})

test/lib/commands/toggle-normalisation-rules.test.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const td = require('testdouble')
1+
const { mockObject, when, verify } = require('../../helpers')
22

33
const ToggleNormalisationRulesCommand = require('../../../lib/commands/toggle-normalisation-rules')
44

@@ -7,7 +7,7 @@ suite('ToggleNormalisationRulesCommand', () => {
77
const { command, deps } = createCommand({ rules: ['RULE'] })
88
await command.execute()
99

10-
td.verify(
10+
verify(
1111
deps.normalisationRuleStore.specifyActiveRules('ACTIVE_RULE_INDICES')
1212
)
1313
})
@@ -16,25 +16,25 @@ suite('ToggleNormalisationRulesCommand', () => {
1616
const { command, deps } = createCommand({ rules: [] })
1717
await command.execute()
1818

19-
td.verify(
19+
verify(
2020
deps.messageBar.showInfo(
2121
'Please set `partialDiff.preComparisonTextNormalizationRules` first'
2222
)
2323
)
2424
})
2525

2626
function createCommand ({ rules }) {
27-
const normalisationRuleStore = td.object(
27+
const normalisationRuleStore = mockObject([
2828
'getAllRules',
2929
'specifyActiveRules'
30-
)
31-
td.when(normalisationRuleStore.getAllRules()).thenReturn(rules)
32-
const normalisationRulePicker = td.object('show')
33-
td
34-
.when(normalisationRulePicker.show(rules))
35-
.thenResolve('ACTIVE_RULE_INDICES')
30+
])
31+
when(normalisationRuleStore.getAllRules()).thenReturn(rules)
32+
33+
const normalisationRulePicker = mockObject('show')
34+
when(normalisationRulePicker.show(rules)).thenResolve('ACTIVE_RULE_INDICES')
35+
3636
const deps = {
37-
messageBar: td.object('showInfo'),
37+
messageBar: mockObject('showInfo'),
3838
normalisationRulePicker,
3939
normalisationRuleStore
4040
}

0 commit comments

Comments
 (0)