This is an automated email from the ASF dual-hosted git repository.
ryanahamilton pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/master by this push:
new eb64155 improve react-query testing (#15043)
eb64155 is described below
commit eb64155e3480d38eba4d2a101cb5e6491d091c9e
Author: Brent Bovenzi <[email protected]>
AuthorDate: Sat Mar 27 16:16:05 2021 -0600
improve react-query testing (#15043)
---
airflow/ui/jest.config.js | 1 +
airflow/ui/src/api/index.ts | 19 +++++++++++++++---
airflow/ui/test/Login.test.tsx | 40 ++++++++++++++++++++++----------------
airflow/ui/test/Pipelines.test.tsx | 29 +++++++++++++++++----------
4 files changed, 59 insertions(+), 30 deletions(-)
diff --git a/airflow/ui/jest.config.js b/airflow/ui/jest.config.js
index 5f5f3e5..2f89890 100644
--- a/airflow/ui/jest.config.js
+++ b/airflow/ui/jest.config.js
@@ -23,5 +23,6 @@
const neutrino = require('neutrino');
process.env.NODE_ENV = process.env.NODE_ENV || 'test';
+process.env.WEBSERVER_URL = process.env.WEBSERVER_URL ||
'http://localhost:9999';
module.exports = neutrino().jest();
diff --git a/airflow/ui/src/api/index.ts b/airflow/ui/src/api/index.ts
index cfcd52d..3bee358 100644
--- a/airflow/ui/src/api/index.ts
+++ b/airflow/ui/src/api/index.ts
@@ -1,3 +1,4 @@
+/* eslint-disable no-console */
/*!
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -18,7 +19,7 @@
*/
import axios, { AxiosResponse } from 'axios';
-import { useQuery } from 'react-query';
+import { useQuery, setLogger } from 'react-query';
import humps from 'humps';
import type { Dag, DagRun, Version } from 'interfaces';
@@ -29,13 +30,25 @@ axios.interceptors.response.use(
(res) => (res.data ? humps.camelizeKeys(res.data) as unknown as
AxiosResponse : res),
);
-const refetchInterval = 1000;
+// turn off logging, retry and refetch on tests
+const isTest = process.env.NODE_ENV === 'test';
+
+setLogger({
+ log: isTest ? () => {} : console.log,
+ warn: isTest ? () => {} : console.warn,
+ error: isTest ? () => {} : console.warn,
+});
+
+const refetchInterval = isTest ? false : 1000;
export function useDags() {
return useQuery<DagsResponse, Error>(
'dags',
(): Promise<DagsResponse> => axios.get('/dags'),
- { refetchInterval },
+ {
+ refetchInterval,
+ retry: !isTest,
+ },
);
}
diff --git a/airflow/ui/test/Login.test.tsx b/airflow/ui/test/Login.test.tsx
index 1685e0e..825bab8 100644
--- a/airflow/ui/test/Login.test.tsx
+++ b/airflow/ui/test/Login.test.tsx
@@ -32,24 +32,30 @@ import { url, defaultHeaders, QueryWrapper } from './utils';
axios.defaults.adapter = require('axios/lib/adapters/http');
-nock(url)
- .defaultReplyHeaders(defaultHeaders)
- .persist()
- .get('/version')
- .reply(200, { version: '', gitVersion: '' });
-
-test('App shows Login screen by default', () => {
- const { getByText } = render(
- <BrowserRouter>
- <App />
- </BrowserRouter>,
- { wrapper: QueryWrapper },
- );
-
- expect(getByText('Password')).toBeInTheDocument();
-});
-
describe('test login component', () => {
+ beforeAll(() => {
+ nock(url)
+ .defaultReplyHeaders(defaultHeaders)
+ .persist()
+ .get('/version')
+ .reply(200, { version: '', gitVersion: '' });
+ });
+
+ afterAll(() => {
+ nock.cleanAll();
+ });
+
+ test('App shows Login screen by default', () => {
+ const { getByText } = render(
+ <BrowserRouter>
+ <App />
+ </BrowserRouter>,
+ { wrapper: QueryWrapper },
+ );
+
+ expect(getByText('Password')).toBeInTheDocument();
+ });
+
test('Button is disabled when there is no username or password', () => {
const { getByTestId } = render(
<BrowserRouter>
diff --git a/airflow/ui/test/Pipelines.test.tsx
b/airflow/ui/test/Pipelines.test.tsx
index 3c98d8d..b40f360 100644
--- a/airflow/ui/test/Pipelines.test.tsx
+++ b/airflow/ui/test/Pipelines.test.tsx
@@ -45,14 +45,20 @@ const sampleDag = {
],
};
-nock(url)
- .defaultReplyHeaders(defaultHeaders)
- .persist()
- .get('/version')
- .reply(200, { version: '', gitVersion: '' });
-
describe('Test Pipelines Table', () => {
- test('Show a loading indicator and have a DAG count of 0 before data loads',
async () => {
+ beforeEach(() => {
+ nock(url)
+ .defaultReplyHeaders(defaultHeaders)
+ .persist()
+ .get('/version')
+ .reply(200, { version: '', gitVersion: '' });
+ });
+
+ afterAll(() => {
+ nock.cleanAll();
+ });
+
+ test('Show a loading indicator before data loads', async () => {
nock(url)
.defaultReplyHeaders(defaultHeaders)
.get('/dags')
@@ -71,11 +77,14 @@ describe('Test Pipelines Table', () => {
await waitFor(() =>
expect(getByText(sampleDag.dagId)).toBeInTheDocument());
});
- test('Show Empty State text if there are no dags', () => {
+ test('Show Empty State text if there are no dags', async () => {
nock(url)
.defaultReplyHeaders(defaultHeaders)
.get('/dags')
- .reply(404, undefined);
+ .reply(404, {
+ dags: [],
+ totalEntries: 0,
+ });
const { getByText } = render(
<QueryWrapper><Pipelines /></QueryWrapper>,
@@ -83,6 +92,6 @@ describe('Test Pipelines Table', () => {
wrapper: RouterWrapper,
},
);
- waitFor(() => expect(getByText('No DAGs found.')).toBeInTheDocument());
+ await waitFor(() => expect(getByText('No Pipelines
found.')).toBeInTheDocument());
});
});