2019-01-15 18:53:27 -05:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF 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.
|
|
|
|
*/
|
2016-09-23 20:41:24 -04:00
|
|
|
import React from 'react';
|
2021-03-03 10:18:14 -05:00
|
|
|
import thunk from 'redux-thunk';
|
|
|
|
import configureStore from 'redux-mock-store';
|
2021-01-25 15:03:16 -05:00
|
|
|
import fetchMock from 'fetch-mock';
|
2020-05-07 17:18:10 -04:00
|
|
|
import QuerySearch from 'src/SqlLab/components/QuerySearch';
|
2021-03-03 10:18:14 -05:00
|
|
|
import { Provider } from 'react-redux';
|
|
|
|
import { supersetTheme, ThemeProvider } from '@superset-ui/core';
|
|
|
|
import { fireEvent, render, screen, act } from '@testing-library/react';
|
|
|
|
import '@testing-library/jest-dom/extend-expect';
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2021-05-18 14:02:06 -04:00
|
|
|
import { user } from './fixtures';
|
2021-03-03 10:18:14 -05:00
|
|
|
|
|
|
|
const mockStore = configureStore([thunk]);
|
2021-05-18 14:02:06 -04:00
|
|
|
const store = mockStore({
|
|
|
|
sqlLab: user,
|
|
|
|
});
|
2017-04-13 18:04:09 -04:00
|
|
|
|
2021-01-25 15:03:16 -05:00
|
|
|
const SEARCH_ENDPOINT = 'glob:*/superset/search_queries?*';
|
2021-03-03 10:18:14 -05:00
|
|
|
const USER_ENDPOINT = 'glob:*/api/v1/query/related/user';
|
|
|
|
const DATABASE_ENDPOINT = 'glob:*/api/v1/database/?*';
|
2021-01-25 15:03:16 -05:00
|
|
|
|
|
|
|
fetchMock.get(SEARCH_ENDPOINT, []);
|
2021-03-03 10:18:14 -05:00
|
|
|
fetchMock.get(USER_ENDPOINT, []);
|
|
|
|
fetchMock.get(DATABASE_ENDPOINT, []);
|
2021-01-25 15:03:16 -05:00
|
|
|
|
2016-09-23 20:41:24 -04:00
|
|
|
describe('QuerySearch', () => {
|
2016-10-25 19:44:32 -04:00
|
|
|
const mockedProps = {
|
2021-01-25 15:03:16 -05:00
|
|
|
actions: { addDangerToast: jest.fn() },
|
2020-10-02 17:59:04 -04:00
|
|
|
displayLimit: 50,
|
2016-10-25 19:44:32 -04:00
|
|
|
};
|
2021-03-03 10:18:14 -05:00
|
|
|
|
2016-10-25 19:44:32 -04:00
|
|
|
it('is valid', () => {
|
2021-03-03 10:18:14 -05:00
|
|
|
expect(
|
|
|
|
React.isValidElement(
|
|
|
|
<ThemeProvider theme={supersetTheme}>
|
|
|
|
<Provider store={store}>
|
|
|
|
<QuerySearch {...mockedProps} />
|
|
|
|
</Provider>
|
|
|
|
</ThemeProvider>,
|
|
|
|
),
|
|
|
|
).toBe(true);
|
2016-09-23 20:41:24 -04:00
|
|
|
});
|
2021-03-03 10:18:14 -05:00
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
// You need this await function in order to change state in the app. In fact you need it everytime you re-render.
|
|
|
|
await act(async () => {
|
|
|
|
render(
|
|
|
|
<ThemeProvider theme={supersetTheme}>
|
|
|
|
<Provider store={store}>
|
|
|
|
<QuerySearch {...mockedProps} />
|
|
|
|
</Provider>
|
|
|
|
</ThemeProvider>,
|
|
|
|
);
|
|
|
|
});
|
2017-06-12 16:21:14 -04:00
|
|
|
});
|
2016-09-23 20:41:24 -04:00
|
|
|
|
2021-03-03 10:18:14 -05:00
|
|
|
it('should have three Selects', () => {
|
|
|
|
expect(screen.getByText(/28 days ago/i)).toBeInTheDocument();
|
|
|
|
expect(screen.getByText(/now/i)).toBeInTheDocument();
|
|
|
|
expect(screen.getByText(/success/i)).toBeInTheDocument();
|
2016-10-28 17:12:53 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('updates fromTime on user selects from time', () => {
|
2021-03-03 10:18:14 -05:00
|
|
|
const role = screen.getByText(/28 days ago/i);
|
|
|
|
fireEvent.keyDown(role, { key: 'ArrowDown', keyCode: 40 });
|
|
|
|
userEvent.click(screen.getByText(/1 hour ago/i));
|
|
|
|
expect(screen.getByText(/1 hour ago/i)).toBeInTheDocument();
|
2016-10-28 17:12:53 -04:00
|
|
|
});
|
|
|
|
|
2021-03-03 10:18:14 -05:00
|
|
|
it('updates toTime on user selects on time', () => {
|
|
|
|
const role = screen.getByText(/now/i);
|
|
|
|
fireEvent.keyDown(role, { key: 'ArrowDown', keyCode: 40 });
|
|
|
|
userEvent.click(screen.getByText(/1 hour ago/i));
|
|
|
|
expect(screen.getByText(/1 hour ago/i)).toBeInTheDocument();
|
2016-10-28 17:12:53 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('updates status on user selects status', () => {
|
2021-03-03 10:18:14 -05:00
|
|
|
const role = screen.getByText(/success/i);
|
|
|
|
fireEvent.keyDown(role, { key: 'ArrowDown', keyCode: 40 });
|
|
|
|
userEvent.click(screen.getByText(/failed/i));
|
|
|
|
expect(screen.getByText(/failed/i)).toBeInTheDocument();
|
2016-09-23 20:41:24 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should have one input for searchText', () => {
|
2021-03-03 10:18:14 -05:00
|
|
|
expect(
|
|
|
|
screen.getByPlaceholderText(/Query search string/i),
|
|
|
|
).toBeInTheDocument();
|
2016-09-23 20:41:24 -04:00
|
|
|
});
|
|
|
|
|
2016-10-28 17:12:53 -04:00
|
|
|
it('updates search text on user inputs search text', () => {
|
2021-03-03 10:18:14 -05:00
|
|
|
const search = screen.getByPlaceholderText(/Query search string/i);
|
|
|
|
userEvent.type(search, 'text');
|
|
|
|
expect(search.value).toBe('text');
|
2016-10-28 17:12:53 -04:00
|
|
|
});
|
|
|
|
|
2021-03-03 10:18:14 -05:00
|
|
|
it('should have one Button', () => {
|
|
|
|
const button = screen.getAllByRole('button');
|
|
|
|
expect(button.length).toEqual(1);
|
2018-10-07 15:48:06 -04:00
|
|
|
});
|
|
|
|
|
2021-03-03 10:18:14 -05:00
|
|
|
it('should call API when search button is pressed', async () => {
|
|
|
|
fetchMock.resetHistory();
|
|
|
|
const button = screen.getByRole('button');
|
|
|
|
await act(async () => {
|
|
|
|
userEvent.click(button);
|
|
|
|
});
|
|
|
|
expect(fetchMock.calls(SEARCH_ENDPOINT)).toHaveLength(1);
|
2016-09-23 20:41:24 -04:00
|
|
|
});
|
2016-10-28 17:12:53 -04:00
|
|
|
|
2021-03-03 10:18:14 -05:00
|
|
|
it('should call API when (only)enter key is pressed', async () => {
|
|
|
|
fetchMock.resetHistory();
|
|
|
|
const search = screen.getByPlaceholderText(/Query search string/i);
|
|
|
|
await act(async () => {
|
|
|
|
userEvent.type(search, 'a');
|
|
|
|
});
|
|
|
|
expect(fetchMock.calls(SEARCH_ENDPOINT)).toHaveLength(0);
|
|
|
|
await act(async () => {
|
|
|
|
userEvent.type(search, '{enter}');
|
|
|
|
});
|
|
|
|
expect(fetchMock.calls(SEARCH_ENDPOINT)).toHaveLength(1);
|
2016-10-28 17:12:53 -04:00
|
|
|
});
|
2016-09-23 20:41:24 -04:00
|
|
|
});
|