Move shlink-web-component tests to their own folder

This commit is contained in:
Alejandro Celaya
2023-08-02 09:01:44 +02:00
parent c48facc863
commit c794ff8b58
124 changed files with 455 additions and 371 deletions

View File

@@ -0,0 +1,65 @@
import { fromPartial } from '@total-typescript/shoehorn';
import type { ShlinkApiClient } from '../../../../src/api/services/ShlinkApiClient';
import type { ShlinkState } from '../../../../src/container/types';
import type { ShortUrl } from '../../../src/short-urls/data';
import {
createShortUrl as createShortUrlCreator,
shortUrlCreationReducerCreator,
} from '../../../src/short-urls/reducers/shortUrlCreation';
describe('shortUrlCreationReducer', () => {
const shortUrl = fromPartial<ShortUrl>({});
const createShortUrlCall = vi.fn();
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ createShortUrl: createShortUrlCall });
const createShortUrl = createShortUrlCreator(buildShlinkApiClient);
const { reducer, resetCreateShortUrl } = shortUrlCreationReducerCreator(createShortUrl);
describe('reducer', () => {
it('returns loading on CREATE_SHORT_URL_START', () => {
expect(reducer(undefined, createShortUrl.pending('', fromPartial({})))).toEqual({
saving: true,
saved: false,
error: false,
});
});
it('returns error on CREATE_SHORT_URL_ERROR', () => {
expect(reducer(undefined, createShortUrl.rejected(null, '', fromPartial({})))).toEqual({
saving: false,
saved: false,
error: true,
});
});
it('returns result on CREATE_SHORT_URL', () => {
expect(reducer(undefined, createShortUrl.fulfilled(shortUrl, '', fromPartial({})))).toEqual({
result: shortUrl,
saving: false,
saved: true,
error: false,
});
});
it('returns default state on RESET_CREATE_SHORT_URL', () => {
expect(reducer(undefined, resetCreateShortUrl())).toEqual({
saving: false,
saved: false,
error: false,
});
});
});
describe('createShortUrl', () => {
const dispatch = vi.fn();
const getState = () => fromPartial<ShlinkState>({});
it('calls API on success', async () => {
createShortUrlCall.mockResolvedValue(shortUrl);
await createShortUrl({ longUrl: 'foo' })(dispatch, getState, {});
expect(createShortUrlCall).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: shortUrl }));
});
});
});

View File

@@ -0,0 +1,76 @@
import { fromPartial } from '@total-typescript/shoehorn';
import type { ShlinkApiClient } from '../../../../src/api/services/ShlinkApiClient';
import type { ProblemDetailsError } from '../../../src/api/types/errors';
import {
deleteShortUrl as deleteShortUrlCreator,
shortUrlDeletionReducerCreator,
} from '../../../src/short-urls/reducers/shortUrlDeletion';
describe('shortUrlDeletionReducer', () => {
const deleteShortUrlCall = vi.fn();
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ deleteShortUrl: deleteShortUrlCall });
const deleteShortUrl = deleteShortUrlCreator(buildShlinkApiClient);
const { reducer, resetDeleteShortUrl } = shortUrlDeletionReducerCreator(deleteShortUrl);
describe('reducer', () => {
it('returns loading on DELETE_SHORT_URL_START', () =>
expect(reducer(undefined, deleteShortUrl.pending('', { shortCode: '' }))).toEqual({
shortCode: '',
loading: true,
error: false,
deleted: false,
}));
it('returns default on RESET_DELETE_SHORT_URL', () =>
expect(reducer(undefined, resetDeleteShortUrl())).toEqual({
shortCode: '',
loading: false,
error: false,
deleted: false,
}));
it('returns shortCode on SHORT_URL_DELETED', () =>
expect(reducer(undefined, deleteShortUrl.fulfilled({ shortCode: 'foo' }, '', { shortCode: 'foo' }))).toEqual({
shortCode: 'foo',
loading: false,
error: false,
deleted: true,
}));
it('returns errorData on DELETE_SHORT_URL_ERROR', () => {
const errorData = fromPartial<ProblemDetailsError>(
{ type: 'bar', detail: 'detail', title: 'title', status: 400 },
);
const error = errorData as unknown as Error;
expect(reducer(undefined, deleteShortUrl.rejected(error, '', { shortCode: '' }))).toEqual({
shortCode: '',
loading: false,
error: true,
deleted: false,
errorData,
});
});
});
describe('deleteShortUrl', () => {
const dispatch = vi.fn();
const getState = vi.fn().mockReturnValue({ selectedServer: {} });
it.each(
[[undefined], [null], ['example.com']],
)('dispatches proper actions if API client request succeeds', async (domain) => {
const shortCode = 'abc123';
await deleteShortUrl({ shortCode, domain })(dispatch, getState, {});
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
payload: { shortCode, domain },
}));
expect(deleteShortUrlCall).toHaveBeenCalledTimes(1);
expect(deleteShortUrlCall).toHaveBeenCalledWith(shortCode, domain);
});
});
});

View File

@@ -0,0 +1,90 @@
import { fromPartial } from '@total-typescript/shoehorn';
import type { ShlinkApiClient } from '../../../../src/api/services/ShlinkApiClient';
import type { ShlinkState } from '../../../../src/container/types';
import type { ShortUrl } from '../../../src/short-urls/data';
import { shortUrlDetailReducerCreator } from '../../../src/short-urls/reducers/shortUrlDetail';
import type { ShortUrlsList } from '../../../src/short-urls/reducers/shortUrlsList';
describe('shortUrlDetailReducer', () => {
const getShortUrlCall = vi.fn();
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ getShortUrl: getShortUrlCall });
const { reducer, getShortUrlDetail } = shortUrlDetailReducerCreator(buildShlinkApiClient);
describe('reducer', () => {
it('returns loading on GET_SHORT_URL_DETAIL_START', () => {
const { loading } = reducer({ loading: false, error: false }, getShortUrlDetail.pending('', { shortCode: '' }));
expect(loading).toEqual(true);
});
it('stops loading and returns error on GET_SHORT_URL_DETAIL_ERROR', () => {
const state = reducer({ loading: true, error: false }, getShortUrlDetail.rejected(null, '', { shortCode: '' }));
const { loading, error } = state;
expect(loading).toEqual(false);
expect(error).toEqual(true);
});
it('return short URL on GET_SHORT_URL_DETAIL', () => {
const actionShortUrl = fromPartial<ShortUrl>({ longUrl: 'foo', shortCode: 'bar' });
const state = reducer(
{ loading: true, error: false },
getShortUrlDetail.fulfilled(actionShortUrl, '', { shortCode: '' }),
);
const { loading, error, shortUrl } = state;
expect(loading).toEqual(false);
expect(error).toEqual(false);
expect(shortUrl).toEqual(actionShortUrl);
});
});
describe('getShortUrlDetail', () => {
const dispatchMock = vi.fn();
const buildGetState = (shortUrlsList?: ShortUrlsList) => () => fromPartial<ShlinkState>({ shortUrlsList });
it.each([
[undefined],
[fromPartial<ShortUrlsList>({})],
[
fromPartial<ShortUrlsList>({
shortUrls: { data: [] },
}),
],
[
fromPartial<ShortUrlsList>({
shortUrls: {
data: [{ shortCode: 'this_will_not_match' }],
},
}),
],
])('performs API call when short URL is not found in local state', async (shortUrlsList?: ShortUrlsList) => {
const resolvedShortUrl = fromPartial<ShortUrl>({ longUrl: 'foo', shortCode: 'abc123' });
getShortUrlCall.mockResolvedValue(resolvedShortUrl);
await getShortUrlDetail({ shortCode: 'abc123', domain: '' })(dispatchMock, buildGetState(shortUrlsList), {});
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenLastCalledWith(expect.objectContaining({ payload: resolvedShortUrl }));
expect(getShortUrlCall).toHaveBeenCalledTimes(1);
});
it('avoids API calls when short URL is found in local state', async () => {
const foundShortUrl = fromPartial<ShortUrl>({ longUrl: 'foo', shortCode: 'abc123' });
getShortUrlCall.mockResolvedValue(fromPartial<ShortUrl>({}));
await getShortUrlDetail(foundShortUrl)(
dispatchMock,
buildGetState(fromPartial({
shortUrls: {
data: [foundShortUrl],
},
})),
{},
);
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenLastCalledWith(expect.objectContaining({ payload: foundShortUrl }));
expect(getShortUrlCall).not.toHaveBeenCalled();
});
});
});

View File

@@ -0,0 +1,62 @@
import { fromPartial } from '@total-typescript/shoehorn';
import type { ShlinkState } from '../../../../src/container/types';
import type { SelectedServer } from '../../../../src/servers/data';
import type { ShortUrl } from '../../../src/short-urls/data';
import {
editShortUrl as editShortUrlCreator,
shortUrlEditionReducerCreator,
} from '../../../src/short-urls/reducers/shortUrlEdition';
describe('shortUrlEditionReducer', () => {
const longUrl = 'https://shlink.io';
const shortCode = 'abc123';
const shortUrl = fromPartial<ShortUrl>({ longUrl, shortCode });
const updateShortUrl = vi.fn().mockResolvedValue(shortUrl);
const buildShlinkApiClient = vi.fn().mockReturnValue({ updateShortUrl });
const editShortUrl = editShortUrlCreator(buildShlinkApiClient);
const { reducer } = shortUrlEditionReducerCreator(editShortUrl);
describe('reducer', () => {
it('returns loading on EDIT_SHORT_URL_START', () => {
expect(reducer(undefined, editShortUrl.pending('', fromPartial({})))).toEqual({
saving: true,
saved: false,
error: false,
});
});
it('returns error on EDIT_SHORT_URL_ERROR', () => {
expect(reducer(undefined, editShortUrl.rejected(null, '', fromPartial({})))).toEqual({
saving: false,
saved: false,
error: true,
});
});
it('returns provided tags and shortCode on SHORT_URL_EDITED', () => {
expect(reducer(undefined, editShortUrl.fulfilled(shortUrl, '', fromPartial({})))).toEqual({
shortUrl,
saving: false,
saved: true,
error: false,
});
});
});
describe('editShortUrl', () => {
const dispatch = vi.fn();
const createGetState = (selectedServer: SelectedServer = null) => () => fromPartial<ShlinkState>({
selectedServer,
});
it.each([[undefined], [null], ['example.com']])('dispatches short URL on success', async (domain) => {
await editShortUrl({ shortCode, domain, data: { longUrl } })(dispatch, createGetState(), {});
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
expect(updateShortUrl).toHaveBeenCalledTimes(1);
expect(updateShortUrl).toHaveBeenCalledWith(shortCode, domain, { longUrl });
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: shortUrl }));
});
});
});

View File

@@ -0,0 +1,203 @@
import { fromPartial } from '@total-typescript/shoehorn';
import type { ShlinkApiClient } from '../../../../src/api/services/ShlinkApiClient';
import type { ShlinkShortUrlsResponse } from '../../../src/api/types';
import type { ShortUrl } from '../../../src/short-urls/data';
import { createShortUrl as createShortUrlCreator } from '../../../src/short-urls/reducers/shortUrlCreation';
import { shortUrlDeleted } from '../../../src/short-urls/reducers/shortUrlDeletion';
import { editShortUrl as editShortUrlCreator } from '../../../src/short-urls/reducers/shortUrlEdition';
import {
listShortUrls as listShortUrlsCreator,
shortUrlsListReducerCreator,
} from '../../../src/short-urls/reducers/shortUrlsList';
import { createNewVisits } from '../../../src/visits/reducers/visitCreation';
import type { CreateVisit } from '../../../src/visits/types';
describe('shortUrlsListReducer', () => {
const shortCode = 'abc123';
const listShortUrlsMock = vi.fn();
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ listShortUrls: listShortUrlsMock });
const listShortUrls = listShortUrlsCreator(buildShlinkApiClient);
const editShortUrl = editShortUrlCreator(buildShlinkApiClient);
const createShortUrl = createShortUrlCreator(buildShlinkApiClient);
const { reducer } = shortUrlsListReducerCreator(listShortUrls, editShortUrl, createShortUrl);
describe('reducer', () => {
it('returns loading on LIST_SHORT_URLS_START', () =>
expect(reducer(undefined, listShortUrls.pending(''))).toEqual({
loading: true,
error: false,
}));
it('returns short URLs on LIST_SHORT_URLS', () =>
expect(reducer(undefined, listShortUrls.fulfilled(fromPartial({ data: [] }), ''))).toEqual({
shortUrls: { data: [] },
loading: false,
error: false,
}));
it('returns error on LIST_SHORT_URLS_ERROR', () =>
expect(reducer(undefined, listShortUrls.rejected(null, ''))).toEqual({
loading: false,
error: true,
}));
it('removes matching URL and reduces total on SHORT_URL_DELETED', () => {
const state = {
shortUrls: fromPartial<ShlinkShortUrlsResponse>({
data: [
{ shortCode },
{ shortCode, domain: 'example.com' },
{ shortCode: 'foo' },
],
pagination: { totalItems: 10 },
}),
loading: false,
error: false,
};
expect(reducer(state, shortUrlDeleted(fromPartial({ shortCode })))).toEqual({
shortUrls: {
data: [{ shortCode, domain: 'example.com' }, { shortCode: 'foo' }],
pagination: { totalItems: 9 },
},
loading: false,
error: false,
});
});
const createNewShortUrlVisit = (visitsCount: number) => fromPartial<CreateVisit>({
shortUrl: { shortCode: 'abc123', visitsCount },
});
it.each([
[[createNewShortUrlVisit(11)], 11],
[[createNewShortUrlVisit(30)], 30],
[[createNewShortUrlVisit(20), createNewShortUrlVisit(40)], 40],
[[], 10],
])('updates visits count on CREATE_VISITS', (createdVisits, expectedCount) => {
const state = {
shortUrls: fromPartial<ShlinkShortUrlsResponse>({
data: [
{ shortCode, domain: 'example.com', visitsCount: 5 },
{ shortCode, visitsCount: 10 },
{ shortCode: 'foo', visitsCount: 8 },
],
}),
loading: false,
error: false,
};
expect(reducer(state, createNewVisits(createdVisits))).toEqual({
shortUrls: {
data: [
{ shortCode, domain: 'example.com', visitsCount: 5 },
{ shortCode, visitsCount: expectedCount },
{ shortCode: 'foo', visitsCount: 8 },
],
},
loading: false,
error: false,
});
});
it.each([
[
[
fromPartial<ShortUrl>({ shortCode }),
fromPartial<ShortUrl>({ shortCode, domain: 'example.com' }),
fromPartial<ShortUrl>({ shortCode: 'foo' }),
],
[{ shortCode: 'newOne' }, { shortCode }, { shortCode, domain: 'example.com' }, { shortCode: 'foo' }],
],
[
[
fromPartial<ShortUrl>({ shortCode }),
fromPartial<ShortUrl>({ shortCode: 'code' }),
fromPartial<ShortUrl>({ shortCode: 'foo' }),
fromPartial<ShortUrl>({ shortCode: 'bar' }),
fromPartial<ShortUrl>({ shortCode: 'baz' }),
],
[{ shortCode: 'newOne' }, { shortCode }, { shortCode: 'code' }, { shortCode: 'foo' }, { shortCode: 'bar' }],
],
[
[
fromPartial<ShortUrl>({ shortCode }),
fromPartial<ShortUrl>({ shortCode: 'code' }),
fromPartial<ShortUrl>({ shortCode: 'foo' }),
fromPartial<ShortUrl>({ shortCode: 'bar' }),
fromPartial<ShortUrl>({ shortCode: 'baz1' }),
fromPartial<ShortUrl>({ shortCode: 'baz2' }),
fromPartial<ShortUrl>({ shortCode: 'baz3' }),
],
[{ shortCode: 'newOne' }, { shortCode }, { shortCode: 'code' }, { shortCode: 'foo' }, { shortCode: 'bar' }],
],
])('prepends new short URL and increases total on CREATE_SHORT_URL', (data, expectedData) => {
const newShortUrl = fromPartial<ShortUrl>({ shortCode: 'newOne' });
const state = {
shortUrls: fromPartial<ShlinkShortUrlsResponse>({
data,
pagination: { totalItems: 15 },
}),
loading: false,
error: false,
};
expect(reducer(state, createShortUrl.fulfilled(newShortUrl, '', fromPartial({})))).toEqual({
shortUrls: {
data: expectedData,
pagination: { totalItems: 16 },
},
loading: false,
error: false,
});
});
it.each([
((): [ShortUrl, ShortUrl[], ShortUrl[]] => {
const editedShortUrl = fromPartial<ShortUrl>({ shortCode: 'notMatching' });
const list: ShortUrl[] = [fromPartial({ shortCode: 'foo' }), fromPartial({ shortCode: 'bar' })];
return [editedShortUrl, list, list];
})(),
((): [ShortUrl, ShortUrl[], ShortUrl[]] => {
const editedShortUrl = fromPartial<ShortUrl>({ shortCode: 'matching', longUrl: 'new_one' });
const list: ShortUrl[] = [
fromPartial({ shortCode: 'matching', longUrl: 'old_one' }),
fromPartial({ shortCode: 'bar' }),
];
const expectedList = [editedShortUrl, list[1]];
return [editedShortUrl, list, expectedList];
})(),
])('updates matching short URL on SHORT_URL_EDITED', (editedShortUrl, initialList, expectedList) => {
const state = {
shortUrls: fromPartial<ShlinkShortUrlsResponse>({
data: initialList,
pagination: { totalItems: 15 },
}),
loading: false,
error: false,
};
const result = reducer(state, editShortUrl.fulfilled(editedShortUrl, '', fromPartial({})));
expect(result.shortUrls?.data).toEqual(expectedList);
});
});
describe('listShortUrls', () => {
const dispatch = vi.fn();
const getState = vi.fn().mockReturnValue({ selectedServer: {} });
it('dispatches proper actions if API client request succeeds', async () => {
listShortUrlsMock.mockResolvedValue({});
await listShortUrls()(dispatch, getState, {});
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: {} }));
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
});
});
});