Removed remaining instances of setImmediate in tests

This commit is contained in:
Alejandro Celaya 2021-10-31 12:33:17 +01:00
parent 57e73dcba6
commit e5f99d0893
4 changed files with 43 additions and 74 deletions

View file

@ -1,11 +1,11 @@
import { useRef, RefObject, ChangeEvent, MutableRefObject, FC } from 'react';
import { Button, UncontrolledTooltip } from 'reactstrap';
import { pipe } from 'ramda';
import { faFileUpload as importIcon } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import ServersImporter from '../services/ServersImporter';
import { ServerData } from '../data';
import './ImportServersBtn.scss';
import { pipe } from 'ramda';
type Ref<T> = RefObject<T> | MutableRefObject<T>;

View file

@ -59,41 +59,29 @@ describe('<DeleteShortUrlModal />', () => {
expect(submit.html()).toContain('Deleting...');
});
it('enables submit button when proper short code is provided', (done) => {
it('enables submit button when proper short code is provided', () => {
const shortCode = 'abc123';
const wrapper = createWrapper({
loading: false,
error: false,
shortCode,
});
const input = wrapper.find('.form-control');
input.simulate('change', { target: { value: shortCode } });
setImmediate(() => {
const submit = wrapper.find('.btn-danger');
expect(submit.prop('disabled')).toEqual(false);
done();
});
expect(wrapper.find('.btn-danger').prop('disabled')).toEqual(true);
wrapper.find('.form-control').simulate('change', { target: { value: shortCode } });
expect(wrapper.find('.btn-danger').prop('disabled')).toEqual(false);
});
it('tries to delete short URL when form is submit', (done) => {
it('tries to delete short URL when form is submit', () => {
const shortCode = 'abc123';
const wrapper = createWrapper({
loading: false,
error: false,
shortCode,
});
const input = wrapper.find('.form-control');
input.simulate('change', { target: { value: shortCode } });
setImmediate(() => {
const form = wrapper.find('form');
expect(deleteShortUrl).not.toHaveBeenCalled();
form.simulate('submit', { preventDefault: identity });
expect(deleteShortUrl).toHaveBeenCalledTimes(1);
done();
});
expect(deleteShortUrl).not.toHaveBeenCalled();
wrapper.find('form').simulate('submit', { preventDefault: identity });
expect(deleteShortUrl).toHaveBeenCalledTimes(1);
});
});

View file

@ -40,32 +40,30 @@ describe('<SortableBarChartCard />', () => {
});
describe('renders properly ordered stats when ordering is set', () => {
let assert: (sortName: string, sortDir: OrderDir, keys: string[], values: number[], done: Function) => void;
let assert: (sortName: string, sortDir: OrderDir, keys: string[], values: number[]) => void;
beforeEach(() => {
const wrapper = createWrapper();
const dropdown = wrapper.renderProp('title' as never)().find(SortingDropdown);
assert = (sortName: string, sortDir: OrderDir, keys: string[], values: number[], done: Function) => {
assert = (sortName: string, sortDir: OrderDir, keys: string[], values: number[]) => {
dropdown.prop('onChange')(sortName, sortDir);
setImmediate(() => {
const stats = wrapper.find(HorizontalBarChart).prop('stats');
expect(Object.keys(stats)).toEqual(keys);
expect(Object.values(stats)).toEqual(values);
done();
});
const stats = wrapper.find(HorizontalBarChart).prop('stats');
expect(Object.keys(stats)).toEqual(keys);
expect(Object.values(stats)).toEqual(values);
};
});
it('name - ASC', (done) => assert('name', 'ASC', [ 'Bar', 'Foo' ], [ 50, 100 ], done));
it('name - DESC', (done) => assert('name', 'DESC', [ 'Foo', 'Bar' ], [ 100, 50 ], done));
it('value - ASC', (done) => assert('value', 'ASC', [ 'Bar', 'Foo' ], [ 50, 100 ], done));
it('value - DESC', (done) => assert('value', 'DESC', [ 'Foo', 'Bar' ], [ 100, 50 ], done));
it('name - ASC', () => assert('name', 'ASC', [ 'Bar', 'Foo' ], [ 50, 100 ]));
it('name - DESC', () => assert('name', 'DESC', [ 'Foo', 'Bar' ], [ 100, 50 ]));
it('value - ASC', () => assert('value', 'ASC', [ 'Bar', 'Foo' ], [ 50, 100 ]));
it('value - DESC', () => assert('value', 'DESC', [ 'Foo', 'Bar' ], [ 100, 50 ]));
});
describe('renders properly paginated stats when pagination is set', () => {
let assert: (itemsPerPage: number, expectedStats: string[], done: Function) => void;
let assert: (itemsPerPage: number, expectedStats: string[]) => void;
beforeEach(() => {
const wrapper = createWrapper(true, range(1, 159).reduce<Stats>((accum, value) => {
@ -75,23 +73,21 @@ describe('<SortableBarChartCard />', () => {
}, {}));
const dropdown = wrapper.renderProp('title' as never)().find(PaginationDropdown);
assert = (itemsPerPage: number, expectedStats: string[], done: Function) => {
assert = (itemsPerPage: number, expectedStats: string[]) => {
dropdown.prop('setValue')(itemsPerPage);
setImmediate(() => {
const stats = wrapper.find(HorizontalBarChart).prop('stats');
expect(Object.keys(stats)).toEqual(expectedStats);
done();
});
const stats = wrapper.find(HorizontalBarChart).prop('stats');
expect(Object.keys(stats)).toEqual(expectedStats);
};
});
const buildExpected = (size: number): string[] => [ 'Foo', 'Bar', ...rangeOf(size - 2, (i) => `key_${i}`) ];
it('50 items per page', (done) => assert(50, buildExpected(50), done));
it('100 items per page', (done) => assert(100, buildExpected(100), done));
it('200 items per page', (done) => assert(200, buildExpected(160), done));
it('500 items per page', (done) => assert(500, buildExpected(160), done));
it('50 items per page', () => assert(50, buildExpected(50)));
it('100 items per page', () => assert(100, buildExpected(100)));
it('200 items per page', () => assert(200, buildExpected(160)));
it('500 items per page', () => assert(500, buildExpected(160)));
});
it('renders extra header content', () => {

View file

@ -33,44 +33,29 @@ describe('<OpenMapModalBtn />', () => {
expect(modal).toHaveLength(1);
});
it('opens dropdown instead of modal when a list of active cities has been provided', (done) => {
it('opens dropdown instead of modal when a list of active cities has been provided', () => {
const wrapper = createWrapper([ 'bar' ]);
const button = wrapper.find('.open-map-modal-btn__btn');
button.simulate('click');
wrapper.find('.open-map-modal-btn__btn').simulate('click');
setImmediate(() => {
const dropdown = wrapper.find(Dropdown);
const modal = wrapper.find(MapModal);
expect(dropdown.prop('isOpen')).toEqual(true);
expect(modal.prop('isOpen')).toEqual(false);
done();
});
expect(wrapper.find(Dropdown).prop('isOpen')).toEqual(true);
expect(wrapper.find(MapModal).prop('isOpen')).toEqual(false);
});
it('filters out non-active cities from list of locations', (done) => {
it('filters out non-active cities from list of locations', () => {
const wrapper = createWrapper([ 'bar' ]);
const button = wrapper.find('.open-map-modal-btn__btn');
button.simulate('click');
setImmediate(() => {
const dropdown = wrapper.find(Dropdown);
const item = dropdown.find(DropdownItem).at(1);
wrapper.find('.open-map-modal-btn__btn').simulate('click');
wrapper.find(Dropdown).find(DropdownItem).at(1).simulate('click');
item.simulate('click');
setImmediate(() => {
const modal = wrapper.find(MapModal);
const modal = wrapper.find(MapModal);
expect(modal.prop('title')).toEqual(title);
expect(modal.prop('locations')).toEqual([
{
cityName: 'bar',
count: 45,
},
]);
done();
});
});
expect(modal.prop('title')).toEqual(title);
expect(modal.prop('locations')).toEqual([
{
cityName: 'bar',
count: 45,
},
]);
});
});