|
| 1 | +import { FeaturesConfigEntity } from 'src/modules/feature/entities/features-config.entity'; |
| 2 | +import { |
| 3 | + FeatureConfig, |
| 4 | + FeatureConfigFilter, FeatureConfigFilterAnd, FeatureConfigFilterOr, |
| 5 | + FeaturesConfig, |
| 6 | + FeaturesConfigData, |
| 7 | +} from 'src/modules/feature/model/features-config'; |
| 8 | +import { classToClass } from 'src/utils'; |
| 9 | +import { Feature } from 'src/modules/feature/model/feature'; |
| 10 | +import { FeatureEntity } from 'src/modules/feature/entities/feature.entity'; |
| 11 | +import { mockAppSettings } from 'src/__mocks__/app-settings'; |
| 12 | +import config from 'src/utils/config'; |
| 13 | +import { KnownFeatures } from 'src/modules/feature/constants'; |
| 14 | +import * as defaultConfig from '../../config/features-config.json'; |
| 15 | + |
| 16 | +export const mockFeaturesConfigId = '1'; |
| 17 | +export const mockFeaturesConfigVersion = defaultConfig.version + 0.111; |
| 18 | +export const mockControlNumber = 7.68; |
| 19 | +export const mockControlGroup = '7'; |
| 20 | + |
| 21 | +export const mockFeaturesConfigJson = { |
| 22 | + version: mockFeaturesConfigVersion, |
| 23 | + features: { |
| 24 | + [KnownFeatures.InsightsRecommendations]: { |
| 25 | + perc: [[1.25, 8.45]], |
| 26 | + flag: true, |
| 27 | + filters: [ |
| 28 | + { |
| 29 | + name: 'agreements.analytics', |
| 30 | + value: true, |
| 31 | + cond: 'eq', |
| 32 | + }, |
| 33 | + ], |
| 34 | + }, |
| 35 | + }, |
| 36 | +}; |
| 37 | + |
| 38 | +export const mockFeaturesConfigJsonComplex = { |
| 39 | + ...mockFeaturesConfigJson, |
| 40 | + features: { |
| 41 | + [KnownFeatures.InsightsRecommendations]: { |
| 42 | + ...mockFeaturesConfigJson.features[KnownFeatures.InsightsRecommendations], |
| 43 | + filters: [ |
| 44 | + { |
| 45 | + or: [ |
| 46 | + { |
| 47 | + name: 'settings.testValue', |
| 48 | + value: 'test', |
| 49 | + cond: 'eq', |
| 50 | + }, |
| 51 | + { |
| 52 | + and: [ |
| 53 | + { |
| 54 | + name: 'agreements.analytics', |
| 55 | + value: true, |
| 56 | + cond: 'eq', |
| 57 | + }, |
| 58 | + { |
| 59 | + or: [ |
| 60 | + { |
| 61 | + name: 'settings.scanThreshold', |
| 62 | + value: mockAppSettings.scanThreshold, |
| 63 | + cond: 'eq', |
| 64 | + }, |
| 65 | + { |
| 66 | + name: 'settings.batchSize', |
| 67 | + value: mockAppSettings.batchSize, |
| 68 | + cond: 'eq', |
| 69 | + }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + }, |
| 79 | +}; |
| 80 | + |
| 81 | +export const mockFeaturesConfigData = Object.assign(new FeaturesConfigData(), { |
| 82 | + ...mockFeaturesConfigJson, |
| 83 | + features: new Map(Object.entries({ |
| 84 | + [KnownFeatures.InsightsRecommendations]: Object.assign(new FeatureConfig(), { |
| 85 | + ...mockFeaturesConfigJson.features[KnownFeatures.InsightsRecommendations], |
| 86 | + filters: [ |
| 87 | + Object.assign(new FeatureConfigFilter(), { |
| 88 | + ...mockFeaturesConfigJson.features[KnownFeatures.InsightsRecommendations].filters[0], |
| 89 | + }), |
| 90 | + ], |
| 91 | + }), |
| 92 | + })), |
| 93 | +}); |
| 94 | + |
| 95 | +export const mockFeaturesConfigDataComplex = Object.assign(new FeaturesConfigData(), { |
| 96 | + ...mockFeaturesConfigJson, |
| 97 | + features: new Map(Object.entries({ |
| 98 | + [KnownFeatures.InsightsRecommendations]: Object.assign(new FeatureConfig(), { |
| 99 | + ...mockFeaturesConfigJson.features[KnownFeatures.InsightsRecommendations], |
| 100 | + filters: [ |
| 101 | + Object.assign(new FeatureConfigFilterOr(), { |
| 102 | + or: [ |
| 103 | + Object.assign(new FeatureConfigFilter(), { |
| 104 | + name: 'settings.testValue', |
| 105 | + value: 'test', |
| 106 | + cond: 'eq', |
| 107 | + }), |
| 108 | + Object.assign(new FeatureConfigFilterAnd(), { |
| 109 | + and: [ |
| 110 | + Object.assign(new FeatureConfigFilter(), { |
| 111 | + name: 'agreements.analytics', |
| 112 | + value: true, |
| 113 | + cond: 'eq', |
| 114 | + }), |
| 115 | + Object.assign(new FeatureConfigFilterOr(), { |
| 116 | + or: [ |
| 117 | + Object.assign(new FeatureConfigFilter(), { |
| 118 | + name: 'settings.scanThreshold', |
| 119 | + value: mockAppSettings.scanThreshold, |
| 120 | + cond: 'eq', |
| 121 | + }), |
| 122 | + Object.assign(new FeatureConfigFilter(), { |
| 123 | + name: 'settings.batchSize', |
| 124 | + value: mockAppSettings.batchSize, |
| 125 | + cond: 'eq', |
| 126 | + }), |
| 127 | + ], |
| 128 | + }), |
| 129 | + ], |
| 130 | + }), |
| 131 | + ], |
| 132 | + }), |
| 133 | + ], |
| 134 | + }), |
| 135 | + })), |
| 136 | +}); |
| 137 | + |
| 138 | +export const mockFeaturesConfig = Object.assign(new FeaturesConfig(), { |
| 139 | + controlNumber: mockControlNumber, |
| 140 | + data: mockFeaturesConfigData, |
| 141 | +}); |
| 142 | + |
| 143 | +export const mockFeaturesConfigComplex = Object.assign(new FeaturesConfig(), { |
| 144 | + controlNumber: mockControlNumber, |
| 145 | + data: mockFeaturesConfigDataComplex, |
| 146 | +}); |
| 147 | + |
| 148 | +export const mockFeaturesConfigEntity = Object.assign(new FeaturesConfigEntity(), { |
| 149 | + ...classToClass(FeaturesConfigEntity, mockFeaturesConfig), |
| 150 | + id: mockFeaturesConfigId, |
| 151 | +}); |
| 152 | + |
| 153 | +export const mockFeaturesConfigEntityComplex = Object.assign(new FeaturesConfigEntity(), { |
| 154 | + ...classToClass(FeaturesConfigEntity, mockFeaturesConfigComplex), |
| 155 | + id: mockFeaturesConfigId, |
| 156 | +}); |
| 157 | + |
| 158 | +export const mockFeature = Object.assign(new Feature(), { |
| 159 | + name: KnownFeatures.InsightsRecommendations, |
| 160 | + flag: true, |
| 161 | +}); |
| 162 | + |
| 163 | +export const mockUnknownFeature = Object.assign(new Feature(), { |
| 164 | + name: 'unknown', |
| 165 | + flag: true, |
| 166 | +}); |
| 167 | + |
| 168 | +export const mockFeatureEntity = Object.assign(new FeatureEntity(), { |
| 169 | + id: 'lr-1', |
| 170 | + name: KnownFeatures.InsightsRecommendations, |
| 171 | + flag: true, |
| 172 | +}); |
| 173 | + |
| 174 | +export const mockServerState = { |
| 175 | + settings: mockAppSettings, |
| 176 | + agreements: mockAppSettings.agreements, |
| 177 | + config: config.get(), |
| 178 | + env: process.env, |
| 179 | +}; |
| 180 | + |
| 181 | +export const mockFeaturesConfigRepository = jest.fn(() => ({ |
| 182 | + getOrCreate: jest.fn().mockResolvedValue(mockFeaturesConfig), |
| 183 | + update: jest.fn().mockResolvedValue(mockFeaturesConfig), |
| 184 | +})); |
| 185 | + |
| 186 | +export const mockFeatureRepository = jest.fn(() => ({ |
| 187 | + get: jest.fn().mockResolvedValue(mockFeature), |
| 188 | + upsert: jest.fn().mockResolvedValue({ updated: 1 }), |
| 189 | + list: jest.fn().mockResolvedValue([mockFeature]), |
| 190 | + delete: jest.fn().mockResolvedValue({ deleted: 1 }), |
| 191 | +})); |
| 192 | + |
| 193 | +export const mockFeaturesConfigService = jest.fn(() => ({ |
| 194 | + sync: jest.fn(), |
| 195 | + getControlInfo: jest.fn().mockResolvedValue({ |
| 196 | + controlNumber: mockControlNumber, |
| 197 | + controlGroup: mockControlGroup, |
| 198 | + }), |
| 199 | +})); |
| 200 | + |
| 201 | +export const mockFeatureService = jest.fn(() => ({ |
| 202 | + isFeatureEnabled: jest.fn().mockResolvedValue(true), |
| 203 | +})); |
| 204 | + |
| 205 | +export const mockFeatureAnalytics = jest.fn(() => ({ |
| 206 | + sendFeatureFlagConfigUpdated: jest.fn(), |
| 207 | + sendFeatureFlagConfigUpdateError: jest.fn(), |
| 208 | + sendFeatureFlagInvalidRemoteConfig: jest.fn(), |
| 209 | + sendFeatureFlagRecalculated: jest.fn(), |
| 210 | +})); |
| 211 | + |
| 212 | +export const mockInsightsRecommendationsFlagStrategy = { |
| 213 | + calculate: jest.fn().mockResolvedValue(true), |
| 214 | +}; |
| 215 | + |
| 216 | +export const mockFeatureFlagProvider = jest.fn(() => ({ |
| 217 | + getStrategy: jest.fn().mockResolvedValue(mockInsightsRecommendationsFlagStrategy), |
| 218 | + calculate: jest.fn().mockResolvedValue(true), |
| 219 | +})); |
0 commit comments