Finish replacing ts-mockery with shoehorn

This commit is contained in:
Alejandro Celaya 2023-04-14 09:28:53 +02:00
parent 04e1950591
commit 26bad75a1a
12 changed files with 131 additions and 166 deletions

19
package-lock.json generated
View file

@ -85,7 +85,6 @@
"resize-observer-polyfill": "^1.5.1",
"sass": "^1.57.1",
"stylelint": "^14.16.0",
"ts-mockery": "^1.2.0",
"typescript": "^5.0.2",
"vite": "^4.2.0",
"vite-plugin-pwa": "^0.14.4"
@ -11902,14 +11901,6 @@
"node": ">=8"
}
},
"node_modules/ts-mockery": {
"version": "1.2.0",
"dev": true,
"license": "MIT",
"peerDependencies": {
"typescript": ">= 2.8"
}
},
"node_modules/ts-toolbelt": {
"version": "6.15.5",
"dev": true,
@ -15897,6 +15888,9 @@
}
}
},
"caniuse-lite": {
"version": "1.0.30001415"
},
"chalk": {
"version": "5.2.0",
"dev": true
@ -20285,11 +20279,6 @@
"version": "3.0.1",
"dev": true
},
"ts-mockery": {
"version": "1.2.0",
"dev": true,
"requires": {}
},
"ts-toolbelt": {
"version": "6.15.5",
"dev": true
@ -20959,4 +20948,4 @@
"dev": true
}
}
}
}

View file

@ -102,7 +102,6 @@
"resize-observer-polyfill": "^1.5.1",
"sass": "^1.57.1",
"stylelint": "^14.16.0",
"ts-mockery": "^1.2.0",
"typescript": "^5.0.2",
"vite": "^4.2.0",
"vite-plugin-pwa": "^0.14.4"

View file

@ -1,14 +1,14 @@
import { screen, waitFor } from '@testing-library/react';
import { Mock } from 'ts-mockery';
import { fromPartial } from '@total-typescript/shoehorn';
import { OpenMapModalBtn } from '../../../src/visits/helpers/OpenMapModalBtn';
import type { CityStats } from '../../../src/visits/types';
import { renderWithEvents } from '../../__helpers__/setUpTest';
describe('<OpenMapModalBtn />', () => {
const title = 'Foo';
const locations = [
Mock.of<CityStats>({ cityName: 'foo', count: 30, latLong: [5, 5] }),
Mock.of<CityStats>({ cityName: 'bar', count: 45, latLong: [88, 88] }),
const locations: CityStats[] = [
fromPartial({ cityName: 'foo', count: 30, latLong: [5, 5] }),
fromPartial({ cityName: 'bar', count: 45, latLong: [88, 88] }),
];
const setUp = (activeCities?: string[]) => renderWithEvents(
<OpenMapModalBtn modalTitle={title} locations={locations} activeCities={activeCities} />,

View file

@ -1,5 +1,5 @@
import { fromPartial } from '@total-typescript/shoehorn';
import { addDays, formatISO, subDays } from 'date-fns';
import { Mock } from 'ts-mockery';
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { ShlinkVisits } from '../../../src/api/types';
import type { ShlinkState } from '../../../src/container/types';
@ -16,25 +16,25 @@ import {
getDomainVisits as getDomainVisitsCreator,
} from '../../../src/visits/reducers/domainVisits';
import { createNewVisits } from '../../../src/visits/reducers/visitCreation';
import type { CreateVisit, Visit } from '../../../src/visits/types';
import type { Visit } from '../../../src/visits/types';
describe('domainVisitsReducer', () => {
const now = new Date();
const visitsMocks = rangeOf(2, () => Mock.all<Visit>());
const visitsMocks = rangeOf(2, () => fromPartial<Visit>({}));
const getDomainVisitsCall = jest.fn();
const buildApiClientMock = () => Mock.of<ShlinkApiClient>({ getDomainVisits: getDomainVisitsCall });
const buildApiClientMock = () => fromPartial<ShlinkApiClient>({ getDomainVisits: getDomainVisitsCall });
const getDomainVisits = getDomainVisitsCreator(buildApiClientMock);
const { reducer, cancelGetVisits: cancelGetDomainVisits } = domainVisitsReducerCreator(getDomainVisits);
beforeEach(jest.clearAllMocks);
describe('reducer', () => {
const buildState = (data: Partial<DomainVisits>) => Mock.of<DomainVisits>(data);
const buildState = (data: Partial<DomainVisits>) => fromPartial<DomainVisits>(data);
it('returns loading on GET_DOMAIN_VISITS_START', () => {
const { loading } = reducer(
buildState({ loading: false }),
getDomainVisits.pending('', Mock.all<LoadDomainVisits>()),
getDomainVisits.pending('', fromPartial<LoadDomainVisits>({})),
);
expect(loading).toEqual(true);
});
@ -52,7 +52,7 @@ describe('domainVisitsReducer', () => {
it('stops loading and returns error on GET_DOMAIN_VISITS_ERROR', () => {
const state = reducer(
buildState({ loading: true, error: false }),
getDomainVisits.rejected(null, '', Mock.all<LoadDomainVisits>()),
getDomainVisits.rejected(null, '', fromPartial({})),
);
const { loading, error } = state;
@ -61,10 +61,10 @@ describe('domainVisitsReducer', () => {
});
it('return visits on GET_DOMAIN_VISITS', () => {
const actionVisits = [Mock.all<Visit>(), Mock.all<Visit>()];
const actionVisits: Visit[] = [fromPartial({}), fromPartial({})];
const { loading, error, visits } = reducer(
buildState({ loading: true, error: false }),
getDomainVisits.fulfilled({ visits: actionVisits }, '', Mock.all<LoadDomainVisits>()),
getDomainVisits.fulfilled({ visits: actionVisits }, '', fromPartial({})),
);
expect(loading).toEqual(false);
@ -75,10 +75,10 @@ describe('domainVisitsReducer', () => {
it.each([
[{ domain: 'foo.com' }, 'foo.com', visitsMocks.length + 1],
[{ domain: 'bar.com' }, 'foo.com', visitsMocks.length],
[Mock.of<DomainVisits>({ domain: 'foo.com' }), 'foo.com', visitsMocks.length + 1],
[Mock.of<DomainVisits>({ domain: DEFAULT_DOMAIN }), null, visitsMocks.length + 1],
[fromPartial<DomainVisits>({ domain: 'foo.com' }), 'foo.com', visitsMocks.length + 1],
[fromPartial<DomainVisits>({ domain: DEFAULT_DOMAIN }), null, visitsMocks.length + 1],
[
Mock.of<DomainVisits>({
fromPartial<DomainVisits>({
domain: 'foo.com',
query: { endDate: formatIsoDate(subDays(now, 1)) ?? undefined },
}),
@ -86,7 +86,7 @@ describe('domainVisitsReducer', () => {
visitsMocks.length,
],
[
Mock.of<DomainVisits>({
fromPartial<DomainVisits>({
domain: 'foo.com',
query: { startDate: formatIsoDate(addDays(now, 1)) ?? undefined },
}),
@ -94,7 +94,7 @@ describe('domainVisitsReducer', () => {
visitsMocks.length,
],
[
Mock.of<DomainVisits>({
fromPartial<DomainVisits>({
domain: 'foo.com',
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
@ -105,7 +105,7 @@ describe('domainVisitsReducer', () => {
visitsMocks.length,
],
[
Mock.of<DomainVisits>({
fromPartial<DomainVisits>({
domain: 'foo.com',
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
@ -116,7 +116,7 @@ describe('domainVisitsReducer', () => {
visitsMocks.length + 1,
],
[
Mock.of<DomainVisits>({
fromPartial<DomainVisits>({
domain: 'bar.com',
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
@ -127,9 +127,9 @@ describe('domainVisitsReducer', () => {
visitsMocks.length,
],
])('prepends new visits on CREATE_VISIT', (state, shortUrlDomain, expectedVisits) => {
const shortUrl = Mock.of<ShortUrl>({ domain: shortUrlDomain });
const shortUrl = fromPartial<ShortUrl>({ domain: shortUrlDomain });
const { visits } = reducer(buildState({ ...state, visits: visitsMocks }), createNewVisits([
Mock.of<CreateVisit>({ shortUrl, visit: { date: formatIsoDate(now) ?? undefined } }),
fromPartial({ shortUrl, visit: { date: formatIsoDate(now) ?? undefined } }),
]));
expect(visits).toHaveLength(expectedVisits);
@ -153,7 +153,7 @@ describe('domainVisitsReducer', () => {
describe('getDomainVisits', () => {
const dispatchMock = jest.fn();
const getState = () => Mock.of<ShlinkState>({
const getState = () => fromPartial<ShlinkState>({
domainVisits: { cancelLoad: false },
});
const domain = 'foo.com';
@ -183,12 +183,12 @@ describe('domainVisitsReducer', () => {
it.each([
[
[Mock.of<Visit>({ date: formatISO(subDays(now, 20)) })],
[fromPartial<Visit>({ date: formatISO(subDays(now, 20)) })],
getDomainVisits.fallbackToInterval('last30Days'),
3,
],
[
[Mock.of<Visit>({ date: formatISO(subDays(now, 100)) })],
[fromPartial<Visit>({ date: formatISO(subDays(now, 100)) })],
getDomainVisits.fallbackToInterval('last180Days'),
3,
],

View file

@ -1,5 +1,5 @@
import { fromPartial } from '@total-typescript/shoehorn';
import { addDays, formatISO, subDays } from 'date-fns';
import { Mock } from 'ts-mockery';
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { ShlinkVisits } from '../../../src/api/types';
import type { ShlinkState } from '../../../src/container/types';
@ -16,16 +16,16 @@ import type { Visit } from '../../../src/visits/types';
describe('nonOrphanVisitsReducer', () => {
const now = new Date();
const visitsMocks = rangeOf(2, () => Mock.all<Visit>());
const visitsMocks = rangeOf(2, () => fromPartial<Visit>({}));
const getNonOrphanVisitsCall = jest.fn();
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ getNonOrphanVisits: getNonOrphanVisitsCall });
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ getNonOrphanVisits: getNonOrphanVisitsCall });
const getNonOrphanVisits = getNonOrphanVisitsCreator(buildShlinkApiClient);
const { reducer, cancelGetVisits: cancelGetNonOrphanVisits } = nonOrphanVisitsReducerCreator(getNonOrphanVisits);
beforeEach(jest.clearAllMocks);
describe('reducer', () => {
const buildState = (data: Partial<VisitsInfo>) => Mock.of<VisitsInfo>(data);
const buildState = (data: Partial<VisitsInfo>) => fromPartial<VisitsInfo>(data);
it('returns loading on GET_NON_ORPHAN_VISITS_START', () => {
const { loading } = reducer(buildState({ loading: false }), getNonOrphanVisits.pending('', {}));
@ -53,7 +53,7 @@ describe('nonOrphanVisitsReducer', () => {
});
it('return visits on GET_NON_ORPHAN_VISITS', () => {
const actionVisits = [Mock.all<Visit>(), Mock.all<Visit>()];
const actionVisits: Visit[] = [fromPartial({}), fromPartial({})];
const { loading, error, visits } = reducer(
buildState({ loading: true, error: false }),
getNonOrphanVisits.fulfilled({ visits: actionVisits }, '', {}),
@ -67,19 +67,19 @@ describe('nonOrphanVisitsReducer', () => {
it.each([
[{}, visitsMocks.length + 2],
[
Mock.of<VisitsInfo>({
fromPartial<VisitsInfo>({
query: { endDate: formatIsoDate(subDays(now, 1)) ?? undefined },
}),
visitsMocks.length,
],
[
Mock.of<VisitsInfo>({
fromPartial<VisitsInfo>({
query: { startDate: formatIsoDate(addDays(now, 1)) ?? undefined },
}),
visitsMocks.length,
],
[
Mock.of<VisitsInfo>({
fromPartial<VisitsInfo>({
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
endDate: formatIsoDate(subDays(now, 2)) ?? undefined,
@ -88,7 +88,7 @@ describe('nonOrphanVisitsReducer', () => {
visitsMocks.length,
],
[
Mock.of<VisitsInfo>({
fromPartial<VisitsInfo>({
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
endDate: formatIsoDate(addDays(now, 3)) ?? undefined,
@ -98,7 +98,7 @@ describe('nonOrphanVisitsReducer', () => {
],
])('prepends new visits on CREATE_VISIT', (state, expectedVisits) => {
const prevState = buildState({ ...state, visits: visitsMocks });
const visit = Mock.of<Visit>({ date: formatIsoDate(now) ?? undefined });
const visit = fromPartial<Visit>({ date: formatIsoDate(now) ?? undefined });
const { visits } = reducer(prevState, createNewVisits([{ visit }, { visit }]));
@ -120,7 +120,7 @@ describe('nonOrphanVisitsReducer', () => {
describe('getNonOrphanVisits', () => {
const dispatchMock = jest.fn();
const getState = () => Mock.of<ShlinkState>({
const getState = () => fromPartial<ShlinkState>({
orphanVisits: { cancelLoad: false },
});
@ -151,12 +151,12 @@ describe('nonOrphanVisitsReducer', () => {
it.each([
[
[Mock.of<Visit>({ date: formatISO(subDays(now, 5)) })],
[fromPartial<Visit>({ date: formatISO(subDays(now, 5)) })],
getNonOrphanVisits.fallbackToInterval('last7Days'),
3,
],
[
[Mock.of<Visit>({ date: formatISO(subDays(now, 200)) })],
[fromPartial<Visit>({ date: formatISO(subDays(now, 200)) })],
getNonOrphanVisits.fallbackToInterval('last365Days'),
3,
],

View file

@ -1,5 +1,5 @@
import { fromPartial } from '@total-typescript/shoehorn';
import { addDays, formatISO, subDays } from 'date-fns';
import { Mock } from 'ts-mockery';
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { ShlinkVisits } from '../../../src/api/types';
import type { ShlinkState } from '../../../src/container/types';
@ -16,16 +16,16 @@ import type { Visit } from '../../../src/visits/types';
describe('orphanVisitsReducer', () => {
const now = new Date();
const visitsMocks = rangeOf(2, () => Mock.all<Visit>());
const visitsMocks = rangeOf(2, () => fromPartial<Visit>({}));
const getOrphanVisitsCall = jest.fn();
const buildShlinkApiClientMock = () => Mock.of<ShlinkApiClient>({ getOrphanVisits: getOrphanVisitsCall });
const buildShlinkApiClientMock = () => fromPartial<ShlinkApiClient>({ getOrphanVisits: getOrphanVisitsCall });
const getOrphanVisits = getOrphanVisitsCreator(buildShlinkApiClientMock);
const { reducer, cancelGetVisits: cancelGetOrphanVisits } = orphanVisitsReducerCreator(getOrphanVisits);
beforeEach(jest.clearAllMocks);
describe('reducer', () => {
const buildState = (data: Partial<VisitsInfo>) => Mock.of<VisitsInfo>(data);
const buildState = (data: Partial<VisitsInfo>) => fromPartial<VisitsInfo>(data);
it('returns loading on GET_ORPHAN_VISITS_START', () => {
const { loading } = reducer(buildState({ loading: false }), getOrphanVisits.pending('', {}));
@ -53,7 +53,7 @@ describe('orphanVisitsReducer', () => {
});
it('return visits on GET_ORPHAN_VISITS', () => {
const actionVisits = [Mock.all<Visit>(), Mock.all<Visit>()];
const actionVisits: Visit[] = [fromPartial({}), fromPartial({})];
const { loading, error, visits } = reducer(
buildState({ loading: true, error: false }),
getOrphanVisits.fulfilled({ visits: actionVisits }, '', {}),
@ -67,19 +67,19 @@ describe('orphanVisitsReducer', () => {
it.each([
[{}, visitsMocks.length + 2],
[
Mock.of<VisitsInfo>({
fromPartial<VisitsInfo>({
query: { endDate: formatIsoDate(subDays(now, 1)) ?? undefined },
}),
visitsMocks.length,
],
[
Mock.of<VisitsInfo>({
fromPartial<VisitsInfo>({
query: { startDate: formatIsoDate(addDays(now, 1)) ?? undefined },
}),
visitsMocks.length,
],
[
Mock.of<VisitsInfo>({
fromPartial<VisitsInfo>({
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
endDate: formatIsoDate(subDays(now, 2)) ?? undefined,
@ -88,7 +88,7 @@ describe('orphanVisitsReducer', () => {
visitsMocks.length,
],
[
Mock.of<VisitsInfo>({
fromPartial<VisitsInfo>({
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
endDate: formatIsoDate(addDays(now, 3)) ?? undefined,
@ -98,7 +98,7 @@ describe('orphanVisitsReducer', () => {
],
])('prepends new visits on CREATE_VISIT', (state, expectedVisits) => {
const prevState = buildState({ ...state, visits: visitsMocks });
const visit = Mock.of<Visit>({ date: formatIsoDate(now) ?? undefined });
const visit = fromPartial<Visit>({ date: formatIsoDate(now) ?? undefined });
const { visits } = reducer(prevState, createNewVisits([{ visit }, { visit }]));
@ -120,7 +120,7 @@ describe('orphanVisitsReducer', () => {
describe('getOrphanVisits', () => {
const dispatchMock = jest.fn();
const getState = () => Mock.of<ShlinkState>({
const getState = () => fromPartial<ShlinkState>({
orphanVisits: { cancelLoad: false },
});
@ -149,12 +149,12 @@ describe('orphanVisitsReducer', () => {
it.each([
[
[Mock.of<Visit>({ date: formatISO(subDays(now, 5)) })],
[fromPartial<Visit>({ date: formatISO(subDays(now, 5)) })],
getOrphanVisits.fallbackToInterval('last7Days'),
3,
],
[
[Mock.of<Visit>({ date: formatISO(subDays(now, 200)) })],
[fromPartial<Visit>({ date: formatISO(subDays(now, 200)) })],
getOrphanVisits.fallbackToInterval('last365Days'),
3,
],

View file

@ -1,5 +1,5 @@
import { fromPartial } from '@total-typescript/shoehorn';
import { addDays, formatISO, subDays } from 'date-fns';
import { Mock } from 'ts-mockery';
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { ShlinkVisits } from '../../../src/api/types';
import type { ShlinkState } from '../../../src/container/types';
@ -13,20 +13,20 @@ import {
shortUrlVisitsReducerCreator,
} from '../../../src/visits/reducers/shortUrlVisits';
import { createNewVisits } from '../../../src/visits/reducers/visitCreation';
import type { CreateVisit, Visit } from '../../../src/visits/types';
import type { Visit } from '../../../src/visits/types';
describe('shortUrlVisitsReducer', () => {
const now = new Date();
const visitsMocks = rangeOf(2, () => Mock.all<Visit>());
const visitsMocks = rangeOf(2, () => fromPartial<Visit>({}));
const getShortUrlVisitsCall = jest.fn();
const buildApiClientMock = () => Mock.of<ShlinkApiClient>({ getShortUrlVisits: getShortUrlVisitsCall });
const buildApiClientMock = () => fromPartial<ShlinkApiClient>({ getShortUrlVisits: getShortUrlVisitsCall });
const getShortUrlVisits = getShortUrlVisitsCreator(buildApiClientMock);
const { reducer, cancelGetVisits: cancelGetShortUrlVisits } = shortUrlVisitsReducerCreator(getShortUrlVisits);
beforeEach(jest.clearAllMocks);
describe('reducer', () => {
const buildState = (data: Partial<ShortUrlVisits>) => Mock.of<ShortUrlVisits>(data);
const buildState = (data: Partial<ShortUrlVisits>) => fromPartial<ShortUrlVisits>(data);
it('returns loading on GET_SHORT_URL_VISITS_START', () => {
const { loading } = reducer(buildState({ loading: false }), getShortUrlVisits.pending('', { shortCode: '' }));
@ -54,7 +54,7 @@ describe('shortUrlVisitsReducer', () => {
});
it('return visits on GET_SHORT_URL_VISITS', () => {
const actionVisits = [Mock.all<Visit>(), Mock.all<Visit>()];
const actionVisits: Visit[] = [fromPartial({}), fromPartial({})];
const { loading, error, visits } = reducer(
buildState({ loading: true, error: false }),
getShortUrlVisits.fulfilled({ visits: actionVisits }, '', { shortCode: '' }),
@ -69,21 +69,21 @@ describe('shortUrlVisitsReducer', () => {
[{ shortCode: 'abc123' }, visitsMocks.length + 1],
[{ shortCode: 'def456' }, visitsMocks.length],
[
Mock.of<ShortUrlVisits>({
fromPartial<ShortUrlVisits>({
shortCode: 'abc123',
query: { endDate: formatIsoDate(subDays(now, 1)) ?? undefined },
}),
visitsMocks.length,
],
[
Mock.of<ShortUrlVisits>({
fromPartial<ShortUrlVisits>({
shortCode: 'abc123',
query: { startDate: formatIsoDate(addDays(now, 1)) ?? undefined },
}),
visitsMocks.length,
],
[
Mock.of<ShortUrlVisits>({
fromPartial<ShortUrlVisits>({
shortCode: 'abc123',
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
@ -93,7 +93,7 @@ describe('shortUrlVisitsReducer', () => {
visitsMocks.length,
],
[
Mock.of<ShortUrlVisits>({
fromPartial<ShortUrlVisits>({
shortCode: 'abc123',
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
@ -103,7 +103,7 @@ describe('shortUrlVisitsReducer', () => {
visitsMocks.length + 1,
],
[
Mock.of<ShortUrlVisits>({
fromPartial<ShortUrlVisits>({
shortCode: 'def456',
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
@ -123,7 +123,7 @@ describe('shortUrlVisitsReducer', () => {
const { visits } = reducer(
prevState,
createNewVisits([Mock.of<CreateVisit>({ shortUrl, visit: { date: formatIsoDate(now) ?? undefined } })]),
createNewVisits([fromPartial({ shortUrl, visit: { date: formatIsoDate(now) ?? undefined } })]),
);
expect(visits).toHaveLength(expectedVisits);
@ -144,8 +144,8 @@ describe('shortUrlVisitsReducer', () => {
describe('getShortUrlVisits', () => {
const dispatchMock = jest.fn();
const getState = () => Mock.of<ShlinkState>({
shortUrlVisits: Mock.of<ShortUrlVisits>({ cancelLoad: false }),
const getState = () => fromPartial<ShlinkState>({
shortUrlVisits: { cancelLoad: false },
});
it.each([
@ -197,12 +197,12 @@ describe('shortUrlVisitsReducer', () => {
it.each([
[
[Mock.of<Visit>({ date: formatISO(subDays(now, 5)) })],
[fromPartial<Visit>({ date: formatISO(subDays(now, 5)) })],
getShortUrlVisits.fallbackToInterval('last7Days'),
3,
],
[
[Mock.of<Visit>({ date: formatISO(subDays(now, 200)) })],
[fromPartial<Visit>({ date: formatISO(subDays(now, 200)) })],
getShortUrlVisits.fallbackToInterval('last365Days'),
3,
],

View file

@ -1,5 +1,5 @@
import { fromPartial } from '@total-typescript/shoehorn';
import { addDays, formatISO, subDays } from 'date-fns';
import { Mock } from 'ts-mockery';
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { ShlinkVisits } from '../../../src/api/types';
import type { ShlinkState } from '../../../src/container/types';
@ -13,20 +13,20 @@ import {
tagVisitsReducerCreator,
} from '../../../src/visits/reducers/tagVisits';
import { createNewVisits } from '../../../src/visits/reducers/visitCreation';
import type { CreateVisit, Visit } from '../../../src/visits/types';
import type { Visit } from '../../../src/visits/types';
describe('tagVisitsReducer', () => {
const now = new Date();
const visitsMocks = rangeOf(2, () => Mock.all<Visit>());
const visitsMocks = rangeOf(2, () => fromPartial<Visit>({}));
const getTagVisitsCall = jest.fn();
const buildShlinkApiClientMock = () => Mock.of<ShlinkApiClient>({ getTagVisits: getTagVisitsCall });
const buildShlinkApiClientMock = () => fromPartial<ShlinkApiClient>({ getTagVisits: getTagVisitsCall });
const getTagVisits = getTagVisitsCreator(buildShlinkApiClientMock);
const { reducer, cancelGetVisits: cancelGetTagVisits } = tagVisitsReducerCreator(getTagVisits);
beforeEach(jest.clearAllMocks);
describe('reducer', () => {
const buildState = (data: Partial<TagVisits>) => Mock.of<TagVisits>(data);
const buildState = (data: Partial<TagVisits>) => fromPartial<TagVisits>(data);
it('returns loading on GET_TAG_VISITS_START', () => {
const { loading } = reducer(buildState({ loading: false }), getTagVisits.pending('', { tag: '' }));
@ -54,7 +54,7 @@ describe('tagVisitsReducer', () => {
});
it('return visits on GET_TAG_VISITS', () => {
const actionVisits = [Mock.all<Visit>(), Mock.all<Visit>()];
const actionVisits: Visit[] = [fromPartial({}), fromPartial({})];
const { loading, error, visits } = reducer(
buildState({ loading: true, error: false }),
getTagVisits.fulfilled({ visits: actionVisits }, '', { tag: '' }),
@ -69,21 +69,21 @@ describe('tagVisitsReducer', () => {
[{ tag: 'foo' }, visitsMocks.length + 1],
[{ tag: 'bar' }, visitsMocks.length],
[
Mock.of<TagVisits>({
fromPartial<TagVisits>({
tag: 'foo',
query: { endDate: formatIsoDate(subDays(now, 1)) ?? undefined },
}),
visitsMocks.length,
],
[
Mock.of<TagVisits>({
fromPartial<TagVisits>({
tag: 'foo',
query: { startDate: formatIsoDate(addDays(now, 1)) ?? undefined },
}),
visitsMocks.length,
],
[
Mock.of<TagVisits>({
fromPartial<TagVisits>({
tag: 'foo',
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
@ -93,7 +93,7 @@ describe('tagVisitsReducer', () => {
visitsMocks.length,
],
[
Mock.of<TagVisits>({
fromPartial<TagVisits>({
tag: 'foo',
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
@ -103,7 +103,7 @@ describe('tagVisitsReducer', () => {
visitsMocks.length + 1,
],
[
Mock.of<TagVisits>({
fromPartial<TagVisits>({
tag: 'bar',
query: {
startDate: formatIsoDate(subDays(now, 5)) ?? undefined,
@ -123,7 +123,7 @@ describe('tagVisitsReducer', () => {
const { visits } = reducer(
prevState,
createNewVisits([Mock.of<CreateVisit>({ shortUrl, visit: { date: formatIsoDate(now) ?? undefined } })]),
createNewVisits([fromPartial({ shortUrl, visit: { date: formatIsoDate(now) ?? undefined } })]),
);
expect(visits).toHaveLength(expectedVisits);
@ -144,7 +144,7 @@ describe('tagVisitsReducer', () => {
describe('getTagVisits', () => {
const dispatchMock = jest.fn();
const getState = () => Mock.of<ShlinkState>({
const getState = () => fromPartial<ShlinkState>({
tagVisits: { cancelLoad: false },
});
const tag = 'foo';
@ -174,12 +174,12 @@ describe('tagVisitsReducer', () => {
it.each([
[
[Mock.of<Visit>({ date: formatISO(subDays(now, 20)) })],
[fromPartial<Visit>({ date: formatISO(subDays(now, 20)) })],
getTagVisits.fallbackToInterval('last30Days'),
3,
],
[
[Mock.of<Visit>({ date: formatISO(subDays(now, 100)) })],
[fromPartial<Visit>({ date: formatISO(subDays(now, 100)) })],
getTagVisits.fallbackToInterval('last180Days'),
3,
],

View file

@ -1,12 +1,12 @@
import { Mock } from 'ts-mockery';
import { fromPartial } from '@total-typescript/shoehorn';
import type { ShortUrl } from '../../../src/short-urls/data';
import { createNewVisits } from '../../../src/visits/reducers/visitCreation';
import type { Visit } from '../../../src/visits/types';
describe('visitCreationReducer', () => {
describe('createNewVisits', () => {
const shortUrl = Mock.all<ShortUrl>();
const visit = Mock.all<Visit>();
const shortUrl = fromPartial<ShortUrl>({});
const visit = fromPartial<Visit>({});
it('just returns the action with proper type', () => {
const { payload } = createNewVisits([{ shortUrl, visit }]);

View file

@ -1,10 +1,9 @@
import { Mock } from 'ts-mockery';
import { fromPartial } from '@total-typescript/shoehorn';
import type { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import type { ShlinkVisitsOverview } from '../../../src/api/types';
import type { ShlinkState } from '../../../src/container/types';
import { createNewVisits } from '../../../src/visits/reducers/visitCreation';
import type {
ParsedVisitsOverview,
PartialVisitsSummary,
VisitsOverview,
} from '../../../src/visits/reducers/visitsOverview';
@ -12,18 +11,18 @@ import {
loadVisitsOverview as loadVisitsOverviewCreator,
visitsOverviewReducerCreator,
} from '../../../src/visits/reducers/visitsOverview';
import type { CreateVisit, OrphanVisit, Visit } from '../../../src/visits/types';
import type { OrphanVisit } from '../../../src/visits/types';
describe('visitsOverviewReducer', () => {
const getVisitsOverview = jest.fn();
const buildApiClientMock = () => Mock.of<ShlinkApiClient>({ getVisitsOverview });
const buildApiClientMock = () => fromPartial<ShlinkApiClient>({ getVisitsOverview });
const loadVisitsOverview = loadVisitsOverviewCreator(buildApiClientMock);
const { reducer } = visitsOverviewReducerCreator(loadVisitsOverview);
beforeEach(jest.clearAllMocks);
describe('reducer', () => {
const state = (payload: Partial<VisitsOverview> = {}) => Mock.of<VisitsOverview>(payload);
const state = (payload: Partial<VisitsOverview> = {}) => fromPartial<VisitsOverview>(payload);
it('returns loading on GET_OVERVIEW_START', () => {
const { loading } = reducer(
@ -45,7 +44,7 @@ describe('visitsOverviewReducer', () => {
});
it('return visits overview on GET_OVERVIEW', () => {
const action = loadVisitsOverview.fulfilled(Mock.of<ParsedVisitsOverview>({
const action = loadVisitsOverview.fulfilled(fromPartial({
nonOrphanVisits: { total: 100 },
}), 'requestId');
const { loading, error, nonOrphanVisits } = reducer(state({ loading: true, error: false }), action);
@ -65,17 +64,11 @@ describe('visitsOverviewReducer', () => {
orphanVisits: { total: providedOrphanVisitsCount },
}),
createNewVisits([
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
fromPartial({ visit: {} }),
fromPartial({ visit: {} }),
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
]),
);
@ -120,18 +113,12 @@ describe('visitsOverviewReducer', () => {
orphanVisits: { total: 200, ...initialOrphanVisits },
}),
createNewVisits([
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({ visit: Mock.of<Visit>({ potentialBot: true }) }),
Mock.of<CreateVisit>({ visit: Mock.of<Visit>({ potentialBot: true }) }),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '', potentialBot: true }),
}),
fromPartial({ visit: {} }),
fromPartial({ visit: { potentialBot: true } }),
fromPartial({ visit: { potentialBot: true } }),
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '', potentialBot: true }) }),
]),
);
@ -142,7 +129,7 @@ describe('visitsOverviewReducer', () => {
describe('loadVisitsOverview', () => {
const dispatchMock = jest.fn();
const getState = () => Mock.of<ShlinkState>();
const getState = () => fromPartial<ShlinkState>({});
it.each([
[
@ -167,7 +154,7 @@ describe('visitsOverviewReducer', () => {
},
],
])('dispatches start and success when promise is resolved', async (serverResult, dispatchedPayload) => {
const resolvedOverview = Mock.of<ShlinkVisitsOverview>(serverResult);
const resolvedOverview = fromPartial<ShlinkVisitsOverview>(serverResult);
getVisitsOverview.mockResolvedValue(resolvedOverview);
await loadVisitsOverview()(dispatchMock, getState, {});

View file

@ -1,10 +1,10 @@
import { Mock } from 'ts-mockery';
import { fromPartial } from '@total-typescript/shoehorn';
import { normalizeVisits, processStatsFromVisits } from '../../../src/visits/services/VisitsParser';
import type { OrphanVisit, Visit, VisitsStats } from '../../../src/visits/types';
describe('VisitsParser', () => {
const visits: Visit[] = [
Mock.of<Visit>({
fromPartial({
userAgent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0',
referer: 'https://google.com',
potentialBot: false,
@ -15,7 +15,7 @@ describe('VisitsParser', () => {
longitude: -543.21,
},
}),
Mock.of<Visit>({
fromPartial({
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0',
referer: 'https://google.com',
potentialBot: false,
@ -26,7 +26,7 @@ describe('VisitsParser', () => {
longitude: 6758,
},
}),
Mock.of<Visit>({
fromPartial({
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
potentialBot: false,
visitLocation: {
@ -34,7 +34,7 @@ describe('VisitsParser', () => {
cityName: '',
},
}),
Mock.of<Visit>({
fromPartial({
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
referer: 'https://m.facebook.com',
potentialBot: false,
@ -45,13 +45,13 @@ describe('VisitsParser', () => {
longitude: -543.21,
},
}),
Mock.of<Visit>({
fromPartial({
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41',
potentialBot: true,
}),
];
const orphanVisits: OrphanVisit[] = [
Mock.of<OrphanVisit>({
fromPartial({
type: 'base_url',
visitedUrl: 'foo',
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0',
@ -64,12 +64,12 @@ describe('VisitsParser', () => {
longitude: 6758,
},
}),
Mock.of<OrphanVisit>({
fromPartial({
type: 'regular_404',
visitedUrl: 'bar',
potentialBot: true,
}),
Mock.of<OrphanVisit>({
fromPartial({
type: 'invalid_short_url',
visitedUrl: 'bar',
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',

View file

@ -1,7 +1,7 @@
import { Mock } from 'ts-mockery';
import { fromPartial } from '@total-typescript/shoehorn';
import type { ShlinkVisitsParams } from '../../../src/api/types';
import { formatIsoDate, parseDate } from '../../../src/utils/helpers/date';
import type { CreateVisit, OrphanVisit, Visit, VisitsParams } from '../../../src/visits/types';
import type { CreateVisit, OrphanVisit, VisitsParams } from '../../../src/visits/types';
import type { GroupedNewVisits } from '../../../src/visits/types/helpers';
import { groupNewVisitsByType, toApiParams } from '../../../src/visits/types/helpers';
@ -11,19 +11,15 @@ describe('visitsTypeHelpers', () => {
[[], { orphanVisits: [], nonOrphanVisits: [] }],
((): [CreateVisit[], GroupedNewVisits] => {
const orphanVisits: CreateVisit[] = [
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
];
const nonOrphanVisits: CreateVisit[] = [
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
fromPartial({ visit: {} }),
fromPartial({ visit: {} }),
fromPartial({ visit: {} }),
fromPartial({ visit: {} }),
fromPartial({ visit: {} }),
];
return [
@ -33,24 +29,18 @@ describe('visitsTypeHelpers', () => {
})(),
((): [CreateVisit[], GroupedNewVisits] => {
const orphanVisits: CreateVisit[] = [
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
Mock.of<CreateVisit>({
visit: Mock.of<OrphanVisit>({ visitedUrl: '' }),
}),
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
fromPartial({ visit: fromPartial<OrphanVisit>({ visitedUrl: '' }) }),
];
return [orphanVisits, { orphanVisits, nonOrphanVisits: [] }];
})(),
((): [CreateVisit[], GroupedNewVisits] => {
const nonOrphanVisits: CreateVisit[] = [
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
Mock.of<CreateVisit>({ visit: Mock.all<Visit>() }),
fromPartial({ visit: {} }),
fromPartial({ visit: {} }),
fromPartial({ visit: {} }),
];
return [nonOrphanVisits, { orphanVisits: [], nonOrphanVisits }];