Improved branching coverage in some parts

This commit is contained in:
Alejandro Celaya 2021-09-20 21:51:51 +02:00
parent 27e3d65143
commit b877aa8e5b
3 changed files with 37 additions and 11 deletions

View file

@ -1,4 +1,3 @@
import qs from 'qs';
import { isEmpty, isNil, reject } from 'ramda'; import { isEmpty, isNil, reject } from 'ramda';
import { AxiosInstance, AxiosResponse, Method } from 'axios'; import { AxiosInstance, AxiosResponse, Method } from 'axios';
import { ShortUrlsListParams } from '../../short-urls/reducers/shortUrlsListParams'; import { ShortUrlsListParams } from '../../short-urls/reducers/shortUrlsListParams';
@ -19,6 +18,7 @@ import {
ShlinkEditDomainRedirects, ShlinkEditDomainRedirects,
ShlinkDomainRedirects, ShlinkDomainRedirects,
} from '../types'; } from '../types';
import { stringifyQuery } from '../../utils/helpers/query';
const buildShlinkBaseUrl = (url: string, apiVersion: number) => url ? `${url}/rest/v${apiVersion}` : ''; const buildShlinkBaseUrl = (url: string, apiVersion: number) => url ? `${url}/rest/v${apiVersion}` : '';
const rejectNilProps = reject(isNil); const rejectNilProps = reject(isNil);
@ -123,7 +123,7 @@ export default class ShlinkApiClient {
headers: { 'X-Api-Key': this.apiKey }, headers: { 'X-Api-Key': this.apiKey },
params: rejectNilProps(query), params: rejectNilProps(query),
data: body, data: body,
paramsSerializer: (params) => qs.stringify(params, { arrayFormat: 'brackets' }), paramsSerializer: stringifyQuery,
}); });
} catch (e) { } catch (e) {
const { response } = e; const { response } = e;

View file

@ -10,28 +10,40 @@ describe('<DomainSelector />', () => {
let wrapper: ShallowWrapper; let wrapper: ShallowWrapper;
const domainsList = Mock.of<DomainsList>({ const domainsList = Mock.of<DomainsList>({
domains: [ domains: [
Mock.of<ShlinkDomain>({ domain: 'default.com', isDefault: true }),
Mock.of<ShlinkDomain>({ domain: 'foo.com' }), Mock.of<ShlinkDomain>({ domain: 'foo.com' }),
Mock.of<ShlinkDomain>({ domain: 'bar.com' }), Mock.of<ShlinkDomain>({ domain: 'bar.com' }),
], ],
}); });
const createWrapper = (value = '') => {
wrapper = shallow(
<DomainSelector value={value} domainsList={domainsList} listDomains={jest.fn()} onChange={jest.fn()} />,
);
beforeEach(() => { return wrapper;
wrapper = shallow(<DomainSelector domainsList={domainsList} listDomains={jest.fn()} onChange={jest.fn()} />); };
});
afterEach(jest.clearAllMocks); afterEach(jest.clearAllMocks);
afterEach(() => wrapper.unmount()); afterEach(() => wrapper.unmount());
it('shows dropdown by default', () => { it.each([
[ '', 'Domain', 'domains-dropdown__toggle-btn' ],
[ 'my-domain.com', 'Domain: my-domain.com', 'domains-dropdown__toggle-btn--active' ],
])('shows dropdown by default', (value, expectedText, expectedClassName) => {
const wrapper = createWrapper(value);
const input = wrapper.find(InputGroup); const input = wrapper.find(InputGroup);
const dropdown = wrapper.find(DropdownBtn); const dropdown = wrapper.find(DropdownBtn);
expect(input).toHaveLength(0); expect(input).toHaveLength(0);
expect(dropdown).toHaveLength(1); expect(dropdown).toHaveLength(1);
expect(dropdown.find(DropdownItem)).toHaveLength(4); expect(dropdown.find(DropdownItem)).toHaveLength(5);
expect(dropdown.prop('text')).toEqual(expectedText);
expect(dropdown.prop('className')).toEqual(expectedClassName);
}); });
it('allows to toggle between dropdown and input', () => { it('allows toggling between dropdown and input', () => {
const wrapper = createWrapper();
wrapper.find(DropdownItem).last().simulate('click'); wrapper.find(DropdownItem).last().simulate('click');
expect(wrapper.find(InputGroup)).toHaveLength(1); expect(wrapper.find(InputGroup)).toHaveLength(1);
expect(wrapper.find(DropdownBtn)).toHaveLength(0); expect(wrapper.find(DropdownBtn)).toHaveLength(0);
@ -40,4 +52,14 @@ describe('<DomainSelector />', () => {
expect(wrapper.find(InputGroup)).toHaveLength(0); expect(wrapper.find(InputGroup)).toHaveLength(0);
expect(wrapper.find(DropdownBtn)).toHaveLength(1); expect(wrapper.find(DropdownBtn)).toHaveLength(1);
}); });
it.each([
[ 0, 'default.com<span class="float-right text-muted">default</span>' ],
[ 1, 'foo.com' ],
[ 2, 'bar.com' ],
])('shows expected content on every item', (index, expectedContent) => {
const item = createWrapper().find(DropdownItem).at(index);
expect(item.html()).toContain(expectedContent);
});
}); });

View file

@ -43,9 +43,13 @@ describe('visitsOverviewReducer', () => {
expect(visitsCount).toEqual(100); expect(visitsCount).toEqual(100);
}); });
it('returns updated amounts on CREATE_VISITS', () => { it.each([
[ 50, 53 ],
[ 0, 3 ],
[ undefined, 3 ],
])('returns updated amounts on CREATE_VISITS', (providedOrphanVisitsCount, expectedOrphanVisitsCount) => {
const { visitsCount, orphanVisitsCount } = reducer( const { visitsCount, orphanVisitsCount } = reducer(
state({ visitsCount: 100, orphanVisitsCount: 50 }), state({ visitsCount: 100, orphanVisitsCount: providedOrphanVisitsCount }),
{ {
type: CREATE_VISITS, type: CREATE_VISITS,
createdVisits: [ createdVisits: [
@ -65,7 +69,7 @@ describe('visitsOverviewReducer', () => {
); );
expect(visitsCount).toEqual(102); expect(visitsCount).toEqual(102);
expect(orphanVisitsCount).toEqual(53); expect(orphanVisitsCount).toEqual(expectedOrphanVisitsCount);
}); });
}); });