"Fossies" - the Fresh Open Source Software Archive

Member "graylog2-server-4.0.6/graylog2-web-interface/src/views/components/searchbar/AbsoluteTimeRangeSelector.test.jsx" (7 Apr 2021, 3419 Bytes) of package /linux/misc/graylog2-server-4.0.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) JSX source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "AbsoluteTimeRangeSelector.test.jsx": 3.3.8_vs_4.0.0.

    1 /*
    2  * Copyright (C) 2020 Graylog, Inc.
    3  *
    4  * This program is free software: you can redistribute it and/or modify
    5  * it under the terms of the Server Side Public License, version 1,
    6  * as published by MongoDB, Inc.
    7  *
    8  * This program is distributed in the hope that it will be useful,
    9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   11  * Server Side Public License for more details.
   12  *
   13  * You should have received a copy of the Server Side Public License
   14  * along with this program. If not, see
   15  * <http://www.mongodb.com/licensing/server-side-public-license>.
   16  */
   17 // @flow strict
   18 import * as React from 'react';
   19 import { asElement, fireEvent, render, waitFor } from 'wrappedTestingLibrary';
   20 import { Formik, Form } from 'formik';
   21 import { act } from 'react-dom/test-utils';
   22 
   23 import AbsoluteTimeRangeSelector from './AbsoluteTimeRangeSelector';
   24 
   25 const renderWithForm = (element) => render((
   26   <Formik initialValues={{ timerange: { type: 'absolute', from: '2020-01-16 10:04:30.329', to: '2020-01-16 12:04:30.329' } }}
   27           onSubmit={() => {}}>
   28     <Form>
   29       {element}
   30     </Form>
   31   </Formik>
   32 ));
   33 
   34 const _findValidationState = (container) => {
   35   const formGroup = container?.matches('.form-group') ? container : container?.querySelector('.form-group');
   36 
   37   return formGroup && formGroup.className.includes('has-error')
   38     ? 'error'
   39     : null;
   40 };
   41 
   42 const _findFormGroup = (element) => element.closest('.form-group');
   43 
   44 const getValidationStateOfInput = (input) => _findValidationState(_findFormGroup(input));
   45 
   46 const changeInput = async (input, value) => {
   47   const { name } = asElement(input, HTMLInputElement);
   48 
   49   await act(async () => { fireEvent.change(input, { target: { value, name } }); });
   50 };
   51 
   52 describe('AbsoluteTimeRangeSelector', () => {
   53   it('does not try to parse an empty date in from field', async () => {
   54     const { getByDisplayValue } = renderWithForm((
   55       <AbsoluteTimeRangeSelector />
   56     ));
   57     const fromDate = getByDisplayValue('2020-01-16 10:04:30.329');
   58 
   59     await changeInput(fromDate, '');
   60 
   61     await waitFor(() => expect(getValidationStateOfInput(fromDate)).toEqual('error'));
   62   });
   63 
   64   it('does not try to parse an empty date in to field', async () => {
   65     const { getByDisplayValue } = renderWithForm((
   66       <AbsoluteTimeRangeSelector />
   67     ));
   68     const toDate = getByDisplayValue('2020-01-16 12:04:30.329');
   69 
   70     await changeInput(toDate, '');
   71 
   72     await waitFor(() => expect(getValidationStateOfInput(toDate)).toEqual('error'));
   73   });
   74 
   75   it('shows error message for from date if parsing fails after changing input', async () => {
   76     const { getByDisplayValue, queryByText } = renderWithForm((
   77       <AbsoluteTimeRangeSelector />
   78     ));
   79 
   80     const fromDate = getByDisplayValue('2020-01-16 10:04:30.329');
   81 
   82     await changeInput(fromDate, 'invalid');
   83 
   84     await waitFor(() => expect(queryByText('Format must be: YYYY-MM-DD [HH:mm:ss[.SSS]]')).not.toBeNull());
   85   });
   86 
   87   it('shows error message for to date if parsing fails after changing input', async () => {
   88     const { getByDisplayValue, queryByText } = renderWithForm((
   89       <AbsoluteTimeRangeSelector />
   90     ));
   91 
   92     const fromDate = getByDisplayValue('2020-01-16 12:04:30.329');
   93 
   94     await changeInput(fromDate, 'invalid');
   95 
   96     await waitFor(() => expect(queryByText('Format must be: YYYY-MM-DD [HH:mm:ss[.SSS]]')).not.toBeNull());
   97   });
   98 });