diff --git a/jest.config.js b/jest.config.js index d8cf9bde..87e110a8 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,19 +1,18 @@ module.exports = { coverageDirectory: '/coverage', collectCoverageFrom: [ - 'src/**/*.{js,ts,tsx}', - '!src/registerServiceWorker.js', - '!src/index.ts', + 'src/**/*.{ts,tsx}', + '!src/*.{ts,tsx}', '!src/reducers/index.ts', '!src/**/provideServices.ts', '!src/container/*.ts', ], coverageThreshold: { global: { + statements: 80, branches: 80, functions: 80, lines: 80, - statements: 80, }, }, resolver: 'jest-pnp-resolver', diff --git a/test/common/services/ImageDownloader.test.ts b/test/common/services/ImageDownloader.test.ts new file mode 100644 index 00000000..185625ce --- /dev/null +++ b/test/common/services/ImageDownloader.test.ts @@ -0,0 +1,25 @@ +import { Mock } from 'ts-mockery'; +import { AxiosInstance } from 'axios'; +import { ImageDownloader } from '../../../src/common/services/ImageDownloader'; +import { windowMock } from '../../mocks/WindowMock'; + +describe('ImageDownloader', () => { + const get = jest.fn(); + const axios = Mock.of({ get }); + let imageDownloader: ImageDownloader; + + beforeEach(() => { + jest.clearAllMocks(); + (global as any).URL = { createObjectURL: () => '' }; + + imageDownloader = new ImageDownloader(axios, windowMock); + }); + + test('calls URL with response type blob', async () => { + get.mockResolvedValue({ data: {} }); + + await imageDownloader.saveImage('/foo/bar.png', 'my-image.png'); + + expect(get).toHaveBeenCalledWith('/foo/bar.png', { responseType: 'blob' }); + }); +}); diff --git a/test/mocks/WindowMock.ts b/test/mocks/WindowMock.ts new file mode 100644 index 00000000..69cb1f4e --- /dev/null +++ b/test/mocks/WindowMock.ts @@ -0,0 +1,18 @@ +import { Mock } from 'ts-mockery'; + +const createLinkMock = () => ({ + setAttribute: jest.fn(), + click: jest.fn(), + style: {}, +}); + +export const appendChild = jest.fn(); + +export const removeChild = jest.fn(); + +export const windowMock = Mock.of({ + document: { + createElement: jest.fn(createLinkMock), + body: { appendChild, removeChild }, + }, +}); diff --git a/test/servers/services/ServersExporter.test.ts b/test/servers/services/ServersExporter.test.ts index cf0fd386..fc990b1a 100644 --- a/test/servers/services/ServersExporter.test.ts +++ b/test/servers/services/ServersExporter.test.ts @@ -2,21 +2,9 @@ import { Mock } from 'ts-mockery'; import { CsvJson } from 'csvjson'; import ServersExporter from '../../../src/servers/services/ServersExporter'; import LocalStorage from '../../../src/utils/services/LocalStorage'; +import { appendChild, removeChild, windowMock } from '../../mocks/WindowMock'; describe('ServersExporter', () => { - const createLinkMock = () => ({ - setAttribute: jest.fn(), - click: jest.fn(), - style: {}, - }); - const appendChild = jest.fn(); - const removeChild = jest.fn(); - const windowMock = Mock.of({ - document: { - createElement: jest.fn(createLinkMock), - body: { appendChild, removeChild }, - }, - }); const storageMock = Mock.of({ get: jest.fn(() => ({ abc123: { diff --git a/test/visits/charts/DoughnutChart.test.tsx b/test/visits/charts/DoughnutChart.test.tsx index 5868aeb3..e216fb38 100644 --- a/test/visits/charts/DoughnutChart.test.tsx +++ b/test/visits/charts/DoughnutChart.test.tsx @@ -3,7 +3,7 @@ import { Doughnut } from 'react-chartjs-2'; import { keys, values } from 'ramda'; import { DoughnutChart } from '../../../src/visits/charts/DoughnutChart'; -describe.skip('', () => { +describe('', () => { let wrapper: ShallowWrapper; const stats = { foo: 123, diff --git a/test/visits/charts/HorizontalBarChart.test.tsx b/test/visits/charts/HorizontalBarChart.test.tsx index d830e4fc..346dc822 100644 --- a/test/visits/charts/HorizontalBarChart.test.tsx +++ b/test/visits/charts/HorizontalBarChart.test.tsx @@ -4,7 +4,7 @@ import { prettify } from '../../../src/utils/helpers/numbers'; import { MAIN_COLOR, MAIN_COLOR_ALPHA } from '../../../src/utils/theme'; import { HorizontalBarChart } from '../../../src/visits/charts/HorizontalBarChart'; -describe.skip('', () => { +describe('', () => { let wrapper: ShallowWrapper; const stats = { foo: 123, @@ -16,7 +16,6 @@ describe.skip('', () => { it('renders Bar with expected properties', () => { wrapper = shallow(); const horizontal = wrapper.find(Bar); - const cols = wrapper.find('.col-sm-12'); expect(horizontal).toHaveLength(1); @@ -37,7 +36,6 @@ describe.skip('', () => { }, y: { stacked: true }, }); - expect(cols).toHaveLength(1); }); it.each([ diff --git a/test/visits/services/VisitsExporter.test.ts b/test/visits/services/VisitsExporter.test.ts index da91fd78..40a34600 100644 --- a/test/visits/services/VisitsExporter.test.ts +++ b/test/visits/services/VisitsExporter.test.ts @@ -2,19 +2,9 @@ import { Mock } from 'ts-mockery'; import { CsvJson } from 'csvjson'; import { VisitsExporter } from '../../../src/visits/services/VisitsExporter'; import { NormalizedVisit } from '../../../src/visits/types'; +import { windowMock } from '../../mocks/WindowMock'; describe('VisitsExporter', () => { - const createLinkMock = () => ({ - setAttribute: jest.fn(), - click: jest.fn(), - style: {}, - }); - const windowMock = Mock.of({ - document: { - createElement: jest.fn(createLinkMock), - body: { appendChild: jest.fn(), removeChild: jest.fn() }, - }, - }); const toCSV = jest.fn(); const csvToJsonMock = Mock.of({ toCSV }); let exporter: VisitsExporter;