From f6e530e132f1868f277fc8c8dc4f5736c0f37ead Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 14 Nov 2019 17:55:18 -0500 Subject: [PATCH] Fix broken tests --- .../server/lib/alert_instance.test.ts | 30 ++++++++++++------- .../alerting/server/lib/alert_instance.ts | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/x-pack/legacy/plugins/alerting/server/lib/alert_instance.test.ts b/x-pack/legacy/plugins/alerting/server/lib/alert_instance.test.ts index d3f1100621246..6a80f4d2de4cb 100644 --- a/x-pack/legacy/plugins/alerting/server/lib/alert_instance.test.ts +++ b/x-pack/legacy/plugins/alerting/server/lib/alert_instance.test.ts @@ -18,9 +18,17 @@ afterAll(() => clock.restore()); describe('hasScheduledActions()', () => { test('defaults to false', () => { const alertInstance = new AlertInstance(); - expect(alertInstance.hasScheduledActions(null)).toEqual(false); + expect(alertInstance.hasScheduledActions()).toEqual(false); }); + test('returns true when scheduleActions is called', () => { + const alertInstance = new AlertInstance(); + alertInstance.scheduleActions('default'); + expect(alertInstance.hasScheduledActions()).toEqual(true); + }); +}); + +describe('isThrottled', () => { test(`should throttle when group didn't change and throttle period is still active`, () => { const alertInstance = new AlertInstance({ meta: { @@ -32,7 +40,7 @@ describe('hasScheduledActions()', () => { }); clock.tick(30000); alertInstance.scheduleActions('default'); - expect(alertInstance.hasScheduledActions('1m')).toEqual(false); + expect(alertInstance.isThrottled('1m')).toEqual(true); }); test(`shouldn't throttle when group didn't change and throttle period expired`, () => { @@ -46,7 +54,7 @@ describe('hasScheduledActions()', () => { }); clock.tick(30000); alertInstance.scheduleActions('default'); - expect(alertInstance.hasScheduledActions('15s')).toEqual(true); + expect(alertInstance.isThrottled('15s')).toEqual(false); }); test(`shouldn't throttle when group changes`, () => { @@ -60,7 +68,7 @@ describe('hasScheduledActions()', () => { }); clock.tick(5000); alertInstance.scheduleActions('other-group'); - expect(alertInstance.hasScheduledActions('1m')).toEqual(true); + expect(alertInstance.isThrottled('1m')).toEqual(false); }); }); @@ -75,9 +83,9 @@ describe('unscheduleActions()', () => { test('makes hasScheduledActions() return false', () => { const alertInstance = new AlertInstance(); alertInstance.scheduleActions('default'); - expect(alertInstance.hasScheduledActions(null)).toEqual(true); + expect(alertInstance.hasScheduledActions()).toEqual(true); alertInstance.unscheduleActions(); - expect(alertInstance.hasScheduledActions(null)).toEqual(false); + expect(alertInstance.hasScheduledActions()).toEqual(false); }); test('makes getScheduledActionOptions() return undefined', () => { @@ -113,10 +121,10 @@ describe('scheduleActions()', () => { }, }); alertInstance.replaceState({ otherField: true }).scheduleActions('default', { field: true }); - expect(alertInstance.hasScheduledActions(null)).toEqual(true); + expect(alertInstance.hasScheduledActions()).toEqual(true); }); - test('makes hasScheduledActions() return false when throttled', () => { + test('makes isThrottled() return true when throttled', () => { const alertInstance = new AlertInstance({ state: { foo: true }, meta: { @@ -127,10 +135,10 @@ describe('scheduleActions()', () => { }, }); alertInstance.replaceState({ otherField: true }).scheduleActions('default', { field: true }); - expect(alertInstance.hasScheduledActions('1m')).toEqual(false); + expect(alertInstance.isThrottled('1m')).toEqual(true); }); - test('make hasScheduledActions() return true when throttled expired', () => { + test('make isThrottled() return false when throttled expired', () => { const alertInstance = new AlertInstance({ state: { foo: true }, meta: { @@ -142,7 +150,7 @@ describe('scheduleActions()', () => { }); clock.tick(120000); alertInstance.replaceState({ otherField: true }).scheduleActions('default', { field: true }); - expect(alertInstance.hasScheduledActions('1m')).toEqual(true); + expect(alertInstance.isThrottled('1m')).toEqual(false); }); test('makes getScheduledActionOptions() return given options', () => { diff --git a/x-pack/legacy/plugins/alerting/server/lib/alert_instance.ts b/x-pack/legacy/plugins/alerting/server/lib/alert_instance.ts index a322444eb695d..1e2cc26f364ad 100644 --- a/x-pack/legacy/plugins/alerting/server/lib/alert_instance.ts +++ b/x-pack/legacy/plugins/alerting/server/lib/alert_instance.ts @@ -69,7 +69,7 @@ export class AlertInstance { } scheduleActions(actionGroup: string, context: Context = {}) { - if (this.hasScheduledActions(null)) { + if (this.hasScheduledActions()) { throw new Error('Alert instance execution has already been scheduled, cannot schedule twice'); } this.scheduledExecutionOptions = { actionGroup, context, state: this.state };