mirror of https://github.com/grafana/grafana
Merge pull request #14786 from grafana/14729-move-testrulebutton-to-toolbar
14729 move testrulebutton to toolbarpull/14813/head
commit
b25214bfe1
@ -0,0 +1,44 @@ |
||||
import React from 'react'; |
||||
import { shallow } from 'enzyme'; |
||||
import { DashboardModel } from '../dashboard/dashboard_model'; |
||||
import { Props, TestRuleButton } from './TestRuleButton'; |
||||
|
||||
jest.mock('app/core/services/backend_srv', () => ({ |
||||
getBackendSrv: () => ({ |
||||
post: jest.fn(), |
||||
}), |
||||
})); |
||||
|
||||
const setup = (propOverrides?: object) => { |
||||
const props: Props = { |
||||
panelId: 1, |
||||
dashboard: new DashboardModel({ panels: [{ id: 1 }] }), |
||||
LoadingPlaceholder: {}, |
||||
}; |
||||
|
||||
Object.assign(props, propOverrides); |
||||
|
||||
const wrapper = shallow(<TestRuleButton {...props} />); |
||||
|
||||
return { wrapper, instance: wrapper.instance() as TestRuleButton }; |
||||
}; |
||||
|
||||
describe('Render', () => { |
||||
it('should render component', () => { |
||||
const { wrapper } = setup(); |
||||
|
||||
expect(wrapper).toMatchSnapshot(); |
||||
}); |
||||
}); |
||||
|
||||
describe('Life cycle', () => { |
||||
describe('component did mount', () => { |
||||
it('should call testRule', () => { |
||||
const { instance } = setup(); |
||||
instance.testRule = jest.fn(); |
||||
instance.componentDidMount(); |
||||
|
||||
expect(instance.testRule).toHaveBeenCalled(); |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,44 @@ |
||||
import React, { PureComponent } from 'react'; |
||||
import { JSONFormatter } from 'app/core/components/JSONFormatter/JSONFormatter'; |
||||
import { getBackendSrv } from 'app/core/services/backend_srv'; |
||||
import { DashboardModel } from '../dashboard/dashboard_model'; |
||||
|
||||
export interface Props { |
||||
panelId: number; |
||||
dashboard: DashboardModel; |
||||
LoadingPlaceholder: any; |
||||
} |
||||
|
||||
interface State { |
||||
isLoading: boolean; |
||||
testRuleResponse: {}; |
||||
} |
||||
|
||||
export class TestRuleButton extends PureComponent<Props, State> { |
||||
readonly state: State = { |
||||
isLoading: false, |
||||
testRuleResponse: {}, |
||||
}; |
||||
|
||||
componentDidMount() { |
||||
this.testRule(); |
||||
} |
||||
|
||||
async testRule() { |
||||
const { panelId, dashboard } = this.props; |
||||
const payload = { dashboard: dashboard.getSaveModelClone(), panelId }; |
||||
const testRuleResponse = await getBackendSrv().post(`/api/alerts/test`, payload); |
||||
this.setState(prevState => ({ ...prevState, isLoading: false, testRuleResponse })); |
||||
} |
||||
|
||||
render() { |
||||
const { testRuleResponse, isLoading } = this.state; |
||||
const { LoadingPlaceholder } = this.props; |
||||
|
||||
if (isLoading === true) { |
||||
return <LoadingPlaceholder text="Evaluating rule" />; |
||||
} |
||||
|
||||
return <JSONFormatter json={testRuleResponse} />; |
||||
} |
||||
} |
@ -0,0 +1,13 @@ |
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP |
||||
|
||||
exports[`Render should render component 1`] = ` |
||||
<JSONFormatter |
||||
config={ |
||||
Object { |
||||
"animateOpen": true, |
||||
} |
||||
} |
||||
json={Object {}} |
||||
open={3} |
||||
/> |
||||
`; |
Loading…
Reference in new issue