diff --git a/test/mocks/WindowMock.ts b/test/__mocks__/Window.mock.ts similarity index 100% rename from test/mocks/WindowMock.ts rename to test/__mocks__/Window.mock.ts diff --git a/test/common/services/ImageDownloader.test.ts b/test/common/services/ImageDownloader.test.ts index 9a640bcd..20a381cf 100644 --- a/test/common/services/ImageDownloader.test.ts +++ b/test/common/services/ImageDownloader.test.ts @@ -1,7 +1,7 @@ import { Mock } from 'ts-mockery'; import { AxiosInstance } from 'axios'; import { ImageDownloader } from '../../../src/common/services/ImageDownloader'; -import { windowMock } from '../../mocks/WindowMock'; +import { windowMock } from '../../__mocks__/Window.mock'; describe('ImageDownloader', () => { const get = jest.fn(); diff --git a/test/common/services/ReportExporter.test.ts b/test/common/services/ReportExporter.test.ts index 5fd5f941..02097005 100644 --- a/test/common/services/ReportExporter.test.ts +++ b/test/common/services/ReportExporter.test.ts @@ -1,6 +1,6 @@ import { ReportExporter } from '../../../src/common/services/ReportExporter'; import { NormalizedVisit } from '../../../src/visits/types'; -import { windowMock } from '../../mocks/WindowMock'; +import { windowMock } from '../../__mocks__/Window.mock'; import { ExportableShortUrl } from '../../../src/short-urls/data'; describe('ReportExporter', () => { diff --git a/test/servers/services/ServersExporter.test.ts b/test/servers/services/ServersExporter.test.ts index 54e93fcf..0aa73ec1 100644 --- a/test/servers/services/ServersExporter.test.ts +++ b/test/servers/services/ServersExporter.test.ts @@ -1,7 +1,7 @@ import { Mock } from 'ts-mockery'; import ServersExporter from '../../../src/servers/services/ServersExporter'; import { LocalStorage } from '../../../src/utils/services/LocalStorage'; -import { appendChild, removeChild, windowMock } from '../../mocks/WindowMock'; +import { appendChild, removeChild, windowMock } from '../../__mocks__/Window.mock'; describe('ServersExporter', () => { const storageMock = Mock.of({ diff --git a/test/short-urls/helpers/ShortUrlsRow.test.tsx b/test/short-urls/helpers/ShortUrlsRow.test.tsx index 899b1797..98b4cfdd 100644 --- a/test/short-urls/helpers/ShortUrlsRow.test.tsx +++ b/test/short-urls/helpers/ShortUrlsRow.test.tsx @@ -2,22 +2,17 @@ import { screen } from '@testing-library/react'; import { Mock } from 'ts-mockery'; import { formatISO } from 'date-fns'; import { ShortUrlsRow as createShortUrlsRow } from '../../../src/short-urls/helpers/ShortUrlsRow'; -import { ColorGenerator } from '../../../src/utils/services/ColorGenerator'; import { TimeoutToggle } from '../../../src/utils/helpers/hooks'; import { ShortUrl } from '../../../src/short-urls/data'; import { ReachableServer } from '../../../src/servers/data'; import { parseDate } from '../../../src/utils/helpers/date'; import { renderWithEvents } from '../../__helpers__/setUpTest'; import { OptionalString } from '../../../src/utils/utils'; +import { colorGeneratorMock } from '../../utils/services/__mocks__/ColorGenerator.mock'; describe('', () => { const timeoutToggle = jest.fn(() => true); const useTimeoutToggle = jest.fn(() => [false, timeoutToggle]) as TimeoutToggle; - const colorGenerator = Mock.of({ - getColorForKey: jest.fn(() => 'red'), - setColorForKey: jest.fn(), - isColorLightForKey: jest.fn(() => false), - }); const server = Mock.of({ url: 'https://doma.in' }); const shortUrl: ShortUrl = { shortCode: 'abc123', @@ -33,7 +28,7 @@ describe('', () => { maxVisits: null, }, }; - const ShortUrlsRow = createShortUrlsRow(() => ShortUrlsRowMenu, colorGenerator, useTimeoutToggle); + const ShortUrlsRow = createShortUrlsRow(() => ShortUrlsRowMenu, colorGeneratorMock, useTimeoutToggle); const setUp = (title?: OptionalString, tags: string[] = []) => renderWithEvents( diff --git a/test/tags/TagsTableRow.test.tsx b/test/tags/TagsTableRow.test.tsx index ffced252..d1f92385 100644 --- a/test/tags/TagsTableRow.test.tsx +++ b/test/tags/TagsTableRow.test.tsx @@ -1,16 +1,31 @@ +import { screen } from '@testing-library/react'; import { shallow, ShallowWrapper } from 'enzyme'; import { Mock } from 'ts-mockery'; -import { Link } from 'react-router-dom'; import { DropdownItem } from 'reactstrap'; +import { MemoryRouter } from 'react-router-dom'; import { TagsTableRow as createTagsTableRow } from '../../src/tags/TagsTableRow'; import { ReachableServer } from '../../src/servers/data'; -import { ColorGenerator } from '../../src/utils/services/ColorGenerator'; import { DropdownBtnMenu } from '../../src/utils/DropdownBtnMenu'; +import { renderWithEvents } from '../__helpers__/setUpTest'; +import { colorGeneratorMock } from '../utils/services/__mocks__/ColorGenerator.mock'; describe('', () => { const DeleteTagConfirmModal = () => null; const EditTagModal = () => null; - const TagsTableRow = createTagsTableRow(DeleteTagConfirmModal, EditTagModal, Mock.all()); + const TagsTableRow = createTagsTableRow(DeleteTagConfirmModal, EditTagModal, colorGeneratorMock); + const setUp = (tagStats?: { visits?: number; shortUrls?: number }) => renderWithEvents( + +
+ + ({ id: 'abc123' })} + /> + +
+ , + ); + let wrapper: ShallowWrapper; const createWrapper = (tagStats?: { visits?: number; shortUrls?: number }) => { wrapper = shallow( @@ -22,22 +37,23 @@ describe('', () => { return wrapper; }; - afterEach(() => wrapper?.unmount()); it.each([ [undefined, '0', '0'], [{ shortUrls: 10, visits: 3480 }, '10', '3,480'], ])('shows expected tag stats', (stats, expectedShortUrls, expectedVisits) => { - const wrapper = createWrapper(stats); - const links = wrapper.find(Link); - const shortUrlsLink = links.first(); - const visitsLink = links.last(); + setUp(stats); - expect(shortUrlsLink.prop('children')).toEqual(expectedShortUrls); - expect(shortUrlsLink.prop('to')).toEqual(`/server/abc123/list-short-urls/1?tags=${encodeURIComponent('foo&bar')}`); - expect(visitsLink.prop('children')).toEqual(expectedVisits); - expect(visitsLink.prop('to')).toEqual('/server/abc123/tag/foo&bar/visits'); + const [shortUrlsLink, visitsLink] = screen.getAllByRole('link'); + + expect(shortUrlsLink).toHaveTextContent(expectedShortUrls); + expect(shortUrlsLink).toHaveAttribute( + 'href', + `/server/abc123/list-short-urls/1?tags=${encodeURIComponent('foo&bar')}`, + ); + expect(visitsLink).toHaveTextContent(expectedVisits); + expect(visitsLink).toHaveAttribute('href', '/server/abc123/tag/foo&bar/visits'); }); it('allows toggling dropdown menu', () => { diff --git a/test/utils/services/__mocks__/ColorGenerator.mock.ts b/test/utils/services/__mocks__/ColorGenerator.mock.ts new file mode 100644 index 00000000..c7fcf522 --- /dev/null +++ b/test/utils/services/__mocks__/ColorGenerator.mock.ts @@ -0,0 +1,8 @@ +import { Mock } from 'ts-mockery'; +import { ColorGenerator } from '../../../../src/utils/services/ColorGenerator'; + +export const colorGeneratorMock = Mock.of({ + getColorForKey: jest.fn(() => 'red'), + setColorForKey: jest.fn(), + isColorLightForKey: jest.fn(() => false), +});