chore: update prettier (#5412)

This commit is contained in:
Vladislav Shkodin 2021-05-19 14:39:35 +02:00 committed by GitHub
parent 46738492a0
commit 39f113715a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
66 changed files with 1625 additions and 1763 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
* text=auto eol=lf

View File

@ -1,4 +1,5 @@
{
"arrowParens": "avoid",
"trailingComma": "all",
"singleQuote": true,
"printWidth": 100

View File

@ -148,7 +148,7 @@
"nock": "^13.0.0",
"node-fetch": "^2.3.0",
"npm-run-all": "^4.1.5",
"prettier": "^1.19.1",
"prettier": "^2.3.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-test-renderer": "^16.8.4",

View File

@ -277,9 +277,9 @@ export default class BitbucketBackend implements Implementation {
}
apiRequestFunction = async (req: ApiRequest) => {
const token = (this.refreshedTokenPromise
? await this.refreshedTokenPromise
: this.token) as string;
const token = (
this.refreshedTokenPromise ? await this.refreshedTokenPromise : this.token
) as string;
const authorizedRequest = unsentRequest.withHeaders({ Authorization: `Bearer ${token}` }, req);
const response: Response = await unsentRequest.performRequest(authorizedRequest);

View File

@ -310,10 +310,10 @@ export default class API {
let responseStatus = 500;
try {
const req = (unsentRequest.fromFetchArguments(url, {
const req = unsentRequest.fromFetchArguments(url, {
...options,
headers,
}) as unknown) as ApiRequest;
}) as unknown as ApiRequest;
const response = await requestWithBackoff(this, req);
responseStatus = response.status;
const parsedResponse = await parser(response);
@ -366,8 +366,7 @@ export default class API {
.catch(() => {
// Meta ref doesn't exist
const readme = {
raw:
'# Netlify CMS\n\nThis tree is used by the Netlify CMS to store metadata information for specific files and branches.',
raw: '# Netlify CMS\n\nThis tree is used by the Netlify CMS to store metadata information for specific files and branches.',
};
return this.uploadBlob(readme)
@ -808,7 +807,8 @@ export default class API {
let branches: string[];
if (this.useOpenAuthoring) {
// open authoring branches can exist without a pr
const cmsBranches: Octokit.GitListMatchingRefsResponse = await this.getOpenAuthoringBranches();
const cmsBranches: Octokit.GitListMatchingRefsResponse =
await this.getOpenAuthoringBranches();
branches = cmsBranches.map(b => b.ref.substring('refs/heads/'.length));
// filter irrelevant branches
const branchesWithFilter = await Promise.all(
@ -1036,7 +1036,7 @@ export default class API {
author,
committer,
);
return (newCommit as unknown) as GitHubCompareCommit;
return newCommit as unknown as GitHubCompareCommit;
} else {
return commit;
}

View File

@ -75,10 +75,8 @@ export default class GitHubAuthenticationPage extends React.Component {
};
const auth = new NetlifyAuthenticator(cfg);
const {
open_authoring: openAuthoring = false,
auth_scope: authScope = '',
} = this.props.config.backend;
const { open_authoring: openAuthoring = false, auth_scope: authScope = '' } =
this.props.config.backend;
const scope = authScope || (openAuthoring ? 'public_repo' : 'repo');
auth.authenticate({ provider: 'github', scope }, (err, data) => {

View File

@ -300,7 +300,7 @@ export default class GraphQLAPI extends API {
const mapped = pullRequests.nodes.map(transformPullRequest);
return ((mapped as unknown) as Octokit.PullsListResponseItem[]).filter(
return (mapped as unknown as Octokit.PullsListResponseItem[]).filter(
pr => pr.head.ref.startsWith(`${CMS_BRANCH_PREFIX}/`) && predicate(pr),
);
}
@ -673,7 +673,7 @@ export default class GraphQLAPI extends API {
},
});
const { pullRequest } = data!.createPullRequest;
return (transformPullRequest(pullRequest) as unknown) as Octokit.PullsCreateResponse;
return transformPullRequest(pullRequest) as unknown as Octokit.PullsCreateResponse;
}
async getFileSha(path: string, { repoURL = this.repoURL, branch = this.branch } = {}) {

View File

@ -537,9 +537,9 @@ export default class GitHub implements Implementation {
}
const readFile = (path: string, id: string | null | undefined) =>
this.api!.readFile(path, id, { repoURL: this.api!.originRepoURL }).catch(() => '') as Promise<
string
>;
this.api!.readFile(path, id, { repoURL: this.api!.originRepoURL }).catch(
() => '',
) as Promise<string>;
const entries = await entriesByFiles(
result.files,

View File

@ -245,10 +245,8 @@ export default class API {
MAINTAINER_ACCESS = 40;
hasWriteAccess = async () => {
const {
shared_with_groups: sharedWithGroups,
permissions,
}: GitLabRepo = await this.requestJSON(this.repoURL);
const { shared_with_groups: sharedWithGroups, permissions }: GitLabRepo =
await this.requestJSON(this.repoURL);
const { project_access: projectAccess, group_access: groupAccess } = permissions;
if (projectAccess && projectAccess.access_level >= this.WRITE_ACCESS) {

View File

@ -287,10 +287,7 @@ describe('gitlab backend', () => {
function interceptFiles(backend, path) {
const api = mockApi(backend);
const url = `${expectedRepoUrl}/repository/files/${encodeURIComponent(path)}/raw`;
api
.get(url)
.query(true)
.reply(200, mockRepo.files[path]);
api.get(url).query(true).reply(200, mockRepo.files[path]);
api
.get(`${expectedRepoUrl}/repository/commits`)
@ -391,10 +388,7 @@ describe('gitlab backend', () => {
it('returns an entry from folder collection', async () => {
const entryTree = mockRepo.tree[collectionContentConfig.folder][0];
const slug = entryTree.path
.split('/')
.pop()
.replace('.md', '');
const slug = entryTree.path.split('/').pop().replace('.md', '');
interceptFiles(backend, entryTree.path);
interceptCollection(backend, collectionContentConfig);

View File

@ -80,7 +80,7 @@ export default class ProxyBackend implements Implementation {
}
authenticate() {
return (Promise.resolve() as unknown) as Promise<User>;
return Promise.resolve() as unknown as Promise<User>;
}
logout() {

View File

@ -54,7 +54,7 @@ function getFile(path: string, tree: RepoTree) {
while (obj && segments.length) {
obj = obj[segments.shift() as string] as RepoTree;
}
return ((obj as unknown) as RepoFile) || {};
return (obj as unknown as RepoFile) || {};
}
function writeFile(path: string, content: string | AssetProxy, tree: RepoTree) {
@ -146,7 +146,7 @@ export default class TestBackend implements Implementation {
}
authenticate() {
return (Promise.resolve() as unknown) as Promise<User>;
return Promise.resolve() as unknown as Promise<User>;
}
logout() {

View File

@ -516,9 +516,12 @@ declare module 'netlify-cms-core' {
};
}
type GetAssetFunction = (
asset: string,
) => { url: string; path: string; field?: any; fileObj: File };
type GetAssetFunction = (asset: string) => {
url: string;
path: string;
field?: any;
fileObj: File;
};
export type PreviewTemplateComponentProps = {
entry: Map<string, any>;

File diff suppressed because it is too large Load Diff

View File

@ -9,9 +9,8 @@ import { State } from '../../types/redux';
import AssetProxy from '../../valueObjects/AssetProxy';
const middlewares = [thunk];
const mockStore = configureMockStore<Partial<State>, ThunkDispatch<State, {}, AnyAction>>(
middlewares,
);
const mockStore =
configureMockStore<Partial<State>, ThunkDispatch<State, {}, AnyAction>>(middlewares);
const mockedSelectMediaFilePath = mocked(selectMediaFilePath);
jest.mock('../../reducers/entries');

View File

@ -1,326 +1,326 @@
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { List, Map } from 'immutable';
import { insertMedia, persistMedia, deleteMedia } from '../mediaLibrary';
jest.mock('../../backend');
jest.mock('../waitUntil');
jest.mock('netlify-cms-lib-util', () => {
const lib = jest.requireActual('netlify-cms-lib-util');
return {
...lib,
getBlobSHA: jest.fn(),
};
});
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
describe('mediaLibrary', () => {
describe('insertMedia', () => {
it('should return mediaPath as string when string is given', () => {
const store = mockStore({
config: {
public_folder: '/media',
},
collections: Map({
posts: Map({ name: 'posts' }),
}),
entryDraft: Map({
entry: Map({ isPersisting: false, collection: 'posts' }),
}),
});
store.dispatch(insertMedia('foo.png'));
expect(store.getActions()[0]).toEqual({
type: 'MEDIA_INSERT',
payload: { mediaPath: '/media/foo.png' },
});
});
it('should return mediaPath as array of strings when array of strings is given', () => {
const store = mockStore({
config: {
public_folder: '/media',
},
collections: Map({
posts: Map({ name: 'posts' }),
}),
entryDraft: Map({
entry: Map({ isPersisting: false, collection: 'posts' }),
}),
});
store.dispatch(insertMedia(['foo.png']));
expect(store.getActions()[0]).toEqual({
type: 'MEDIA_INSERT',
payload: { mediaPath: ['/media/foo.png'] },
});
});
});
const { currentBackend } = require('coreSrc/backend');
const backend = {
persistMedia: jest.fn(() => ({ id: 'id' })),
deleteMedia: jest.fn(),
};
currentBackend.mockReturnValue(backend);
describe('persistMedia', () => {
global.URL = { createObjectURL: jest.fn().mockReturnValue('displayURL') };
beforeEach(() => {
jest.clearAllMocks();
});
it('should not persist media when editing draft', () => {
const { getBlobSHA } = require('netlify-cms-lib-util');
getBlobSHA.mockReturnValue('000000000000000');
const store = mockStore({
config: {
media_folder: 'static/media',
slug: {
encoding: 'unicode',
clean_accents: false,
sanitize_replacement: '-',
},
},
collections: Map({
posts: Map({ name: 'posts' }),
}),
integrations: Map(),
mediaLibrary: Map({
files: List(),
}),
entryDraft: Map({
entry: Map({ isPersisting: false, collection: 'posts' }),
}),
});
const file = new File([''], 'name.png');
return store.dispatch(persistMedia(file)).then(() => {
const actions = store.getActions();
expect(actions).toHaveLength(2);
expect(actions[0].type).toEqual('ADD_ASSET');
expect(actions[0].payload).toEqual(
expect.objectContaining({
path: 'static/media/name.png',
}),
);
expect(actions[1].type).toEqual('ADD_DRAFT_ENTRY_MEDIA_FILE');
expect(actions[1].payload).toEqual(
expect.objectContaining({
draft: true,
id: '000000000000000',
path: 'static/media/name.png',
size: file.size,
name: file.name,
}),
);
expect(getBlobSHA).toHaveBeenCalledTimes(1);
expect(getBlobSHA).toHaveBeenCalledWith(file);
expect(backend.persistMedia).toHaveBeenCalledTimes(0);
});
});
it('should persist media when not editing draft', () => {
const store = mockStore({
config: {
media_folder: 'static/media',
slug: {
encoding: 'unicode',
clean_accents: false,
sanitize_replacement: '-',
},
},
collections: Map({
posts: Map({ name: 'posts' }),
}),
integrations: Map(),
mediaLibrary: Map({
files: List(),
}),
entryDraft: Map({
entry: Map(),
}),
});
const file = new File([''], 'name.png');
return store.dispatch(persistMedia(file)).then(() => {
const actions = store.getActions();
expect(actions).toHaveLength(3);
expect(actions).toHaveLength(3);
expect(actions[0]).toEqual({ type: 'MEDIA_PERSIST_REQUEST' });
expect(actions[1].type).toEqual('ADD_ASSET');
expect(actions[1].payload).toEqual(
expect.objectContaining({
path: 'static/media/name.png',
}),
);
expect(actions[2]).toEqual({
type: 'MEDIA_PERSIST_SUCCESS',
payload: {
file: { id: 'id' },
},
});
expect(backend.persistMedia).toHaveBeenCalledTimes(1);
expect(backend.persistMedia).toHaveBeenCalledWith(
store.getState().config,
expect.objectContaining({
path: 'static/media/name.png',
}),
);
});
});
it('should sanitize media name if needed when persisting', () => {
const store = mockStore({
config: {
media_folder: 'static/media',
slug: {
encoding: 'ascii',
clean_accents: true,
sanitize_replacement: '_',
},
},
collections: Map({
posts: Map({ name: 'posts' }),
}),
integrations: Map(),
mediaLibrary: Map({
files: List(),
}),
entryDraft: Map({
entry: Map(),
}),
});
const file = new File([''], 'abc DEF éâçÖ $;, .png');
return store.dispatch(persistMedia(file)).then(() => {
const actions = store.getActions();
expect(actions).toHaveLength(3);
expect(actions[0]).toEqual({ type: 'MEDIA_PERSIST_REQUEST' });
expect(actions[1].type).toEqual('ADD_ASSET');
expect(actions[1].payload).toEqual(
expect.objectContaining({
path: 'static/media/abc_def_eaco_.png',
}),
);
expect(actions[2]).toEqual({
type: 'MEDIA_PERSIST_SUCCESS',
payload: {
file: { id: 'id' },
},
});
expect(backend.persistMedia).toHaveBeenCalledTimes(1);
expect(backend.persistMedia).toHaveBeenCalledWith(
store.getState().config,
expect.objectContaining({
path: 'static/media/abc_def_eaco_.png',
}),
);
});
});
});
describe('deleteMedia', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should delete non draft file', () => {
const store = mockStore({
config: {
publish_mode: 'editorial_workflow',
},
collections: Map(),
integrations: Map(),
mediaLibrary: Map({
files: List(),
}),
entryDraft: Map({
entry: Map({ isPersisting: false }),
}),
});
const file = { name: 'name.png', id: 'id', path: 'static/media/name.png', draft: false };
return store.dispatch(deleteMedia(file)).then(() => {
const actions = store.getActions();
expect(actions).toHaveLength(4);
expect(actions[0]).toEqual({ type: 'MEDIA_DELETE_REQUEST' });
expect(actions[1]).toEqual({
type: 'REMOVE_ASSET',
payload: 'static/media/name.png',
});
expect(actions[2]).toEqual({
type: 'MEDIA_DELETE_SUCCESS',
payload: { file },
});
expect(actions[3]).toEqual({
type: 'REMOVE_DRAFT_ENTRY_MEDIA_FILE',
payload: { id: 'id' },
});
expect(backend.deleteMedia).toHaveBeenCalledTimes(1);
expect(backend.deleteMedia).toHaveBeenCalledWith(
store.getState().config,
'static/media/name.png',
);
});
});
it('should not delete a draft file', () => {
const store = mockStore({
config: {
publish_mode: 'editorial_workflow',
},
collections: Map(),
integrations: Map(),
mediaLibrary: Map({
files: List(),
}),
entryDraft: Map({
entry: Map({ isPersisting: false }),
}),
});
const file = { name: 'name.png', id: 'id', path: 'static/media/name.png', draft: true };
return store.dispatch(deleteMedia(file)).then(() => {
const actions = store.getActions();
expect(actions).toHaveLength(2);
expect(actions[0]).toEqual({
type: 'REMOVE_ASSET',
payload: 'static/media/name.png',
});
expect(actions[1]).toEqual({
type: 'REMOVE_DRAFT_ENTRY_MEDIA_FILE',
payload: { id: 'id' },
});
expect(backend.deleteMedia).toHaveBeenCalledTimes(0);
});
});
});
});
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { List, Map } from 'immutable';
import { insertMedia, persistMedia, deleteMedia } from '../mediaLibrary';
jest.mock('../../backend');
jest.mock('../waitUntil');
jest.mock('netlify-cms-lib-util', () => {
const lib = jest.requireActual('netlify-cms-lib-util');
return {
...lib,
getBlobSHA: jest.fn(),
};
});
const middlewares = [thunk];
const mockStore = configureMockStore(middlewares);
describe('mediaLibrary', () => {
describe('insertMedia', () => {
it('should return mediaPath as string when string is given', () => {
const store = mockStore({
config: {
public_folder: '/media',
},
collections: Map({
posts: Map({ name: 'posts' }),
}),
entryDraft: Map({
entry: Map({ isPersisting: false, collection: 'posts' }),
}),
});
store.dispatch(insertMedia('foo.png'));
expect(store.getActions()[0]).toEqual({
type: 'MEDIA_INSERT',
payload: { mediaPath: '/media/foo.png' },
});
});
it('should return mediaPath as array of strings when array of strings is given', () => {
const store = mockStore({
config: {
public_folder: '/media',
},
collections: Map({
posts: Map({ name: 'posts' }),
}),
entryDraft: Map({
entry: Map({ isPersisting: false, collection: 'posts' }),
}),
});
store.dispatch(insertMedia(['foo.png']));
expect(store.getActions()[0]).toEqual({
type: 'MEDIA_INSERT',
payload: { mediaPath: ['/media/foo.png'] },
});
});
});
const { currentBackend } = require('coreSrc/backend');
const backend = {
persistMedia: jest.fn(() => ({ id: 'id' })),
deleteMedia: jest.fn(),
};
currentBackend.mockReturnValue(backend);
describe('persistMedia', () => {
global.URL = { createObjectURL: jest.fn().mockReturnValue('displayURL') };
beforeEach(() => {
jest.clearAllMocks();
});
it('should not persist media when editing draft', () => {
const { getBlobSHA } = require('netlify-cms-lib-util');
getBlobSHA.mockReturnValue('000000000000000');
const store = mockStore({
config: {
media_folder: 'static/media',
slug: {
encoding: 'unicode',
clean_accents: false,
sanitize_replacement: '-',
},
},
collections: Map({
posts: Map({ name: 'posts' }),
}),
integrations: Map(),
mediaLibrary: Map({
files: List(),
}),
entryDraft: Map({
entry: Map({ isPersisting: false, collection: 'posts' }),
}),
});
const file = new File([''], 'name.png');
return store.dispatch(persistMedia(file)).then(() => {
const actions = store.getActions();
expect(actions).toHaveLength(2);
expect(actions[0].type).toEqual('ADD_ASSET');
expect(actions[0].payload).toEqual(
expect.objectContaining({
path: 'static/media/name.png',
}),
);
expect(actions[1].type).toEqual('ADD_DRAFT_ENTRY_MEDIA_FILE');
expect(actions[1].payload).toEqual(
expect.objectContaining({
draft: true,
id: '000000000000000',
path: 'static/media/name.png',
size: file.size,
name: file.name,
}),
);
expect(getBlobSHA).toHaveBeenCalledTimes(1);
expect(getBlobSHA).toHaveBeenCalledWith(file);
expect(backend.persistMedia).toHaveBeenCalledTimes(0);
});
});
it('should persist media when not editing draft', () => {
const store = mockStore({
config: {
media_folder: 'static/media',
slug: {
encoding: 'unicode',
clean_accents: false,
sanitize_replacement: '-',
},
},
collections: Map({
posts: Map({ name: 'posts' }),
}),
integrations: Map(),
mediaLibrary: Map({
files: List(),
}),
entryDraft: Map({
entry: Map(),
}),
});
const file = new File([''], 'name.png');
return store.dispatch(persistMedia(file)).then(() => {
const actions = store.getActions();
expect(actions).toHaveLength(3);
expect(actions).toHaveLength(3);
expect(actions[0]).toEqual({ type: 'MEDIA_PERSIST_REQUEST' });
expect(actions[1].type).toEqual('ADD_ASSET');
expect(actions[1].payload).toEqual(
expect.objectContaining({
path: 'static/media/name.png',
}),
);
expect(actions[2]).toEqual({
type: 'MEDIA_PERSIST_SUCCESS',
payload: {
file: { id: 'id' },
},
});
expect(backend.persistMedia).toHaveBeenCalledTimes(1);
expect(backend.persistMedia).toHaveBeenCalledWith(
store.getState().config,
expect.objectContaining({
path: 'static/media/name.png',
}),
);
});
});
it('should sanitize media name if needed when persisting', () => {
const store = mockStore({
config: {
media_folder: 'static/media',
slug: {
encoding: 'ascii',
clean_accents: true,
sanitize_replacement: '_',
},
},
collections: Map({
posts: Map({ name: 'posts' }),
}),
integrations: Map(),
mediaLibrary: Map({
files: List(),
}),
entryDraft: Map({
entry: Map(),
}),
});
const file = new File([''], 'abc DEF éâçÖ $;, .png');
return store.dispatch(persistMedia(file)).then(() => {
const actions = store.getActions();
expect(actions).toHaveLength(3);
expect(actions[0]).toEqual({ type: 'MEDIA_PERSIST_REQUEST' });
expect(actions[1].type).toEqual('ADD_ASSET');
expect(actions[1].payload).toEqual(
expect.objectContaining({
path: 'static/media/abc_def_eaco_.png',
}),
);
expect(actions[2]).toEqual({
type: 'MEDIA_PERSIST_SUCCESS',
payload: {
file: { id: 'id' },
},
});
expect(backend.persistMedia).toHaveBeenCalledTimes(1);
expect(backend.persistMedia).toHaveBeenCalledWith(
store.getState().config,
expect.objectContaining({
path: 'static/media/abc_def_eaco_.png',
}),
);
});
});
});
describe('deleteMedia', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should delete non draft file', () => {
const store = mockStore({
config: {
publish_mode: 'editorial_workflow',
},
collections: Map(),
integrations: Map(),
mediaLibrary: Map({
files: List(),
}),
entryDraft: Map({
entry: Map({ isPersisting: false }),
}),
});
const file = { name: 'name.png', id: 'id', path: 'static/media/name.png', draft: false };
return store.dispatch(deleteMedia(file)).then(() => {
const actions = store.getActions();
expect(actions).toHaveLength(4);
expect(actions[0]).toEqual({ type: 'MEDIA_DELETE_REQUEST' });
expect(actions[1]).toEqual({
type: 'REMOVE_ASSET',
payload: 'static/media/name.png',
});
expect(actions[2]).toEqual({
type: 'MEDIA_DELETE_SUCCESS',
payload: { file },
});
expect(actions[3]).toEqual({
type: 'REMOVE_DRAFT_ENTRY_MEDIA_FILE',
payload: { id: 'id' },
});
expect(backend.deleteMedia).toHaveBeenCalledTimes(1);
expect(backend.deleteMedia).toHaveBeenCalledWith(
store.getState().config,
'static/media/name.png',
);
});
});
it('should not delete a draft file', () => {
const store = mockStore({
config: {
publish_mode: 'editorial_workflow',
},
collections: Map(),
integrations: Map(),
mediaLibrary: Map({
files: List(),
}),
entryDraft: Map({
entry: Map({ isPersisting: false }),
}),
});
const file = { name: 'name.png', id: 'id', path: 'static/media/name.png', draft: true };
return store.dispatch(deleteMedia(file)).then(() => {
const actions = store.getActions();
expect(actions).toHaveLength(2);
expect(actions[0]).toEqual({
type: 'REMOVE_ASSET',
payload: 'static/media/name.png',
});
expect(actions[1]).toEqual({
type: 'REMOVE_DRAFT_ENTRY_MEDIA_FILE',
payload: { id: 'id' },
});
expect(backend.deleteMedia).toHaveBeenCalledTimes(0);
});
});
});
});

View File

@ -474,9 +474,11 @@ export async function handleLocalBackend(originalConfig: CmsConfig) {
return originalConfig;
}
const { proxyUrl, publish_modes: publishModes, type: backendType } = await detectProxyServer(
originalConfig.local_backend,
);
const {
proxyUrl,
publish_modes: publishModes,
type: backendType,
} = await detectProxyServer(originalConfig.local_backend);
if (!proxyUrl) {
return originalConfig;

View File

@ -519,7 +519,7 @@ export function unpublishPublishedEntry(collection: Collection, slug: string) {
const state = getState();
const backend = currentBackend(state.config);
const entry = selectEntry(state, collection.get('name'), slug);
const entryDraft = (Map().set('entry', entry) as unknown) as EntryDraft;
const entryDraft = Map().set('entry', entry) as unknown as EntryDraft;
dispatch(unpublishedEntryPersisting(collection, slug));
return backend
.deleteEntry(state, collection, slug)

View File

@ -1322,7 +1322,7 @@ export function resolveBackend(config: CmsConfig) {
}
}
export const currentBackend = (function() {
export const currentBackend = (function () {
let backend: Backend;
return (config: CmsConfig) => {

View File

@ -25,7 +25,7 @@ import Header from './Header';
TopBarProgress.config({
barColors: {
'0': colors.active,
0: colors.active,
'1.0': colors.active,
},
shadowBlur: 0,

View File

@ -192,7 +192,7 @@ export class Editor extends React.Component {
window.removeEventListener('beforeunload', this.exitBlocker);
}
createBackup = debounce(function(entry, collection) {
createBackup = debounce(function (entry, collection) {
this.props.persistLocalBackup(entry, collection);
}, 2000);
@ -202,14 +202,8 @@ export class Editor extends React.Component {
};
handleChangeStatus = newStatusName => {
const {
entryDraft,
updateUnpublishedEntryStatus,
collection,
slug,
currentStatus,
t,
} = this.props;
const { entryDraft, updateUnpublishedEntryStatus, collection, slug, currentStatus, t } =
this.props;
if (entryDraft.get('hasChanged')) {
window.alert(t('editor.editor.onUpdatingWithUnsavedChanges'));
return;
@ -318,15 +312,8 @@ export class Editor extends React.Component {
};
handleDeleteUnpublishedChanges = async () => {
const {
entryDraft,
collection,
slug,
deleteUnpublishedEntry,
loadEntry,
isModification,
t,
} = this.props;
const { entryDraft, collection, slug, deleteUnpublishedEntry, loadEntry, isModification, t } =
this.props;
if (
entryDraft.get('hasChanged') &&
!window.confirm(t('editor.editor.onDeleteUnpublishedChangesWithUnsavedChanges'))

View File

@ -24,10 +24,10 @@ const timestampTag = {
tag: '!timestamp',
test: RegExp(
'^' +
'([0-9]{4})-([0-9]{2})-([0-9]{2})' + // YYYY-MM-DD
'T' + // T
'([0-9]{2}):([0-9]{2}):([0-9]{2}(\\.[0-9]+)?)' + // HH:MM:SS(.ss)?
'Z' + // Z
'([0-9]{4})-([0-9]{2})-([0-9]{2})' + // YYYY-MM-DD
'T' + // T
'([0-9]{2}):([0-9]{2}):([0-9]{2}(\\.[0-9]+)?)' + // HH:MM:SS(.ss)?
'Z' + // Z
'$',
),
resolve: (str: string) => new Date(str),

View File

@ -20,7 +20,7 @@ export function resolveIntegrations(interationsConfig, getToken) {
return integrationInstances;
}
export const getIntegrationProvider = (function() {
export const getIntegrationProvider = (function () {
let integrations = null;
return (interationsConfig, getToken, provider) => {

View File

@ -182,7 +182,7 @@ export function previewUrlFormatter(
let fields = entry.get('data') as Map<string, string>;
fields = addFileTemplateFields(entry.get('path'), fields, collection.get('folder'));
const dateFieldName = getDateField() || selectInferedField(collection, 'date');
const date = parseDateFromEntry((entry as unknown) as Map<string, unknown>, dateFieldName);
const date = parseDateFromEntry(entry as unknown as Map<string, unknown>, dateFieldName);
// Prepare and sanitize slug variables only, leave the rest of the
// `preview_path` template as is.
@ -213,7 +213,7 @@ export function summaryFormatter(summaryTemplate: string, entry: EntryMap, colle
let entryData = entry.get('data');
const date =
parseDateFromEntry(
(entry as unknown) as Map<string, unknown>,
entry as unknown as Map<string, unknown>,
selectInferedField(collection, 'date'),
) || null;
const identifier = entryData.getIn(keyToPathArray(selectIdentifier(collection) as string));
@ -247,7 +247,7 @@ export function folderFormatter(
const date =
parseDateFromEntry(
(entry as unknown) as Map<string, unknown>,
entry as unknown as Map<string, unknown>,
selectInferedField(collection, 'date'),
) || null;
const identifier = fields.getIn(keyToPathArray(selectIdentifier(collection) as string));

View File

@ -36,7 +36,8 @@ export function stripProtocol(urlString: string) {
* but JS stores strings as UTF-16/UCS-2 internally, so we should not normalize or re-encode.
*/
const uriChars = /[\w\-.~]/i;
const ucsChars = /[\xA0-\u{D7FF}\u{F900}-\u{FDCF}\u{FDF0}-\u{FFEF}\u{10000}-\u{1FFFD}\u{20000}-\u{2FFFD}\u{30000}-\u{3FFFD}\u{40000}-\u{4FFFD}\u{50000}-\u{5FFFD}\u{60000}-\u{6FFFD}\u{70000}-\u{7FFFD}\u{80000}-\u{8FFFD}\u{90000}-\u{9FFFD}\u{A0000}-\u{AFFFD}\u{B0000}-\u{BFFFD}\u{C0000}-\u{CFFFD}\u{D0000}-\u{DFFFD}\u{E1000}-\u{EFFFD}]/u;
const ucsChars =
/[\xA0-\u{D7FF}\u{F900}-\u{FDCF}\u{FDF0}-\u{FFEF}\u{10000}-\u{1FFFD}\u{20000}-\u{2FFFD}\u{30000}-\u{3FFFD}\u{40000}-\u{4FFFD}\u{50000}-\u{5FFFD}\u{60000}-\u{6FFFD}\u{70000}-\u{7FFFD}\u{80000}-\u{8FFFD}\u{90000}-\u{9FFFD}\u{A0000}-\u{AFFFD}\u{B0000}-\u{BFFFD}\u{C0000}-\u{CFFFD}\u{D0000}-\u{DFFFD}\u{E1000}-\u{EFFFD}]/u;
function validURIChar(char: string) {
return uriChars.test(char);
@ -80,9 +81,7 @@ export function sanitizeURI(
// `Array.from` must be used instead of `String.split` because
// `split` converts things like emojis into UTF-16 surrogate pairs.
return Array.from(str)
.map(getCharReplacer(encoding, replacement))
.join('');
return Array.from(str).map(getCharReplacer(encoding, replacement)).join('');
}
export function sanitizeChar(char: string, options?: CmsSlug) {
@ -95,8 +94,11 @@ export function sanitizeSlug(str: string, options?: CmsSlug) {
throw new Error('The input slug must be a string.');
}
const { encoding, clean_accents: stripDiacritics, sanitize_replacement: replacement } =
options || {};
const {
encoding,
clean_accents: stripDiacritics,
sanitize_replacement: replacement,
} = options || {};
const sanitizedSlug = flow([
...(stripDiacritics ? [diacritics.remove] : []),

View File

@ -25,7 +25,7 @@ function handleInsert(url: string) {
}
const initializeMediaLibrary = once(async function initializeMediaLibrary(name, options) {
const lib = (getMediaLibrary(name) as unknown) as MediaLibrary | undefined;
const lib = getMediaLibrary(name) as unknown as MediaLibrary | undefined;
if (!lib) {
const err = new Error(
`Missing external media library '${name}'. Please use 'registerMediaLibrary' to register it.`,

View File

@ -368,30 +368,15 @@ describe('collections', () => {
});
expect(selectField(collection, 'en.title')).toBe(
collection
.get('fields')
.get(0)
.get('fields')
.get(0),
collection.get('fields').get(0).get('fields').get(0),
);
expect(selectField(collection, 'it.title.subTitle')).toBe(
collection
.get('fields')
.get(2)
.get('field')
.get('fields')
.get(0),
collection.get('fields').get(2).get('field').get('fields').get(0),
);
expect(selectField(collection, 'fr.title.variableType')).toBe(
collection
.get('fields')
.get(3)
.get('fields')
.get(0)
.get('types')
.get(0),
collection.get('fields').get(3).get('fields').get(0).get('types').get(0),
);
});
});

View File

@ -144,10 +144,7 @@ export function selectFieldsWithMediaFolders(collection: Collection, slug: strin
const fields = collection.get('fields').toArray();
return getFieldsWithMediaFolders(fields);
} else if (collection.has('files')) {
const fields =
getFileFromSlug(collection, slug)
?.get('fields')
.toArray() || [];
const fields = getFileFromSlug(collection, slug)?.get('fields').toArray() || [];
return getFieldsWithMediaFolders(fields);
}
@ -317,16 +314,18 @@ export function selectInferedField(collection: Collection, fieldName: string) {
if (fieldName === 'title' && collection.get('identifier_field')) {
return selectIdentifier(collection);
}
const inferableField = (INFERABLE_FIELDS as Record<
string,
{
type: string;
synonyms: string[];
secondaryTypes: string[];
fallbackToFirstField: boolean;
showError: boolean;
}
>)[fieldName];
const inferableField = (
INFERABLE_FIELDS as Record<
string,
{
type: string;
synonyms: string[];
secondaryTypes: string[];
fallbackToFirstField: boolean;
showError: boolean;
}
>
)[fieldName];
const fields = collection.get('fields');
let field;

View File

@ -105,10 +105,9 @@ function persistSort(sort: Sort | undefined) {
const storageSort: StorageSort = {};
sort.keySeq().forEach(key => {
const collection = key as string;
const sortObjects = (sort
.get(collection)
.valueSeq()
.toJS() as SortObject[]).map((value, index) => ({ ...value, index }));
const sortObjects = (sort.get(collection).valueSeq().toJS() as SortObject[]).map(
(value, index) => ({ ...value, index }),
);
sortObjects.forEach(value => {
set(storageSort, [collection, value.key], value);
@ -333,7 +332,7 @@ function entries(
}
case CHANGE_VIEW_STYLE: {
const payload = (action.payload as unknown) as ChangeViewStylePayload;
const payload = action.payload as unknown as ChangeViewStylePayload;
const { style } = payload;
const newState = state.withMutations(map => {
map.setIn(['viewStyle'], style);
@ -492,10 +491,8 @@ export function selectGroups(state: Entries, collection: Collection) {
return [];
}
let groups: Record<
string,
{ id: string; label: string; value: string | boolean | undefined }
> = {};
let groups: Record<string, { id: string; label: string; value: string | boolean | undefined }> =
{};
const groupedEntries = groupBy(entries.toArray(), entry => {
const group = getGroup(entry, selectedGroup);
groups = { ...groups, [group.id]: group };

View File

@ -262,7 +262,7 @@ export function selectMediaFileByPath(state: State, path: string) {
export function selectMediaDisplayURL(state: State, id: string) {
const displayUrlState = state.mediaLibrary.getIn(
['displayURLs', id],
(Map() as unknown) as DisplayURLState,
Map() as unknown as DisplayURLState,
);
return displayUrlState;
}

View File

@ -7,7 +7,7 @@ import { State } from '../types/redux';
import { Reducer } from 'react';
const store = createStore<State | undefined, AnyAction, unknown, unknown>(
(createRootReducer() as unknown) as Reducer<State | undefined, AnyAction>,
createRootReducer() as unknown as Reducer<State | undefined, AnyAction>,
composeWithDevTools(applyMiddleware(thunkMiddleware as ThunkMiddleware<State>, waitUntilAction)),
);

View File

@ -51,13 +51,14 @@ export const waitUntilAction: Middleware<{}, State, Dispatch> = ({
}
}
return (next: Dispatch<AnyAction>) => (action: AnyAction): null | AnyAction => {
if (action.type === WAIT_UNTIL_ACTION) {
pending.push(action as WaitAction);
return null;
}
const result = next(action);
checkPending(action);
return result;
};
return (next: Dispatch<AnyAction>) =>
(action: AnyAction): null | AnyAction => {
if (action.type === WAIT_UNTIL_ACTION) {
pending.push(action as WaitAction);
return null;
}
const result = next(action);
checkPending(action);
return result;
};
};

View File

@ -3,7 +3,7 @@ import { mocked } from 'ts-jest/utils';
jest.mock('history');
const history = ({ push: jest.fn(), replace: jest.fn() } as unknown) as History;
const history = { push: jest.fn(), replace: jest.fn() } as unknown as History;
const mockedCreateHashHistory = mocked(createHashHistory);
mockedCreateHashHistory.mockReturnValue(history);

View File

@ -11,7 +11,7 @@ export interface StaticallyTypedRecord<T> {
K1 extends keyof T,
K2 extends keyof T[K1],
K3 extends keyof T[K1][K2],
V extends T[K1][K2][K3]
V extends T[K1][K2][K3],
>(
keys: [K1, K2, K3],
defaultValue?: V,

View File

@ -26,10 +26,7 @@ function generateVerifierCode() {
async function createCodeChallenge(codeVerifier) {
const sha = await sha256(codeVerifier);
// https://tools.ietf.org/html/rfc7636#appendix-A
return btoa(sha)
.split('=')[0]
.replace(/\+/g, '-')
.replace(/\//g, '_');
return btoa(sha).split('=')[0].replace(/\+/g, '-').replace(/\//g, '_');
}
const CODE_VERIFIER_STORAGE_KEY = 'netlify-cms-pkce-verifier-code';

View File

@ -1,96 +1,96 @@
import { parseLinkHeader, getAllResponses, getPathDepth, filterByExtension } from '../backendUtil';
import { oneLine } from 'common-tags';
import nock from 'nock';
describe('parseLinkHeader', () => {
it('should return the right rel urls', () => {
const url = 'https://api.github.com/resource';
const link = oneLine`
<${url}?page=1>; rel="first",
<${url}?page=2>; rel="prev",
<${url}?page=4>; rel="next",
<${url}?page=5>; rel="last"
`;
const linkHeader = parseLinkHeader(link);
expect(linkHeader.next).toBe(`${url}?page=4`);
expect(linkHeader.last).toBe(`${url}?page=5`);
expect(linkHeader.first).toBe(`${url}?page=1`);
expect(linkHeader.prev).toBe(`${url}?page=2`);
});
});
describe('getAllResponses', () => {
function generatePulls(length) {
return Array.from({ length }, (_, id) => {
return { id: id + 1, number: `134${id}`, state: 'open' };
});
}
function createLinkHeaders({ page, pageCount }) {
const pageNum = parseInt(page, 10);
const pageCountNum = parseInt(pageCount, 10);
const url = 'https://api.github.com/pulls';
function link(linkPage) {
return `<${url}?page=${linkPage}>`;
}
const linkHeader = oneLine`
${pageNum === 1 ? '' : `${link(1)}; rel="first",`}
${pageNum === pageCountNum ? '' : `${link(pageCount)}; rel="last",`}
${pageNum === 1 ? '' : `${link(pageNum - 1)}; rel="prev",`}
${pageNum === pageCountNum ? '' : `${link(pageNum + 1)}; rel="next",`}
`.slice(0, -1);
return { Link: linkHeader };
}
function interceptCall({ perPage = 30, repeat = 1, data = [] } = {}) {
nock('https://api.github.com')
.get('/pulls')
.query(true)
.times(repeat)
.reply(uri => {
const searchParams = new URLSearchParams(uri.split('?')[1]);
const page = searchParams.get('page') || 1;
const pageCount = data.length <= perPage ? 1 : Math.ceil(data.length / perPage);
const pageLastIndex = page * perPage;
const pageFirstIndex = pageLastIndex - perPage;
const resp = data.slice(pageFirstIndex, pageLastIndex);
return [200, resp, createLinkHeaders({ page, pageCount })];
});
}
it('should return all paged response', async () => {
interceptCall({ repeat: 3, data: generatePulls(70) });
const res = await getAllResponses('https://api.github.com/pulls', {}, 'next', url => url);
const pages = await Promise.all(res.map(res => res.json()));
expect(pages[0]).toHaveLength(30);
expect(pages[1]).toHaveLength(30);
expect(pages[2]).toHaveLength(10);
});
});
describe('getPathDepth', () => {
it('should return 1 for empty string', () => {
expect(getPathDepth('')).toBe(1);
});
it('should return 2 for path of one nested folder', () => {
expect(getPathDepth('{{year}}/{{slug}}')).toBe(2);
});
});
describe('filterByExtension', () => {
it('should return true when extension matches', () => {
expect(filterByExtension({ path: 'file.html.md' }, '.html.md')).toBe(true);
expect(filterByExtension({ path: 'file.html.md' }, 'html.md')).toBe(true);
});
it("should return false when extension doesn't match", () => {
expect(filterByExtension({ path: 'file.json' }, '.html.md')).toBe(false);
expect(filterByExtension({ path: 'file.json' }, 'html.md')).toBe(false);
});
});
import { parseLinkHeader, getAllResponses, getPathDepth, filterByExtension } from '../backendUtil';
import { oneLine } from 'common-tags';
import nock from 'nock';
describe('parseLinkHeader', () => {
it('should return the right rel urls', () => {
const url = 'https://api.github.com/resource';
const link = oneLine`
<${url}?page=1>; rel="first",
<${url}?page=2>; rel="prev",
<${url}?page=4>; rel="next",
<${url}?page=5>; rel="last"
`;
const linkHeader = parseLinkHeader(link);
expect(linkHeader.next).toBe(`${url}?page=4`);
expect(linkHeader.last).toBe(`${url}?page=5`);
expect(linkHeader.first).toBe(`${url}?page=1`);
expect(linkHeader.prev).toBe(`${url}?page=2`);
});
});
describe('getAllResponses', () => {
function generatePulls(length) {
return Array.from({ length }, (_, id) => {
return { id: id + 1, number: `134${id}`, state: 'open' };
});
}
function createLinkHeaders({ page, pageCount }) {
const pageNum = parseInt(page, 10);
const pageCountNum = parseInt(pageCount, 10);
const url = 'https://api.github.com/pulls';
function link(linkPage) {
return `<${url}?page=${linkPage}>`;
}
const linkHeader = oneLine`
${pageNum === 1 ? '' : `${link(1)}; rel="first",`}
${pageNum === pageCountNum ? '' : `${link(pageCount)}; rel="last",`}
${pageNum === 1 ? '' : `${link(pageNum - 1)}; rel="prev",`}
${pageNum === pageCountNum ? '' : `${link(pageNum + 1)}; rel="next",`}
`.slice(0, -1);
return { Link: linkHeader };
}
function interceptCall({ perPage = 30, repeat = 1, data = [] } = {}) {
nock('https://api.github.com')
.get('/pulls')
.query(true)
.times(repeat)
.reply(uri => {
const searchParams = new URLSearchParams(uri.split('?')[1]);
const page = searchParams.get('page') || 1;
const pageCount = data.length <= perPage ? 1 : Math.ceil(data.length / perPage);
const pageLastIndex = page * perPage;
const pageFirstIndex = pageLastIndex - perPage;
const resp = data.slice(pageFirstIndex, pageLastIndex);
return [200, resp, createLinkHeaders({ page, pageCount })];
});
}
it('should return all paged response', async () => {
interceptCall({ repeat: 3, data: generatePulls(70) });
const res = await getAllResponses('https://api.github.com/pulls', {}, 'next', url => url);
const pages = await Promise.all(res.map(res => res.json()));
expect(pages[0]).toHaveLength(30);
expect(pages[1]).toHaveLength(30);
expect(pages[2]).toHaveLength(10);
});
});
describe('getPathDepth', () => {
it('should return 1 for empty string', () => {
expect(getPathDepth('')).toBe(1);
});
it('should return 2 for path of one nested folder', () => {
expect(getPathDepth('{{year}}/{{slug}}')).toBe(2);
});
});
describe('filterByExtension', () => {
it('should return true when extension matches', () => {
expect(filterByExtension({ path: 'file.html.md' }, '.html.md')).toBe(true);
expect(filterByExtension({ path: 'file.html.md' }, 'html.md')).toBe(true);
});
it("should return false when extension doesn't match", () => {
expect(filterByExtension({ path: 'file.json' }, '.html.md')).toBe(false);
expect(filterByExtension({ path: 'file.json' }, 'html.md')).toBe(false);
});
});

View File

@ -3,11 +3,10 @@ import unsentRequest from '../unsentRequest';
describe('unsentRequest', () => {
describe('withHeaders', () => {
it('should create new request with headers', () => {
expect(
unsentRequest
.withHeaders({ Authorization: 'token' })('path')
.toJS(),
).toEqual({ url: 'path', headers: { Authorization: 'token' } });
expect(unsentRequest.withHeaders({ Authorization: 'token' })('path').toJS()).toEqual({
url: 'path',
headers: { Authorization: 'token' },
});
});
it('should add headers to existing request', () => {

View File

@ -7,7 +7,7 @@ export default function loadScript(url) {
const head = document.getElementsByTagName('head')[0];
const script = document.createElement('script');
script.src = url;
script.onload = script.onreadystatechange = function() {
script.onload = script.onreadystatechange = function () {
if (
!done &&
(!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete')

View File

@ -60,13 +60,7 @@ function toURL(req) {
}
function toFetchArguments(req) {
return [
toURL(req),
req
.remove('url')
.remove('params')
.toJS(),
];
return [toURL(req), req.remove('url').remove('params').toJS()];
}
function maybeRequestArg(req) {

View File

@ -601,7 +601,7 @@ describe('joi', () => {
} as express.Request;
const json = jest.fn();
const status = jest.fn(() => ({ json }));
const res: express.Response = ({ status } as unknown) as express.Response;
const res: express.Response = { status } as unknown as express.Response;
joi(defaultSchema())(req, res, next);

View File

@ -132,9 +132,7 @@ export function defaultSchema({ path = requiredString } = {}) {
cmsLabelPrefix: Joi.string().optional(),
entry: dataFile, // entry is kept for backwards compatibility
dataFiles: Joi.array().items(dataFile),
assets: Joi.array()
.items(asset)
.required(),
assets: Joi.array().items(asset).required(),
options: Joi.object({
collectionName: Joi.string(),
commitMessage: requiredString,
@ -207,10 +205,7 @@ export function defaultSchema({ path = requiredString } = {}) {
is: 'deleteFiles',
then: defaultParams
.keys({
paths: Joi.array()
.items(path)
.min(1)
.required(),
paths: Joi.array().items(path).min(1).required(),
options: Joi.object({
commitMessage: requiredString,
}).required(),

View File

@ -24,7 +24,7 @@ type FsOptions = {
};
export function localFsMiddleware({ repoPath, logger }: FsOptions) {
return async function(req: express.Request, res: express.Response) {
return async function (req: express.Request, res: express.Response) {
try {
const { body } = req;

View File

@ -123,7 +123,7 @@ describe('localGitMiddleware', () => {
describe('localGitMiddleware', () => {
const json = jest.fn();
const status = jest.fn(() => ({ json }));
const res: express.Response = ({ status } as unknown) as express.Response;
const res: express.Response = { status } as unknown as express.Response;
const repoPath = '.';

View File

@ -173,7 +173,7 @@ export function localGitMiddleware({ repoPath, logger }: GitOptions) {
// we can only perform a single git operation at any given time
const mutex = withTimeout(new Mutex(), 3000, new Error('Request timed out'));
return async function(req: express.Request, res: express.Response) {
return async function (req: express.Request, res: express.Response) {
let release;
try {
release = await mutex.acquire();
@ -345,12 +345,8 @@ export function localGitMiddleware({ repoPath, logger }: GitOptions) {
break;
}
case 'updateUnpublishedEntryStatus': {
const {
collection,
slug,
newStatus,
cmsLabelPrefix,
} = body.params as UpdateUnpublishedEntryStatusParams;
const { collection, slug, newStatus, cmsLabelPrefix } =
body.params as UpdateUnpublishedEntryStatusParams;
const contentKey = generateContentKey(collection, slug);
const cmsBranch = branchFromContentKey(contentKey);
const description = statusToLabel(newStatus, cmsLabelPrefix || '');

View File

@ -3,10 +3,7 @@ import path from 'path';
import { promises as fs } from 'fs';
function sha256(buffer: Buffer) {
return crypto
.createHash('sha256')
.update(buffer)
.digest('hex');
return crypto.createHash('sha256').update(buffer).digest('hex');
}
// normalize windows os path format

View File

@ -17,14 +17,8 @@ function BooleanBackground({ isActive, ...props }) {
export default class BooleanControl extends React.Component {
render() {
const {
value,
forID,
onChange,
classNameWrapper,
setActiveStyle,
setInactiveStyle,
} = this.props;
const { value, forID, onChange, classNameWrapper, setActiveStyle, setInactiveStyle } =
this.props;
return (
<div className={classNameWrapper}>
<Toggle

View File

@ -111,15 +111,8 @@ export default class ColorControl extends React.Component {
this.props.onChange(formattedColor);
};
render() {
const {
forID,
value,
field,
onChange,
classNameWrapper,
setActiveStyle,
setInactiveStyle,
} = this.props;
const { forID, value, field, onChange, classNameWrapper, setActiveStyle, setInactiveStyle } =
this.props;
const allowInput = field.get('allowInput', false);

View File

@ -20,11 +20,11 @@ function NowButton({ t, handleChange }) {
>
<button
css={css`
${buttons.button}
${buttons.default}
${buttons.button}
${buttons.default}
${buttons.lightBlue}
${buttons.small}
`}
`}
onClick={() => {
handleChange(moment());
}}

View File

@ -40,10 +40,7 @@ describe.skip('slate', () => {
);
function fn(editor) {
editor
.deleteBackward()
.insertText('b')
.setBlocks('heading-one');
editor.deleteBackward().insertText('b').setBlocks('heading-one');
}
const [actual, expected] = run(input, output, fn);

View File

@ -19,16 +19,10 @@ function ForceInsert({ defaultType }) {
forceInsertBeforeNode(editor, node) {
const block = { type: defaultType, object: 'block' };
const parent = editor.value.document.getParent(node.key);
return editor
.insertNodeByKey(parent.key, 0, block)
.moveToStartOfNode(parent)
.focus();
return editor.insertNodeByKey(parent.key, 0, block).moveToStartOfNode(parent).focus();
},
forceInsertAfterNode(editor, node) {
return editor
.moveToEndOfNode(node)
.insertBlock(defaultType)
.focus();
return editor.moveToEndOfNode(node).insertBlock(defaultType).focus();
},
moveToEndOfDocument(editor) {
const lastBlock = editor.value.document.nodes.last();

View File

@ -7,10 +7,7 @@ function LineBreak() {
if (!isShiftEnter) {
return next();
}
return editor
.insertInline('break')
.insertText('')
.moveToStartOfNextText();
return editor.insertInline('break').insertText('').moveToStartOfNextText();
},
};
}

View File

@ -25,24 +25,7 @@ const skips = [
{ number: 507, reason: 'Remark allows a space between link alt and url' },
{
number: [
511,
516,
525,
528,
529,
530,
532,
533,
534,
540,
541,
542,
543,
546,
548,
560,
565,
567,
511, 516, 525, 528, 529, 530, 532, 533, 534, 540, 541, 542, 543, 546, 548, 560, 565, 567,
],
reason: 'we convert link references to standard links, but Remark also fails these',
},
@ -81,7 +64,7 @@ const parse = flow([markdownToSlate, slateToMarkdown]);
* tests, of which we're passing about 300 as of introduction of this suite. To
* work on improving Commonmark support, update __fixtures__/commonmarkExpected.json
*/
describe.skip('Commonmark support', function() {
describe.skip('Commonmark support', function () {
const specs =
onlys.length > 0
? commonmarkSpec.filter(({ number }) => onlys.includes(number))

View File

@ -23,8 +23,7 @@ describe('markdownToSlate', () => {
},
{
object: 'text',
text:
'this_mark, and your charge is but a penny; tothisa penny more; and so on to the full glass—the Cape Horn measure, which you may gulp down for a shilling.\\n\\nUpon entering the place I found a number of young seamen gathered about a table, examining by a dim light divers specimens ofskrimshander',
text: 'this_mark, and your charge is but a penny; tothisa penny more; and so on to the full glass—the Cape Horn measure, which you may gulp down for a shilling.\\n\\nUpon entering the place I found a number of young seamen gathered about a table, examining by a dim light divers specimens ofskrimshander',
marks: [
{
type: 'italic',

View File

@ -3,10 +3,7 @@ import markdownToRemark from 'remark-parse';
import remarkAllowHtmlEntities from '../remarkAllowHtmlEntities';
function process(markdown) {
const mdast = unified()
.use(markdownToRemark)
.use(remarkAllowHtmlEntities)
.parse(markdown);
const mdast = unified().use(markdownToRemark).use(remarkAllowHtmlEntities).parse(markdown);
/**
* The MDAST will look like:

View File

@ -4,9 +4,7 @@ import remarkEscapeMarkdownEntities from '../remarkEscapeMarkdownEntities';
function process(text) {
const tree = u('root', [u('text', text)]);
const escapedMdast = unified()
.use(remarkEscapeMarkdownEntities)
.runSync(tree);
const escapedMdast = unified().use(remarkEscapeMarkdownEntities).runSync(tree);
return escapedMdast.children[0].value;
}

View File

@ -12,10 +12,7 @@ function input(markdown) {
}
function output(markdown) {
return unified()
.use(markdownToRemark)
.use(remarkToMarkdown)
.processSync(markdown).contents;
return unified().use(markdownToRemark).use(remarkToMarkdown).processSync(markdown).contents;
}
describe('remarkPaddedLinks', () => {

View File

@ -4,9 +4,7 @@ import remarkStripTrailingBreaks from '../remarkStripTrailingBreaks';
function process(children) {
const tree = u('root', children);
const strippedMdast = unified()
.use(remarkStripTrailingBreaks)
.runSync(tree);
const strippedMdast = unified().use(remarkStripTrailingBreaks).runSync(tree);
return strippedMdast.children;
}

View File

@ -72,9 +72,7 @@ export function markdownToRemark(markdown) {
/**
* Further transform the MDAST with plugins.
*/
const result = unified()
.use(remarkSquashReferences)
.runSync(parsed);
const result = unified().use(remarkSquashReferences).runSync(parsed);
return result;
}
@ -173,9 +171,7 @@ export function markdownToHtml(markdown, { getAsset, resolveWidget } = {}) {
* pastes.
*/
export function htmlToSlate(html) {
const hast = unified()
.use(htmlToRehype, { fragment: true })
.parse(html);
const hast = unified().use(htmlToRehype, { fragment: true }).parse(html);
const mdast = unified()
.use(rehypePaperEmoji)

View File

@ -233,11 +233,8 @@ export default function slateToRemark(raw, { voidCodeBlock }) {
const node = markNodes[0];
convertedNodes.push(convertInlineNode(node, convertInlineAndTextChildren(node.nodes)));
} else {
const {
leadingWhitespace,
trailingWhitespace,
centerNodes,
} = normalizeFlankingWhitespace(markNodes);
const { leadingWhitespace, trailingWhitespace, centerNodes } =
normalizeFlankingWhitespace(markNodes);
const children = convertInlineAndTextChildren(centerNodes);
const markNode = u(markMap[markType], children);

View File

@ -247,15 +247,8 @@ export default class RelationControl extends React.Component {
}, 500);
render() {
const {
value,
field,
forID,
classNameWrapper,
setActiveStyle,
setInactiveStyle,
queryHits,
} = this.props;
const { value, field, forID, classNameWrapper, setActiveStyle, setInactiveStyle, queryHits } =
this.props;
const isMultiple = this.isMultiple();
const isClearable = !field.get('required', true) || isMultiple;

View File

@ -1,479 +1,479 @@
import React from 'react';
import { fromJS } from 'immutable';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { NetlifyCmsWidgetRelation } from '../';
jest.mock('react-window', () => {
function FixedSizeList(props) {
return props.itemData.options;
}
return {
FixedSizeList,
};
});
const RelationControl = NetlifyCmsWidgetRelation.controlComponent;
const fieldConfig = {
name: 'post',
collection: 'posts',
display_fields: ['title', 'slug'],
search_fields: ['title', 'body'],
value_field: 'title',
};
const customizedOptionsLengthConfig = {
name: 'post',
collection: 'posts',
display_fields: ['title', 'slug'],
search_fields: ['title', 'body'],
value_field: 'title',
options_length: 10,
};
const deeplyNestedFieldConfig = {
name: 'post',
collection: 'posts',
display_fields: ['title', 'slug', 'deeply.nested.post.field'],
search_fields: ['deeply.nested.post.field'],
value_field: 'title',
};
const nestedFieldConfig = {
name: 'post',
collection: 'posts',
display_fields: ['title', 'slug', 'nested.field_1'],
search_fields: ['nested.field_1', 'nested.field_2'],
value_field: 'title',
};
function generateHits(length) {
const hits = Array.from({ length }, (val, idx) => {
const title = `Post # ${idx + 1}`;
const slug = `post-number-${idx + 1}`;
const path = `posts/${slug}.md`;
return { collection: 'posts', data: { title, slug }, slug, path };
});
return [
...hits,
{
collection: 'posts',
data: {
title: 'Deeply nested post',
slug: 'post-deeply-nested',
deeply: {
nested: {
post: {
field: 'Deeply nested field',
},
},
},
},
},
{
collection: 'posts',
data: {
title: 'Nested post',
slug: 'post-nested',
nested: {
field_1: 'Nested field 1',
field_2: 'Nested field 2',
},
},
},
{
collection: 'posts',
data: { title: 'YAML post', slug: 'post-yaml', body: 'Body yaml' },
},
{
collection: 'posts',
data: { title: 'JSON post', slug: 'post-json', body: 'Body json' },
},
];
}
const simpleFileCollectionHits = [{ data: { categories: ['category 1', 'category 2'] } }];
const nestedFileCollectionHits = [
{
data: {
nested: {
categories: [
{
name: 'category 1',
id: 'cat1',
},
{
name: 'category 2',
id: 'cat2',
},
],
},
},
},
];
const numberFieldsHits = [
{
collection: 'posts',
data: {
title: 'post # 1',
slug: 'post-1',
index: 1,
},
},
{
collection: 'posts',
data: {
title: 'post # 2',
slug: 'post-2',
index: 2,
},
},
];
class RelationController extends React.Component {
state = {
value: this.props.value,
queryHits: [],
};
mounted = false;
componentDidMount() {
this.mounted = true;
}
componentWillUnmount() {
this.mounted = false;
}
handleOnChange = jest.fn(value => {
this.setState({ ...this.state, value });
});
setQueryHits = jest.fn(queryHits => {
if (this.mounted) {
this.setState({ ...this.state, queryHits });
}
});
query = jest.fn((...args) => {
const queryHits = generateHits(25);
const [, collection, , term, file, optionsLength] = args;
let hits = queryHits;
if (collection === 'numbers_collection') {
hits = numberFieldsHits;
} else if (file === 'nested_file') {
hits = nestedFileCollectionHits;
} else if (file === 'simple_file') {
hits = simpleFileCollectionHits;
} else if (term === 'JSON post') {
hits = [queryHits[queryHits.length - 1]];
} else if (term === 'YAML' || term === 'YAML post') {
hits = [queryHits[queryHits.length - 2]];
} else if (term === 'Nested') {
hits = [queryHits[queryHits.length - 3]];
} else if (term === 'Deeply nested') {
hits = [queryHits[queryHits.length - 4]];
}
hits = hits.slice(0, optionsLength);
this.setQueryHits(hits);
return Promise.resolve({ payload: { hits } });
});
render() {
return this.props.children({
value: this.state.value,
handleOnChange: this.handleOnChange,
query: this.query,
queryHits: this.state.queryHits,
setQueryHits: this.setQueryHits,
});
}
}
function setup({ field, value }) {
let renderArgs;
const setActiveSpy = jest.fn();
const setInactiveSpy = jest.fn();
const helpers = render(
<RelationController value={value}>
{({ handleOnChange, value, query, queryHits, setQueryHits }) => {
renderArgs = { value, onChangeSpy: handleOnChange, setQueryHitsSpy: setQueryHits };
return (
<RelationControl
field={field}
value={value}
query={query}
queryHits={queryHits}
onChange={handleOnChange}
forID="relation-field"
classNameWrapper=""
setActiveStyle={setActiveSpy}
setInactiveStyle={setInactiveSpy}
/>
);
}}
</RelationController>,
);
const input = helpers.container.querySelector('input');
return {
...helpers,
...renderArgs,
setActiveSpy,
setInactiveSpy,
input,
};
}
describe('Relation widget', () => {
it('should list the first 20 option hits on initial load', async () => {
const field = fromJS(fieldConfig);
const { getAllByText, input } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(getAllByText(/^Post # (\d{1,2}) post-number-\1$/)).toHaveLength(20);
});
});
it('should list the first 10 option hits on initial load', async () => {
const field = fromJS(customizedOptionsLengthConfig);
const { getAllByText, input } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(getAllByText(/^Post # (\d{1,2}) post-number-\1$/)).toHaveLength(10);
});
});
it('should update option list based on search term', async () => {
const field = fromJS(fieldConfig);
const { getAllByText, input } = setup({ field });
fireEvent.change(input, { target: { value: 'YAML' } });
await waitFor(() => {
expect(getAllByText('YAML post post-yaml')).toHaveLength(1);
});
});
it('should call onChange with correct selectedItem value and metadata', async () => {
const field = fromJS(fieldConfig);
const { getByText, input, onChangeSpy } = setup({ field });
const value = 'Post # 1';
const label = 'Post # 1 post-number-1';
const metadata = {
post: { posts: { 'Post # 1': { title: 'Post # 1', slug: 'post-number-1' } } },
};
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
fireEvent.click(getByText(label));
expect(onChangeSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledWith(value, metadata);
});
});
it('should update metadata for initial preview', async () => {
const field = fromJS(fieldConfig);
const value = 'Post # 1';
const { getByText, onChangeSpy, setQueryHitsSpy } = setup({ field, value });
const label = 'Post # 1 post-number-1';
const metadata = {
post: { posts: { 'Post # 1': { title: 'Post # 1', slug: 'post-number-1' } } },
};
setQueryHitsSpy(generateHits(1));
await waitFor(() => {
expect(getByText(label)).toBeInTheDocument();
expect(onChangeSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledWith(value, metadata);
});
});
it('should update option list based on nested search term', async () => {
const field = fromJS(nestedFieldConfig);
const { getAllByText, input } = setup({ field });
fireEvent.change(input, { target: { value: 'Nested' } });
await waitFor(() => {
expect(getAllByText('Nested post post-nested Nested field 1')).toHaveLength(1);
});
});
it('should update option list based on deeply nested search term', async () => {
const field = fromJS(deeplyNestedFieldConfig);
const { getAllByText, input } = setup({ field });
fireEvent.change(input, { target: { value: 'Deeply nested' } });
await waitFor(() => {
expect(
getAllByText('Deeply nested post post-deeply-nested Deeply nested field'),
).toHaveLength(1);
});
});
it('should handle string templates', async () => {
const stringTemplateConfig = {
name: 'post',
collection: 'posts',
display_fields: ['{{slug}}', '{{filename}}', '{{extension}}'],
search_fields: ['slug'],
value_field: '{{slug}}',
};
const field = fromJS(stringTemplateConfig);
const { getByText, input, onChangeSpy } = setup({ field });
const value = 'post-number-1';
const label = 'post-number-1 post-number-1 md';
const metadata = {
post: { posts: { 'post-number-1': { title: 'Post # 1', slug: 'post-number-1' } } },
};
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
fireEvent.click(getByText(label));
expect(onChangeSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledWith(value, metadata);
});
});
it('should default display_fields to value_field', async () => {
const field = fromJS(fieldConfig).delete('display_fields');
const { getAllByText, input } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(getAllByText(/^Post # (\d{1,2})$/)).toHaveLength(20);
});
});
it('should keep number type of referenced field', async () => {
const fieldConfig = {
name: 'numbers',
collection: 'numbers_collection',
value_field: 'index',
search_fields: ['index'],
display_fields: ['title'],
};
const field = fromJS(fieldConfig);
const { getByText, getAllByText, input, onChangeSpy } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(getAllByText(/^post # \d$/)).toHaveLength(2);
});
fireEvent.keyDown(input, { key: 'ArrowDown' });
fireEvent.click(getByText('post # 1'));
fireEvent.keyDown(input, { key: 'ArrowDown' });
fireEvent.click(getByText('post # 2'));
expect(onChangeSpy).toHaveBeenCalledTimes(2);
expect(onChangeSpy).toHaveBeenCalledWith(1, {
numbers: { numbers_collection: { '1': { index: 1, slug: 'post-1', title: 'post # 1' } } },
});
expect(onChangeSpy).toHaveBeenCalledWith(2, {
numbers: { numbers_collection: { '2': { index: 2, slug: 'post-2', title: 'post # 2' } } },
});
});
describe('with multiple', () => {
it('should call onChange with correct selectedItem value and metadata', async () => {
const field = fromJS({ ...fieldConfig, multiple: true });
const { getByText, input, onChangeSpy } = setup({ field });
const metadata1 = {
post: { posts: { 'Post # 1': { title: 'Post # 1', slug: 'post-number-1' } } },
};
const metadata2 = {
post: { posts: { 'Post # 2': { title: 'Post # 2', slug: 'post-number-2' } } },
};
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
fireEvent.click(getByText('Post # 1 post-number-1'));
});
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
fireEvent.click(getByText('Post # 2 post-number-2'));
});
expect(onChangeSpy).toHaveBeenCalledTimes(2);
expect(onChangeSpy).toHaveBeenCalledWith(fromJS(['Post # 1']), metadata1);
expect(onChangeSpy).toHaveBeenCalledWith(fromJS(['Post # 1', 'Post # 2']), metadata2);
});
it('should update metadata for initial preview', async () => {
const field = fromJS({ ...fieldConfig, multiple: true });
const value = fromJS(['YAML post', 'JSON post']);
const { getByText, onChangeSpy } = setup({ field, value });
const metadata = {
post: {
posts: {
'YAML post': { title: 'YAML post', slug: 'post-yaml', body: 'Body yaml' },
'JSON post': { title: 'JSON post', slug: 'post-json', body: 'Body json' },
},
},
};
await waitFor(() => {
expect(getByText('YAML post post-yaml')).toBeInTheDocument();
expect(getByText('JSON post post-json')).toBeInTheDocument();
expect(onChangeSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledWith(value, metadata);
});
});
});
describe('with file collection', () => {
const fileFieldConfig = {
name: 'categories',
collection: 'file',
file: 'simple_file',
value_field: 'categories.*',
display_fields: ['categories.*'],
};
it('should handle simple list', async () => {
const field = fromJS(fileFieldConfig);
const { getAllByText, input, getByText } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(getAllByText(/category/)).toHaveLength(2);
expect(getByText('category 1')).toBeInTheDocument();
expect(getByText('category 2')).toBeInTheDocument();
});
});
it('should handle nested list', async () => {
const field = fromJS({
...fileFieldConfig,
file: 'nested_file',
value_field: 'nested.categories.*.id',
display_fields: ['nested.categories.*.name'],
});
const { getAllByText, input, getByText } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(getAllByText(/category/)).toHaveLength(2);
expect(getByText('category 1')).toBeInTheDocument();
expect(getByText('category 2')).toBeInTheDocument();
});
});
});
});
import React from 'react';
import { fromJS } from 'immutable';
import { render, fireEvent, waitFor } from '@testing-library/react';
import { NetlifyCmsWidgetRelation } from '../';
jest.mock('react-window', () => {
function FixedSizeList(props) {
return props.itemData.options;
}
return {
FixedSizeList,
};
});
const RelationControl = NetlifyCmsWidgetRelation.controlComponent;
const fieldConfig = {
name: 'post',
collection: 'posts',
display_fields: ['title', 'slug'],
search_fields: ['title', 'body'],
value_field: 'title',
};
const customizedOptionsLengthConfig = {
name: 'post',
collection: 'posts',
display_fields: ['title', 'slug'],
search_fields: ['title', 'body'],
value_field: 'title',
options_length: 10,
};
const deeplyNestedFieldConfig = {
name: 'post',
collection: 'posts',
display_fields: ['title', 'slug', 'deeply.nested.post.field'],
search_fields: ['deeply.nested.post.field'],
value_field: 'title',
};
const nestedFieldConfig = {
name: 'post',
collection: 'posts',
display_fields: ['title', 'slug', 'nested.field_1'],
search_fields: ['nested.field_1', 'nested.field_2'],
value_field: 'title',
};
function generateHits(length) {
const hits = Array.from({ length }, (val, idx) => {
const title = `Post # ${idx + 1}`;
const slug = `post-number-${idx + 1}`;
const path = `posts/${slug}.md`;
return { collection: 'posts', data: { title, slug }, slug, path };
});
return [
...hits,
{
collection: 'posts',
data: {
title: 'Deeply nested post',
slug: 'post-deeply-nested',
deeply: {
nested: {
post: {
field: 'Deeply nested field',
},
},
},
},
},
{
collection: 'posts',
data: {
title: 'Nested post',
slug: 'post-nested',
nested: {
field_1: 'Nested field 1',
field_2: 'Nested field 2',
},
},
},
{
collection: 'posts',
data: { title: 'YAML post', slug: 'post-yaml', body: 'Body yaml' },
},
{
collection: 'posts',
data: { title: 'JSON post', slug: 'post-json', body: 'Body json' },
},
];
}
const simpleFileCollectionHits = [{ data: { categories: ['category 1', 'category 2'] } }];
const nestedFileCollectionHits = [
{
data: {
nested: {
categories: [
{
name: 'category 1',
id: 'cat1',
},
{
name: 'category 2',
id: 'cat2',
},
],
},
},
},
];
const numberFieldsHits = [
{
collection: 'posts',
data: {
title: 'post # 1',
slug: 'post-1',
index: 1,
},
},
{
collection: 'posts',
data: {
title: 'post # 2',
slug: 'post-2',
index: 2,
},
},
];
class RelationController extends React.Component {
state = {
value: this.props.value,
queryHits: [],
};
mounted = false;
componentDidMount() {
this.mounted = true;
}
componentWillUnmount() {
this.mounted = false;
}
handleOnChange = jest.fn(value => {
this.setState({ ...this.state, value });
});
setQueryHits = jest.fn(queryHits => {
if (this.mounted) {
this.setState({ ...this.state, queryHits });
}
});
query = jest.fn((...args) => {
const queryHits = generateHits(25);
const [, collection, , term, file, optionsLength] = args;
let hits = queryHits;
if (collection === 'numbers_collection') {
hits = numberFieldsHits;
} else if (file === 'nested_file') {
hits = nestedFileCollectionHits;
} else if (file === 'simple_file') {
hits = simpleFileCollectionHits;
} else if (term === 'JSON post') {
hits = [queryHits[queryHits.length - 1]];
} else if (term === 'YAML' || term === 'YAML post') {
hits = [queryHits[queryHits.length - 2]];
} else if (term === 'Nested') {
hits = [queryHits[queryHits.length - 3]];
} else if (term === 'Deeply nested') {
hits = [queryHits[queryHits.length - 4]];
}
hits = hits.slice(0, optionsLength);
this.setQueryHits(hits);
return Promise.resolve({ payload: { hits } });
});
render() {
return this.props.children({
value: this.state.value,
handleOnChange: this.handleOnChange,
query: this.query,
queryHits: this.state.queryHits,
setQueryHits: this.setQueryHits,
});
}
}
function setup({ field, value }) {
let renderArgs;
const setActiveSpy = jest.fn();
const setInactiveSpy = jest.fn();
const helpers = render(
<RelationController value={value}>
{({ handleOnChange, value, query, queryHits, setQueryHits }) => {
renderArgs = { value, onChangeSpy: handleOnChange, setQueryHitsSpy: setQueryHits };
return (
<RelationControl
field={field}
value={value}
query={query}
queryHits={queryHits}
onChange={handleOnChange}
forID="relation-field"
classNameWrapper=""
setActiveStyle={setActiveSpy}
setInactiveStyle={setInactiveSpy}
/>
);
}}
</RelationController>,
);
const input = helpers.container.querySelector('input');
return {
...helpers,
...renderArgs,
setActiveSpy,
setInactiveSpy,
input,
};
}
describe('Relation widget', () => {
it('should list the first 20 option hits on initial load', async () => {
const field = fromJS(fieldConfig);
const { getAllByText, input } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(getAllByText(/^Post # (\d{1,2}) post-number-\1$/)).toHaveLength(20);
});
});
it('should list the first 10 option hits on initial load', async () => {
const field = fromJS(customizedOptionsLengthConfig);
const { getAllByText, input } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(getAllByText(/^Post # (\d{1,2}) post-number-\1$/)).toHaveLength(10);
});
});
it('should update option list based on search term', async () => {
const field = fromJS(fieldConfig);
const { getAllByText, input } = setup({ field });
fireEvent.change(input, { target: { value: 'YAML' } });
await waitFor(() => {
expect(getAllByText('YAML post post-yaml')).toHaveLength(1);
});
});
it('should call onChange with correct selectedItem value and metadata', async () => {
const field = fromJS(fieldConfig);
const { getByText, input, onChangeSpy } = setup({ field });
const value = 'Post # 1';
const label = 'Post # 1 post-number-1';
const metadata = {
post: { posts: { 'Post # 1': { title: 'Post # 1', slug: 'post-number-1' } } },
};
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
fireEvent.click(getByText(label));
expect(onChangeSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledWith(value, metadata);
});
});
it('should update metadata for initial preview', async () => {
const field = fromJS(fieldConfig);
const value = 'Post # 1';
const { getByText, onChangeSpy, setQueryHitsSpy } = setup({ field, value });
const label = 'Post # 1 post-number-1';
const metadata = {
post: { posts: { 'Post # 1': { title: 'Post # 1', slug: 'post-number-1' } } },
};
setQueryHitsSpy(generateHits(1));
await waitFor(() => {
expect(getByText(label)).toBeInTheDocument();
expect(onChangeSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledWith(value, metadata);
});
});
it('should update option list based on nested search term', async () => {
const field = fromJS(nestedFieldConfig);
const { getAllByText, input } = setup({ field });
fireEvent.change(input, { target: { value: 'Nested' } });
await waitFor(() => {
expect(getAllByText('Nested post post-nested Nested field 1')).toHaveLength(1);
});
});
it('should update option list based on deeply nested search term', async () => {
const field = fromJS(deeplyNestedFieldConfig);
const { getAllByText, input } = setup({ field });
fireEvent.change(input, { target: { value: 'Deeply nested' } });
await waitFor(() => {
expect(
getAllByText('Deeply nested post post-deeply-nested Deeply nested field'),
).toHaveLength(1);
});
});
it('should handle string templates', async () => {
const stringTemplateConfig = {
name: 'post',
collection: 'posts',
display_fields: ['{{slug}}', '{{filename}}', '{{extension}}'],
search_fields: ['slug'],
value_field: '{{slug}}',
};
const field = fromJS(stringTemplateConfig);
const { getByText, input, onChangeSpy } = setup({ field });
const value = 'post-number-1';
const label = 'post-number-1 post-number-1 md';
const metadata = {
post: { posts: { 'post-number-1': { title: 'Post # 1', slug: 'post-number-1' } } },
};
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
fireEvent.click(getByText(label));
expect(onChangeSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledWith(value, metadata);
});
});
it('should default display_fields to value_field', async () => {
const field = fromJS(fieldConfig).delete('display_fields');
const { getAllByText, input } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(getAllByText(/^Post # (\d{1,2})$/)).toHaveLength(20);
});
});
it('should keep number type of referenced field', async () => {
const fieldConfig = {
name: 'numbers',
collection: 'numbers_collection',
value_field: 'index',
search_fields: ['index'],
display_fields: ['title'],
};
const field = fromJS(fieldConfig);
const { getByText, getAllByText, input, onChangeSpy } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(getAllByText(/^post # \d$/)).toHaveLength(2);
});
fireEvent.keyDown(input, { key: 'ArrowDown' });
fireEvent.click(getByText('post # 1'));
fireEvent.keyDown(input, { key: 'ArrowDown' });
fireEvent.click(getByText('post # 2'));
expect(onChangeSpy).toHaveBeenCalledTimes(2);
expect(onChangeSpy).toHaveBeenCalledWith(1, {
numbers: { numbers_collection: { 1: { index: 1, slug: 'post-1', title: 'post # 1' } } },
});
expect(onChangeSpy).toHaveBeenCalledWith(2, {
numbers: { numbers_collection: { 2: { index: 2, slug: 'post-2', title: 'post # 2' } } },
});
});
describe('with multiple', () => {
it('should call onChange with correct selectedItem value and metadata', async () => {
const field = fromJS({ ...fieldConfig, multiple: true });
const { getByText, input, onChangeSpy } = setup({ field });
const metadata1 = {
post: { posts: { 'Post # 1': { title: 'Post # 1', slug: 'post-number-1' } } },
};
const metadata2 = {
post: { posts: { 'Post # 2': { title: 'Post # 2', slug: 'post-number-2' } } },
};
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
fireEvent.click(getByText('Post # 1 post-number-1'));
});
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
fireEvent.click(getByText('Post # 2 post-number-2'));
});
expect(onChangeSpy).toHaveBeenCalledTimes(2);
expect(onChangeSpy).toHaveBeenCalledWith(fromJS(['Post # 1']), metadata1);
expect(onChangeSpy).toHaveBeenCalledWith(fromJS(['Post # 1', 'Post # 2']), metadata2);
});
it('should update metadata for initial preview', async () => {
const field = fromJS({ ...fieldConfig, multiple: true });
const value = fromJS(['YAML post', 'JSON post']);
const { getByText, onChangeSpy } = setup({ field, value });
const metadata = {
post: {
posts: {
'YAML post': { title: 'YAML post', slug: 'post-yaml', body: 'Body yaml' },
'JSON post': { title: 'JSON post', slug: 'post-json', body: 'Body json' },
},
},
};
await waitFor(() => {
expect(getByText('YAML post post-yaml')).toBeInTheDocument();
expect(getByText('JSON post post-json')).toBeInTheDocument();
expect(onChangeSpy).toHaveBeenCalledTimes(1);
expect(onChangeSpy).toHaveBeenCalledWith(value, metadata);
});
});
});
describe('with file collection', () => {
const fileFieldConfig = {
name: 'categories',
collection: 'file',
file: 'simple_file',
value_field: 'categories.*',
display_fields: ['categories.*'],
};
it('should handle simple list', async () => {
const field = fromJS(fileFieldConfig);
const { getAllByText, input, getByText } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(getAllByText(/category/)).toHaveLength(2);
expect(getByText('category 1')).toBeInTheDocument();
expect(getByText('category 2')).toBeInTheDocument();
});
});
it('should handle nested list', async () => {
const field = fromJS({
...fileFieldConfig,
file: 'nested_file',
value_field: 'nested.categories.*.id',
display_fields: ['nested.categories.*.name'],
});
const { getAllByText, input, getByText } = setup({ field });
fireEvent.keyDown(input, { key: 'ArrowDown' });
await waitFor(() => {
expect(getAllByText(/category/)).toHaveLength(2);
expect(getByText('category 1')).toBeInTheDocument();
expect(getByText('category 2')).toBeInTheDocument();
});
});
});
});

View File

@ -28,14 +28,8 @@ export default class TextControl extends React.Component {
}
render() {
const {
forID,
value,
onChange,
classNameWrapper,
setActiveStyle,
setInactiveStyle,
} = this.props;
const { forID, value, onChange, classNameWrapper, setActiveStyle, setInactiveStyle } =
this.props;
return (
<Textarea

View File

@ -57,7 +57,7 @@ module.exports = {
noInlineHighlight: true,
},
},
]
],
},
},
'gatsby-transformer-yaml',

View File

@ -9,15 +9,8 @@ import BlogPostTemplate from '../components/blog-post-template';
function BlogPost({ data }) {
const { html, frontmatter } = data.markdownRemark;
const {
author,
title,
date,
description,
meta_description,
twitter_image,
canonical_url,
} = frontmatter;
const { author, title, date, description, meta_description, twitter_image, canonical_url } =
frontmatter;
const { siteUrl } = data.site.siteMetadata;
const twitterImageUrl =
twitter_image && `${trimEnd(siteUrl, '/')}/${trimStart(twitter_image, '/')}`;

View File

@ -7,10 +7,7 @@ import Layout from '../components/layout';
import DocsTemplate from '../components/docs-template';
function filenameFromPath(p) {
return p
.split('/')
.slice(-1)[0]
.split('.')[0];
return p.split('/').slice(-1)[0].split('.')[0];
}
function toMenu(menu, nav) {

View File

@ -14244,10 +14244,10 @@ prettier-linter-helpers@^1.0.0:
dependencies:
fast-diff "^1.1.2"
prettier@^1.19.1:
version "1.19.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.19.1.tgz#f7d7f5ff8a9cd872a7be4ca142095956a60797cb"
integrity sha512-s7PoyDv/II1ObgQunCbB9PdLmUcBZcnWOcxDh7O0N/UwDEsHyqkW+Qh28jW+mVuCdx7gLB0BotYI1Y6uI9iyew==
prettier@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.3.0.tgz#b6a5bf1284026ae640f17f7ff5658a7567fc0d18"
integrity sha512-kXtO4s0Lz/DW/IJ9QdWhAf7/NmPWQXkFr/r/WkR3vyI+0v8amTDxiaQSLzs8NBlytfLWX/7uQUMIW677yLKl4w==
pretty-bytes@^5.6.0:
version "5.6.0"