From e6932e73539cdbc1e507bf319865c2bcb8292447 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 30 Apr 2022 12:01:50 +0200 Subject: [PATCH] Created DomainVisits test --- config/jest/setupTests.ts | 4 ++ package-lock.json | 64 +++++++++++++++++++++++++++++ package.json | 2 + src/visits/reducers/domainVisits.ts | 2 - test/visits/DomainVisits.test.tsx | 52 +++++++++++++++++++++++ 5 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 test/visits/DomainVisits.test.tsx diff --git a/config/jest/setupTests.ts b/config/jest/setupTests.ts index 7b0828bf..b75cbb7d 100644 --- a/config/jest/setupTests.ts +++ b/config/jest/setupTests.ts @@ -1 +1,5 @@ import '@testing-library/jest-dom'; +import 'jest-canvas-mock'; +import ResizeObserver from 'resize-observer-polyfill'; + +(global as any).ResizeObserver = ResizeObserver; diff --git a/package-lock.json b/package-lock.json index 3d806f5e..e63f3651 100644 --- a/package-lock.json +++ b/package-lock.json @@ -80,7 +80,9 @@ "eslint": "^8.12.0", "identity-obj-proxy": "^3.0.0", "jest": "^27.5.1", + "jest-canvas-mock": "^2.4.0", "react-scripts": "^5.0.0", + "resize-observer-polyfill": "^1.5.1", "sass": "^1.49.9", "serve": "^13.0.2", "stryker-cli": "^1.0.2", @@ -8421,6 +8423,12 @@ "node": ">=4" } }, + "node_modules/cssfontparser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/cssfontparser/-/cssfontparser-1.2.1.tgz", + "integrity": "sha1-9AIvyPlwDGgCnVQghK+69CWj8+M=", + "dev": true + }, "node_modules/cssnano-utils": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/cssnano-utils/-/cssnano-utils-3.1.0.tgz", @@ -13573,6 +13581,16 @@ } } }, + "node_modules/jest-canvas-mock": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/jest-canvas-mock/-/jest-canvas-mock-2.4.0.tgz", + "integrity": "sha512-mmMpZzpmLzn5vepIaHk5HoH3Ka4WykbSoLuG/EKoJd0x0ID/t+INo1l8ByfcUJuDM+RIsL4QDg/gDnBbrj2/IQ==", + "dev": true, + "dependencies": { + "cssfontparser": "^1.2.1", + "moo-color": "^1.0.2" + } + }, "node_modules/jest-changed-files": { "version": "27.5.1", "resolved": "https://registry.npmjs.org/jest-changed-files/-/jest-changed-files-27.5.1.tgz", @@ -16879,6 +16897,15 @@ "integrity": "sha512-I1mnb5xn4fO80BH9BLcF0yLypy2UKl+Cb01Fu0hJRkJjlCRtxZMWkTdAtDd5ZqCOxtCkhmRwyI57vWT+1iZ67w==", "dev": true }, + "node_modules/moo-color": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/moo-color/-/moo-color-1.0.3.tgz", + "integrity": "sha512-i/+ZKXMDf6aqYtBhuOcej71YSlbjT3wCO/4H1j8rPvxDJEifdwgg5MaFyu6iYAT8GBZJg2z0dkgK4YMzvURALQ==", + "dev": true, + "dependencies": { + "color-name": "^1.1.4" + } + }, "node_modules/ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -22002,6 +22029,12 @@ "integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=", "dev": true }, + "node_modules/resize-observer-polyfill": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz", + "integrity": "sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==", + "dev": true + }, "node_modules/resolve": { "version": "1.22.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.0.tgz", @@ -33502,6 +33535,12 @@ "integrity": "sha512-/Tb/JcjK111nNScGob5MNtsntNM1aCNUDipB/TkwZFhyDrrE47SOx/18wF2bbjgc3ZzCSKW1T5nt5EbFoAz/Vg==", "dev": true }, + "cssfontparser": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/cssfontparser/-/cssfontparser-1.2.1.tgz", + "integrity": "sha1-9AIvyPlwDGgCnVQghK+69CWj8+M=", + "dev": true + }, "cssnano-utils": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/cssnano-utils/-/cssnano-utils-3.1.0.tgz", @@ -37451,6 +37490,16 @@ "jest-cli": "^27.5.1" } }, + "jest-canvas-mock": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/jest-canvas-mock/-/jest-canvas-mock-2.4.0.tgz", + "integrity": "sha512-mmMpZzpmLzn5vepIaHk5HoH3Ka4WykbSoLuG/EKoJd0x0ID/t+INo1l8ByfcUJuDM+RIsL4QDg/gDnBbrj2/IQ==", + "dev": true, + "requires": { + "cssfontparser": "^1.2.1", + "moo-color": "^1.0.2" + } + }, "jest-changed-files": { "version": "27.5.1", "resolved": "https://registry.npmjs.org/jest-changed-files/-/jest-changed-files-27.5.1.tgz", @@ -39901,6 +39950,15 @@ "integrity": "sha512-I1mnb5xn4fO80BH9BLcF0yLypy2UKl+Cb01Fu0hJRkJjlCRtxZMWkTdAtDd5ZqCOxtCkhmRwyI57vWT+1iZ67w==", "dev": true }, + "moo-color": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/moo-color/-/moo-color-1.0.3.tgz", + "integrity": "sha512-i/+ZKXMDf6aqYtBhuOcej71YSlbjT3wCO/4H1j8rPvxDJEifdwgg5MaFyu6iYAT8GBZJg2z0dkgK4YMzvURALQ==", + "dev": true, + "requires": { + "color-name": "^1.1.4" + } + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -43544,6 +43602,12 @@ "integrity": "sha1-kl0mAdOaxIXgkc8NpcbmlNw9yv8=", "dev": true }, + "resize-observer-polyfill": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz", + "integrity": "sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==", + "dev": true + }, "resolve": { "version": "1.22.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.0.tgz", diff --git a/package.json b/package.json index 69a7e1f5..91f5ccb2 100644 --- a/package.json +++ b/package.json @@ -96,7 +96,9 @@ "eslint": "^8.12.0", "identity-obj-proxy": "^3.0.0", "jest": "^27.5.1", + "jest-canvas-mock": "^2.4.0", "react-scripts": "^5.0.0", + "resize-observer-polyfill": "^1.5.1", "sass": "^1.49.9", "serve": "^13.0.2", "stryker-cli": "^1.0.2", diff --git a/src/visits/reducers/domainVisits.ts b/src/visits/reducers/domainVisits.ts index e5652140..3570e023 100644 --- a/src/visits/reducers/domainVisits.ts +++ b/src/visits/reducers/domainVisits.ts @@ -22,7 +22,6 @@ export const DEFAULT_DOMAIN = 'DEFAULT'; export interface DomainVisits extends VisitsInfo { domain: string; - domainId: string; } export interface DomainVisitsAction extends Action { @@ -40,7 +39,6 @@ type DomainVisitsCombinedAction = DomainVisitsAction const initialState: DomainVisits = { visits: [], domain: '', - domainId: '', loading: false, loadingLarge: false, error: false, diff --git a/test/visits/DomainVisits.test.tsx b/test/visits/DomainVisits.test.tsx new file mode 100644 index 00000000..7e9761d2 --- /dev/null +++ b/test/visits/DomainVisits.test.tsx @@ -0,0 +1,52 @@ +import { fireEvent, render, screen } from '@testing-library/react'; +import { MemoryRouter } from 'react-router-dom'; +import { Mock } from 'ts-mockery'; +import { formatISO } from 'date-fns'; +import { DomainVisits as createDomainVisits } from '../../src/visits/DomainVisits'; +import { ReportExporter } from '../../src/common/services/ReportExporter'; +import { MercureBoundProps } from '../../src/mercure/helpers/boundToMercureHub'; +import { DomainVisits } from '../../src/visits/reducers/domainVisits'; +import { Settings } from '../../src/settings/reducers/settings'; +import { SelectedServer } from '../../src/servers/data'; +import { Visit } from '../../src/visits/types'; + +jest.mock('react-router-dom', () => ({ + ...jest.requireActual('react-router-dom'), + useParams: jest.fn().mockReturnValue({ domain: 'foo.com_DEFAULT' }), +})); + +describe('', () => { + const exportVisits = jest.fn(); + const getDomainVisits = jest.fn(); + const cancelGetDomainVisits = jest.fn(); + const domainVisits = Mock.of({ visits: [Mock.of({ date: formatISO(new Date()) })] }); + const DomainVisits = createDomainVisits(Mock.of({ exportVisits })); + + beforeEach(() => render( + + ({ mercureInfo: {} })} + getDomainVisits={getDomainVisits} + cancelGetDomainVisits={cancelGetDomainVisits} + domainVisits={domainVisits} + settings={Mock.all()} + selectedServer={Mock.all()} + /> + , + )); + + it('wraps visits stats and header', () => { + expect(screen.getByRole('heading', { name: '"foo.com" visits' })).toBeInTheDocument(); + expect(getDomainVisits).toHaveBeenCalledWith('DEFAULT', expect.anything(), expect.anything()); + }); + + it('exports visits when clicking the button', () => { + const btn = screen.getByRole('button', { name: 'Export (1)' }); + + expect(exportVisits).not.toHaveBeenCalled(); + expect(btn).toBeInTheDocument(); + + fireEvent.click(btn); + expect(exportVisits).toHaveBeenCalledWith('domain_foo.com_visits.csv', expect.anything()); + }); +});