mirror of
https://github.com/apache/superset.git
synced 2026-04-23 18:14:56 +00:00
test: add React Testing Library (#11771)
This commit is contained in:
85
superset-frontend/spec/javascripts/components/Timer_spec.tsx
Normal file
85
superset-frontend/spec/javascripts/components/Timer_spec.tsx
Normal file
@@ -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('');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user