shlink-web-client/test/short-urls/reducers/shortUrlsList.test.js

138 lines
4.1 KiB
JavaScript
Raw Normal View History

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';
2018-12-21 12:58:51 +03:00
describe('shortUrlsListReducer', () => {
describe('reducer', () => {
it('returns loading on LIST_SHORT_URLS_START', () =>
expect(reducer(undefined, { type: LIST_SHORT_URLS_START })).toEqual({
shortUrls: {},
loading: true,
error: false,
}));
it('returns short URLs on LIST_SHORT_URLS', () =>
expect(reducer(undefined, { type: LIST_SHORT_URLS, shortUrls: { data: [], paginator: {} } })).toEqual({
shortUrls: { data: [], paginator: {} },
loading: false,
error: false,
}));
it('returns error on LIST_SHORT_URLS_ERROR', () =>
expect(reducer(undefined, { type: LIST_SHORT_URLS_ERROR })).toEqual({
shortUrls: {},
loading: false,
error: true,
}));
it('Updates tags on matching URL on SHORT_URL_TAGS_EDITED', () => {
const shortCode = 'abc123';
const tags = [ 'foo', 'bar', 'baz' ];
const state = {
shortUrls: {
data: [
{ shortCode, tags: [] },
{ shortCode: 'foo', tags: [] },
],
},
};
expect(reducer(state, { type: SHORT_URL_TAGS_EDITED, shortCode, tags })).toEqual({
shortUrls: {
data: [
{ shortCode, tags },
{ shortCode: 'foo', tags: [] },
],
},
});
});
2020-01-19 15:20:46 +03:00
it('Updates meta on matching URL on SHORT_URL_META_EDITED', () => {
const shortCode = 'abc123';
const meta = {
maxVisits: 5,
validSince: '2020-05-05',
};
const state = {
shortUrls: {
data: [
{ shortCode, meta: { maxVisits: 10 } },
{ shortCode: 'foo', meta: null },
],
},
};
expect(reducer(state, { type: SHORT_URL_META_EDITED, shortCode, meta })).toEqual({
shortUrls: {
data: [
{ shortCode, meta },
{ shortCode: 'foo', meta: null },
],
},
});
});
2018-12-21 12:58:51 +03:00
it('Removes matching URL on SHORT_URL_DELETED', () => {
const shortCode = 'abc123';
const state = {
shortUrls: {
data: [
{ shortCode },
{ shortCode: 'foo' },
],
},
};
expect(reducer(state, { type: SHORT_URL_DELETED, shortCode })).toEqual({
shortUrls: {
data: [{ shortCode: 'foo' }],
},
});
});
});
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
afterEach(() => {
2019-04-19 13:41:59 +03:00
dispatch.mockReset();
getState.mockClear();
2018-12-21 12:58:51 +03:00
});
it('dispatches proper actions if API client request succeeds', async () => {
const apiClientMock = {
2019-04-19 13:41:59 +03:00
listShortUrls: jest.fn().mockResolvedValue([]),
2018-12-21 12:58:51 +03:00
};
await listShortUrls(() => apiClientMock)()(dispatch, getState);
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
2019-04-19 13:41:59 +03:00
expect(apiClientMock.listShortUrls).toHaveBeenCalledTimes(1);
2018-12-21 12:58:51 +03:00
});
it('dispatches proper actions if API client request fails', async () => {
const apiClientMock = {
2019-04-19 13:41:59 +03:00
listShortUrls: jest.fn().mockRejectedValue(),
2018-12-21 12:58:51 +03:00
};
await listShortUrls(() => apiClientMock)()(dispatch, getState);
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
2019-04-19 13:41:59 +03:00
expect(apiClientMock.listShortUrls).toHaveBeenCalledTimes(1);
2018-12-21 12:58:51 +03:00
});
});
});