From 5942cd6fcf107b2769b2d3cff976c354be7d4c2b Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 22 Dec 2022 18:39:09 +0100 Subject: [PATCH] Ensured proper amount of visits is displayed on short URLs based on config --- src/short-urls/helpers/ShortUrlsRow.tsx | 20 +++++++++++----- src/short-urls/services/provideServices.ts | 3 +++ test/short-urls/helpers/ShortUrlsRow.test.tsx | 23 ++++++++++++++----- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/short-urls/helpers/ShortUrlsRow.tsx b/src/short-urls/helpers/ShortUrlsRow.tsx index e962da53..549266c4 100644 --- a/src/short-urls/helpers/ShortUrlsRow.tsx +++ b/src/short-urls/helpers/ShortUrlsRow.tsx @@ -1,4 +1,4 @@ -import { useEffect, useRef } from 'react'; +import { FC, useEffect, useRef } from 'react'; import { ExternalLink } from 'react-external-link'; import { ColorGenerator } from '../../utils/services/ColorGenerator'; import { TimeoutToggle } from '../../utils/helpers/hooks'; @@ -6,6 +6,7 @@ import { SelectedServer } from '../../servers/data'; import { CopyToClipboardIcon } from '../../utils/CopyToClipboardIcon'; import { ShortUrl } from '../data'; import { Time } from '../../utils/dates/Time'; +import { Settings } from '../../settings/reducers/settings'; import { ShortUrlVisitsCount } from './ShortUrlVisitsCount'; import { ShortUrlsRowMenuType } from './ShortUrlsRowMenu'; import { Tags } from './Tags'; @@ -18,19 +19,26 @@ interface ShortUrlsRowProps { shortUrl: ShortUrl; } +interface ShortUrlsRowConnectProps extends ShortUrlsRowProps { + settings: Settings; +} + +export type ShortUrlsRowType = FC; + export const ShortUrlsRow = ( ShortUrlsRowMenu: ShortUrlsRowMenuType, colorGenerator: ColorGenerator, useTimeoutToggle: TimeoutToggle, -) => ({ shortUrl, selectedServer, onTagClick }: ShortUrlsRowProps) => { +) => ({ shortUrl, selectedServer, onTagClick, settings }: ShortUrlsRowConnectProps) => { const [copiedToClipboard, setCopiedToClipboard] = useTimeoutToggle(); const [active, setActive] = useTimeoutToggle(false, 500); const isFirstRun = useRef(true); + const { visits } = settings; useEffect(() => { !isFirstRun.current && setActive(); isFirstRun.current = false; - }, [shortUrl.visitsCount]); + }, [shortUrl.visitsSummary?.total, shortUrl.visitsSummary?.nonBots, shortUrl.visitsCount]); return ( @@ -64,7 +72,9 @@ export const ShortUrlsRow = ( ); }; - -export type ShortUrlsRowType = ReturnType; diff --git a/src/short-urls/services/provideServices.ts b/src/short-urls/services/provideServices.ts index eeb10ce9..185ab81e 100644 --- a/src/short-urls/services/provideServices.ts +++ b/src/short-urls/services/provideServices.ts @@ -28,7 +28,10 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => { )); bottle.serviceFactory('ShortUrlsTable', ShortUrlsTable, 'ShortUrlsRow'); + bottle.serviceFactory('ShortUrlsRow', ShortUrlsRow, 'ShortUrlsRowMenu', 'ColorGenerator', 'useTimeoutToggle'); + bottle.decorator('ShortUrlsRow', connect(['settings'])); + bottle.serviceFactory('ShortUrlsRowMenu', ShortUrlsRowMenu, 'DeleteShortUrlModal', 'QrCodeModal'); bottle.serviceFactory('CreateShortUrlResult', CreateShortUrlResult, 'useTimeoutToggle'); bottle.serviceFactory('ShortUrlForm', ShortUrlForm, 'TagsSelector', 'DomainSelector'); diff --git a/test/short-urls/helpers/ShortUrlsRow.test.tsx b/test/short-urls/helpers/ShortUrlsRow.test.tsx index 35e2a858..9884b0e1 100644 --- a/test/short-urls/helpers/ShortUrlsRow.test.tsx +++ b/test/short-urls/helpers/ShortUrlsRow.test.tsx @@ -5,12 +5,20 @@ import { addDays, formatISO, subDays } from 'date-fns'; import { ShortUrlsRow as createShortUrlsRow } from '../../../src/short-urls/helpers/ShortUrlsRow'; import { TimeoutToggle } from '../../../src/utils/helpers/hooks'; import { ShortUrl, ShortUrlMeta } from '../../../src/short-urls/data'; +import { Settings } from '../../../src/settings/reducers/settings'; import { ReachableServer } from '../../../src/servers/data'; import { parseDate, now } from '../../../src/utils/helpers/date'; import { renderWithEvents } from '../../__helpers__/setUpTest'; import { OptionalString } from '../../../src/utils/utils'; import { colorGeneratorMock } from '../../utils/services/__mocks__/ColorGenerator.mock'; +interface SetUpOptions { + title?: OptionalString; + tags?: string[]; + meta?: ShortUrlMeta; + settings?: Partial; +} + describe('', () => { const timeoutToggle = jest.fn(() => true); const useTimeoutToggle = jest.fn(() => [false, timeoutToggle]) as TimeoutToggle; @@ -35,15 +43,14 @@ describe('', () => { }, }; const ShortUrlsRow = createShortUrlsRow(() => ShortUrlsRowMenu, colorGeneratorMock, useTimeoutToggle); - const setUp = ( - { title, tags = [], meta = {} }: { title?: OptionalString; tags?: string[]; meta?: ShortUrlMeta } = {}, - ) => renderWithEvents( + const setUp = ({ title, tags = [], meta = {}, settings = {} }: SetUpOptions = {}) => renderWithEvents( null} + settings={Mock.of(settings)} />
, @@ -97,9 +104,13 @@ describe('', () => { expectedContents.forEach((content) => expect(cell).toHaveTextContent(content)); }); - it('renders visits count in fifth row', () => { - setUp(); - expect(screen.getAllByRole('cell')[4]).toHaveTextContent(`${shortUrl.visitsCount}`); + it.each([ + [{}, shortUrl.visitsSummary?.total], + [Mock.of({ visits: { excludeBots: false } }), shortUrl.visitsSummary?.total], + [Mock.of({ visits: { excludeBots: true } }), shortUrl.visitsSummary?.nonBots], + ])('renders visits count in fifth row', (settings, expectedAmount) => { + setUp({ settings }); + expect(screen.getAllByRole('cell')[4]).toHaveTextContent(`${expectedAmount}`); }); it('updates state when copied to clipboard', async () => {