-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathgreeting.controller.unit.ts
More file actions
178 lines (138 loc) · 5.84 KB
/
greeting.controller.unit.ts
File metadata and controls
178 lines (138 loc) · 5.84 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright IBM Corp. and LoopBack contributors 2026. All Rights Reserved.
// Node module: @loopback/example-metrics-prometheus
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
createStubInstance,
expect,
sinon,
StubbedInstanceWithSinonAccessor,
} from '@loopback/testlab';
import {GreetingController} from '../../../controllers';
import {GreetingService} from '../../../services';
describe('GreetingController (unit)', () => {
let controller: GreetingController;
let greetingService: StubbedInstanceWithSinonAccessor<GreetingService>;
beforeEach(() => {
greetingService = createStubInstance(GreetingService);
controller = new GreetingController(greetingService);
});
describe('greet', () => {
it('calls greeting service with name', async () => {
const greeting = '[2026-02-11T12:00:00.000Z: 50] Hello, World';
greetingService.stubs.greet.resolves(greeting);
const result = await controller.greet('World');
expect(result).to.deepEqual([greeting]);
sinon.assert.calledWith(greetingService.stubs.greet, 'World');
});
it('returns single greeting by default', async () => {
const greeting = '[2026-02-11T12:00:00.000Z: 25] Hello, Alice';
greetingService.stubs.greet.resolves(greeting);
const result = await controller.greet('Alice');
expect(result).to.have.length(1);
expect(result[0]).to.equal(greeting);
});
it('returns multiple greetings when count is specified', async () => {
const greeting1 = '[2026-02-11T12:00:00.000Z: 10] Hello, Bob';
const greeting2 = '[2026-02-11T12:00:00.100Z: 20] Hello, Bob';
const greeting3 = '[2026-02-11T12:00:00.200Z: 30] Hello, Bob';
greetingService.stubs.greet
.onFirstCall()
.resolves(greeting1)
.onSecondCall()
.resolves(greeting2)
.onThirdCall()
.resolves(greeting3);
const result = await controller.greet('Bob', 3);
expect(result).to.have.length(3);
expect(result).to.deepEqual([greeting1, greeting2, greeting3]);
sinon.assert.calledThrice(greetingService.stubs.greet);
});
it('handles count parameter of 1', async () => {
const greeting = '[2026-02-11T12:00:00.000Z: 15] Hello, Charlie';
greetingService.stubs.greet.resolves(greeting);
const result = await controller.greet('Charlie', 1);
expect(result).to.have.length(1);
sinon.assert.calledOnce(greetingService.stubs.greet);
});
it('handles count parameter of 0', async () => {
const result = await controller.greet('Dave', 0);
expect(result).to.have.length(0);
sinon.assert.notCalled(greetingService.stubs.greet);
});
it('handles large count values', async () => {
const greeting = '[2026-02-11T12:00:00.000Z: 40] Hello, Eve';
greetingService.stubs.greet.resolves(greeting);
const result = await controller.greet('Eve', 10);
expect(result).to.have.length(10);
sinon.assert.callCount(greetingService.stubs.greet, 10);
});
it('calls service concurrently for multiple greetings', async () => {
const greeting = '[2026-02-11T12:00:00.000Z: 35] Hello, Frank';
greetingService.stubs.greet.resolves(greeting);
await controller.greet('Frank', 5);
// All calls should be made concurrently (Promise.all)
sinon.assert.callCount(greetingService.stubs.greet, 5);
greetingService.stubs.greet.getCalls().forEach(call => {
sinon.assert.calledWith(call, 'Frank');
});
});
it('handles different names', async () => {
greetingService.stubs.greet
.withArgs('Alice')
.resolves('[2026-02-11T12:00:00.000Z: 10] Hello, Alice');
greetingService.stubs.greet
.withArgs('Bob')
.resolves('[2026-02-11T12:00:00.000Z: 20] Hello, Bob');
const result1 = await controller.greet('Alice');
const result2 = await controller.greet('Bob');
expect(result1[0]).to.match(/Hello, Alice/);
expect(result2[0]).to.match(/Hello, Bob/);
});
it('handles empty name', async () => {
const greeting = '[2026-02-11T12:00:00.000Z: 5] Hello, ';
greetingService.stubs.greet.resolves(greeting);
const result = await controller.greet('');
expect(result).to.have.length(1);
sinon.assert.calledWith(greetingService.stubs.greet, '');
});
it('handles special characters in name', async () => {
const greeting = '[2026-02-11T12:00:00.000Z: 45] Hello, Test@123';
greetingService.stubs.greet.resolves(greeting);
const result = await controller.greet('Test@123');
expect(result[0]).to.equal(greeting);
sinon.assert.calledWith(greetingService.stubs.greet, 'Test@123');
});
});
describe('service injection', () => {
it('injects greeting service', () => {
expect(controller).to.have.property('greetingService');
});
it('uses service with interceptors', () => {
// The controller is configured to use asProxyWithInterceptors
// This ensures metrics interceptor can track the service calls
expect(controller).to.be.instanceOf(GreetingController);
});
});
describe('error handling', () => {
it('propagates service errors', async () => {
const error = new Error('Service error');
greetingService.stubs.greet.rejects(error);
await expect(controller.greet('ErrorTest')).to.be.rejectedWith(
'Service error',
);
});
it('handles rejection in one of multiple calls', async () => {
const error = new Error('One call failed');
greetingService.stubs.greet
.onFirstCall()
.resolves('[2026-02-11T12:00:00.000Z: 10] Hello, Test')
.onSecondCall()
.rejects(error);
await expect(controller.greet('Test', 2)).to.be.rejectedWith(
'One call failed',
);
});
});
});
// Made with Bob