From e5f99d0893fa0d117e9a414f721a6d7902085657 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 31 Oct 2021 12:33:17 +0100 Subject: [PATCH] Removed remaining instances of setImmediate in tests --- src/servers/helpers/ImportServersBtn.tsx | 2 +- .../helpers/DeleteShortUrlModal.test.tsx | 28 ++++-------- .../charts/SortableBarChartCard.test.tsx | 42 ++++++++--------- test/visits/helpers/OpenMapModalBtn.test.tsx | 45 +++++++------------ 4 files changed, 43 insertions(+), 74 deletions(-) diff --git a/src/servers/helpers/ImportServersBtn.tsx b/src/servers/helpers/ImportServersBtn.tsx index 5e84b314..3f27cfc2 100644 --- a/src/servers/helpers/ImportServersBtn.tsx +++ b/src/servers/helpers/ImportServersBtn.tsx @@ -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 = RefObject | MutableRefObject; diff --git a/test/short-urls/helpers/DeleteShortUrlModal.test.tsx b/test/short-urls/helpers/DeleteShortUrlModal.test.tsx index 0ee94da8..af4a6014 100644 --- a/test/short-urls/helpers/DeleteShortUrlModal.test.tsx +++ b/test/short-urls/helpers/DeleteShortUrlModal.test.tsx @@ -59,41 +59,29 @@ describe('', () => { 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); }); }); diff --git a/test/visits/charts/SortableBarChartCard.test.tsx b/test/visits/charts/SortableBarChartCard.test.tsx index 24ff1ad5..f3b4e1f2 100644 --- a/test/visits/charts/SortableBarChartCard.test.tsx +++ b/test/visits/charts/SortableBarChartCard.test.tsx @@ -40,32 +40,30 @@ describe('', () => { }); 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((accum, value) => { @@ -75,23 +73,21 @@ describe('', () => { }, {})); 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', () => { diff --git a/test/visits/helpers/OpenMapModalBtn.test.tsx b/test/visits/helpers/OpenMapModalBtn.test.tsx index 60fce34a..534ac087 100644 --- a/test/visits/helpers/OpenMapModalBtn.test.tsx +++ b/test/visits/helpers/OpenMapModalBtn.test.tsx @@ -33,44 +33,29 @@ describe('', () => { 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, + }, + ]); }); });