-
Notifications
You must be signed in to change notification settings - Fork 7
/
braintree.module.spec.ts
110 lines (96 loc) · 3.4 KB
/
braintree.module.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
import { Test, TestingModule } from '@nestjs/testing';
import * as braintree from 'braintree';
import { ConfigService, ConfigModule } from 'nestjs-config';
import * as path from 'path';
import {
BraintreeModule,
BRAINTREE_OPTIONS_PROVIDER,
BraintreeProvider,
InjectBraintreeProvider,
} from '..';
import { Injectable, Module } from '@nestjs/common';
describe('Braintree Module', () => {
it('Does it instance with options using registry', async () => {
const module = await Test.createTestingModule({
imports: [
BraintreeModule.forRoot({
environment: braintree.Environment.Sandbox,
merchantId: 'merchantId',
publicKey: 'publicKey',
privateKey: 'privateKey',
}),
],
}).compile();
const options = module.get(BRAINTREE_OPTIONS_PROVIDER);
const provider = module.get<BraintreeProvider>(BraintreeProvider);
expect(options.environment).toBe(braintree.Environment.Sandbox);
expect(options.merchantId).toBe('merchantId');
expect(options.publicKey).toBe('publicKey');
expect(options.privateKey).toBe('privateKey');
expect(provider).toBeInstanceOf(BraintreeProvider);
});
it('Does it instance with options using async registry', async () => {
const module = await Test.createTestingModule({
imports: [
ConfigModule.load(
path.resolve(__dirname, '__stubs__', 'config', '*.ts'),
),
BraintreeModule.forRootAsync({
useFactory: config => config.get('braintree'),
inject: [ConfigService],
}),
],
}).compile();
const options = module.get(BRAINTREE_OPTIONS_PROVIDER);
const provider = module.get<BraintreeProvider>(BraintreeProvider);
expect(options.environment).toBe(braintree.Environment.Sandbox);
expect(typeof options.merchantId).toBe('string');
expect(typeof options.publicKey).toBe('string');
expect(typeof options.privateKey).toBe('string');
expect(provider).toBeInstanceOf(BraintreeProvider);
});
it('BraintreeProvider is avaliable to providers', async () => {
class TestProvider {
constructor(
@InjectBraintreeProvider()
private readonly braintreeProvider: BraintreeProvider,
) {}
getProvider(): BraintreeProvider {
return this.braintreeProvider;
}
}
const module = await Test.createTestingModule({
imports: [
ConfigModule.load(
path.resolve(__dirname, '__stubs__', 'config', '*.ts'),
),
BraintreeModule.forRootAsync({
useFactory: config => config.get('braintree'),
inject: [ConfigService],
}),
],
providers: [TestProvider],
}).compile();
const provider = module.get<TestProvider>(TestProvider);
expect(provider.getProvider()).toBeInstanceOf(BraintreeProvider);
});
it('BraintreeModule.forFeature', async () => {
@Module({
imports: [BraintreeModule.forFeature()],
})
class TestModule {}
const module: TestingModule = await Test.createTestingModule({
imports: [
BraintreeModule.forRoot({
environment: braintree.Environment.Sandbox,
merchantId: 'merchantId',
publicKey: 'publicKey',
privateKey: 'privateKey',
}),
TestModule,
],
}).compile();
const testProvider = module.select(TestModule).get(BraintreeProvider);
expect(testProvider).toBeInstanceOf(BraintreeProvider);
});
});