mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-14 11:33:51 +00:00
Migrated first short URL reducers to typescript
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { Mock } from 'ts-mockery';
|
||||
import reducer, {
|
||||
CREATE_SHORT_URL_START,
|
||||
CREATE_SHORT_URL_ERROR,
|
||||
@@ -6,33 +7,40 @@ import reducer, {
|
||||
createShortUrl,
|
||||
resetCreateShortUrl,
|
||||
} from '../../../src/short-urls/reducers/shortUrlCreation';
|
||||
import { ShortUrl } from '../../../src/short-urls/data';
|
||||
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
|
||||
import { ShlinkState } from '../../../src/container/types';
|
||||
|
||||
describe('shortUrlCreationReducer', () => {
|
||||
const shortUrl = Mock.all<ShortUrl>();
|
||||
|
||||
describe('reducer', () => {
|
||||
it('returns loading on CREATE_SHORT_URL_START', () => {
|
||||
expect(reducer({}, { type: CREATE_SHORT_URL_START })).toEqual({
|
||||
expect(reducer(undefined, { type: CREATE_SHORT_URL_START } as any)).toEqual({
|
||||
result: null,
|
||||
saving: true,
|
||||
error: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error on CREATE_SHORT_URL_ERROR', () => {
|
||||
expect(reducer({}, { type: CREATE_SHORT_URL_ERROR })).toEqual({
|
||||
expect(reducer(undefined, { type: CREATE_SHORT_URL_ERROR } as any)).toEqual({
|
||||
result: null,
|
||||
saving: false,
|
||||
error: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('returns result on CREATE_SHORT_URL', () => {
|
||||
expect(reducer({}, { type: CREATE_SHORT_URL, result: 'foo' })).toEqual({
|
||||
expect(reducer(undefined, { type: CREATE_SHORT_URL, result: shortUrl } as any)).toEqual({
|
||||
result: shortUrl,
|
||||
saving: false,
|
||||
error: false,
|
||||
result: 'foo',
|
||||
});
|
||||
});
|
||||
|
||||
it('returns default state on RESET_CREATE_SHORT_URL', () => {
|
||||
expect(reducer({}, { type: RESET_CREATE_SHORT_URL })).toEqual({
|
||||
expect(reducer(undefined, { type: RESET_CREATE_SHORT_URL } as any)).toEqual({
|
||||
result: null,
|
||||
saving: false,
|
||||
error: false,
|
||||
@@ -46,31 +54,30 @@ describe('shortUrlCreationReducer', () => {
|
||||
});
|
||||
|
||||
describe('createShortUrl', () => {
|
||||
const createApiClientMock = (result) => ({
|
||||
createShortUrl: jest.fn(() => result),
|
||||
const createApiClientMock = (result: Promise<ShortUrl>) => Mock.of<ShlinkApiClient>({
|
||||
createShortUrl: jest.fn().mockReturnValue(result),
|
||||
});
|
||||
const dispatch = jest.fn();
|
||||
const getState = () => ({});
|
||||
const getState = () => Mock.all<ShlinkState>();
|
||||
|
||||
afterEach(jest.resetAllMocks);
|
||||
|
||||
it('calls API on success', async () => {
|
||||
const result = 'foo';
|
||||
const apiClientMock = createApiClientMock(Promise.resolve(result));
|
||||
const dispatchable = createShortUrl(() => apiClientMock)({});
|
||||
const apiClientMock = createApiClientMock(Promise.resolve(shortUrl));
|
||||
const dispatchable = createShortUrl(() => apiClientMock)({ longUrl: 'foo' });
|
||||
|
||||
await dispatchable(dispatch, getState);
|
||||
|
||||
expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1);
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: CREATE_SHORT_URL_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: CREATE_SHORT_URL, result });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: CREATE_SHORT_URL, result: shortUrl });
|
||||
});
|
||||
|
||||
it('throws on error', async () => {
|
||||
const error = 'Error';
|
||||
const apiClientMock = createApiClientMock(Promise.reject(error));
|
||||
const dispatchable = createShortUrl(() => apiClientMock)({});
|
||||
const dispatchable = createShortUrl(() => apiClientMock)({ longUrl: 'foo' });
|
||||
|
||||
expect.assertions(5);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import moment from 'moment';
|
||||
import { Mock } from 'ts-mockery';
|
||||
import reducer, {
|
||||
EDIT_SHORT_URL_META_START,
|
||||
EDIT_SHORT_URL_META_ERROR,
|
||||
@@ -7,6 +8,7 @@ import reducer, {
|
||||
editShortUrlMeta,
|
||||
resetShortUrlMeta,
|
||||
} from '../../../src/short-urls/reducers/shortUrlMeta';
|
||||
import { ShlinkState } from '../../../src/container/types';
|
||||
|
||||
describe('shortUrlMetaReducer', () => {
|
||||
const meta = {
|
||||
@@ -17,21 +19,25 @@ describe('shortUrlMetaReducer', () => {
|
||||
|
||||
describe('reducer', () => {
|
||||
it('returns loading on EDIT_SHORT_URL_META_START', () => {
|
||||
expect(reducer({}, { type: EDIT_SHORT_URL_META_START })).toEqual({
|
||||
expect(reducer(undefined, { type: EDIT_SHORT_URL_META_START } as any)).toEqual({
|
||||
meta: {},
|
||||
shortCode: null,
|
||||
saving: true,
|
||||
error: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error on EDIT_SHORT_URL_META_ERROR', () => {
|
||||
expect(reducer({}, { type: EDIT_SHORT_URL_META_ERROR })).toEqual({
|
||||
expect(reducer(undefined, { type: EDIT_SHORT_URL_META_ERROR } as any)).toEqual({
|
||||
meta: {},
|
||||
shortCode: null,
|
||||
saving: false,
|
||||
error: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('returns provided tags and shortCode on SHORT_URL_META_EDITED', () => {
|
||||
expect(reducer({}, { type: SHORT_URL_META_EDITED, meta, shortCode })).toEqual({
|
||||
expect(reducer(undefined, { type: SHORT_URL_META_EDITED, payload: { meta, shortCode } })).toEqual({
|
||||
meta,
|
||||
shortCode,
|
||||
saving: false,
|
||||
@@ -40,7 +46,7 @@ describe('shortUrlMetaReducer', () => {
|
||||
});
|
||||
|
||||
it('goes back to initial state on RESET_EDIT_SHORT_URL_META', () => {
|
||||
expect(reducer({}, { type: RESET_EDIT_SHORT_URL_META })).toEqual({
|
||||
expect(reducer(undefined, { type: RESET_EDIT_SHORT_URL_META } as any)).toEqual({
|
||||
meta: {},
|
||||
shortCode: null,
|
||||
saving: false,
|
||||
@@ -53,18 +59,21 @@ describe('shortUrlMetaReducer', () => {
|
||||
const updateShortUrlMeta = jest.fn().mockResolvedValue({});
|
||||
const buildShlinkApiClient = jest.fn().mockReturnValue({ updateShortUrlMeta });
|
||||
const dispatch = jest.fn();
|
||||
const getState = () => Mock.all<ShlinkState>();
|
||||
|
||||
afterEach(jest.clearAllMocks);
|
||||
|
||||
it.each([[ undefined ], [ null ], [ 'example.com' ]])('dispatches metadata on success', async (domain) => {
|
||||
await editShortUrlMeta(buildShlinkApiClient)(shortCode, domain, meta)(dispatch);
|
||||
const payload = { meta, shortCode, domain };
|
||||
|
||||
await editShortUrlMeta(buildShlinkApiClient)(shortCode, domain, meta)(dispatch, getState);
|
||||
|
||||
expect(buildShlinkApiClient).toHaveBeenCalledTimes(1);
|
||||
expect(updateShortUrlMeta).toHaveBeenCalledTimes(1);
|
||||
expect(updateShortUrlMeta).toHaveBeenCalledWith(shortCode, domain, meta);
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_META_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SHORT_URL_META_EDITED, meta, shortCode, domain });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: SHORT_URL_META_EDITED, payload });
|
||||
});
|
||||
|
||||
it('dispatches error on failure', async () => {
|
||||
@@ -73,7 +82,7 @@ describe('shortUrlMetaReducer', () => {
|
||||
updateShortUrlMeta.mockRejectedValue(error);
|
||||
|
||||
try {
|
||||
await editShortUrlMeta(buildShlinkApiClient)(shortCode, undefined, meta)(dispatch);
|
||||
await editShortUrlMeta(buildShlinkApiClient)(shortCode, undefined, meta)(dispatch, getState);
|
||||
} catch (e) {
|
||||
expect(e).toBe(error);
|
||||
}
|
||||
Reference in New Issue
Block a user