Improved domainsList reducer test

This commit is contained in:
Alejandro Celaya 2021-08-22 09:00:58 +02:00
parent 69cb3bd619
commit 8e71b2e2b1
3 changed files with 60 additions and 16 deletions

View file

@ -32,24 +32,26 @@ const DefaultDomain: FC = () => (
export const DomainRow: FC<DomainRowProps> = ({ domain, editDomainRedirects, defaultRedirects }) => {
const [ isOpen, toggle ] = useToggle();
const { domain: authority, isDefault, redirects } = domain;
const domainId = `domainEdit${authority.replace('.', '')}`;
return (
<tr>
<td>{domain.isDefault ? <DefaultDomain /> : ''}</td>
<th>{domain.domain}</th>
<td>{domain.redirects?.baseUrlRedirect ?? <Nr fallback={defaultRedirects?.baseUrlRedirect} />}</td>
<td>{domain.redirects?.regular404Redirect ?? <Nr fallback={defaultRedirects?.regular404Redirect} />}</td>
<td>{isDefault ? <DefaultDomain /> : ''}</td>
<th>{authority}</th>
<td>{redirects?.baseUrlRedirect ?? <Nr fallback={defaultRedirects?.baseUrlRedirect} />}</td>
<td>{redirects?.regular404Redirect ?? <Nr fallback={defaultRedirects?.regular404Redirect} />}</td>
<td>
{domain.redirects?.invalidShortUrlRedirect ?? <Nr fallback={defaultRedirects?.invalidShortUrlRedirect} />}
{redirects?.invalidShortUrlRedirect ?? <Nr fallback={defaultRedirects?.invalidShortUrlRedirect} />}
</td>
<td className="text-right">
<span id={`domainEdit${domain.domain.replace('.', '')}`}>
<Button outline size="sm" disabled={domain.isDefault} onClick={domain.isDefault ? undefined : toggle}>
<FontAwesomeIcon icon={domain.isDefault ? forbiddenIcon : editIcon} />
<span id={domainId}>
<Button outline size="sm" disabled={isDefault} onClick={isDefault ? undefined : toggle}>
<FontAwesomeIcon icon={isDefault ? forbiddenIcon : editIcon} />
</Button>
</span>
{domain.isDefault && (
<UncontrolledTooltip target={`domainEdit${domain.domain.replace('.', '')}`} placement="left">
{isDefault && (
<UncontrolledTooltip target={domainId} placement="left">
Redirects for default domain cannot be edited here.
<br />
Use config options or env vars.

View file

@ -37,12 +37,12 @@ const initialState: DomainsList = {
error: false,
};
type DomainsCombinedAction = ListDomainsAction
export type DomainsCombinedAction = ListDomainsAction
& ApiErrorAction
& FilterDomainsAction
& EditDomainRedirectsAction;
const replaceRedirectsOnDomain = (domain: string, redirects: ShlinkDomainRedirects) =>
export const replaceRedirectsOnDomain = (domain: string, redirects: ShlinkDomainRedirects) =>
(d: ShlinkDomain): ShlinkDomain => d.domain !== domain ? d : { ...d, redirects };
export default buildReducer<DomainsList, DomainsCombinedAction>({

View file

@ -3,17 +3,23 @@ import reducer, {
LIST_DOMAINS,
LIST_DOMAINS_ERROR,
LIST_DOMAINS_START,
ListDomainsAction,
FILTER_DOMAINS,
DomainsCombinedAction,
DomainsList,
listDomains as listDomainsAction,
filterDomains as filterDomainsAction,
replaceRedirectsOnDomain,
} from '../../../src/domains/reducers/domainsList';
import { ShlinkDomain } from '../../../src/api/types';
import { EDIT_DOMAIN_REDIRECTS } from '../../../src/domains/reducers/domainRedirects';
import { ShlinkDomain, ShlinkDomainRedirects } from '../../../src/api/types';
import ShlinkApiClient from '../../../src/api/services/ShlinkApiClient';
describe('domainsList', () => {
const domains = [ Mock.all<ShlinkDomain>(), Mock.all<ShlinkDomain>(), Mock.all<ShlinkDomain>() ];
const filteredDomains = [ Mock.of<ShlinkDomain>({ domain: 'foo' }), Mock.of<ShlinkDomain>({ domain: 'boo' }) ];
const domains = [ ...filteredDomains, Mock.of<ShlinkDomain>({ domain: 'bar' }) ];
describe('reducer', () => {
const action = (type: string, args: Partial<ListDomainsAction> = {}): any => Mock.of<ListDomainsAction>(
const action = (type: string, args: Partial<DomainsCombinedAction> = {}) => Mock.of<DomainsCombinedAction>(
{ type, ...args },
);
@ -34,6 +40,32 @@ describe('domainsList', () => {
{ domains, filteredDomains: domains, loading: false, error: false },
);
});
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)),
});
});
});
describe('listDomains', () => {
@ -66,4 +98,14 @@ describe('domainsList', () => {
expect(listDomains).toHaveBeenCalledTimes(1);
});
});
describe('filterDomains', () => {
it.each([
[ 'foo' ],
[ 'bar' ],
[ 'something' ],
])('creates action as expected', (searchTerm) => {
expect(filterDomainsAction(searchTerm)).toEqual({ type: FILTER_DOMAINS, searchTerm });
});
});
});