-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathvariation.test.ts
87 lines (70 loc) · 3.03 KB
/
variation.test.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
import { getVariation } from '../variation'
import type { Emitter } from 'mitt'
import { type DefaultVariationEventPayload, Event } from '../types'
describe('getVariation', () => {
describe('without debug', () => {
it('should return the stored value when it exists', () => {
const storage = { testFlag: true, otherFlag: true, anotherFlag: false }
const mockMetricsHandler = jest.fn()
const mockEventBus: Emitter = {
emit: jest.fn(),
on: jest.fn(),
off: jest.fn(),
all: new Map()
}
const result = getVariation('testFlag', false, storage, mockMetricsHandler, mockEventBus)
expect(result).toBe(true)
expect(mockMetricsHandler).toHaveBeenCalledWith('testFlag', true)
expect(mockEventBus.emit).not.toHaveBeenCalled()
})
it('should return the default value and emit event when it is missing', () => {
const storage = {}
const mockMetricsHandler = jest.fn()
const mockEventBus: Emitter = {
emit: jest.fn(),
on: jest.fn(),
off: jest.fn(),
all: new Map()
}
const defaultValue = false
const result = getVariation('testFlag', defaultValue, storage, mockMetricsHandler, mockEventBus)
expect(result).toBe(defaultValue)
expect(mockMetricsHandler).not.toHaveBeenCalled()
const expectedEvent: DefaultVariationEventPayload = { flag: 'testFlag', defaultVariation: defaultValue }
expect(mockEventBus.emit).toHaveBeenCalledWith(Event.ERROR_DEFAULT_VARIATION_RETURNED, expectedEvent)
})
})
describe('with debug', () => {
const flagIdentifier = 'testFlag'
it('should return debug type with stored value', () => {
const storage = { testFlag: true, otherFlag: true, anotherFlag: false }
const mockMetricsHandler = jest.fn()
const mockEventBus: Emitter = {
emit: jest.fn(),
on: jest.fn(),
off: jest.fn(),
all: new Map()
}
const result = getVariation('testFlag', false, storage, mockMetricsHandler, mockEventBus, true)
expect(result).toEqual({ value: true, isDefaultValue: false })
expect(mockMetricsHandler).toHaveBeenCalledWith(flagIdentifier, true)
expect(mockEventBus.emit).not.toHaveBeenCalled()
})
it('should return debug type with default value when flag is missing', () => {
const storage = { otherFlag: true }
const mockMetricsHandler = jest.fn()
const mockEventBus: Emitter = {
emit: jest.fn(),
on: jest.fn(),
off: jest.fn(),
all: new Map()
}
const defaultValue = false
const result = getVariation('testFlag', defaultValue, storage, mockMetricsHandler, mockEventBus, true)
expect(result).toEqual({ value: defaultValue, isDefaultValue: true })
expect(mockMetricsHandler).not.toHaveBeenCalled()
const expectedEvent: DefaultVariationEventPayload = { flag: 'testFlag', defaultVariation: defaultValue }
expect(mockEventBus.emit).toHaveBeenCalledWith(Event.ERROR_DEFAULT_VARIATION_RETURNED, expectedEvent)
})
})
})