diff --git a/packages/netlify-cms-core/src/actions/collections.js b/packages/netlify-cms-core/src/actions/collections.js deleted file mode 100644 index 53cd4ab0..00000000 --- a/packages/netlify-cms-core/src/actions/collections.js +++ /dev/null @@ -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)); -} diff --git a/packages/netlify-cms-core/src/actions/collections.ts b/packages/netlify-cms-core/src/actions/collections.ts new file mode 100644 index 00000000..25c51c93 --- /dev/null +++ b/packages/netlify-cms-core/src/actions/collections.ts @@ -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)); +} diff --git a/packages/netlify-cms-core/src/lib/urlHelper.ts b/packages/netlify-cms-core/src/lib/urlHelper.ts index f17d3529..9137a964 100644 --- a/packages/netlify-cms-core/src/lib/urlHelper.ts +++ b/packages/netlify-cms-core/src/lib/urlHelper.ts @@ -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); } diff --git a/packages/netlify-cms-core/src/routing/__tests__/history.spec.js b/packages/netlify-cms-core/src/routing/__tests__/history.spec.ts similarity index 68% rename from packages/netlify-cms-core/src/routing/__tests__/history.spec.js rename to packages/netlify-cms-core/src/routing/__tests__/history.spec.ts index eb18786e..6622edb8 100644 --- a/packages/netlify-cms-core/src/routing/__tests__/history.spec.js +++ b/packages/netlify-cms-core/src/routing/__tests__/history.spec.ts @@ -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'); }); diff --git a/packages/netlify-cms-core/src/routing/history.js b/packages/netlify-cms-core/src/routing/history.ts similarity index 58% rename from packages/netlify-cms-core/src/routing/history.js rename to packages/netlify-cms-core/src/routing/history.ts index abc13658..9a2b5db5 100644 --- a/packages/netlify-cms-core/src/routing/history.js +++ b/packages/netlify-cms-core/src/routing/history.ts @@ -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;