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

View file

@ -59,41 +59,29 @@ describe('<DeleteShortUrlModal />', () => {
expect(submit.html()).toContain('Deleting...'); 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 shortCode = 'abc123';
const wrapper = createWrapper({ const wrapper = createWrapper({
loading: false, loading: false,
error: false, error: false,
shortCode, shortCode,
}); });
const input = wrapper.find('.form-control');
input.simulate('change', { target: { value: shortCode } }); expect(wrapper.find('.btn-danger').prop('disabled')).toEqual(true);
setImmediate(() => { wrapper.find('.form-control').simulate('change', { target: { value: shortCode } });
const submit = wrapper.find('.btn-danger'); expect(wrapper.find('.btn-danger').prop('disabled')).toEqual(false);
expect(submit.prop('disabled')).toEqual(false);
done();
});
}); });
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 shortCode = 'abc123';
const wrapper = createWrapper({ const wrapper = createWrapper({
loading: false, loading: false,
error: false, error: false,
shortCode, shortCode,
}); });
const input = wrapper.find('.form-control');
input.simulate('change', { target: { value: shortCode } }); expect(deleteShortUrl).not.toHaveBeenCalled();
setImmediate(() => { wrapper.find('form').simulate('submit', { preventDefault: identity });
const form = wrapper.find('form'); expect(deleteShortUrl).toHaveBeenCalledTimes(1);
expect(deleteShortUrl).not.toHaveBeenCalled();
form.simulate('submit', { preventDefault: identity });
expect(deleteShortUrl).toHaveBeenCalledTimes(1);
done();
});
}); });
}); });

View file

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

View file

@ -33,44 +33,29 @@ describe('<OpenMapModalBtn />', () => {
expect(modal).toHaveLength(1); 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 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(() => { expect(wrapper.find(Dropdown).prop('isOpen')).toEqual(true);
const dropdown = wrapper.find(Dropdown); expect(wrapper.find(MapModal).prop('isOpen')).toEqual(false);
const modal = wrapper.find(MapModal);
expect(dropdown.prop('isOpen')).toEqual(true);
expect(modal.prop('isOpen')).toEqual(false);
done();
});
}); });
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 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(() => { wrapper.find(Dropdown).find(DropdownItem).at(1).simulate('click');
const dropdown = wrapper.find(Dropdown);
const item = dropdown.find(DropdownItem).at(1);
item.simulate('click'); const modal = wrapper.find(MapModal);
setImmediate(() => {
const modal = wrapper.find(MapModal);
expect(modal.prop('title')).toEqual(title); expect(modal.prop('title')).toEqual(title);
expect(modal.prop('locations')).toEqual([ expect(modal.prop('locations')).toEqual([
{ {
cityName: 'bar', cityName: 'bar',
count: 45, count: 45,
}, },
]); ]);
done();
});
});
}); });
}); });