shlink-web-client/test/domains/reducers/domainsList.test.ts

112 lines
3.9 KiB
TypeScript
Raw Normal View History

2020-11-28 14:22:52 +03:00
import { Mock } from 'ts-mockery';
import reducer, {
LIST_DOMAINS,
LIST_DOMAINS_ERROR,
LIST_DOMAINS_START,
2021-08-22 10:00:58 +03:00
FILTER_DOMAINS,
DomainsCombinedAction,
DomainsList,
2020-11-28 14:22:52 +03:00
listDomains as listDomainsAction,
2021-08-22 10:00:58 +03:00
filterDomains as filterDomainsAction,
replaceRedirectsOnDomain,
2020-11-28 14:22:52 +03:00
} from '../../../src/domains/reducers/domainsList';
2021-08-22 10:00:58 +03:00
import { EDIT_DOMAIN_REDIRECTS } from '../../../src/domains/reducers/domainRedirects';
import { ShlinkDomain, ShlinkDomainRedirects } from '../../../src/api/types';
import ShlinkApiClient from '../../../src/api/services/ShlinkApiClient';
2020-11-28 14:22:52 +03:00
describe('domainsList', () => {
2021-08-22 10:00:58 +03:00
const filteredDomains = [ Mock.of<ShlinkDomain>({ domain: 'foo' }), Mock.of<ShlinkDomain>({ domain: 'boo' }) ];
const domains = [ ...filteredDomains, Mock.of<ShlinkDomain>({ domain: 'bar' }) ];
2020-11-28 14:22:52 +03:00
describe('reducer', () => {
2021-08-22 10:00:58 +03:00
const action = (type: string, args: Partial<DomainsCombinedAction> = {}) => Mock.of<DomainsCombinedAction>(
2020-11-28 14:22:52 +03:00
{ type, ...args },
);
it('returns loading on LIST_DOMAINS_START', () => {
expect(reducer(undefined, action(LIST_DOMAINS_START))).toEqual(
{ domains: [], filteredDomains: [], loading: true, error: false },
);
2020-11-28 14:22:52 +03:00
});
it('returns error on LIST_DOMAINS_ERROR', () => {
expect(reducer(undefined, action(LIST_DOMAINS_ERROR))).toEqual(
{ domains: [], filteredDomains: [], loading: false, error: true },
);
2020-11-28 14:22:52 +03:00
});
it('returns domains on LIST_DOMAINS', () => {
expect(reducer(undefined, action(LIST_DOMAINS, { domains }))).toEqual(
{ domains, filteredDomains: domains, loading: false, error: false },
);
2020-11-28 14:22:52 +03:00
});
2021-08-22 10:00:58 +03:00
it('filters domains on FILTER_DOMAINS', () => {
expect(reducer(Mock.of<DomainsList>({ domains }), action(FILTER_DOMAINS, { searchTerm: 'oo' }))).toEqual(
{ domains, filteredDomains },
);
});
it.each([
[ 'foo' ],
[ 'bar' ],
[ 'does_not_exist' ],
])('replaces redirects on proper domain on EDIT_DOMAIN_REDIRECTS', (domain) => {
const redirects: ShlinkDomainRedirects = {
baseUrlRedirect: 'bar',
regular404Redirect: 'foo',
invalidShortUrlRedirect: null,
};
expect(reducer(
Mock.of<DomainsList>({ domains, filteredDomains }),
action(EDIT_DOMAIN_REDIRECTS, { domain, redirects }),
)).toEqual({
domains: domains.map(replaceRedirectsOnDomain(domain, redirects)),
filteredDomains: filteredDomains.map(replaceRedirectsOnDomain(domain, redirects)),
});
});
2020-11-28 14:22:52 +03:00
});
describe('listDomains', () => {
const dispatch = jest.fn();
const getState = jest.fn();
const listDomains = jest.fn();
const buildShlinkApiClient = () => Mock.of<ShlinkApiClient>({ listDomains });
beforeEach(jest.clearAllMocks);
it('dispatches error when loading domains fails', async () => {
listDomains.mockRejectedValue(new Error('error'));
await listDomainsAction(buildShlinkApiClient)()(dispatch, getState);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_DOMAINS_START });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_DOMAINS_ERROR });
expect(listDomains).toHaveBeenCalledTimes(1);
});
it('dispatches domains once loaded', async () => {
listDomains.mockResolvedValue(domains);
await listDomainsAction(buildShlinkApiClient)()(dispatch, getState);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_DOMAINS_START });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_DOMAINS, domains });
expect(listDomains).toHaveBeenCalledTimes(1);
});
});
2021-08-22 10:00:58 +03:00
describe('filterDomains', () => {
it.each([
[ 'foo' ],
[ 'bar' ],
[ 'something' ],
])('creates action as expected', (searchTerm) => {
expect(filterDomainsAction(searchTerm)).toEqual({ type: FILTER_DOMAINS, searchTerm });
});
});
2020-11-28 14:22:52 +03:00
});