Skip to content

Commit

Permalink
Convert default_watch.json to a JS object in order to avoid TS compla…
Browse files Browse the repository at this point in the history
…ints (#89488)

* Remove unused defaultWatchJson static member from public JsonWatch model.
  • Loading branch information
cjcenizal authored Jan 28, 2021
1 parent b2d4412 commit fd2e9d0
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export const defaultWatch = {
trigger: {
schedule: {
interval: '30m',
},
},
input: {
search: {
request: {
body: {
size: 0,
query: {
match_all: {},
},
},
indices: ['*'],
},
},
},
condition: {
compare: {
'ctx.payload.hits.total': {
gte: 10,
},
},
},
actions: {
'my-logging-action': {
logging: {
text: 'There are {{ctx.payload.hits.total}} documents in your index. Threshold is 10.',
},
},
},
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*/

export const Watch: any;
export const defaultWatch: any;
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*/

export { Watch } from './watch';
export { defaultWatch } from './default_watch';
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

import uuid from 'uuid';
import { get } from 'lodash';
import { BaseWatch } from './base_watch';
import { ACTION_TYPES, WATCH_TYPES } from '../../../../common/constants';
import defaultWatchJson from './default_watch.json';
import { i18n } from '@kbn/i18n';

import { ACTION_TYPES, WATCH_TYPES } from '../../../../common/constants';
import { BaseWatch } from './base_watch';
import { defaultWatch } from './default_watch';

/**
* {@code JsonWatch} allows a user to create a Watch by writing the raw JSON.
*/
Expand All @@ -20,11 +21,11 @@ export class JsonWatch extends BaseWatch {
props.id = typeof props.id === 'undefined' ? uuid.v4() : props.id;
super(props);
const existingWatch = get(props, 'watch');
this.watch = existingWatch ? existingWatch : defaultWatchJson;
this.watch = existingWatch ? existingWatch : defaultWatch;
this.watchString = get(
props,
'watchString',
JSON.stringify(existingWatch ? existingWatch : defaultWatchJson, null, 2)
JSON.stringify(existingWatch ? existingWatch : defaultWatch, null, 2)
);
this.id = props.id;
}
Expand Down Expand Up @@ -113,7 +114,6 @@ export class JsonWatch extends BaseWatch {
return new JsonWatch(upstreamWatch);
}

static defaultWatchJson = defaultWatchJson;
static typeName = i18n.translate('xpack.watcher.models.jsonWatch.typeName', {
defaultMessage: 'Advanced Watch',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
*/

import { act } from 'react-dom/test-utils';

import { getExecuteDetails } from '../__fixtures__';
import { defaultWatch } from '../public/application/models/watch';
import { setupEnvironment, pageHelpers, nextTick, wrapBodyResponse } from './helpers';
import { WatchCreateJsonTestBed } from './helpers/watch_create_json.helpers';
import { WATCH } from './helpers/jest_constants';
import defaultWatchJson from '../public/application/models/watch/default_watch.json';
import { getExecuteDetails } from '../__fixtures__';

const { setup } = pageHelpers.watchCreateJson;

Expand Down Expand Up @@ -117,7 +118,7 @@ describe('<JsonWatchEdit /> create route', () => {
},
},
],
watch: defaultWatchJson,
watch: defaultWatch,
})
);
});
Expand Down Expand Up @@ -172,7 +173,7 @@ describe('<JsonWatchEdit /> create route', () => {

const latestRequest = server.requests[server.requests.length - 1];

const actionModes = Object.keys(defaultWatchJson.actions).reduce(
const actionModes = Object.keys(defaultWatch.actions).reduce(
(actionAccum: any, action) => {
actionAccum[action] = 'simulate';
return actionAccum;
Expand All @@ -186,7 +187,7 @@ describe('<JsonWatchEdit /> create route', () => {
isNew: true,
isActive: true,
actions: [],
watch: defaultWatchJson,
watch: defaultWatch,
};

expect(latestRequest.requestBody).toEqual(
Expand Down Expand Up @@ -234,7 +235,7 @@ describe('<JsonWatchEdit /> create route', () => {

const latestRequest = server.requests[server.requests.length - 1];

const actionModes = Object.keys(defaultWatchJson.actions).reduce(
const actionModes = Object.keys(defaultWatch.actions).reduce(
(actionAccum: any, action) => {
actionAccum[action] = ACTION_MODE;
return actionAccum;
Expand All @@ -248,7 +249,7 @@ describe('<JsonWatchEdit /> create route', () => {
isNew: true,
isActive: true,
actions: [],
watch: defaultWatchJson,
watch: defaultWatch,
};

const triggeredTime = `now+${TRIGGERED_TIME}s`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import { act } from 'react-dom/test-utils';
import axiosXhrAdapter from 'axios/lib/adapters/xhr';
import axios from 'axios';
import { getRandomString } from '@kbn/test/jest';

import { getWatch } from '../__fixtures__';
import { defaultWatch } from '../public/application/models/watch';
import { setupEnvironment, pageHelpers, nextTick, wrapBodyResponse } from './helpers';
import { WatchEditTestBed } from './helpers/watch_edit.helpers';
import { WATCH } from './helpers/jest_constants';
import defaultWatchJson from '../public/application/models/watch/default_watch.json';
import { getWatch } from '../__fixtures__';
import { getRandomString } from '@kbn/test/jest';

const mockHttpClient = axios.create({ adapter: axiosXhrAdapter });

Expand Down Expand Up @@ -69,7 +70,7 @@ describe('<WatchEdit />', () => {
expect(exists('jsonWatchForm')).toBe(true);
expect(find('nameInput').props().value).toBe(watch.name);
expect(find('idInput').props().value).toBe(watch.id);
expect(JSON.parse(codeEditor.props().value as string)).toEqual(defaultWatchJson);
expect(JSON.parse(codeEditor.props().value as string)).toEqual(defaultWatch);

// ID should not be editable
expect(find('idInput').props().readOnly).toEqual(true);
Expand Down Expand Up @@ -112,7 +113,7 @@ describe('<WatchEdit />', () => {
},
},
],
watch: defaultWatchJson,
watch: defaultWatch,
})
);
});
Expand Down

0 comments on commit fd2e9d0

Please sign in to comment.