-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
/
methods-repl-fn.spec.ts
108 lines (87 loc) · 2.83 KB
/
methods-repl-fn.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
import { expect } from 'chai';
import * as sinon from 'sinon';
import { clc } from '@nestjs/common/utils/cli-colors.util';
import { MethodsReplFn } from '../../../repl/native-functions';
import { ReplContext } from '../../../repl/repl-context';
import { NestContainer } from '../../../injector/container';
describe('MethodsReplFn', () => {
let methodsReplFn: MethodsReplFn;
let replContext: ReplContext;
let mockApp: {
container: NestContainer;
get: sinon.SinonStub;
resolve: sinon.SinonSpy;
select: sinon.SinonSpy;
};
before(async () => {
const container = new NestContainer();
const aModuleRef = await container.addModule(class ModuleA {}, []);
const bModuleRef = await container.addModule(class ModuleB {}, []);
container.addController(class ControllerA {}, aModuleRef.token);
container.addProvider(class ProviderA1 {}, aModuleRef.token);
container.addProvider(class ProviderA2 {}, aModuleRef.token);
container.addProvider(class ProviderB1 {}, bModuleRef.token);
container.addProvider(class ProviderB2 {}, bModuleRef.token);
mockApp = {
container,
get: sinon.stub(),
resolve: sinon.spy(),
select: sinon.spy(),
};
replContext = new ReplContext(mockApp as any);
});
beforeEach(() => {
methodsReplFn = replContext.nativeFunctions.get('methods') as MethodsReplFn;
// To avoid coloring the output:
sinon.stub(clc, 'yellow').callsFake(text => text);
sinon.stub(clc, 'green').callsFake(text => text);
});
afterEach(() => sinon.restore());
it('the function name should be "methods"', () => {
expect(methodsReplFn).to.not.be.undefined;
expect(methodsReplFn.fnDefinition.name).to.eql('methods');
});
describe('action', () => {
describe('when token is a class reference', () => {
it('should print all class methods', () => {
class BaseService {
create() {}
}
class TestService extends BaseService {
findAll() {}
findOne() {}
}
let outputText = '';
sinon
.stub(replContext, 'writeToStdout')
.callsFake(text => (outputText += text));
methodsReplFn.action(TestService);
expect(outputText).to.equal(`
Methods:
◻ findAll
◻ findOne
◻ create
`);
});
});
describe('when token is a string', () => {
it('should grab provider from the container and print its all methods', () => {
class ProviderA1 {
findAll() {}
findOne() {}
}
let outputText = '';
sinon
.stub(replContext, 'writeToStdout')
.callsFake(text => (outputText += text));
mockApp.get.callsFake(() => new ProviderA1());
methodsReplFn.action('ProviderA1');
expect(outputText).to.equal(`
Methods:
◻ findAll
◻ findOne
`);
});
});
});
});