mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-01 13:16:42 +00:00
Finished migrating remaining reducers to TS
This commit is contained in:
@@ -1,21 +1,28 @@
|
||||
import { Mock } from 'ts-mockery';
|
||||
import reducer, {
|
||||
getShortUrlDetail,
|
||||
GET_SHORT_URL_DETAIL_START,
|
||||
GET_SHORT_URL_DETAIL_ERROR,
|
||||
GET_SHORT_URL_DETAIL,
|
||||
ShortUrlDetailAction,
|
||||
} from '../../../src/visits/reducers/shortUrlDetail';
|
||||
import { ShortUrl } from '../../../src/short-urls/data';
|
||||
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
|
||||
import { ShlinkState } from '../../../src/container/types';
|
||||
|
||||
describe('shortUrlDetailReducer', () => {
|
||||
describe('reducer', () => {
|
||||
const action = (type: string) => Mock.of<ShortUrlDetailAction>({ type });
|
||||
|
||||
it('returns loading on GET_SHORT_URL_DETAIL_START', () => {
|
||||
const state = reducer({ loading: false }, { type: GET_SHORT_URL_DETAIL_START });
|
||||
const state = reducer({ loading: false, error: false }, action(GET_SHORT_URL_DETAIL_START));
|
||||
const { loading } = state;
|
||||
|
||||
expect(loading).toEqual(true);
|
||||
});
|
||||
|
||||
it('stops loading and returns error on GET_SHORT_URL_DETAIL_ERROR', () => {
|
||||
const state = reducer({ loading: true, error: false }, { type: GET_SHORT_URL_DETAIL_ERROR });
|
||||
const state = reducer({ loading: true, error: false }, action(GET_SHORT_URL_DETAIL_ERROR));
|
||||
const { loading, error } = state;
|
||||
|
||||
expect(loading).toEqual(false);
|
||||
@@ -23,7 +30,7 @@ describe('shortUrlDetailReducer', () => {
|
||||
});
|
||||
|
||||
it('return short URL on GET_SHORT_URL_DETAIL', () => {
|
||||
const actionShortUrl = { longUrl: 'foo', shortCode: 'bar' };
|
||||
const actionShortUrl = Mock.of<ShortUrl>({ longUrl: 'foo', shortCode: 'bar' });
|
||||
const state = reducer({ loading: true, error: false }, { type: GET_SHORT_URL_DETAIL, shortUrl: actionShortUrl });
|
||||
const { loading, error, shortUrl } = state;
|
||||
|
||||
@@ -34,18 +41,18 @@ describe('shortUrlDetailReducer', () => {
|
||||
});
|
||||
|
||||
describe('getShortUrlDetail', () => {
|
||||
const buildApiClientMock = (returned) => ({
|
||||
getShortUrl: jest.fn(() => returned),
|
||||
const buildApiClientMock = (returned: Promise<ShortUrl>) => Mock.of<ShlinkApiClient>({
|
||||
getShortUrl: jest.fn(async () => returned),
|
||||
});
|
||||
const dispatchMock = jest.fn();
|
||||
const getState = () => ({});
|
||||
const getState = () => Mock.of<ShlinkState>();
|
||||
|
||||
beforeEach(() => dispatchMock.mockReset());
|
||||
|
||||
it('dispatches start and error when promise is rejected', async () => {
|
||||
const ShlinkApiClient = buildApiClientMock(Promise.reject());
|
||||
|
||||
await getShortUrlDetail(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
|
||||
await getShortUrlDetail(() => ShlinkApiClient)('abc123', '')(dispatchMock, getState);
|
||||
|
||||
expect(dispatchMock).toHaveBeenCalledTimes(2);
|
||||
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
|
||||
@@ -54,10 +61,10 @@ describe('shortUrlDetailReducer', () => {
|
||||
});
|
||||
|
||||
it('dispatches start and success when promise is resolved', async () => {
|
||||
const resolvedShortUrl = { longUrl: 'foo', shortCode: 'bar' };
|
||||
const resolvedShortUrl = Mock.of<ShortUrl>({ longUrl: 'foo', shortCode: 'bar' });
|
||||
const ShlinkApiClient = buildApiClientMock(Promise.resolve(resolvedShortUrl));
|
||||
|
||||
await getShortUrlDetail(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
|
||||
await getShortUrlDetail(() => ShlinkApiClient)('abc123', '')(dispatchMock, getState);
|
||||
|
||||
expect(dispatchMock).toHaveBeenCalledTimes(2);
|
||||
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Mock } from 'ts-mockery';
|
||||
import reducer, {
|
||||
getShortUrlVisits,
|
||||
cancelGetShortUrlVisits,
|
||||
@@ -7,34 +8,44 @@ import reducer, {
|
||||
GET_SHORT_URL_VISITS_LARGE,
|
||||
GET_SHORT_URL_VISITS_CANCEL,
|
||||
GET_SHORT_URL_VISITS_PROGRESS_CHANGED,
|
||||
ShortUrlVisits,
|
||||
} from '../../../src/visits/reducers/shortUrlVisits';
|
||||
import { CREATE_VISIT } from '../../../src/visits/reducers/visitCreation';
|
||||
import { rangeOf } from '../../../src/utils/utils';
|
||||
import { Visit } from '../../../src/visits/types';
|
||||
import { ShlinkVisits } from '../../../src/utils/services/types';
|
||||
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
|
||||
import { ShlinkState } from '../../../src/container/types';
|
||||
|
||||
describe('shortUrlVisitsReducer', () => {
|
||||
const visitsMocks = rangeOf(2, () => Mock.all<Visit>());
|
||||
|
||||
describe('reducer', () => {
|
||||
const buildState = (data: Partial<ShortUrlVisits>) => Mock.of<ShortUrlVisits>(data);
|
||||
|
||||
it('returns loading on GET_SHORT_URL_VISITS_START', () => {
|
||||
const state = reducer({ loading: false }, { type: GET_SHORT_URL_VISITS_START });
|
||||
const state = reducer(buildState({ loading: false }), { type: GET_SHORT_URL_VISITS_START } as any);
|
||||
const { loading } = state;
|
||||
|
||||
expect(loading).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns loadingLarge on GET_SHORT_URL_VISITS_LARGE', () => {
|
||||
const state = reducer({ loadingLarge: false }, { type: GET_SHORT_URL_VISITS_LARGE });
|
||||
const state = reducer(buildState({ loadingLarge: false }), { type: GET_SHORT_URL_VISITS_LARGE } as any);
|
||||
const { loadingLarge } = state;
|
||||
|
||||
expect(loadingLarge).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns cancelLoad on GET_SHORT_URL_VISITS_CANCEL', () => {
|
||||
const state = reducer({ cancelLoad: false }, { type: GET_SHORT_URL_VISITS_CANCEL });
|
||||
const state = reducer(buildState({ cancelLoad: false }), { type: GET_SHORT_URL_VISITS_CANCEL } as any);
|
||||
const { cancelLoad } = state;
|
||||
|
||||
expect(cancelLoad).toEqual(true);
|
||||
});
|
||||
|
||||
it('stops loading and returns error on GET_SHORT_URL_VISITS_ERROR', () => {
|
||||
const state = reducer({ loading: true, error: false }, { type: GET_SHORT_URL_VISITS_ERROR });
|
||||
const state = reducer(buildState({ loading: true, error: false }), { type: GET_SHORT_URL_VISITS_ERROR } as any);
|
||||
const { loading, error } = state;
|
||||
|
||||
expect(loading).toEqual(false);
|
||||
@@ -43,7 +54,10 @@ describe('shortUrlVisitsReducer', () => {
|
||||
|
||||
it('return visits on GET_SHORT_URL_VISITS', () => {
|
||||
const actionVisits = [{}, {}];
|
||||
const state = reducer({ loading: true, error: false }, { type: GET_SHORT_URL_VISITS, visits: actionVisits });
|
||||
const state = reducer(
|
||||
buildState({ loading: true, error: false }),
|
||||
{ type: GET_SHORT_URL_VISITS, visits: actionVisits } as any,
|
||||
);
|
||||
const { loading, error, visits } = state;
|
||||
|
||||
expect(loading).toEqual(false);
|
||||
@@ -52,42 +66,44 @@ describe('shortUrlVisitsReducer', () => {
|
||||
});
|
||||
|
||||
it.each([
|
||||
[{ shortCode: 'abc123' }, [{}, {}, {}]],
|
||||
[{ shortCode: 'def456' }, [{}, {}]],
|
||||
[{ shortCode: 'abc123' }, [ ...visitsMocks, {}]],
|
||||
[{ shortCode: 'def456' }, visitsMocks ],
|
||||
])('appends a new visit on CREATE_VISIT', (state, expectedVisits) => {
|
||||
const shortUrl = {
|
||||
shortCode: 'abc123',
|
||||
};
|
||||
const prevState = {
|
||||
const prevState = buildState({
|
||||
...state,
|
||||
visits: [{}, {}],
|
||||
};
|
||||
visits: visitsMocks,
|
||||
});
|
||||
|
||||
const { visits } = reducer(prevState, { type: CREATE_VISIT, shortUrl, visit: {} });
|
||||
const { visits } = reducer(prevState, { type: CREATE_VISIT, shortUrl, visit: {} } as any);
|
||||
|
||||
expect(visits).toEqual(expectedVisits);
|
||||
});
|
||||
|
||||
it('returns new progress on GET_SHORT_URL_VISITS_PROGRESS_CHANGED', () => {
|
||||
const state = reducer({}, { type: GET_SHORT_URL_VISITS_PROGRESS_CHANGED, progress: 85 });
|
||||
const state = reducer(undefined, { type: GET_SHORT_URL_VISITS_PROGRESS_CHANGED, progress: 85 } as any);
|
||||
|
||||
expect(state).toEqual({ progress: 85 });
|
||||
expect(state).toEqual(expect.objectContaining({ progress: 85 }));
|
||||
});
|
||||
});
|
||||
|
||||
describe('getShortUrlVisits', () => {
|
||||
const buildApiClientMock = (returned) => ({
|
||||
getShortUrlVisits: jest.fn(typeof returned === 'function' ? returned : () => returned),
|
||||
type GetVisitsReturn = Promise<ShlinkVisits> | ((shortCode: string, query: any) => Promise<ShlinkVisits>);
|
||||
|
||||
const buildApiClientMock = (returned: GetVisitsReturn) => Mock.of<ShlinkApiClient>({
|
||||
getShortUrlVisits: jest.fn(typeof returned === 'function' ? returned : async () => returned),
|
||||
});
|
||||
const dispatchMock = jest.fn();
|
||||
const getState = () => ({
|
||||
shortUrlVisits: { cancelVisits: false },
|
||||
const getState = () => Mock.of<ShlinkState>({
|
||||
shortUrlVisits: Mock.of<ShortUrlVisits>({ cancelLoad: false }),
|
||||
});
|
||||
|
||||
beforeEach(() => dispatchMock.mockReset());
|
||||
|
||||
it('dispatches start and error when promise is rejected', async () => {
|
||||
const ShlinkApiClient = buildApiClientMock(Promise.reject());
|
||||
const ShlinkApiClient = buildApiClientMock(Promise.reject() as any);
|
||||
|
||||
await getShortUrlVisits(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
|
||||
|
||||
@@ -102,10 +118,10 @@ describe('shortUrlVisitsReducer', () => {
|
||||
[{}, undefined ],
|
||||
[{ domain: 'foobar.com' }, 'foobar.com' ],
|
||||
])('dispatches start and success when promise is resolved', async (query, domain) => {
|
||||
const visits = [{}, {}];
|
||||
const visits = visitsMocks;
|
||||
const shortCode = 'abc123';
|
||||
const ShlinkApiClient = buildApiClientMock(Promise.resolve({
|
||||
data: visits,
|
||||
data: visitsMocks,
|
||||
pagination: {
|
||||
currentPage: 1,
|
||||
pagesCount: 1,
|
||||
@@ -122,9 +138,9 @@ describe('shortUrlVisitsReducer', () => {
|
||||
|
||||
it('performs multiple API requests when response contains more pages', async () => {
|
||||
const expectedRequests = 3;
|
||||
const ShlinkApiClient = buildApiClientMock((shortCode, { page }) =>
|
||||
const ShlinkApiClient = buildApiClientMock(async (_, { page }) =>
|
||||
Promise.resolve({
|
||||
data: [{}, {}],
|
||||
data: visitsMocks,
|
||||
pagination: {
|
||||
currentPage: page,
|
||||
pagesCount: expectedRequests,
|
||||
@@ -135,7 +151,7 @@ describe('shortUrlVisitsReducer', () => {
|
||||
|
||||
expect(ShlinkApiClient.getShortUrlVisits).toHaveBeenCalledTimes(expectedRequests);
|
||||
expect(dispatchMock).toHaveBeenNthCalledWith(3, expect.objectContaining({
|
||||
visits: [{}, {}, {}, {}, {}, {}],
|
||||
visits: [ ...visitsMocks, ...visitsMocks, ...visitsMocks ],
|
||||
}));
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import { Mock } from 'ts-mockery';
|
||||
import reducer, {
|
||||
getTagVisits,
|
||||
cancelGetTagVisits,
|
||||
@@ -7,34 +8,44 @@ import reducer, {
|
||||
GET_TAG_VISITS_LARGE,
|
||||
GET_TAG_VISITS_CANCEL,
|
||||
GET_TAG_VISITS_PROGRESS_CHANGED,
|
||||
TagVisits,
|
||||
} from '../../../src/visits/reducers/tagVisits';
|
||||
import { CREATE_VISIT } from '../../../src/visits/reducers/visitCreation';
|
||||
import { rangeOf } from '../../../src/utils/utils';
|
||||
import { Visit } from '../../../src/visits/types';
|
||||
import { ShlinkVisits } from '../../../src/utils/services/types';
|
||||
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
|
||||
import { ShlinkState } from '../../../src/container/types';
|
||||
|
||||
describe('tagVisitsReducer', () => {
|
||||
const visitsMocks = rangeOf(2, () => Mock.all<Visit>());
|
||||
|
||||
describe('reducer', () => {
|
||||
const buildState = (data: Partial<TagVisits>) => Mock.of<TagVisits>(data);
|
||||
|
||||
it('returns loading on GET_TAG_VISITS_START', () => {
|
||||
const state = reducer({ loading: false }, { type: GET_TAG_VISITS_START });
|
||||
const state = reducer(buildState({ loading: false }), { type: GET_TAG_VISITS_START } as any);
|
||||
const { loading } = state;
|
||||
|
||||
expect(loading).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns loadingLarge on GET_TAG_VISITS_LARGE', () => {
|
||||
const state = reducer({ loadingLarge: false }, { type: GET_TAG_VISITS_LARGE });
|
||||
const state = reducer(buildState({ loadingLarge: false }), { type: GET_TAG_VISITS_LARGE } as any);
|
||||
const { loadingLarge } = state;
|
||||
|
||||
expect(loadingLarge).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns cancelLoad on GET_TAG_VISITS_CANCEL', () => {
|
||||
const state = reducer({ cancelLoad: false }, { type: GET_TAG_VISITS_CANCEL });
|
||||
const state = reducer(buildState({ cancelLoad: false }), { type: GET_TAG_VISITS_CANCEL } as any);
|
||||
const { cancelLoad } = state;
|
||||
|
||||
expect(cancelLoad).toEqual(true);
|
||||
});
|
||||
|
||||
it('stops loading and returns error on GET_TAG_VISITS_ERROR', () => {
|
||||
const state = reducer({ loading: true, error: false }, { type: GET_TAG_VISITS_ERROR });
|
||||
const state = reducer(buildState({ loading: true, error: false }), { type: GET_TAG_VISITS_ERROR } as any);
|
||||
const { loading, error } = state;
|
||||
|
||||
expect(loading).toEqual(false);
|
||||
@@ -43,7 +54,10 @@ describe('tagVisitsReducer', () => {
|
||||
|
||||
it('return visits on GET_TAG_VISITS', () => {
|
||||
const actionVisits = [{}, {}];
|
||||
const state = reducer({ loading: true, error: false }, { type: GET_TAG_VISITS, visits: actionVisits });
|
||||
const state = reducer(
|
||||
buildState({ loading: true, error: false }),
|
||||
{ type: GET_TAG_VISITS, visits: actionVisits } as any,
|
||||
);
|
||||
const { loading, error, visits } = state;
|
||||
|
||||
expect(loading).toEqual(false);
|
||||
@@ -52,36 +66,38 @@ describe('tagVisitsReducer', () => {
|
||||
});
|
||||
|
||||
it.each([
|
||||
[{ tag: 'foo' }, [{}, {}, {}]],
|
||||
[{ tag: 'bar' }, [{}, {}]],
|
||||
[{ tag: 'foo' }, [ ...visitsMocks, {}]],
|
||||
[{ tag: 'bar' }, visitsMocks ],
|
||||
])('appends a new visit on CREATE_VISIT', (state, expectedVisits) => {
|
||||
const shortUrl = {
|
||||
tags: [ 'foo', 'baz' ],
|
||||
};
|
||||
const prevState = {
|
||||
const prevState = buildState({
|
||||
...state,
|
||||
visits: [{}, {}],
|
||||
};
|
||||
visits: visitsMocks,
|
||||
});
|
||||
|
||||
const { visits } = reducer(prevState, { type: CREATE_VISIT, shortUrl, visit: {} });
|
||||
const { visits } = reducer(prevState, { type: CREATE_VISIT, shortUrl, visit: {} } as any);
|
||||
|
||||
expect(visits).toEqual(expectedVisits);
|
||||
});
|
||||
|
||||
it('returns new progress on GET_TAG_VISITS_PROGRESS_CHANGED', () => {
|
||||
const state = reducer({}, { type: GET_TAG_VISITS_PROGRESS_CHANGED, progress: 85 });
|
||||
const state = reducer(undefined, { type: GET_TAG_VISITS_PROGRESS_CHANGED, progress: 85 } as any);
|
||||
|
||||
expect(state).toEqual({ progress: 85 });
|
||||
expect(state).toEqual(expect.objectContaining({ progress: 85 }));
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTagVisits', () => {
|
||||
const buildApiClientMock = (returned) => ({
|
||||
getTagVisits: jest.fn(typeof returned === 'function' ? returned : () => returned),
|
||||
type GetVisitsReturn = Promise<ShlinkVisits> | ((shortCode: string, query: any) => Promise<ShlinkVisits>);
|
||||
|
||||
const buildApiClientMock = (returned: GetVisitsReturn) => Mock.of<ShlinkApiClient>({
|
||||
getTagVisits: jest.fn(typeof returned === 'function' ? returned : async () => returned),
|
||||
});
|
||||
const dispatchMock = jest.fn();
|
||||
const getState = () => ({
|
||||
tagVisits: { cancelVisits: false },
|
||||
const getState = () => Mock.of<ShlinkState>({
|
||||
tagVisits: { cancelLoad: false },
|
||||
});
|
||||
|
||||
beforeEach(jest.resetAllMocks);
|
||||
@@ -101,10 +117,10 @@ describe('tagVisitsReducer', () => {
|
||||
[ undefined ],
|
||||
[{}],
|
||||
])('dispatches start and success when promise is resolved', async (query) => {
|
||||
const visits = [{}, {}];
|
||||
const visits = visitsMocks;
|
||||
const tag = 'foo';
|
||||
const ShlinkApiClient = buildApiClientMock(Promise.resolve({
|
||||
data: visits,
|
||||
data: visitsMocks,
|
||||
pagination: {
|
||||
currentPage: 1,
|
||||
pagesCount: 1,
|
||||
Reference in New Issue
Block a user