Skip to content

Commit

Permalink
fix: tests and types
Browse files Browse the repository at this point in the history
  • Loading branch information
KaneFreeman committed Oct 26, 2023
1 parent b9504fb commit d6fc0c5
Show file tree
Hide file tree
Showing 105 changed files with 1,023 additions and 713 deletions.
8 changes: 4 additions & 4 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
"build": "cross-env NODE_ENV=production run-s clean build:webpack build:types",
"clean": "rimraf dist dev-test/dist",
"dev": "run-s clean serve",
"format:prettier": "prettier \"src/**/*.{js,jsx,ts,tsx,css}\" --write",
"format:prettier": "prettier \"{src,test}/**/*.{js,jsx,ts,tsx,css}\" --write",
"format": "run-s \"lint:js --fix --quiet\" \"format:prettier\"",
"lint-quiet": "run-p -c --aggregate-output \"lint:* --quiet\"",
"lint:format": "prettier \"src/**/*.{js,jsx,ts,tsx,css}\" --list-different",
"lint:js": "eslint --color \"src/**/*.{ts,tsx}\"",
"lint:format": "prettier \"{src,test}/**/*.{js,jsx,ts,tsx,css}\" --list-different",
"lint:js": "eslint --color \"{src,test}/**/*.{ts,tsx}\" --max-warnings=0",
"lint": "run-p -c --aggregate-output \"lint:*\"",
"prepublishOnly": "yarn build ",
"prepack": "cp ../../README.md ./",
Expand All @@ -30,7 +30,7 @@
"test:integration": "cross-env NODE_ENV=test jest -c jest.config.integration.js",
"test:ci": "cross-env NODE_ENV=test jest --maxWorkers=2 --coverage",
"test:integration:ci": "cross-env NODE_ENV=test jest -c jest.config.integration.js --maxWorkers=2",
"type-check": "tsc --watch"
"type-check": "tsc --watch --project tsconfig.dev.json"
},
"main": "dist/static-cms-core.js",
"types": "dist/index.d.ts",
Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/__mocks__/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ const {
mergeExpandedEntries: actualMergeExpandedEntries,
} = jest.requireActual('@staticcms/core/backend');

const isGitBackend = jest.fn().mockReturnValue(true);

export const resolveBackend = jest.fn().mockReturnValue({
isGitBackend,
});
export const resolveBackend = jest.fn().mockReturnValue({});

export const currentBackend = jest.fn();

Expand Down
69 changes: 37 additions & 32 deletions packages/core/src/__tests__/backend.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createMockFolderCollection } from '@staticcms/test/data/collections.mock';
import { createMockConfig } from '@staticcms/test/data/config.mock';
import { createMockFolderCollectionWithDefaults } from '@staticcms/test/data/collections.mock';
import { createMockConfig, createMockConfigWithDefaults } from '@staticcms/test/data/config.mock';
import {
createMockEntry,
createMockExpandedEntry,
Expand All @@ -18,7 +18,12 @@ import { sanitizeChar, sanitizeSlug } from '../lib/urlHelper';
import { asyncLock } from '../lib/util/asyncLock';
import localForage from '../lib/util/localForage';

import type { BackendClass, BackendInitializer, Collection, Config } from '../interface';
import type {
BackendClass,
BackendInitializer,
CollectionWithDefaults,
ConfigWithDefaults,
} from '../interface';
import type { RootState } from '../store';
import type { AssetProxy } from '../valueObjects';

Expand All @@ -30,14 +35,14 @@ jest.mock('../lib/urlHelper');
describe('Backend', () => {
describe('filterEntries', () => {
let backend: Backend;
let collection: Collection;
let collection: CollectionWithDefaults;

beforeEach(() => {
(getBackend as jest.Mock).mockReturnValue({
init: jest.fn(),
});

collection = createMockFolderCollection();
collection = createMockFolderCollectionWithDefaults();

backend = resolveBackend(
createMockConfig({
Expand Down Expand Up @@ -154,9 +159,9 @@ describe('Backend', () => {

(getBackend as jest.Mock).mockReturnValue(initializer);

const collection = createMockFolderCollection({
const collection = createMockFolderCollectionWithDefaults({
name: 'posts',
}) as Collection;
}) as CollectionWithDefaults;

const backend = new Backend(initializer, {
config: createMockConfig({ collections: [collection] }),
Expand All @@ -183,9 +188,9 @@ describe('Backend', () => {

(getBackend as jest.Mock).mockReturnValue(initializer);

const collection = createMockFolderCollection({
const collection = createMockFolderCollectionWithDefaults({
name: 'posts',
}) as Collection;
}) as CollectionWithDefaults;

const backend = new Backend(initializer, {
config: createMockConfig({ collections: [collection] }),
Expand All @@ -212,10 +217,10 @@ describe('Backend', () => {

(getBackend as jest.Mock).mockReturnValue(initializer);

const collection = createMockFolderCollection({
const collection = createMockFolderCollectionWithDefaults({
name: 'posts',
format: 'json-frontmatter',
}) as Collection;
}) as CollectionWithDefaults;

const backend = new Backend(initializer, {
config: createMockConfig({ collections: [collection] }),
Expand Down Expand Up @@ -261,10 +266,10 @@ describe('Backend', () => {

(getBackend as jest.Mock).mockReturnValue(initializer);

const collection = createMockFolderCollection({
const collection = createMockFolderCollectionWithDefaults({
name: 'posts',
format: 'json-frontmatter',
}) as Collection;
}) as CollectionWithDefaults;

const backend = new Backend(initializer, {
config: createMockConfig({ collections: [collection] }),
Expand Down Expand Up @@ -317,10 +322,10 @@ describe('Backend', () => {

(getBackend as jest.Mock).mockReturnValue(initializer);

const collection = createMockFolderCollection({
const collection = createMockFolderCollectionWithDefaults({
name: 'posts',
format: 'json-frontmatter',
}) as Collection;
}) as CollectionWithDefaults;

const backend = new Backend(initializer, {
config: createMockConfig({ collections: [collection] }),
Expand Down Expand Up @@ -354,10 +359,10 @@ describe('Backend', () => {

(getBackend as jest.Mock).mockReturnValue(initializer);

const collection = createMockFolderCollection({
const collection = createMockFolderCollectionWithDefaults({
name: 'posts',
format: 'json-frontmatter',
}) as Collection;
}) as CollectionWithDefaults;

const backend = new Backend(initializer, {
config: createMockConfig({ collections: [collection] }),
Expand Down Expand Up @@ -405,9 +410,9 @@ describe('Backend', () => {

(getBackend as jest.Mock).mockReturnValue(initializer);

const collection = createMockFolderCollection({
const collection = createMockFolderCollectionWithDefaults({
name: 'posts',
}) as Collection;
}) as CollectionWithDefaults;

const config = createMockConfig({ backend: { name: 'github' }, collections: [collection] });

Expand Down Expand Up @@ -448,11 +453,11 @@ describe('Backend', () => {

(getBackend as jest.Mock).mockReturnValue(initializer);

const collection = createMockFolderCollection({
const collection = createMockFolderCollectionWithDefaults({
name: 'posts',
folder: 'src/posts',
fields: [],
}) as Collection;
}) as CollectionWithDefaults;

const config = createMockConfig({
media_folder: 'static/images',
Expand Down Expand Up @@ -507,7 +512,7 @@ describe('Backend', () => {

(getBackend as jest.Mock).mockReturnValue(initializer);

const collection = createMockFolderCollection({
const collection = createMockFolderCollectionWithDefaults({
name: 'posts',
fields: [
{
Expand All @@ -518,7 +523,7 @@ describe('Backend', () => {
folder: 'posts',
slug: '{{slug}}',
path: 'sub_dir/{{slug}}',
}) as unknown as Collection;
}) as unknown as CollectionWithDefaults;

const entry = createMockEntry({
data: {
Expand Down Expand Up @@ -554,7 +559,7 @@ describe('Backend', () => {

(implementation.getEntry as jest.Mock).mockResolvedValueOnce({ data: 'data' });

const collection = createMockFolderCollection({
const collection = createMockFolderCollectionWithDefaults({
name: 'posts',
fields: [
{
Expand All @@ -565,7 +570,7 @@ describe('Backend', () => {
folder: 'posts',
slug: '{{slug}}',
path: 'sub_dir/{{slug}}',
}) as unknown as Collection;
}) as unknown as CollectionWithDefaults;

const entry = createMockEntry({
data: {
Expand Down Expand Up @@ -615,7 +620,7 @@ describe('Backend', () => {

describe('search/query', () => {
const collections = [
createMockFolderCollection({
createMockFolderCollectionWithDefaults({
name: 'posts',
folder: 'posts',
fields: [
Expand All @@ -626,7 +631,7 @@ describe('Backend', () => {
{ name: 'nested', widget: 'object', fields: [{ name: 'title', widget: 'string' }] },
],
}),
createMockFolderCollection({
createMockFolderCollectionWithDefaults({
name: 'pages',
folder: 'pages',
fields: [
Expand All @@ -637,7 +642,7 @@ describe('Backend', () => {
{ name: 'nested', widget: 'object', fields: [{ name: 'title', widget: 'string' }] },
],
}),
] as unknown as Collection[];
] as unknown as CollectionWithDefaults[];

const posts = [
createMockEntry({
Expand Down Expand Up @@ -686,7 +691,7 @@ describe('Backend', () => {
}),
];

let config: Config;
let config: ConfigWithDefaults;

const implementation = {
listAllEntries: jest.fn(),
Expand All @@ -702,7 +707,7 @@ describe('Backend', () => {

(getBackend as jest.Mock).mockReturnValue(initializer);

config = createMockConfig({ collections });
config = createMockConfigWithDefaults({ collections });

backend = new Backend(initializer, { config, backendName: 'github' });
(backend.listAllEntries as jest.Mock) = jest.fn(collection => {
Expand Down Expand Up @@ -760,7 +765,7 @@ describe('Backend', () => {

it('should search in file collection using top level fields', async () => {
const collections = [
createMockFolderCollection({
createMockFolderCollectionWithDefaults({
name: 'files',
files: [
{
Expand All @@ -777,7 +782,7 @@ describe('Backend', () => {
},
],
}),
] as unknown as Collection[];
] as unknown as CollectionWithDefaults[];

expect(await backend.search(collections, 'find me by author')).toEqual({
entries: [files[0]],
Expand Down
34 changes: 24 additions & 10 deletions packages/core/src/actions/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1158,11 +1158,18 @@ describe('config', () => {
expect(dispatch).toHaveBeenCalledWith({
type: 'CONFIG_SUCCESS',
payload: {
backend: { repo: 'test-repo' },
collections: [],
publish_mode: 'simple',
slug: { encoding: 'unicode', clean_accents: false, sanitize_replacement: '-' },
public_folder: '/',
config: {
backend: { repo: 'test-repo' },
collections: [],
publish_mode: 'simple',
slug: { encoding: 'unicode', clean_accents: false, sanitize_replacement: '-' },
public_folder: '/',
},
originalConfig: {
backend: {
repo: 'test-repo',
},
},
},
});
});
Expand Down Expand Up @@ -1221,11 +1228,18 @@ describe('config', () => {
expect(dispatch).toHaveBeenNthCalledWith(2, {
type: 'CONFIG_SUCCESS',
payload: {
backend: { repo: 'github' },
collections: [],
publish_mode: 'simple',
slug: { encoding: 'unicode', clean_accents: false, sanitize_replacement: '-' },
public_folder: '/',
config: {
backend: { repo: 'github' },
collections: [],
publish_mode: 'simple',
slug: { encoding: 'unicode', clean_accents: false, sanitize_replacement: '-' },
public_folder: '/',
},
originalConfig: {
backend: {
repo: 'github',
},
},
},
});
});
Expand Down
Loading

0 comments on commit d6fc0c5

Please sign in to comment.