Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kertal committed Jun 23, 2020
1 parent f1e90cb commit 5e03c84
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 22 deletions.
23 changes: 2 additions & 21 deletions src/plugins/discover/public/application/angular/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const {
} = getServices();

import { getRootBreadcrumbs, getSavedSearchBreadcrumbs } from '../helpers/breadcrumbs';
import { validateTimeRange } from '../helpers/validate_time_range';
import {
esFilters,
fieldFormats,
Expand Down Expand Up @@ -782,7 +783,7 @@ function discoverController(
if (!init.complete) return;
$scope.fetchCounter++;
$scope.fetchError = undefined;
if (!validateTimeRange()) {
if (!validateTimeRange(timefilter.getTime(), toastNotifications)) {
$scope.resultState = 'none';
return;
}
Expand Down Expand Up @@ -917,26 +918,6 @@ function discoverController(
});
}

function validateTimeRange() {
const { from, to } = timefilter.getTime();
if (!from || !to || !dateMath.parse(from) || !dateMath.parse(to)) {
toastNotifications.addDanger({
title: i18n.translate('discover.notifications.invalidTimeRangeTitle', {
defaultMessage: `Invalid time range`,
}),
text: i18n.translate('discover.notifications.invalidTimeRangeText', {
defaultMessage: `The provided time range is invalid. (from: '{from}', to: '{to}')`,
values: {
from,
to,
},
}),
});
return false;
}
return true;
}

$scope.updateTime = function () {
const { from, to } = timefilter.getTime();
// this is the timerange for the histogram, should be refactored
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { validateTimeRange } from './validate_time_range';
import { notificationServiceMock } from '../../../../../core/public/mocks';

describe('Discover validateTimeRange', () => {
test('validates given time ranges correctly', async () => {
const { toasts } = notificationServiceMock.createStartContract();
[
{ from: '', to: '', result: false },
{ from: 'now', to: 'now+1h', result: true },
{ from: 'now', to: 'lala+1h', result: false },
{ from: '', to: 'now', result: false },
{ from: 'now', to: '', result: false },
{ from: ' 2020-06-02T13:36:13.689Z', to: 'now', result: true },
{ from: ' 2020-06-02T13:36:13.689Z', to: '2020-06-02T13:36:13.690Z', result: true },
].map((test) => {
expect(validateTimeRange({ from: test.from, to: test.to }, toasts)).toEqual(test.result);
});
});

test('displays a toast when invalid data is entered', async () => {
const { toasts } = notificationServiceMock.createStartContract();
expect(validateTimeRange({ from: 'now', to: 'null' }, toasts)).toEqual(false);
expect(toasts.addDanger).toHaveBeenCalledWith({
title: 'Invalid time range',
text: "The provided time range is invalid. (from: 'now', to: 'null')",
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import dateMath from '@elastic/datemath';
import { i18n } from '@kbn/i18n';
import { ToastsStart } from 'kibana/public';

/**
* Validates a given time filter range, provided by URL or UI
* Unless valid, it returns false and displays a notification
*/
export function validateTimeRange(
{ from, to }: { from: string; to: string },
toastNotifications: ToastsStart
): boolean {
const fromMoment = dateMath.parse(from);
const toMoment = dateMath.parse(to);
if (!fromMoment || !toMoment || !fromMoment.isValid() || !toMoment.isValid()) {
toastNotifications.addDanger({
title: i18n.translate('discover.notifications.invalidTimeRangeTitle', {
defaultMessage: `Invalid time range`,
}),
text: i18n.translate('discover.notifications.invalidTimeRangeText', {
defaultMessage: `The provided time range is invalid. (from: '{from}', to: '{to}')`,
values: {
from,
to,
},
}),
});
return false;
}
return true;
}
11 changes: 11 additions & 0 deletions test/functional/apps/discover/_discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,5 +257,16 @@ export default function ({ getService, getPageObjects }) {
expect(refreshedTimeString).not.to.be(initialTimeString);
});
});

describe('invalid time range in URL', function () {
it('should display a "Invalid time range toast"', async function () {
await PageObjects.common.navigateToUrl('discover', '#/?_g=(time:(from:now-15m,to:null))', {
useActualUrl: true,
});
await PageObjects.header.awaitKibanaChrome();
const toastMessage = await PageObjects.common.closeToast();
expect(toastMessage).to.be('Invalid time range');
});
});
});
}
2 changes: 1 addition & 1 deletion test/functional/page_objects/common_page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ export function CommonPageProvider({ getService, getPageObjects }: FtrProviderCo
const toast = await find.byCssSelector('.euiToast', 2 * defaultFindTimeout);
await toast.moveMouseTo();
const title = await (await find.byCssSelector('.euiToastHeader__title')).getVisibleText();
log.debug(`Toast title: ${title}`);

await find.clickByCssSelector('.euiToast__closeButton');
return title;
}
Expand Down

0 comments on commit 5e03c84

Please sign in to comment.