refactor: migrate history to TypeScript (#4759)

This commit is contained in:
Vladislav Shkodin 2020-12-23 14:18:41 +02:00 committed by GitHub
parent f60c2871d3
commit 38c96a4133
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 31 deletions

View File

@ -1,18 +0,0 @@
import history from 'Routing/history';
import { getCollectionUrl, getNewEntryUrl } from 'Lib/urlHelper';
export function searchCollections(query, collection) {
if (collection) {
history.push(`/collections/${collection}/search/${query}`);
} else {
history.push(`/search/${query}`);
}
}
export function showCollection(collectionName) {
history.push(getCollectionUrl(collectionName));
}
export function createNewEntry(collectionName) {
history.push(getNewEntryUrl(collectionName));
}

View File

@ -0,0 +1,18 @@
import history from '../routing/history';
import { getCollectionUrl, getNewEntryUrl } from '../lib/urlHelper';
export function searchCollections(query: string, collection: string) {
if (collection) {
history.push(`/collections/${collection}/search/${query}`);
} else {
history.push(`/search/${query}`);
}
}
export function showCollection(collectionName: string) {
history.push(getCollectionUrl(collectionName));
}
export function createNewEntry(collectionName: string) {
history.push(getNewEntryUrl(collectionName));
}

View File

@ -4,15 +4,15 @@ import sanitizeFilename from 'sanitize-filename';
import { isString, escapeRegExp, flow, partialRight } from 'lodash';
import { SlugConfig } from '../types/redux';
function getUrl(urlString: string, direct: boolean) {
function getUrl(urlString: string, direct?: boolean) {
return `${direct ? '/#' : ''}${urlString}`;
}
export function getCollectionUrl(collectionName: string, direct: boolean) {
export function getCollectionUrl(collectionName: string, direct?: boolean) {
return getUrl(`/collections/${collectionName}`, direct);
}
export function getNewEntryUrl(collectionName: string, direct: boolean) {
export function getNewEntryUrl(collectionName: string, direct?: boolean) {
return getUrl(`/collections/${collectionName}/new`, direct);
}

View File

@ -1,20 +1,23 @@
import { createHashHistory, History } from 'history';
import { mocked } from 'ts-jest/utils';
jest.mock('history');
describe('history', () => {
const { createHashHistory } = require('history');
const history = { push: jest.fn(), replace: jest.fn() };
createHashHistory.mockReturnValue(history);
const history = ({ push: jest.fn(), replace: jest.fn() } as unknown) as History;
const mockedCreateHashHistory = mocked(createHashHistory);
mockedCreateHashHistory.mockReturnValue(history);
describe('history', () => {
beforeEach(() => {
jest.clearAllMocks();
});
describe('navigateToCollection', () => {
it('should push route', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { navigateToCollection } = require('../history');
navigateToCollection('posts');
expect(history.push).toHaveBeenCalledTimes(1);
expect(history.push).toHaveBeenCalledWith('/collections/posts');
});
@ -22,10 +25,10 @@ describe('history', () => {
describe('navigateToNewEntry', () => {
it('should replace route', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { navigateToNewEntry } = require('../history');
navigateToNewEntry('posts');
expect(history.replace).toHaveBeenCalledTimes(1);
expect(history.replace).toHaveBeenCalledWith('/collections/posts/new');
});
@ -33,10 +36,10 @@ describe('history', () => {
describe('navigateToEntry', () => {
it('should replace route', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { navigateToEntry } = require('../history');
navigateToEntry('posts', 'index');
expect(history.replace).toHaveBeenCalledTimes(1);
expect(history.replace).toHaveBeenCalledWith('/collections/posts/entries/index');
});

View File

@ -2,11 +2,11 @@ import { createHashHistory } from 'history';
const history = createHashHistory();
export const navigateToCollection = collectionName =>
export const navigateToCollection = (collectionName: string) =>
history.push(`/collections/${collectionName}`);
export const navigateToNewEntry = collectionName =>
export const navigateToNewEntry = (collectionName: string) =>
history.replace(`/collections/${collectionName}/new`);
export const navigateToEntry = (collectionName, slug) =>
export const navigateToEntry = (collectionName: string, slug: string) =>
history.replace(`/collections/${collectionName}/entries/${slug}`);
export default history;