mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-02-27 04:06:39 +00:00
Removed remaining usages of sinon
This commit is contained in:
@@ -4,7 +4,6 @@ import { DropdownItem } from 'reactstrap';
|
||||
import { identity, values } from 'ramda';
|
||||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
||||
import { faSortAmountDown as caretDownIcon } from '@fortawesome/free-solid-svg-icons';
|
||||
import * as sinon from 'sinon';
|
||||
import SortingDropdown from '../../src/utils/SortingDropdown';
|
||||
|
||||
describe('<SortingDropdown />', () => {
|
||||
@@ -44,35 +43,35 @@ describe('<SortingDropdown />', () => {
|
||||
});
|
||||
|
||||
it('triggers change function when item is clicked and no order field was provided', () => {
|
||||
const onChange = sinon.spy();
|
||||
const onChange = jest.fn();
|
||||
const wrapper = createWrapper({ onChange });
|
||||
const firstItem = wrapper.find(DropdownItem).first();
|
||||
|
||||
firstItem.simulate('click');
|
||||
|
||||
expect(onChange.callCount).toEqual(1);
|
||||
expect(onChange.calledWith('foo', 'ASC')).toEqual(true);
|
||||
expect(onChange).toHaveBeenCalledTimes(1);
|
||||
expect(onChange).toHaveBeenCalledWith('foo', 'ASC');
|
||||
});
|
||||
|
||||
it('triggers change function when item is clicked and an order field was provided', () => {
|
||||
const onChange = sinon.spy();
|
||||
const onChange = jest.fn();
|
||||
const wrapper = createWrapper({ onChange, orderField: 'baz', orderDir: 'ASC' });
|
||||
const firstItem = wrapper.find(DropdownItem).first();
|
||||
|
||||
firstItem.simulate('click');
|
||||
|
||||
expect(onChange.callCount).toEqual(1);
|
||||
expect(onChange.calledWith('foo', 'ASC')).toEqual(true);
|
||||
expect(onChange).toHaveBeenCalledTimes(1);
|
||||
expect(onChange).toHaveBeenCalledWith('foo', 'ASC');
|
||||
});
|
||||
|
||||
it('updates order dir when already selected item is clicked', () => {
|
||||
const onChange = sinon.spy();
|
||||
const onChange = jest.fn();
|
||||
const wrapper = createWrapper({ onChange, orderField: 'foo', orderDir: 'ASC' });
|
||||
const firstItem = wrapper.find(DropdownItem).first();
|
||||
|
||||
firstItem.simulate('click');
|
||||
|
||||
expect(onChange.callCount).toEqual(1);
|
||||
expect(onChange.calledWith('foo', 'DESC')).toEqual(true);
|
||||
expect(onChange).toHaveBeenCalledTimes(1);
|
||||
expect(onChange).toHaveBeenCalledWith('foo', 'DESC');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
import * as sinon from 'sinon';
|
||||
import ColorGenerator from '../../../src/utils/services/ColorGenerator';
|
||||
|
||||
describe('ColorGenerator', () => {
|
||||
let colorGenerator;
|
||||
const storageMock = {
|
||||
set: sinon.fake(),
|
||||
get: sinon.fake.returns(undefined),
|
||||
set: jest.fn(),
|
||||
get: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
storageMock.set.resetHistory();
|
||||
storageMock.get.resetHistory();
|
||||
storageMock.set.mockReset();
|
||||
storageMock.get.mockReset();
|
||||
|
||||
colorGenerator = new ColorGenerator(storageMock);
|
||||
});
|
||||
@@ -21,14 +20,14 @@ describe('ColorGenerator', () => {
|
||||
colorGenerator.setColorForKey('foo', color);
|
||||
|
||||
expect(colorGenerator.getColorForKey('foo')).toEqual(color);
|
||||
expect(storageMock.set.callCount).toEqual(1);
|
||||
expect(storageMock.get.callCount).toEqual(1);
|
||||
expect(storageMock.set).toHaveBeenCalledTimes(1);
|
||||
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('generates a random color when none is available for requested key', () => {
|
||||
expect(colorGenerator.getColorForKey('bar')).toEqual(expect.stringMatching(/^#(?:[0-9a-fA-F]{6})$/));
|
||||
expect(storageMock.set.callCount).toEqual(1);
|
||||
expect(storageMock.get.callCount).toEqual(1);
|
||||
expect(storageMock.set).toHaveBeenCalledTimes(1);
|
||||
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('trims and lower cases keys before trying to match', () => {
|
||||
@@ -42,7 +41,7 @@ describe('ColorGenerator', () => {
|
||||
expect(colorGenerator.getColorForKey('FOO')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey('FOO ')).toEqual(color);
|
||||
expect(colorGenerator.getColorForKey(' FoO ')).toEqual(color);
|
||||
expect(storageMock.set.callCount).toEqual(1);
|
||||
expect(storageMock.get.callCount).toEqual(1);
|
||||
expect(storageMock.set).toHaveBeenCalledTimes(1);
|
||||
expect(storageMock.get).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import sinon from 'sinon';
|
||||
import { head, last } from 'ramda';
|
||||
import ShlinkApiClient from '../../../src/utils/services/ShlinkApiClient';
|
||||
|
||||
describe('ShlinkApiClient', () => {
|
||||
@@ -35,23 +33,21 @@ describe('ShlinkApiClient', () => {
|
||||
});
|
||||
|
||||
it('removes all empty options', async () => {
|
||||
const axiosSpy = sinon.spy(createAxiosMock({ data: shortUrl }));
|
||||
const axiosSpy = jest.fn(createAxiosMock({ data: shortUrl }));
|
||||
const { createShortUrl } = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
await createShortUrl(
|
||||
{ foo: 'bar', empty: undefined, anotherEmpty: null }
|
||||
);
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(axiosArgs.data).toEqual({ foo: 'bar' });
|
||||
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({ data: { foo: 'bar' } }));
|
||||
});
|
||||
});
|
||||
|
||||
describe('getShortUrlVisits', () => {
|
||||
it('properly returns short URL visits', async () => {
|
||||
const expectedVisits = [ 'foo', 'bar' ];
|
||||
const axiosSpy = sinon.spy(createAxiosMock({
|
||||
const axiosSpy = jest.fn(createAxiosMock({
|
||||
data: {
|
||||
visits: {
|
||||
data: expectedVisits,
|
||||
@@ -61,55 +57,55 @@ describe('ShlinkApiClient', () => {
|
||||
const { getShortUrlVisits } = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
const actualVisits = await getShortUrlVisits('abc123', {});
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect({ data: expectedVisits }).toEqual(actualVisits);
|
||||
expect(axiosArgs.url).toContain('/short-urls/abc123/visits');
|
||||
expect(axiosArgs.method).toEqual('GET');
|
||||
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
||||
url: '/short-urls/abc123/visits',
|
||||
method: 'GET',
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
describe('getShortUrl', () => {
|
||||
it('properly returns short URL', async () => {
|
||||
const expectedShortUrl = { foo: 'bar' };
|
||||
const axiosSpy = sinon.spy(createAxiosMock({
|
||||
const axiosSpy = jest.fn(createAxiosMock({
|
||||
data: expectedShortUrl,
|
||||
}));
|
||||
const { getShortUrl } = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
const result = await getShortUrl('abc123');
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(expectedShortUrl).toEqual(result);
|
||||
expect(axiosArgs.url).toContain('/short-urls/abc123');
|
||||
expect(axiosArgs.method).toEqual('GET');
|
||||
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
||||
url: '/short-urls/abc123',
|
||||
method: 'GET',
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateShortUrlTags', () => {
|
||||
it('properly updates short URL tags', async () => {
|
||||
const expectedTags = [ 'foo', 'bar' ];
|
||||
const axiosSpy = sinon.spy(createAxiosMock({
|
||||
const axiosSpy = jest.fn(createAxiosMock({
|
||||
data: { tags: expectedTags },
|
||||
}));
|
||||
const { updateShortUrlTags } = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
const result = await updateShortUrlTags('abc123', expectedTags);
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(expectedTags).toEqual(result);
|
||||
expect(axiosArgs.url).toContain('/short-urls/abc123/tags');
|
||||
expect(axiosArgs.method).toEqual('PUT');
|
||||
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
||||
url: '/short-urls/abc123/tags',
|
||||
method: 'PUT',
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
describe('listTags', () => {
|
||||
it('properly returns list of tags', async () => {
|
||||
const expectedTags = [ 'foo', 'bar' ];
|
||||
const axiosSpy = sinon.spy(createAxiosMock({
|
||||
const axiosSpy = jest.fn(createAxiosMock({
|
||||
data: {
|
||||
tags: { data: expectedTags },
|
||||
},
|
||||
@@ -117,28 +113,25 @@ describe('ShlinkApiClient', () => {
|
||||
const { listTags } = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
const result = await listTags();
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(expectedTags).toEqual(result);
|
||||
expect(axiosArgs.url).toContain('/tags');
|
||||
expect(axiosArgs.method).toEqual('GET');
|
||||
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({ url: '/tags', method: 'GET' }));
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteTags', () => {
|
||||
it('properly deletes provided tags', async () => {
|
||||
const tags = [ 'foo', 'bar' ];
|
||||
const axiosSpy = sinon.spy(createAxiosMock({}));
|
||||
const axiosSpy = jest.fn(createAxiosMock({}));
|
||||
const { deleteTags } = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
await deleteTags(tags);
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(axiosArgs.url).toContain('/tags');
|
||||
expect(axiosArgs.method).toEqual('DELETE');
|
||||
expect(axiosArgs.params).toEqual({ tags });
|
||||
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
||||
url: '/tags',
|
||||
method: 'DELETE',
|
||||
params: { tags },
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -146,30 +139,30 @@ describe('ShlinkApiClient', () => {
|
||||
it('properly edits provided tag', async () => {
|
||||
const oldName = 'foo';
|
||||
const newName = 'bar';
|
||||
const axiosSpy = sinon.spy(createAxiosMock({}));
|
||||
const axiosSpy = jest.fn(createAxiosMock({}));
|
||||
const { editTag } = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
await editTag(oldName, newName);
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(axiosArgs.url).toContain('/tags');
|
||||
expect(axiosArgs.method).toEqual('PUT');
|
||||
expect(axiosArgs.data).toEqual({ oldName, newName });
|
||||
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
||||
url: '/tags',
|
||||
method: 'PUT',
|
||||
data: { oldName, newName },
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteShortUrl', () => {
|
||||
it('properly deletes provided short URL', async () => {
|
||||
const axiosSpy = sinon.spy(createAxiosMock({}));
|
||||
const axiosSpy = jest.fn(createAxiosMock({}));
|
||||
const { deleteShortUrl } = new ShlinkApiClient(axiosSpy);
|
||||
|
||||
await deleteShortUrl('abc123');
|
||||
const lastAxiosCall = last(axiosSpy.getCalls());
|
||||
const axiosArgs = head(lastAxiosCall.args);
|
||||
|
||||
expect(axiosArgs.url).toContain('/short-urls/abc123');
|
||||
expect(axiosArgs.method).toEqual('DELETE');
|
||||
expect(axiosSpy).toHaveBeenCalledWith(expect.objectContaining({
|
||||
url: '/short-urls/abc123',
|
||||
method: 'DELETE',
|
||||
}));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
import * as sinon from 'sinon';
|
||||
import Storage from '../../../src/utils/services/Storage';
|
||||
|
||||
describe('Storage', () => {
|
||||
const localStorageMock = {
|
||||
getItem: sinon.fake((key) => key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null),
|
||||
setItem: sinon.spy(),
|
||||
getItem: jest.fn((key) => key === 'shlink.foo' ? JSON.stringify({ foo: 'bar' }) : null),
|
||||
setItem: jest.fn(),
|
||||
};
|
||||
let storage;
|
||||
|
||||
beforeEach(() => {
|
||||
localStorageMock.getItem.resetHistory();
|
||||
localStorageMock.setItem.resetHistory();
|
||||
localStorageMock.getItem.mockClear();
|
||||
localStorageMock.setItem.mockReset();
|
||||
|
||||
storage = new Storage(localStorageMock);
|
||||
});
|
||||
@@ -21,18 +20,15 @@ describe('Storage', () => {
|
||||
|
||||
storage.set('foo', value);
|
||||
|
||||
expect(localStorageMock.setItem.callCount).toEqual(1);
|
||||
expect(localStorageMock.setItem.getCall(0).args).toEqual([
|
||||
'shlink.foo',
|
||||
JSON.stringify(value),
|
||||
]);
|
||||
expect(localStorageMock.setItem).toHaveBeenCalledTimes(1);
|
||||
expect(localStorageMock.setItem).toHaveBeenCalledWith('shlink.foo', JSON.stringify(value));
|
||||
});
|
||||
});
|
||||
|
||||
describe('get', () => {
|
||||
it('fetches item from local storage', () => {
|
||||
storage.get('foo');
|
||||
expect(localStorageMock.getItem.callCount).toEqual(1);
|
||||
expect(localStorageMock.getItem).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('returns parsed value when requested value is found in local storage', () => {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import * as sinon from 'sinon';
|
||||
import L from 'leaflet';
|
||||
import marker2x from 'leaflet/dist/images/marker-icon-2x.png';
|
||||
import marker from 'leaflet/dist/images/marker-icon.png';
|
||||
@@ -14,19 +13,18 @@ import {
|
||||
describe('utils', () => {
|
||||
describe('stateFlagTimeout', () => {
|
||||
it('sets state and initializes timeout with provided delay', () => {
|
||||
const setTimeout = sinon.fake((callback) => callback());
|
||||
const setState = sinon.spy();
|
||||
const setTimeout = jest.fn((callback) => callback());
|
||||
const setState = jest.fn();
|
||||
const stateFlagTimeout = stateFlagTimeoutFactory(setTimeout);
|
||||
const delay = 5000;
|
||||
const expectedSetStateCalls = 2;
|
||||
|
||||
stateFlagTimeout(setState, 'foo', false, delay);
|
||||
|
||||
expect(setState.callCount).toEqual(expectedSetStateCalls);
|
||||
expect(setState.getCall(0).args).toEqual([{ foo: false }]);
|
||||
expect(setState.getCall(1).args).toEqual([{ foo: true }]);
|
||||
expect(setTimeout.callCount).toEqual(1);
|
||||
expect(setTimeout.getCall(0).args[1]).toEqual(delay);
|
||||
expect(setState).toHaveBeenCalledTimes(2);
|
||||
expect(setState).toHaveBeenNthCalledWith(1, { foo: false });
|
||||
expect(setState).toHaveBeenNthCalledWith(2, { foo: true });
|
||||
expect(setTimeout).toHaveBeenCalledTimes(1);
|
||||
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), delay);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user