Skip to content

Commit

Permalink
Fix broken tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Nov 14, 2019
1 parent e73f4d2 commit f6e530e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
30 changes: 19 additions & 11 deletions x-pack/legacy/plugins/alerting/server/lib/alert_instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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`, () => {
Expand All @@ -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`, () => {
Expand All @@ -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);
});
});

Expand All @@ -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', () => {
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down

0 comments on commit f6e530e

Please sign in to comment.