2020-08-27 19:31:56 +03:00
|
|
|
import { Mock } from 'ts-mockery';
|
2018-12-21 12:58:51 +03:00
|
|
|
import reducer, {
|
|
|
|
LIST_SHORT_URLS,
|
|
|
|
LIST_SHORT_URLS_ERROR,
|
|
|
|
LIST_SHORT_URLS_START,
|
|
|
|
listShortUrls,
|
|
|
|
} from '../../../src/short-urls/reducers/shortUrlsList';
|
|
|
|
import { SHORT_URL_TAGS_EDITED } from '../../../src/short-urls/reducers/shortUrlTags';
|
|
|
|
import { SHORT_URL_DELETED } from '../../../src/short-urls/reducers/shortUrlDeletion';
|
2020-01-19 15:20:46 +03:00
|
|
|
import { SHORT_URL_META_EDITED } from '../../../src/short-urls/reducers/shortUrlMeta';
|
2020-09-12 12:31:44 +03:00
|
|
|
import { CREATE_VISITS } from '../../../src/visits/reducers/visitCreation';
|
2020-08-27 19:31:56 +03:00
|
|
|
import { ShortUrl } from '../../../src/short-urls/data';
|
|
|
|
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
|
2020-12-22 11:49:13 +03:00
|
|
|
import { ShlinkPaginator, ShlinkShortUrlsResponse } from '../../../src/api/types';
|
2020-12-08 12:57:27 +03:00
|
|
|
import { CREATE_SHORT_URL } from '../../../src/short-urls/reducers/shortUrlCreation';
|
|
|
|
import { SHORT_URL_EDITED } from '../../../src/short-urls/reducers/shortUrlEdition';
|
2018-12-21 12:58:51 +03:00
|
|
|
|
|
|
|
describe('shortUrlsListReducer', () => {
|
|
|
|
describe('reducer', () => {
|
|
|
|
it('returns loading on LIST_SHORT_URLS_START', () =>
|
2020-08-27 19:31:56 +03:00
|
|
|
expect(reducer(undefined, { type: LIST_SHORT_URLS_START } as any)).toEqual({
|
2018-12-21 12:58:51 +03:00
|
|
|
loading: true,
|
|
|
|
error: false,
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('returns short URLs on LIST_SHORT_URLS', () =>
|
2020-08-27 19:31:56 +03:00
|
|
|
expect(reducer(undefined, { type: LIST_SHORT_URLS, shortUrls: { data: [] } } as any)).toEqual({
|
|
|
|
shortUrls: { data: [] },
|
2018-12-21 12:58:51 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('returns error on LIST_SHORT_URLS_ERROR', () =>
|
2020-08-27 19:31:56 +03:00
|
|
|
expect(reducer(undefined, { type: LIST_SHORT_URLS_ERROR } as any)).toEqual({
|
2018-12-21 12:58:51 +03:00
|
|
|
loading: false,
|
|
|
|
error: true,
|
|
|
|
}));
|
|
|
|
|
2020-04-18 13:09:51 +03:00
|
|
|
it('updates tags on matching URL on SHORT_URL_TAGS_EDITED', () => {
|
2018-12-21 12:58:51 +03:00
|
|
|
const shortCode = 'abc123';
|
|
|
|
const tags = [ 'foo', 'bar', 'baz' ];
|
|
|
|
const state = {
|
2020-08-30 20:45:17 +03:00
|
|
|
shortUrls: Mock.of<ShlinkShortUrlsResponse>({
|
2018-12-21 12:58:51 +03:00
|
|
|
data: [
|
2020-08-27 19:31:56 +03:00
|
|
|
Mock.of<ShortUrl>({ shortCode, tags: [] }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode, tags: [], domain: 'example.com' }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode: 'foo', tags: [] }),
|
2018-12-21 12:58:51 +03:00
|
|
|
],
|
2020-08-30 20:45:17 +03:00
|
|
|
}),
|
2020-08-27 19:31:56 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
2018-12-21 12:58:51 +03:00
|
|
|
};
|
|
|
|
|
2020-08-27 19:31:56 +03:00
|
|
|
expect(reducer(state, { type: SHORT_URL_TAGS_EDITED, shortCode, tags, domain: null } as any)).toEqual({
|
2018-12-21 12:58:51 +03:00
|
|
|
shortUrls: {
|
|
|
|
data: [
|
|
|
|
{ shortCode, tags },
|
2020-02-08 12:46:11 +03:00
|
|
|
{ shortCode, tags: [], domain: 'example.com' },
|
2018-12-21 12:58:51 +03:00
|
|
|
{ shortCode: 'foo', tags: [] },
|
|
|
|
],
|
|
|
|
},
|
2020-08-27 19:31:56 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
2018-12-21 12:58:51 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-18 13:09:51 +03:00
|
|
|
it('updates meta on matching URL on SHORT_URL_META_EDITED', () => {
|
2020-01-19 15:20:46 +03:00
|
|
|
const shortCode = 'abc123';
|
2020-02-08 12:46:11 +03:00
|
|
|
const domain = 'example.com';
|
2020-01-19 15:20:46 +03:00
|
|
|
const meta = {
|
|
|
|
maxVisits: 5,
|
|
|
|
validSince: '2020-05-05',
|
|
|
|
};
|
|
|
|
const state = {
|
2020-08-30 20:45:17 +03:00
|
|
|
shortUrls: Mock.of<ShlinkShortUrlsResponse>({
|
2020-01-19 15:20:46 +03:00
|
|
|
data: [
|
2020-08-27 19:31:56 +03:00
|
|
|
Mock.of<ShortUrl>({ shortCode, meta: { maxVisits: 10 }, domain }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode, meta: { maxVisits: 50 } }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode: 'foo', meta: {} }),
|
2020-01-19 15:20:46 +03:00
|
|
|
],
|
2020-08-30 20:45:17 +03:00
|
|
|
}),
|
2020-08-27 19:31:56 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
2020-01-19 15:20:46 +03:00
|
|
|
};
|
|
|
|
|
2020-08-27 19:31:56 +03:00
|
|
|
expect(reducer(state, { type: SHORT_URL_META_EDITED, shortCode, meta, domain } as any)).toEqual({
|
2020-01-19 15:20:46 +03:00
|
|
|
shortUrls: {
|
|
|
|
data: [
|
2020-02-08 12:46:11 +03:00
|
|
|
{ shortCode, meta, domain: 'example.com' },
|
|
|
|
{ shortCode, meta: { maxVisits: 50 } },
|
2020-08-27 19:31:56 +03:00
|
|
|
{ shortCode: 'foo', meta: {} },
|
2020-01-19 15:20:46 +03:00
|
|
|
],
|
|
|
|
},
|
2020-08-27 19:31:56 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
2020-01-19 15:20:46 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-12-08 12:57:27 +03:00
|
|
|
it('removes matching URL and reduces total on SHORT_URL_DELETED', () => {
|
2018-12-21 12:58:51 +03:00
|
|
|
const shortCode = 'abc123';
|
|
|
|
const state = {
|
2020-08-30 20:45:17 +03:00
|
|
|
shortUrls: Mock.of<ShlinkShortUrlsResponse>({
|
2018-12-21 12:58:51 +03:00
|
|
|
data: [
|
2020-08-27 19:31:56 +03:00
|
|
|
Mock.of<ShortUrl>({ shortCode }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode, domain: 'example.com' }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode: 'foo' }),
|
2018-12-21 12:58:51 +03:00
|
|
|
],
|
2020-12-08 12:57:27 +03:00
|
|
|
pagination: Mock.of<ShlinkPaginator>({
|
|
|
|
totalItems: 10,
|
|
|
|
}),
|
2020-08-30 20:45:17 +03:00
|
|
|
}),
|
2020-08-27 19:31:56 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
2018-12-21 12:58:51 +03:00
|
|
|
};
|
|
|
|
|
2020-08-27 19:31:56 +03:00
|
|
|
expect(reducer(state, { type: SHORT_URL_DELETED, shortCode } as any)).toEqual({
|
2018-12-21 12:58:51 +03:00
|
|
|
shortUrls: {
|
2020-02-08 12:46:11 +03:00
|
|
|
data: [{ shortCode, domain: 'example.com' }, { shortCode: 'foo' }],
|
2020-12-08 12:57:27 +03:00
|
|
|
pagination: { totalItems: 9 },
|
|
|
|
},
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates edited short URL on SHORT_URL_EDITED', () => {
|
|
|
|
const shortCode = 'abc123';
|
|
|
|
const state = {
|
|
|
|
shortUrls: Mock.of<ShlinkShortUrlsResponse>({
|
|
|
|
data: [
|
|
|
|
Mock.of<ShortUrl>({ shortCode, longUrl: 'old' }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode, domain: 'example.com', longUrl: 'foo' }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode: 'foo', longUrl: 'bar' }),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(reducer(state, { type: SHORT_URL_EDITED, shortCode, longUrl: 'newValue' } as any)).toEqual({
|
|
|
|
shortUrls: {
|
|
|
|
data: [
|
|
|
|
{ shortCode, longUrl: 'newValue' },
|
|
|
|
{ shortCode, longUrl: 'foo', domain: 'example.com' },
|
|
|
|
{ shortCode: 'foo', longUrl: 'bar' },
|
|
|
|
],
|
2018-12-21 12:58:51 +03:00
|
|
|
},
|
2020-08-27 19:31:56 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
2018-12-21 12:58:51 +03:00
|
|
|
});
|
|
|
|
});
|
2020-04-18 13:09:51 +03:00
|
|
|
|
2020-05-10 20:02:58 +03:00
|
|
|
it('updates visits count on CREATE_VISIT', () => {
|
2020-04-18 13:09:51 +03:00
|
|
|
const shortCode = 'abc123';
|
|
|
|
const shortUrl = {
|
|
|
|
shortCode,
|
|
|
|
visitsCount: 11,
|
|
|
|
};
|
|
|
|
const state = {
|
2020-08-30 20:45:17 +03:00
|
|
|
shortUrls: Mock.of<ShlinkShortUrlsResponse>({
|
2020-04-18 13:09:51 +03:00
|
|
|
data: [
|
2020-08-27 19:31:56 +03:00
|
|
|
Mock.of<ShortUrl>({ shortCode, domain: 'example.com', visitsCount: 5 }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode, visitsCount: 10 }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode: 'foo', visitsCount: 8 }),
|
2020-04-18 13:09:51 +03:00
|
|
|
],
|
2020-08-30 20:45:17 +03:00
|
|
|
}),
|
2020-08-27 19:31:56 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
2020-04-18 13:09:51 +03:00
|
|
|
};
|
|
|
|
|
2020-09-12 12:31:44 +03:00
|
|
|
expect(reducer(state, { type: CREATE_VISITS, createdVisits: [{ shortUrl }] } as any)).toEqual({
|
2020-04-18 13:09:51 +03:00
|
|
|
shortUrls: {
|
|
|
|
data: [
|
|
|
|
{ shortCode, domain: 'example.com', visitsCount: 5 },
|
|
|
|
{ shortCode, visitsCount: 11 },
|
|
|
|
{ shortCode: 'foo', visitsCount: 8 },
|
|
|
|
],
|
|
|
|
},
|
2020-08-27 19:31:56 +03:00
|
|
|
loading: false,
|
|
|
|
error: false,
|
2020-04-18 13:09:51 +03:00
|
|
|
});
|
|
|
|
});
|
2020-12-08 12:57:27 +03:00
|
|
|
|
|
|
|
it('prepends new short URL and increases total on CREATE_SHORT_URL', () => {
|
|
|
|
const newShortUrl = Mock.of<ShortUrl>({ shortCode: 'newOne' });
|
|
|
|
const shortCode = 'abc123';
|
|
|
|
const state = {
|
|
|
|
shortUrls: Mock.of<ShlinkShortUrlsResponse>({
|
|
|
|
data: [
|
|
|
|
Mock.of<ShortUrl>({ shortCode }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode, domain: 'example.com' }),
|
|
|
|
Mock.of<ShortUrl>({ shortCode: 'foo' }),
|
|
|
|
],
|
|
|
|
pagination: Mock.of<ShlinkPaginator>({
|
|
|
|
totalItems: 15,
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(reducer(state, { type: CREATE_SHORT_URL, result: newShortUrl } as any)).toEqual({
|
|
|
|
shortUrls: {
|
|
|
|
data: [{ shortCode: 'newOne' }, { shortCode }, { shortCode, domain: 'example.com' }],
|
|
|
|
pagination: { totalItems: 16 },
|
|
|
|
},
|
|
|
|
loading: false,
|
|
|
|
error: false,
|
|
|
|
});
|
|
|
|
});
|
2018-12-21 12:58:51 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('listShortUrls', () => {
|
2019-04-19 13:41:59 +03:00
|
|
|
const dispatch = jest.fn();
|
|
|
|
const getState = jest.fn().mockReturnValue({ selectedServer: {} });
|
2018-12-21 12:58:51 +03:00
|
|
|
|
2020-08-27 19:31:56 +03:00
|
|
|
afterEach(jest.clearAllMocks);
|
2018-12-21 12:58:51 +03:00
|
|
|
|
|
|
|
it('dispatches proper actions if API client request succeeds', async () => {
|
2020-08-27 19:31:56 +03:00
|
|
|
const listShortUrlsMock = jest.fn().mockResolvedValue([]);
|
|
|
|
const apiClientMock = Mock.of<ShlinkApiClient>({ listShortUrls: listShortUrlsMock });
|
2018-12-21 12:58:51 +03:00
|
|
|
|
|
|
|
await listShortUrls(() => apiClientMock)()(dispatch, getState);
|
|
|
|
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_SHORT_URLS_START });
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_SHORT_URLS, shortUrls: [], params: {} });
|
2018-12-21 12:58:51 +03:00
|
|
|
|
2020-08-27 19:31:56 +03:00
|
|
|
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
|
2018-12-21 12:58:51 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches proper actions if API client request fails', async () => {
|
2020-08-27 19:31:56 +03:00
|
|
|
const listShortUrlsMock = jest.fn().mockRejectedValue(undefined);
|
|
|
|
const apiClientMock = Mock.of<ShlinkApiClient>({ listShortUrls: listShortUrlsMock });
|
2018-12-21 12:58:51 +03:00
|
|
|
|
|
|
|
await listShortUrls(() => apiClientMock)()(dispatch, getState);
|
|
|
|
|
2019-04-19 13:52:55 +03:00
|
|
|
expect(dispatch).toHaveBeenCalledTimes(2);
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_SHORT_URLS_START });
|
|
|
|
expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_SHORT_URLS_ERROR, params: {} });
|
2018-12-21 12:58:51 +03:00
|
|
|
|
2020-08-27 19:31:56 +03:00
|
|
|
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
|
2018-12-21 12:58:51 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|