test: add React Testing Library (#11771)
This commit is contained in:
parent
3035090915
commit
a5af2ade0a
|
|
@ -165,11 +165,11 @@ module.exports = {
|
|||
'src/**/*.test.js',
|
||||
'src/**/*.test.jsx',
|
||||
],
|
||||
plugins: ['jest', 'no-only-tests'],
|
||||
plugins: ['jest', 'jest-dom', 'no-only-tests'],
|
||||
env: {
|
||||
'jest/globals': true,
|
||||
},
|
||||
extends: ['plugin:jest/recommended'],
|
||||
extends: ['plugin:jest/recommended', 'plugin:testing-library/react'],
|
||||
rules: {
|
||||
'import/no-extraneous-dependencies': [
|
||||
'error',
|
||||
|
|
|
|||
|
|
@ -26,10 +26,7 @@ module.exports = {
|
|||
'^spec/(.*)$': '<rootDir>/spec/$1',
|
||||
},
|
||||
testEnvironment: 'enzyme',
|
||||
setupFilesAfterEnv: [
|
||||
'<rootDir>/node_modules/jest-enzyme/lib/index.js',
|
||||
'<rootDir>/spec/helpers/shim.ts',
|
||||
],
|
||||
setupFilesAfterEnv: ['<rootDir>/spec/helpers/setup.ts'],
|
||||
testURL: 'http://localhost',
|
||||
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'],
|
||||
coverageDirectory: '<rootDir>/coverage/',
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -195,6 +195,8 @@
|
|||
"@storybook/preset-typescript": "^3.0.0",
|
||||
"@storybook/react": "^6.0.28",
|
||||
"@svgr/webpack": "^5.4.0",
|
||||
"@testing-library/jest-dom": "^5.11.6",
|
||||
"@testing-library/react": "^11.2.0",
|
||||
"@types/classnames": "^2.2.10",
|
||||
"@types/dom-to-image": "^2.6.0",
|
||||
"@types/enzyme": "^3.10.5",
|
||||
|
|
@ -245,21 +247,23 @@
|
|||
"eslint-plugin-cypress": "^2.11.1",
|
||||
"eslint-plugin-import": "^2.22.0",
|
||||
"eslint-plugin-jest": "^23.17.1",
|
||||
"eslint-plugin-jest-dom": "^3.2.4",
|
||||
"eslint-plugin-jsx-a11y": "^6.3.1",
|
||||
"eslint-plugin-no-only-tests": "^2.0.1",
|
||||
"eslint-plugin-prettier": "^3.1.3",
|
||||
"eslint-plugin-react": "^7.20.6",
|
||||
"eslint-plugin-react-hooks": "^4.1.2",
|
||||
"eslint-plugin-testing-library": "^3.10.1",
|
||||
"exports-loader": "^0.7.0",
|
||||
"fetch-mock": "^7.7.3",
|
||||
"file-loader": "^6.0.0",
|
||||
"fork-ts-checker-webpack-plugin": "^0.4.9",
|
||||
"ignore-styles": "^5.0.1",
|
||||
"imports-loader": "^0.7.1",
|
||||
"jest": "^26.1.0",
|
||||
"jest": "^26.6.3",
|
||||
"jest-environment-enzyme": "^7.1.2",
|
||||
"jest-enzyme": "^7.1.2",
|
||||
"jsdom": "^16.2.2",
|
||||
"jsdom": "^16.4.0",
|
||||
"less": "^3.12.2",
|
||||
"less-loader": "^5.0.0",
|
||||
"mini-css-extract-plugin": "^0.4.0",
|
||||
|
|
|
|||
|
|
@ -17,15 +17,18 @@
|
|||
* under the License.
|
||||
*/
|
||||
{
|
||||
"plugins": ["jest", "no-only-tests"],
|
||||
"plugins": ["jest", "jest-dom", "no-only-tests", "testing-library"],
|
||||
"env": {
|
||||
"jest/globals": true
|
||||
},
|
||||
"extends": ["plugin:jest/recommended"],
|
||||
"extends": [
|
||||
"plugin:jest/recommended",
|
||||
"plugin:testing-library/react"
|
||||
],
|
||||
"rules": {
|
||||
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }],
|
||||
"jest/consistent-test-it": "error",
|
||||
"no-only-tests/no-only-tests": "error",
|
||||
"prefer-promise-reject-errors": 0
|
||||
"prefer-promise-reject-errors": 0
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* 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.
|
||||
*/
|
||||
// `jest-dom` must be imported before `jest-enzyme` because there are conflicts
|
||||
// between these two.
|
||||
import '@testing-library/jest-dom/extend-expect';
|
||||
import 'jest-enzyme';
|
||||
import './shim';
|
||||
import { configure as configureTestingLibrary } from '@testing-library/react';
|
||||
|
||||
configureTestingLibrary({
|
||||
testIdAttribute: 'data-test-id',
|
||||
});
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* 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.
|
||||
*/
|
||||
import React, { ReactNode, ReactElement } from 'react';
|
||||
import { render, RenderOptions } from '@testing-library/react';
|
||||
import { ThemeProvider, supersetTheme } from '@superset-ui/core';
|
||||
|
||||
function SupersetProviders({ children }: { children?: ReactNode }) {
|
||||
return <ThemeProvider theme={supersetTheme}>{children}</ThemeProvider>;
|
||||
}
|
||||
|
||||
const customRender = (
|
||||
ui: ReactElement,
|
||||
options?: Omit<RenderOptions, 'queries'>,
|
||||
) => render(ui, { wrapper: SupersetProviders, ...options });
|
||||
|
||||
export function sleep(time: number) {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(resolve, time);
|
||||
});
|
||||
}
|
||||
|
||||
export * from '@testing-library/react';
|
||||
export { customRender as render };
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
import React from 'react';
|
||||
import { render, sleep, waitFor } from 'spec/helpers/testing-library';
|
||||
import Timer from 'src/components/Timer';
|
||||
import { now } from 'src/modules/dates';
|
||||
|
||||
function parseTime(text?: string | null) {
|
||||
return !!text && Number(text.replace(/:/g, ''));
|
||||
}
|
||||
|
||||
describe('Timer', () => {
|
||||
const mockProps = {
|
||||
startTime: now(),
|
||||
endTime: undefined,
|
||||
isRunning: true,
|
||||
status: 'warning',
|
||||
};
|
||||
|
||||
it('should render correctly', async () => {
|
||||
const screen = render(<Timer {...mockProps} />);
|
||||
const node = screen.getByRole('timer');
|
||||
let text = node.textContent || '';
|
||||
expect(node).toBeInTheDocument();
|
||||
expect(node).toHaveClass('label-warning');
|
||||
expect(node).toHaveTextContent('00:00:00.00');
|
||||
// should start running
|
||||
await waitFor(() => {
|
||||
expect(parseTime(screen.getByRole('timer')?.textContent)).toBeGreaterThan(
|
||||
0.2,
|
||||
);
|
||||
});
|
||||
text = node.textContent || '';
|
||||
|
||||
// should stop
|
||||
screen.rerender(<Timer {...mockProps} isRunning={false} />);
|
||||
// the same node should still be in DOM and the content should not change
|
||||
expect(screen.getByRole('timer')).toBe(node);
|
||||
expect(node).toHaveTextContent(text);
|
||||
|
||||
// the timestamp should not change even after while
|
||||
await sleep(100);
|
||||
expect(screen.getByRole('timer')).toBe(node);
|
||||
expect(node).toHaveTextContent(text);
|
||||
|
||||
// should continue and start from stopped time
|
||||
screen.rerender(<Timer {...mockProps} isRunning />);
|
||||
expect(screen.getByRole('timer')).toBe(node);
|
||||
expect(node).toHaveTextContent(text);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByRole('timer')).toBe(node);
|
||||
expect(parseTime(node.textContent)).toBeGreaterThan(0.3);
|
||||
});
|
||||
|
||||
// should restart from start
|
||||
screen.rerender(<Timer {...mockProps} startTime={now()} />);
|
||||
await waitFor(() => {
|
||||
expect(parseTime(node.textContent)).toBeLessThan(0.1);
|
||||
});
|
||||
// should continue to run
|
||||
await waitFor(() => {
|
||||
expect(parseTime(node.textContent)).toBeGreaterThan(0.3);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -17,20 +17,20 @@
|
|||
* under the License.
|
||||
*/
|
||||
import React from 'react';
|
||||
import { shallow } from 'enzyme';
|
||||
import { render } from 'spec/helpers/testing-library';
|
||||
import ControlSetRow from 'src/explore/components/ControlRow';
|
||||
|
||||
describe('ControlSetRow', () => {
|
||||
it('renders a single row with one element', () => {
|
||||
// eslint-disable-next-line jsx-a11y/anchor-has-content
|
||||
const wrapper = shallow(<ControlSetRow controls={[<a />]} />);
|
||||
expect(wrapper.find('.row')).toExist();
|
||||
expect(wrapper.find('.row').find('a')).toExist();
|
||||
const { getAllByText } = render(
|
||||
<ControlSetRow controls={[<p>My Control 1</p>]} />,
|
||||
);
|
||||
expect(getAllByText('My Control 1')).toHaveLength(1);
|
||||
});
|
||||
it('renders a single row with two elements', () => {
|
||||
// eslint-disable-next-line jsx-a11y/anchor-has-content
|
||||
const wrapper = shallow(<ControlSetRow controls={[<a />, <a />]} />);
|
||||
expect(wrapper.find('.row')).toExist();
|
||||
expect(wrapper.find('.row').find('a')).toHaveLength(2);
|
||||
const { getAllByText } = render(
|
||||
<ControlSetRow controls={[<p>My Control 1</p>, <p>My Control 2</p>]} />,
|
||||
);
|
||||
expect(getAllByText(/My Control/)).toHaveLength(2);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,48 +0,0 @@
|
|||
/**
|
||||
* 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.
|
||||
*/
|
||||
import React from 'react';
|
||||
import { styledMount as mount } from 'spec/helpers/theming';
|
||||
import Timer from 'src/components/Timer';
|
||||
import { now } from 'src/modules/dates';
|
||||
|
||||
describe('Timer', () => {
|
||||
let wrapper;
|
||||
const mockedProps = {
|
||||
endTime: null,
|
||||
isRunning: true,
|
||||
status: 'warning',
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockedProps.startTime = now() + 1;
|
||||
wrapper = mount(<Timer {...mockedProps} />);
|
||||
});
|
||||
|
||||
it('renders correctly', () => {
|
||||
expect(React.isValidElement(<Timer {...mockedProps} />)).toBe(true);
|
||||
expect(wrapper.find('span').hasClass('label-warning')).toBe(true);
|
||||
});
|
||||
|
||||
it('should start timer and sets clockStr', async () => {
|
||||
expect.assertions(2);
|
||||
expect(wrapper.find('span').text()).toBe('');
|
||||
await new Promise(r => setTimeout(r, 35));
|
||||
expect(wrapper.find('span').text()).not.toBe('');
|
||||
});
|
||||
});
|
||||
|
|
@ -33,6 +33,7 @@ export interface LabelProps {
|
|||
bsStyle?: string;
|
||||
style?: CSSProperties;
|
||||
children?: React.ReactNode;
|
||||
role?: string;
|
||||
}
|
||||
|
||||
const SupersetLabel = styled(BootstrapLabel)`
|
||||
|
|
|
|||
|
|
@ -40,8 +40,8 @@ export default function Timer({
|
|||
startTime,
|
||||
status = 'success',
|
||||
}: TimerProps) {
|
||||
const [clockStr, setClockStr] = useState('');
|
||||
const timer = useRef<NodeJS.Timeout>();
|
||||
const [clockStr, setClockStr] = useState('00:00:00.00');
|
||||
const timer = useRef<ReturnType<typeof setInterval>>();
|
||||
|
||||
useEffect(() => {
|
||||
const stopTimer = () => {
|
||||
|
|
@ -67,5 +67,9 @@ export default function Timer({
|
|||
return stopTimer;
|
||||
}, [endTime, isRunning, startTime]);
|
||||
|
||||
return <TimerLabel bsStyle={status}>{clockStr}</TimerLabel>;
|
||||
return (
|
||||
<TimerLabel bsStyle={status} role="timer">
|
||||
{clockStr}
|
||||
</TimerLabel>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -130,6 +130,7 @@ function processHTML(proxyResponse, response) {
|
|||
body = Buffer.concat([body, data]);
|
||||
})
|
||||
.on('error', error => {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(error);
|
||||
response.end(`Error fetching proxied request: ${error.message}`);
|
||||
})
|
||||
|
|
|
|||
Loading…
Reference in New Issue