Merge branch 'renovate/all-minor-patch' into 'main'
[ProtonMail-WebClient.git] / packages / testing / lib / features.ts
blob9cd25cd30a9a8c1670606b62054c778c07b8f11f
1 import type { FeatureCode } from '@proton/features';
3 import { addApiMock } from './api';
5 export const defaultFeatureFlagValue = {
6     Code: '',
7     Type: 'boolean',
8     Global: false,
9     DefaultValue: false,
10     Value: false,
11     UpdateTime: 1616511553,
12     Writable: true,
15 export const featureFlags: { [code: string]: any } = {};
17 export const getFeatureFlagsState = (features: [FeatureCode, boolean | object][], fetchedAt = Date.now()) => {
18     return Object.fromEntries(
19         features.map(([featureCode, value]) => {
20             return [
21                 featureCode,
22                 {
23                     value: {
24                         ...defaultFeatureFlagValue,
25                         Code: featureCode,
26                         Value: value,
27                     },
28                     meta: {
29                         fetchedAt,
30                     },
31                 },
32             ];
33         })
34     );
37 export const getFeatureFlags = (features: [FeatureCode, boolean | object][]) => {
38     return Object.fromEntries(
39         features.map(([featureCode, value]) => {
40             return [
41                 featureCode,
42                 {
43                     ...defaultFeatureFlagValue,
44                     Code: featureCode,
45                     Value: value,
46                 },
47             ];
48         })
49     );
52 export const registerFeatureFlagsApiMock = () => {
53     addApiMock(
54         'core/v4/features',
55         (args) => {
56             const { Code } = args.params;
57             const features: string[] = Code.split(',');
58             return {
59                 Features: features.map((code) =>
60                     featureFlags[code] ? featureFlags[code] : { ...defaultFeatureFlagValue, Code: code }
61                 ),
62             };
63         },
64         'get'
65     );