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
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 { isString, escapeRegExp, flow, partialRight } from 'lodash';
import { SlugConfig } from '../types/redux'; import { SlugConfig } from '../types/redux';
function getUrl(urlString: string, direct: boolean) { function getUrl(urlString: string, direct?: boolean) {
return `${direct ? '/#' : ''}${urlString}`; return `${direct ? '/#' : ''}${urlString}`;
} }
export function getCollectionUrl(collectionName: string, direct: boolean) { export function getCollectionUrl(collectionName: string, direct?: boolean) {
return getUrl(`/collections/${collectionName}`, direct); 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); 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'); jest.mock('history');
describe('history', () => { const history = ({ push: jest.fn(), replace: jest.fn() } as unknown) as History;
const { createHashHistory } = require('history'); const mockedCreateHashHistory = mocked(createHashHistory);
const history = { push: jest.fn(), replace: jest.fn() }; mockedCreateHashHistory.mockReturnValue(history);
createHashHistory.mockReturnValue(history);
describe('history', () => {
beforeEach(() => { beforeEach(() => {
jest.clearAllMocks(); jest.clearAllMocks();
}); });
describe('navigateToCollection', () => { describe('navigateToCollection', () => {
it('should push route', () => { it('should push route', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { navigateToCollection } = require('../history'); const { navigateToCollection } = require('../history');
navigateToCollection('posts'); navigateToCollection('posts');
expect(history.push).toHaveBeenCalledTimes(1); expect(history.push).toHaveBeenCalledTimes(1);
expect(history.push).toHaveBeenCalledWith('/collections/posts'); expect(history.push).toHaveBeenCalledWith('/collections/posts');
}); });
@ -22,10 +25,10 @@ describe('history', () => {
describe('navigateToNewEntry', () => { describe('navigateToNewEntry', () => {
it('should replace route', () => { it('should replace route', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { navigateToNewEntry } = require('../history'); const { navigateToNewEntry } = require('../history');
navigateToNewEntry('posts'); navigateToNewEntry('posts');
expect(history.replace).toHaveBeenCalledTimes(1); expect(history.replace).toHaveBeenCalledTimes(1);
expect(history.replace).toHaveBeenCalledWith('/collections/posts/new'); expect(history.replace).toHaveBeenCalledWith('/collections/posts/new');
}); });
@ -33,10 +36,10 @@ describe('history', () => {
describe('navigateToEntry', () => { describe('navigateToEntry', () => {
it('should replace route', () => { it('should replace route', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { navigateToEntry } = require('../history'); const { navigateToEntry } = require('../history');
navigateToEntry('posts', 'index'); navigateToEntry('posts', 'index');
expect(history.replace).toHaveBeenCalledTimes(1); expect(history.replace).toHaveBeenCalledTimes(1);
expect(history.replace).toHaveBeenCalledWith('/collections/posts/entries/index'); expect(history.replace).toHaveBeenCalledWith('/collections/posts/entries/index');
}); });

View File

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