-
-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathmeta_step_logging_test.js
More file actions
115 lines (94 loc) · 3.54 KB
/
meta_step_logging_test.js
File metadata and controls
115 lines (94 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { expect } from 'chai'
import sinon from 'sinon'
import event from '../../../lib/event.js'
import MetaStep from '../../../lib/step/meta.js'
import Step from '../../../lib/step.js'
describe('MetaStep Logging', () => {
let eventStub
beforeEach(() => {
event.cleanDispatcher()
eventStub = {
stepStarted: sinon.spy(),
stepFinished: sinon.spy(),
}
event.dispatcher.on(event.step.started, eventStub.stepStarted)
event.dispatcher.on(event.step.finished, eventStub.stepFinished)
})
afterEach(() => {
event.cleanDispatcher()
})
it('should emit step.started and step.finished for MetaStep without child steps', () => {
const metaStep = new MetaStep('I', 'doSomething')
const fn = () => {
// MetaStep that doesn't call any child I steps
console.log('Just a console.log')
}
metaStep.run(fn)
// Should emit step.started and step.finished
expect(eventStub.stepStarted.calledOnce).to.be.true
expect(eventStub.stepFinished.calledOnce).to.be.true
expect(eventStub.stepStarted.firstCall.args[0]).to.equal(metaStep)
expect(eventStub.stepFinished.firstCall.args[0]).to.equal(metaStep)
})
it('should NOT emit events for MetaStep WITH child steps', () => {
const metaStep = new MetaStep('I', 'doSomethingWithChild')
const fn = () => {
// Simulate a child step being registered
const childStep = new Step({ helper: 'test' }, 'childAction')
event.emit(event.step.before, childStep)
}
metaStep.run(fn)
// Should NOT emit step.started and step.finished for the MetaStep
// because it has child steps
expect(eventStub.stepStarted.called).to.be.false
expect(eventStub.stepFinished.called).to.be.false
})
it('should emit events for async MetaStep without child steps', async () => {
const metaStep = new MetaStep('I', 'doSomethingAsync')
const fn = async () => {
// Async MetaStep that doesn't call any child I steps
await Promise.resolve()
console.log('Just an async operation')
}
await metaStep.run(fn)
// Should emit step.started and step.finished
expect(eventStub.stepStarted.calledOnce).to.be.true
expect(eventStub.stepFinished.calledOnce).to.be.true
expect(eventStub.stepStarted.firstCall.args[0]).to.equal(metaStep)
expect(eventStub.stepFinished.firstCall.args[0]).to.equal(metaStep)
})
it('should NOT emit events for async MetaStep WITH child steps', async () => {
const metaStep = new MetaStep('I', 'doSomethingAsyncWithChild')
const fn = async () => {
// Simulate a child step being registered
const childStep = new Step({ helper: 'test' }, 'childAction')
event.emit(event.step.before, childStep)
await Promise.resolve()
}
await metaStep.run(fn)
// Should NOT emit step.started and step.finished for the MetaStep
// because it has child steps
expect(eventStub.stepStarted.called).to.be.false
expect(eventStub.stepFinished.called).to.be.false
})
it('should set status to success when MetaStep completes without error', () => {
const metaStep = new MetaStep('I', 'doSomething')
const fn = () => {
return 'success'
}
metaStep.run(fn)
expect(metaStep.status).to.equal('success')
})
it('should set status to failed when MetaStep throws error', () => {
const metaStep = new MetaStep('I', 'doSomethingThatFails')
const fn = () => {
throw new Error('Test error')
}
try {
metaStep.run(fn)
} catch (err) {
// Expected to throw
}
expect(metaStep.status).to.equal('failed')
})
})