-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathunleash.service.spec.ts
188 lines (156 loc) · 4.99 KB
/
unleash.service.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
import { Test, TestingModule } from '@nestjs/testing'
import {
UnleashStrategiesService,
UnleashStrategy,
UNLEASH_MODULE_OPTIONS,
} from '..'
import { ToggleEntity } from './entity/toggle.entity'
import { MetricsService } from './metrics.service'
import { ToggleRepository } from './repository/toggle-repository'
import { UnleashContext } from './unleash.context'
import { UnleashService } from './unleash.service'
jest.mock('@nestjs/common/services/logger.service')
type Optional<T, K extends keyof T> = Omit<T, K> & Partial<T>
function createFeatureToggle(
params: Optional<
ToggleEntity,
'id' | 'enabled' | 'description' | 'strategies'
>,
): ToggleEntity {
return {
strategies: [],
enabled: true,
description: '',
id: params.name,
...params,
}
}
class AlwaysOnStrategy implements UnleashStrategy {
name = 'alwaysOn'
isEnabled(_parameters: unknown, _context: UnleashContext): boolean {
return true
}
}
class AlwaysOffStrategy implements UnleashStrategy {
name = 'alwaysOff'
isEnabled(_parameters: unknown, _context: UnleashContext): boolean {
return false
}
}
class AlwaysThrowsStrategy implements UnleashStrategy {
name = 'alwaysThrows'
isEnabled(_parameters: unknown, _context: UnleashContext): boolean {
throw new Error('ohoh')
}
}
describe('UnleashService', () => {
let service: UnleashService
let toggles: ToggleRepository
let metrics: jest.Mocked<MetricsService>
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ToggleRepository,
UnleashService,
{
provide: UnleashStrategiesService,
useValue: {
find(name: string): UnleashStrategy | undefined {
return [
new AlwaysOffStrategy(),
new AlwaysOnStrategy(),
new AlwaysThrowsStrategy(),
].find((strategy) => strategy.name === name)
},
},
},
{ provide: MetricsService, useValue: { increase: jest.fn() } },
{ provide: UNLEASH_MODULE_OPTIONS, useValue: {} },
UnleashContext,
],
}).compile()
metrics = module.get(MetricsService)
toggles = module.get(ToggleRepository)
service = await module.resolve(UnleashService)
})
describe('_isEnabled()', () => {
it('returns the default value if the feature is not found', () => {
expect(service.isEnabled('foo', true)).toBe(true)
expect(service.isEnabled('foo', false)).toBe(false)
})
it('returns the default value if the feature exists, but is disabled', () => {
toggles.create(createFeatureToggle({ name: 'foo', enabled: false }))
expect(service.isEnabled('foo', false)).toBe(false)
expect(service.isEnabled('foo', true)).toBe(true)
})
describe('returns the enabled property if no strategy is found', () => {
test('enabled: true', () => {
toggles.create(createFeatureToggle({ name: 'foo', enabled: true }))
expect(service.isEnabled('foo')).toBe(true)
})
test('enabled: false', () => {
toggles.create(createFeatureToggle({ name: 'foo', enabled: false }))
expect(service.isEnabled('foo')).toBe(false)
})
})
it('returns false if the strategy is not found', () => {
toggles.create(
createFeatureToggle({
name: 'foo',
strategies: [{ name: 'something', parameters: {} }],
}),
)
expect(service.isEnabled('foo')).toBe(false)
})
describe('strategy testing', () => {
test('enabled: true', () => {
toggles.create(
createFeatureToggle({
name: 'foo',
strategies: [{ name: 'alwaysOn', parameters: {} }],
}),
)
expect(service.isEnabled('foo')).toBe(true)
})
test('enabled: false', () => {
toggles.create(
createFeatureToggle({
name: 'foo',
strategies: [{ name: 'alwaysOff', parameters: {} }],
}),
)
expect(service.isEnabled('foo')).toBe(false)
})
it('interprets exceptions as `false`', () => {
toggles.create(
createFeatureToggle({
name: 'foo',
strategies: [{ name: 'alwaysThrows', parameters: {} }],
}),
)
expect(service.isEnabled('foo')).toBe(false)
// @ts-ignore
expect(service.logger.error).toHaveBeenCalledWith(
'ohoh',
expect.any(String), // stack
)
})
it('warns when a stale toggle is used', () => {
toggles.create(
createFeatureToggle({
name: 'foo',
strategies: [],
stale: true,
}),
)
service.isEnabled('foo')
// @ts-ignore
expect(service.logger.warn).toHaveBeenCalledWith('Toggle is stale: foo')
})
})
test('isEnabled()', () => {
expect(service.isEnabled('foo')).toBe(false)
expect(metrics.increase).toHaveBeenCalledWith('foo', false)
})
})
})