-
Notifications
You must be signed in to change notification settings - Fork 694
Expand file tree
/
Copy pathUserPreferenceCheckboxField.spec.tsx
More file actions
71 lines (60 loc) · 2.85 KB
/
UserPreferenceCheckboxField.spec.tsx
File metadata and controls
71 lines (60 loc) · 2.85 KB
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
import * as React from 'react';
import { Checkbox, Skeleton } from '@patternfly/react-core';
import { shallow, ShallowWrapper } from 'enzyme';
import { UserPreferenceFieldType } from '@console/dynamic-plugin-sdk/src/extensions/user-preferences';
import { useUserSettings, useTelemetry } from '@console/shared';
import UserPreferenceCheckboxField from '../UserPreferenceCheckboxField';
jest.mock('@console/shared/src/hooks/useUserSettings', () => ({
useUserSettings: jest.fn(),
}));
jest.mock('@console/shared/src/hooks/useTelemetry', () => ({
useTelemetry: jest.fn(),
}));
const mockUserSettings = useUserSettings as jest.Mock;
const mockUseTelemetry = useTelemetry as jest.Mock;
describe('UserPreferenceCheckboxField', () => {
type UserPreferenceCheckboxFieldProps = React.ComponentProps<typeof UserPreferenceCheckboxField>;
const props: UserPreferenceCheckboxFieldProps = {
type: UserPreferenceFieldType.checkbox,
id: 'id',
userSettingsKey: '',
label: 'label',
trueValue: 'trueValue',
falseValue: 'falseValue',
};
let wrapper: ShallowWrapper<UserPreferenceCheckboxFieldProps>;
beforeEach(() => {
mockUseTelemetry.mockReturnValue(jest.fn());
});
afterEach(() => {
jest.resetAllMocks();
});
it('should render skeleton if user preferences have not loaded', () => {
mockUserSettings.mockReturnValue(['', () => {}, false]);
wrapper = shallow(<UserPreferenceCheckboxField {...props} />);
expect(wrapper.find(Skeleton).exists()).toBeTruthy();
});
it('should render checkbox if user preferences have loaded', () => {
mockUserSettings.mockReturnValue(['trueValue', () => {}, true]);
wrapper = shallow(<UserPreferenceCheckboxField {...props} />);
expect(wrapper.find(Checkbox).exists()).toBeTruthy();
});
it('should render with isChecked true if defaultValue is equal to trueValue and user preference has loaded but is not defined', () => {
mockUserSettings.mockImplementation(() => {
const [val, setVal] = React.useState('');
return [val, setVal, true];
});
wrapper = shallow(<UserPreferenceCheckboxField {...props} defaultValue="trueValue" />);
expect(wrapper.find(Checkbox).props().isChecked).toBe(true);
});
it('should render with isChecked true if user preference has loaded and is equal to trueValue', () => {
mockUserSettings.mockReturnValue(['trueValue', () => {}, true]);
wrapper = shallow(<UserPreferenceCheckboxField {...props} defaultValue="falseValue" />);
expect(wrapper.find(Checkbox).props().isChecked).toBe(true);
});
it('should render with isChecked false if user preference has loaded and is equal to falseValue', () => {
mockUserSettings.mockReturnValue(['falseValue', () => {}, true]);
wrapper = shallow(<UserPreferenceCheckboxField {...props} defaultValue="trueValue" />);
expect(wrapper.find(Checkbox).props().isChecked).toBe(false);
});
});