123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- import selectEvent from 'react-select-event';
- import {initializeOrg} from 'sentry-test/initializeOrg';
- import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
- import ProjectsStore from 'sentry/stores/projectsStore';
- import TeamStore from 'sentry/stores/teamStore';
- import {metric} from 'sentry/utils/analytics';
- import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
- import AlertsContainer from 'sentry/views/alerts';
- import AlertBuilderProjectProvider from 'sentry/views/alerts/builder/projectProvider';
- import ProjectAlertsCreate from 'sentry/views/alerts/create';
- jest.unmock('sentry/utils/recreateRoute');
- jest.mock('sentry/actionCreators/members');
- jest.mock('react-router');
- jest.mock('sentry/utils/analytics', () => ({
- metric: {
- startTransaction: jest.fn(() => ({
- setTag: jest.fn(),
- setData: jest.fn(),
- })),
- endTransaction: jest.fn(),
- mark: jest.fn(),
- measure: jest.fn(),
- },
- trackAdvancedAnalyticsEvent: jest.fn(),
- }));
- jest.mock('sentry/utils/analytics/trackAdvancedAnalyticsEvent');
- describe('ProjectAlertsCreate', function () {
- beforeEach(function () {
- TeamStore.init();
- TeamStore.loadInitialData([], false, null);
- MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/rules/configuration/',
- body: TestStubs.ProjectAlertRuleConfiguration(),
- });
- MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/rules/1/',
- body: TestStubs.ProjectAlertRule(),
- });
- MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/environments/',
- body: TestStubs.Environments(),
- });
- MockApiClient.addMockResponse({
- url: `/projects/org-slug/project-slug/?expand=hasAlertIntegration`,
- body: {},
- });
- MockApiClient.addMockResponse({
- url: `/projects/org-slug/project-slug/ownership/`,
- method: 'GET',
- body: {
- fallthrough: false,
- autoAssignment: false,
- },
- });
- });
- afterEach(function () {
- MockApiClient.clearMockResponses();
- jest.clearAllMocks();
- });
- const createWrapper = (props = {}, location = {}) => {
- const {organization, project, router} = initializeOrg(props);
- ProjectsStore.loadInitialData([project]);
- const params = {orgId: organization.slug, projectId: project.slug};
- const wrapper = render(
- <AlertsContainer>
- <AlertBuilderProjectProvider params={params}>
- <ProjectAlertsCreate
- params={params}
- location={{
- pathname: `/organizations/org-slug/alerts/rules/${project.slug}/new/`,
- query: {createFromWizard: true},
- ...location,
- }}
- router={router}
- />
- </AlertBuilderProjectProvider>
- </AlertsContainer>,
- {organization}
- );
- return {
- wrapper,
- organization,
- project,
- router,
- };
- };
- it('adds default parameters if wizard was skipped', function () {
- const location = {query: {}};
- const wrapper = createWrapper(undefined, location);
- expect(wrapper.router.replace).toHaveBeenCalledWith({
- pathname: '/organizations/org-slug/alerts/new/metric',
- query: {
- aggregate: 'count()',
- dataset: 'events',
- eventTypes: 'error',
- project: 'project-slug',
- },
- });
- });
- describe('Issue Alert', function () {
- it('loads default values', function () {
- createWrapper();
- expect(screen.getByText('All Environments')).toBeInTheDocument();
- expect(screen.getAllByDisplayValue('all')).toHaveLength(2);
- expect(screen.getByText('30 minutes')).toBeInTheDocument();
- });
- it('can remove filters', async function () {
- createWrapper();
- const mock = MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/rules/',
- method: 'POST',
- body: TestStubs.ProjectAlertRule(),
- });
- // Change name of alert rule
- userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
- // Add a filter and remove it
- await selectEvent.select(screen.getByText('Add optional filter...'), [
- 'The issue is older or newer than...',
- ]);
- userEvent.click(screen.getByLabelText('Delete Node'));
- userEvent.click(screen.getByText('Save Rule'));
- expect(mock).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- data: {
- actionMatch: 'all',
- actions: [],
- conditions: [],
- filterMatch: 'all',
- filters: [],
- frequency: 30,
- name: 'My Rule Name',
- owner: null,
- },
- })
- );
- // updateOnboardingTask triggers an out of band state update
- await act(tick);
- });
- it('can remove triggers', async function () {
- const {organization} = createWrapper();
- const mock = MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/rules/',
- method: 'POST',
- body: TestStubs.ProjectAlertRule(),
- });
- // Change name of alert rule
- userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
- // Add a trigger and remove it
- await selectEvent.select(screen.getByText('Add optional trigger...'), [
- 'A new issue is created',
- ]);
- userEvent.click(screen.getByLabelText('Delete Node'));
- expect(trackAdvancedAnalyticsEvent).toHaveBeenCalledWith(
- 'edit_alert_rule.add_row',
- {
- name: 'sentry.rules.conditions.first_seen_event.FirstSeenEventCondition',
- organization,
- project_id: '2',
- type: 'conditions',
- }
- );
- userEvent.click(screen.getByText('Save Rule'));
- expect(mock).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- data: {
- actionMatch: 'all',
- actions: [],
- conditions: [],
- filterMatch: 'all',
- filters: [],
- frequency: 30,
- name: 'My Rule Name',
- owner: null,
- },
- })
- );
- // updateOnboardingTask triggers an out of band state update
- await act(tick);
- });
- it('can remove actions', async function () {
- createWrapper();
- const mock = MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/rules/',
- method: 'POST',
- body: TestStubs.ProjectAlertRule(),
- });
- // Change name of alert rule
- userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
- // Add an action and remove it
- await selectEvent.select(screen.getByText('Add action...'), [
- 'Send a notification to all legacy integrations',
- ]);
- userEvent.click(screen.getByLabelText('Delete Node'));
- userEvent.click(screen.getByText('Save Rule'));
- expect(mock).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- data: {
- actionMatch: 'all',
- actions: [],
- conditions: [],
- filterMatch: 'all',
- filters: [],
- frequency: 30,
- name: 'My Rule Name',
- owner: null,
- },
- })
- );
- // updateOnboardingTask triggers an out of band state update
- await act(tick);
- });
- describe('updates and saves', function () {
- let mock;
- beforeEach(function () {
- mock = MockApiClient.addMockResponse({
- url: '/projects/org-slug/project-slug/rules/',
- method: 'POST',
- body: TestStubs.ProjectAlertRule(),
- });
- });
- afterEach(function () {
- jest.clearAllMocks();
- });
- it('environment, action and filter match', async function () {
- const wrapper = createWrapper();
- // Change target environment
- await selectEvent.select(screen.getByText('All Environments'), ['production']);
- // Change actionMatch and filterMatch dropdown
- const allDropdowns = screen.getAllByText('all');
- expect(allDropdowns).toHaveLength(2);
- await selectEvent.select(allDropdowns[0], ['any']);
- await selectEvent.select(allDropdowns[1], ['any']);
- // Change name of alert rule
- userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
- userEvent.click(screen.getByText('Save Rule'));
- expect(mock).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- data: {
- actionMatch: 'any',
- filterMatch: 'any',
- conditions: [],
- actions: [],
- filters: [],
- environment: 'production',
- frequency: 30,
- name: 'My Rule Name',
- owner: null,
- },
- })
- );
- expect(metric.startTransaction).toHaveBeenCalledWith({name: 'saveAlertRule'});
- await waitFor(() => {
- expect(wrapper.router.push).toHaveBeenCalledWith({
- pathname: '/organizations/org-slug/alerts/rules/project-slug/1/details/',
- });
- });
- });
- it('new condition', async function () {
- const wrapper = createWrapper();
- // Change name of alert rule
- userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
- // Add another condition
- await selectEvent.select(screen.getByText('Add optional filter...'), [
- "The event's tags match {key} {match} {value}",
- ]);
- // Edit new Condition
- userEvent.paste(screen.getByPlaceholderText('key'), 'conditionKey');
- userEvent.paste(screen.getByPlaceholderText('value'), 'conditionValue');
- await selectEvent.select(screen.getByText('contains'), ['does not equal']);
- userEvent.click(screen.getByText('Save Rule'));
- expect(mock).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- data: {
- actionMatch: 'all',
- actions: [],
- conditions: [],
- filterMatch: 'all',
- filters: [
- {
- id: 'sentry.rules.filters.tagged_event.TaggedEventFilter',
- key: 'conditionKey',
- match: 'ne',
- value: 'conditionValue',
- },
- ],
- frequency: 30,
- name: 'My Rule Name',
- owner: null,
- },
- })
- );
- expect(metric.startTransaction).toHaveBeenCalledWith({name: 'saveAlertRule'});
- await waitFor(() => {
- expect(wrapper.router.push).toHaveBeenCalledWith({
- pathname: '/organizations/org-slug/alerts/rules/project-slug/1/details/',
- });
- });
- });
- it('new filter', async function () {
- const wrapper = createWrapper();
- // Change name of alert rule
- userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
- // Add a new filter
- await selectEvent.select(screen.getByText('Add optional filter...'), [
- 'The issue is older or newer than...',
- ]);
- userEvent.paste(screen.getByPlaceholderText('10'), '12');
- userEvent.click(screen.getByText('Save Rule'));
- expect(mock).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- data: {
- actionMatch: 'all',
- filterMatch: 'all',
- filters: [
- {
- id: 'sentry.rules.filters.age_comparison.AgeComparisonFilter',
- comparison_type: 'older',
- time: 'minute',
- value: '12',
- },
- ],
- actions: [],
- conditions: [],
- frequency: 30,
- name: 'My Rule Name',
- owner: null,
- },
- })
- );
- expect(metric.startTransaction).toHaveBeenCalledWith({name: 'saveAlertRule'});
- await waitFor(() => {
- expect(wrapper.router.push).toHaveBeenCalledWith({
- pathname: '/organizations/org-slug/alerts/rules/project-slug/1/details/',
- });
- });
- });
- it('new action', async function () {
- const wrapper = createWrapper();
- // Change name of alert rule
- userEvent.paste(screen.getByPlaceholderText('Enter Alert Name'), 'My Rule Name');
- // Add a new action
- await selectEvent.select(screen.getByText('Add action...'), [
- 'Issue Owners, Team, or Member',
- ]);
- // Update action interval
- await selectEvent.select(screen.getByText('30 minutes'), ['60 minutes']);
- userEvent.click(screen.getByText('Save Rule'));
- expect(mock).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- data: {
- actionMatch: 'all',
- actions: [
- {id: 'sentry.mail.actions.NotifyEmailAction', targetType: 'IssueOwners'},
- ],
- conditions: [],
- filterMatch: 'all',
- filters: [],
- frequency: '60',
- name: 'My Rule Name',
- owner: null,
- },
- })
- );
- expect(metric.startTransaction).toHaveBeenCalledWith({name: 'saveAlertRule'});
- await waitFor(() => {
- expect(wrapper.router.push).toHaveBeenCalledWith({
- pathname: '/organizations/org-slug/alerts/rules/project-slug/1/details/',
- });
- });
- });
- });
- });
- });
|