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