static-cms/packages/core/test/data/widgets.mock.ts

77 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2023-03-30 13:29:09 -04:00
/* eslint-disable import/prefer-default-export */
import { applyDefaults } from '@staticcms/core/actions/config';
import { createMockFolderCollection } from './collections.mock';
2023-03-30 13:29:09 -04:00
import { createMockConfig } from './config.mock';
import { createMockEntry } from './entry.mock';
2023-09-06 16:30:51 -04:00
import type { BaseField, UnknownField, WidgetControlProps } from '@staticcms/core';
2023-03-30 13:29:09 -04:00
2023-05-10 09:09:36 -04:00
jest.mock('@staticcms/core/backend');
2023-09-06 16:30:51 -04:00
export const createMockWidgetControlProps = <T, F extends BaseField = UnknownField>(
2023-03-30 13:29:09 -04:00
options: Omit<
Partial<WidgetControlProps<T, F>>,
| 'field'
| 'data'
| 'hasErrors'
| 'onChange'
| 'openMediaLibrary'
| 'removeInsertedMedia'
| 'query'
| 't'
> &
Pick<WidgetControlProps<T, F>, 'field'>,
): WidgetControlProps<T, F> => {
const {
value: rawValue,
path: rawPath,
errors: rawErrors,
fieldsErrors: rawFieldsErrors,
collection: rawCollection,
config: rawConfig,
entry: rawEntry,
...extra
} = options;
const value = rawValue ?? null;
const collection = rawCollection ?? createMockFolderCollection({}, options.field);
2023-03-30 13:29:09 -04:00
const config = rawConfig ?? createMockConfig({ collections: [collection] });
const entry = rawEntry ?? createMockEntry({ data: { [options.field.name]: value } });
const path = rawPath ?? '';
const errors = rawErrors ?? [];
const fieldsErrors =
rawFieldsErrors ?? (rawErrors ? { [`${path}.${options.field.name}`]: errors } : {});
const hasErrors = Boolean(rawErrors && rawErrors.length > 0);
const configWithDefaults = applyDefaults(config);
2023-03-30 13:29:09 -04:00
return {
label: 'Mock Widget',
config: configWithDefaults,
collection: configWithDefaults.collections[0],
2023-04-13 13:27:13 -04:00
collectionFile: undefined,
2023-03-30 13:29:09 -04:00
entry,
value,
path,
fieldsErrors,
errors,
hasErrors,
submitted: false,
forList: false,
listItemPath: undefined,
2023-03-30 13:29:09 -04:00
forSingleList: false,
disabled: false,
locale: undefined,
i18n: undefined,
duplicate: false,
2023-04-17 14:32:14 -04:00
controlled: false,
2023-03-30 13:29:09 -04:00
onChange: jest.fn(),
clearChildValidation: jest.fn(),
2023-03-30 13:29:09 -04:00
query: jest.fn(),
t: jest.fn(),
...extra,
};
};