refactor: migrate history to TypeScript (#4759)
This commit is contained in:
committed by
GitHub
parent
f60c2871d3
commit
38c96a4133
@ -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');
|
||||
});
|
@ -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;
|
Reference in New Issue
Block a user