-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
/
interceptors-consumer.spec.ts
189 lines (182 loc) 路 5.97 KB
/
interceptors-consumer.spec.ts
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
179
180
181
182
183
184
185
186
187
188
189
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
import { AsyncLocalStorage } from 'async_hooks';
import { expect } from 'chai';
import { Observable, lastValueFrom, of, retry } from 'rxjs';
import * as sinon from 'sinon';
import { InterceptorsConsumer } from '../../interceptors/interceptors-consumer';
describe('InterceptorsConsumer', () => {
let consumer: InterceptorsConsumer;
let interceptors: any[];
beforeEach(() => {
consumer = new InterceptorsConsumer();
interceptors = [
{
intercept: sinon.stub().callsFake((ctx, handler) => handler.handle()),
},
{
intercept: sinon
.stub()
.callsFake(async (ctx, handler) => handler.handle()),
},
];
});
describe('intercept', () => {
describe('when interceptors array is empty', () => {
let next: sinon.SinonSpy;
beforeEach(() => {
next = sinon.spy();
});
it('should call next()', async () => {
await consumer.intercept([], null, { constructor: null }, null, next);
expect(next.calledOnce).to.be.true;
});
});
describe('when interceptors array is not empty', () => {
let next: sinon.SinonSpy;
beforeEach(() => {
next = sinon.stub().returns(Promise.resolve(''));
});
it('does not call `intercept` (lazy evaluation)', async () => {
await consumer.intercept(
interceptors,
null,
{ constructor: null },
null,
next,
);
expect(interceptors[0].intercept.called).to.be.false;
expect(interceptors[1].intercept.called).to.be.false;
});
it('should call every `intercept` method when subscribe', async () => {
const intercepted = await consumer.intercept(
interceptors,
null,
{ constructor: null },
null,
next,
);
await transformToResult(intercepted);
expect(interceptors[0].intercept.calledOnce).to.be.true;
expect(interceptors[1].intercept.calledOnce).to.be.true;
});
it('should not call `next` (lazy evaluation)', async () => {
await consumer.intercept(
interceptors,
null,
{ constructor: null },
null,
next,
);
expect(next.called).to.be.false;
});
it('should call `next` when subscribe', async () => {
const intercepted = await consumer.intercept(
interceptors,
null,
{ constructor: null },
null,
next,
);
await transformToResult(intercepted);
expect(next.called).to.be.true;
});
});
describe('when AsyncLocalStorage is used', () => {
it('should allow an interceptor to set values in AsyncLocalStorage that are accessible from the controller', async () => {
const storage = new AsyncLocalStorage<Record<string, any>>();
class StorageInterceptor implements NestInterceptor {
intercept(
_context: ExecutionContext,
next: CallHandler<any>,
): Observable<any> | Promise<Observable<any>> {
return storage.run({ value: 'hello' }, () => next.handle());
}
}
const next = () => {
return Promise.resolve(storage.getStore().value);
};
const intercepted = await consumer.intercept(
[new StorageInterceptor()],
null,
{ constructor: null },
null,
next,
);
const result = await transformToResult(intercepted);
expect(result).to.equal('hello');
});
});
describe('when retrying is enabled', () => {
it('should retry a specified amount of times', async () => {
let count = 0;
const next = () => {
count++;
if (count < 3) {
return Promise.reject(new Error('count not reached'));
}
return Promise.resolve(count);
};
class RetryInterceptor implements NestInterceptor {
intercept(
_context: ExecutionContext,
next: CallHandler<any>,
): Observable<any> | Promise<Observable<any>> {
return next.handle().pipe(retry(4));
}
}
const intercepted = await consumer.intercept(
[new RetryInterceptor()],
null,
{ constructor: null },
null,
next,
);
expect(await transformToResult(intercepted)).to.equal(3);
});
});
});
describe('createContext', () => {
it('should return execution context object', () => {
const instance = { constructor: {} };
const callback = () => null;
const context = consumer.createContext([], instance, callback);
expect(context.getClass()).to.be.eql(instance.constructor);
expect(context.getHandler()).to.be.eql(callback);
});
});
describe('transformDeferred', () => {
describe('when next() result is plain value', () => {
it('should return Observable', async () => {
const val = 3;
const next = async () => val;
expect(await lastValueFrom(consumer.transformDeferred(next))).to.be.eql(
val,
);
});
});
describe('when next() result is Promise', () => {
it('should return Observable', async () => {
const val = 3;
const next = async () => val;
expect(await lastValueFrom(consumer.transformDeferred(next))).to.be.eql(
val,
);
});
});
describe('when next() result is Observable', () => {
it('should return Observable', async () => {
const val = 3;
const next = async () => of(val);
expect(
await lastValueFrom(consumer.transformDeferred(next) as any),
).to.be.eql(val);
});
});
});
});
async function transformToResult(resultOrDeferred: any) {
if (resultOrDeferred && typeof resultOrDeferred.subscribe === 'function') {
return lastValueFrom(resultOrDeferred);
}
return resultOrDeferred;
}