Merge branch 'renovate/all-minor-patch' into 'main'
[ProtonMail-WebClient.git] / packages / testing / lib / mockUseFeature.ts
blob1c7a5bdb4b59c94d1213f1a3895723ccc769e003
1 import * as useFeatureModule from '@proton/features/useFeature';
2 import type { DeepPartial } from '@proton/shared/lib/interfaces';
4 type HookReturnType = ReturnType<typeof useFeatureModule.default>;
6 /**
7  * Basic and raw useFeature mock
8  *
9  * Example:
10  * ```
11  * const mockedUseFeature = mockUseFeature({ feature: { Value: true } });
12  * expect(mockedUseFeature.code).toBe('FeatureCode');
13  * ```
14  *
15  * More advanced mocks will be needed in case of components with multiple calls to useFeature
16  */
17 export const mockUseFeature = (value?: DeepPartial<ReturnType<typeof useFeatureModule.default>>) => {
18     const mockedUseFeature = jest.spyOn(useFeatureModule, 'default');
20     mockedUseFeature.mockImplementation(
21         (code) =>
22             ({
23                 feature: {
24                     Type: 'boolean',
25                     DefaultValue: false,
26                     Value: false,
27                     Minimum: 0,
28                     Maximum: 1,
29                     Global: false,
30                     Writable: false,
31                     ExpirationTime: -1,
32                     UpdateTime: -1,
33                     Options: [],
34                     ...value?.feature,
35                     Code: code,
36                 },
37                 loading: false,
38                 get: jest.fn(),
39                 update: jest.fn(),
40                 ...value,
41                 code,
42             }) as HookReturnType
43     );
45     return mockedUseFeature;