convert AlertRuleList test to RTL (#55045)

This commit is contained in:
Ashley Harrison 2022-09-13 09:29:29 +01:00 committed by GitHub
parent ab045184e4
commit 031c186617
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 68 additions and 65 deletions

View File

@ -59,9 +59,6 @@ exports[`no enzyme tests`] = {
"public/app/core/components/Select/MetricSelect.test.tsx:1074737147": [ "public/app/core/components/Select/MetricSelect.test.tsx:1074737147": [
[0, 19, 13, "RegExp match", "2409514259"] [0, 19, 13, "RegExp match", "2409514259"]
], ],
"public/app/features/alerting/AlertRuleList.test.tsx:2998938420": [
[0, 19, 13, "RegExp match", "2409514259"]
],
"public/app/features/dimensions/editors/ThresholdsEditor/ThresholdsEditor.test.tsx:145048794": [ "public/app/features/dimensions/editors/ThresholdsEditor/ThresholdsEditor.test.tsx:145048794": [
[0, 17, 13, "RegExp match", "2409514259"] [0, 17, 13, "RegExp match", "2409514259"]
], ],
@ -3189,9 +3186,6 @@ exports[`better eslint`] = {
"public/app/features/admin/ldap/LdapPage.tsx:5381": [ "public/app/features/admin/ldap/LdapPage.tsx:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"] [0, 0, 0, "Unexpected any. Specify a different type.", "0"]
], ],
"public/app/features/alerting/AlertRuleList.test.tsx:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
],
"public/app/features/alerting/AlertRuleList.tsx:5381": [ "public/app/features/alerting/AlertRuleList.tsx:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"], [0, 0, 0, "Do not use any type assertions.", "0"],
[0, 0, 0, "Do not use any type assertions.", "1"] [0, 0, 0, "Do not use any type assertions.", "1"]

View File

@ -1,13 +1,13 @@
import { shallow } from 'enzyme'; import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react'; import React from 'react';
import { openMenu } from 'react-select-event';
import { mockToolkitActionCreator } from 'test/core/redux/mocks'; import { mockToolkitActionCreator } from 'test/core/redux/mocks';
import { NavModel } from '@grafana/data';
import { locationService } from '@grafana/runtime'; import { locationService } from '@grafana/runtime';
import { getRouteComponentProps } from 'app/core/navigation/__mocks__/routeProps'; import { getRouteComponentProps } from 'app/core/navigation/__mocks__/routeProps';
import appEvents from '../../core/app_events'; import appEvents from '../../core/app_events';
import { AlertRule } from '../../types';
import { ShowModalReactEvent } from '../../types/events'; import { ShowModalReactEvent } from '../../types/events';
import { AlertHowToModal } from './AlertHowToModal'; import { AlertHowToModal } from './AlertHowToModal';
@ -18,92 +18,101 @@ jest.mock('../../core/app_events', () => ({
publish: jest.fn(), publish: jest.fn(),
})); }));
const setup = (propOverrides?: object) => { const defaultProps: Props = {
const props: Props = {
...getRouteComponentProps({}), ...getRouteComponentProps({}),
navModel: {} as NavModel, navModel: {
alertRules: [] as AlertRule[], main: {
getAlertRulesAsync: jest.fn(), text: 'foo',
setSearchQuery: mockToolkitActionCreator(setSearchQuery), },
togglePauseAlertRule: jest.fn(), node: {
text: 'foo',
},
},
search: '', search: '',
isLoading: false, isLoading: false,
alertRules: [],
getAlertRulesAsync: jest.fn().mockResolvedValue([]),
setSearchQuery: mockToolkitActionCreator(setSearchQuery),
togglePauseAlertRule: jest.fn(),
}; };
Object.assign(props, propOverrides); const setup = (propOverrides?: object) => {
const props: Props = {
...defaultProps,
...propOverrides,
};
const wrapper = shallow(<AlertRuleListUnconnected {...props} />); const { rerender } = render(<AlertRuleListUnconnected {...props} />);
return { return {
wrapper, rerender,
instance: wrapper.instance() as AlertRuleListUnconnected,
}; };
}; };
describe('Life cycle', () => { afterEach(() => {
describe('component did mount', () => { jest.clearAllMocks();
it('should call fetchrules', () => {
const { instance } = setup();
instance.fetchRules = jest.fn();
instance.componentDidMount();
expect(instance.fetchRules).toHaveBeenCalled();
});
}); });
describe('component did update', () => { describe('AlertRuleList', () => {
it('should call fetchrules if props differ', () => { it('should call fetchrules when mounting', () => {
const { instance } = setup(); jest.spyOn(AlertRuleListUnconnected.prototype, 'fetchRules');
instance.fetchRules = jest.fn();
instance.componentDidUpdate({ queryParams: { state: 'ok' } } as any); expect(AlertRuleListUnconnected.prototype.fetchRules).not.toHaveBeenCalled();
setup();
expect(instance.fetchRules).toHaveBeenCalled(); expect(AlertRuleListUnconnected.prototype.fetchRules).toHaveBeenCalled();
}); });
});
it('should call fetchrules when props change', () => {
const fetchRulesSpy = jest.spyOn(AlertRuleListUnconnected.prototype, 'fetchRules');
expect(AlertRuleListUnconnected.prototype.fetchRules).not.toHaveBeenCalled();
const { rerender } = setup();
expect(AlertRuleListUnconnected.prototype.fetchRules).toHaveBeenCalled();
fetchRulesSpy.mockReset();
rerender(<AlertRuleListUnconnected {...defaultProps} queryParams={{ state: 'ok' }} />);
expect(AlertRuleListUnconnected.prototype.fetchRules).toHaveBeenCalled();
}); });
describe('Functions', () => {
describe('Get state filter', () => { describe('Get state filter', () => {
it('should get all if prop is not set', () => { it('should be all if prop is not set', () => {
const { instance } = setup(); setup();
const stateFilter = instance.getStateFilter(); expect(screen.getByText('All')).toBeInTheDocument();
expect(stateFilter).toEqual('all');
}); });
it('should return state filter if set', () => { it('should return state filter if set', () => {
const { instance } = setup({ setup({
queryParams: { state: 'ok' }, queryParams: { state: 'not_ok' },
}); });
expect(screen.getByText('Not OK')).toBeInTheDocument();
const stateFilter = instance.getStateFilter();
expect(stateFilter).toEqual('ok');
}); });
}); });
describe('State filter changed', () => { describe('State filter changed', () => {
it('should update location', () => { it('should update location', async () => {
const { instance } = setup(); setup();
const mockEvent = { value: 'alerting' }; const stateFilterSelect = screen.getByLabelText('States');
instance.onStateFilterChanged(mockEvent); openMenu(stateFilterSelect);
expect(locationService.getSearchObject().state).toBe('alerting'); await userEvent.click(screen.getByText('Not OK'));
expect(locationService.getSearchObject().state).toBe('not_ok');
}); });
}); });
describe('Open how to', () => { describe('Open how to', () => {
it('should emit show-modal event', () => { it('should emit show-modal event', async () => {
const { instance } = setup(); setup();
instance.onOpenHowTo();
await userEvent.click(screen.getByRole('button', { name: 'How to add an alert' }));
expect(appEvents.publish).toHaveBeenCalledWith(new ShowModalReactEvent({ component: AlertHowToModal })); expect(appEvents.publish).toHaveBeenCalledWith(new ShowModalReactEvent({ component: AlertHowToModal }));
}); });
}); });
describe('Search query change', () => { describe('Search query change', () => {
it('should set search query', () => { it('should set search query', async () => {
const { instance } = setup(); setup();
instance.onSearchQueryChange('dashboard');
expect(instance.props.setSearchQuery).toHaveBeenCalledWith('dashboard'); await userEvent.click(screen.getByPlaceholderText('Search alerts'));
await userEvent.paste('dashboard');
expect(defaultProps.setSearchQuery).toHaveBeenCalledWith('dashboard');
}); });
}); });
}); });