Skip to content

Commit

Permalink
fix: add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barthc committed Mar 3, 2020
1 parent 6b6d25f commit 4a1dc92
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 46 deletions.
5 changes: 1 addition & 4 deletions packages/netlify-cms-backend-github/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,7 @@ export default class API {
let entries = diffs.filter(d => d.patch && !d.filename.endsWith('.svg'));
let mediaFiles = difference(diffs, entries);
entries = entries.map(e => ({ path: e.filename, newFile: e.status === 'added' }));
mediaFiles = mediaFiles.map(({ filename: path, sha: id }) => ({
path,
id,
}));
mediaFiles = mediaFiles.map(e => ({ path: e.filename, id: e.sha }));
const label = pullRequest.labels.find(l => isCMSLabel(l.name)) as { name: string };
const status = labelToStatus(label.name);
return { branch, collection, slug, status, entries, mediaFiles, pullRequest };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ describe('github API', () => {
},
];

await api.persistFiles(entry, mediaFiles, { useWorkflow: true });
await api.persistFiles([entry], mediaFiles, { useWorkflow: true });

expect(api.uploadBlob).toHaveBeenCalledTimes(3);
expect(api.uploadBlob).toHaveBeenCalledWith(entry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ describe('github backend implementation', () => {
describe('unpublishedEntry', () => {
const generateContentKey = jest.fn();
const readUnpublishedBranchFile = jest.fn();
const loadEntryMediaFiles = jest.fn();

const mockAPI = {
generateContentKey,
Expand All @@ -174,44 +175,34 @@ describe('github backend implementation', () => {
it('should return unpublished entry', async () => {
const gitHubImplementation = new GitHubImplementation(config);
gitHubImplementation.api = mockAPI;
gitHubImplementation.loadEntryMediaFiles = jest
.fn()
.mockResolvedValue([{ path: 'image.png', id: 'sha' }]);

generateContentKey.mockReturnValue('contentKey');

const data = {
fileData: 'fileData',
slug: 'slug',
file: { path: 'entry-path', id: null },
data: 'fileData',
isModification: true,
metaData: {
branch: 'branch',
objects: {
entry: { path: 'entry-path', mediaFiles: [{ path: 'image.png', id: 'sha' }] },
},
},
mediaFiles: [{ path: 'image.png', id: 'sha' }],
};
readUnpublishedBranchFile.mockResolvedValue(data);

const collection = 'posts';
await expect(gitHubImplementation.unpublishedEntry(collection, 'slug')).resolves.toEqual({
slug: 'slug',
file: { path: 'entry-path', id: null },
data: 'fileData',
metaData: data.metaData,
mediaFiles: [{ path: 'image.png', id: 'sha' }],
isModification: true,
});
await expect(
gitHubImplementation.unpublishedEntry(collection, 'slug', { loadEntryMediaFiles }),
);

expect(generateContentKey).toHaveBeenCalledTimes(1);
expect(generateContentKey).toHaveBeenCalledWith('posts', 'slug');

expect(readUnpublishedBranchFile).toHaveBeenCalledTimes(1);
expect(readUnpublishedBranchFile).toHaveBeenCalledWith('contentKey');

expect(gitHubImplementation.loadEntryMediaFiles).toHaveBeenCalledTimes(1);
expect(gitHubImplementation.loadEntryMediaFiles).toHaveBeenCalledWith('branch', [
{ path: 'image.png', id: 'sha' },
]);
expect(readUnpublishedBranchFile).toHaveBeenCalledWith('contentKey', loadEntryMediaFiles);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('test backend implementation', () => {
const backend = new TestBackend();

const entry = { path: 'posts/some-post.md', raw: 'content', slug: 'some-post.md' };
await backend.persistEntry(entry, [], { newEntry: true });
await backend.persistEntry([entry], [], { newEntry: true });

expect(window.repoFiles).toEqual({
posts: {
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('test backend implementation', () => {
const backend = new TestBackend();

const entry = { path: 'posts/new-post.md', raw: 'content', slug: 'new-post.md' };
await backend.persistEntry(entry, [], { newEntry: true });
await backend.persistEntry([entry], [], { newEntry: true });

expect(window.repoFiles).toEqual({
pages: {
Expand All @@ -107,7 +107,7 @@ describe('test backend implementation', () => {
const slug = 'dir1/dir2/some-post.md';
const path = `posts/${slug}`;
const entry = { path, raw: 'content', slug };
await backend.persistEntry(entry, [], { newEntry: true });
await backend.persistEntry([entry], [], { newEntry: true });

expect(window.repoFiles).toEqual({
posts: {
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('test backend implementation', () => {
const slug = 'dir1/dir2/some-post.md';
const path = `posts/${slug}`;
const entry = { path, raw: 'new content', slug };
await backend.persistEntry(entry, [], { newEntry: false });
await backend.persistEntry([entry], [], { newEntry: false });

expect(window.repoFiles).toEqual({
posts: {
Expand Down
121 changes: 121 additions & 0 deletions packages/netlify-cms-core/src/__tests__/backend.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,125 @@ describe('Backend', () => {
);
});
});

describe('combineMultiContentEntries', () => {
const implementation = {
init: jest.fn(() => implementation),
};
const config = Map({});
const backend = new Backend(implementation, { config, backendName: 'github' });

it('should combine mutiple content same folder entries', () => {
const entries = [
{ path: 'posts/post.en.md', data: { title: 'Title en', content: 'Content en' } },
{ path: 'posts/post.fr.md', data: { title: 'Title fr', content: 'Content fr' } },
];
const collection = fromJS({ multi_content: 'same_folder' });

expect(backend.combineMultiContentEntries(entries, collection)).toEqual([
{
path: 'posts/post.md',
raw: '',
data: {
en: { title: 'Title en', content: 'Content en' },
fr: { title: 'Title fr', content: 'Content fr' },
},
},
]);
});

it('should combine mutiple content different folder entries', () => {
const entries = [
{ path: 'posts/en/post.md', data: { title: 'Title en', content: 'Content en' } },
{ path: 'posts/fr/post.md', data: { title: 'Title fr', content: 'Content fr' } },
];
const collection = fromJS({ multi_content: 'diff_folder' });

expect(backend.combineMultiContentEntries(entries, collection)).toEqual([
{
path: 'posts/post.md',
raw: '',
data: {
en: { title: 'Title en', content: 'Content en' },
fr: { title: 'Title fr', content: 'Content fr' },
},
},
]);
});
});

describe('listAllMultipleEntires', () => {
const implementation = {
init: jest.fn(() => implementation),
};
const config = Map({});
const backend = new Backend(implementation, { config, backendName: 'github' });
const locales = fromJS(['en', 'fr']);

it('should combine mutiple content same folder entries', async () => {
const entries = [
{
slug: 'post.en',
path: 'posts/post.en.md',
data: { title: 'Title en', content: 'Content en' },
},
{
slug: 'post.fr',
path: 'posts/post.fr.md',
data: { title: 'Title fr', content: 'Content fr' },
},
];
const collection = fromJS({ multi_content: 'same_folder' });

backend.listAllEntries = jest.fn().mockResolvedValue(entries);

await expect(backend.listAllMultipleEntires(collection, '', locales)).resolves.toEqual({
entries: [
{
slug: 'post',
path: 'posts/post.md',
raw: '',
data: {
en: { title: 'Title en', content: 'Content en' },
fr: { title: 'Title fr', content: 'Content fr' },
},
multiContentKey: 'posts/post',
},
],
});
});

it('should combine mutiple content different folder entries', async () => {
const entries = [
{
slug: 'en/post',
path: 'posts/en/post.md',
data: { title: 'Title en', content: 'Content en' },
},
{
slug: 'fr/post',
path: 'posts/fr/post.md',
data: { title: 'Title fr', content: 'Content fr' },
},
];
const collection = fromJS({ multi_content: 'diff_folder' });

backend.listAllEntries = jest.fn().mockResolvedValue(entries);

await expect(backend.listAllMultipleEntires(collection, '', locales)).resolves.toEqual({
entries: [
{
slug: 'post',
path: 'posts/post.md',
raw: '',
data: {
en: { title: 'Title en', content: 'Content en' },
fr: { title: 'Title fr', content: 'Content fr' },
},
multiContentKey: 'posts/post.md',
},
],
});
});
});
});
35 changes: 34 additions & 1 deletion packages/netlify-cms-core/src/actions/__tests__/config.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fromJS } from 'immutable';
import { applyDefaults, detectProxyServer, handleLocalBackend } from '../config';
import { applyDefaults, detectProxyServer, handleLocalBackend, addLocaleFields } from '../config';

jest.spyOn(console, 'log').mockImplementation(() => {});

Expand Down Expand Up @@ -311,4 +311,37 @@ describe('config', () => {
);
});
});

describe('addLocaleFields', () => {
it('should add locale fields', () => {
const fields = [
{ name: 'title', widget: 'string' },
{ name: 'content', widget: 'markdown' },
];
const actual = addLocaleFields(fields, ['en', 'fr']);

expect(actual).toEqual([
{
label: 'en',
name: 'en',
widget: 'object',
multiContentId: Symbol.for('multiContentId'),
fields: [
{ name: 'title', widget: 'string' },
{ name: 'content', widget: 'markdown' },
],
},
{
label: 'fr',
name: 'fr',
widget: 'object',
multiContentId: Symbol.for('multiContentId'),
fields: [
{ name: 'title', widget: 'string' },
{ name: 'content', widget: 'markdown' },
],
},
]);
});
});
});
22 changes: 11 additions & 11 deletions packages/netlify-cms-core/src/actions/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,20 @@ export function applyDefaults(config) {
map.setIn(['slug', 'sanitize_replacement'], '-');
}

const langs = map.get('locales');
const locales = map.get('locales');
// Strip leading slash from collection folders and files
map.set(
'collections',
map.get('collections').map(collection => {
const folder = collection.get('folder');
if (folder) {
if (collection.has('media_folder') && !collection.has('public_folder')) {
collection = collection.set('public_folder', collection.get('media_folder'));
}

const fields = collection.get('fields');
const identifier_field = selectIdentifier(collection);
if (langs && fields && collection.get('multi_content')) {
// add languague fields
if (locales && fields && collection.get('multi_content')) {
// add locale fields
collection = collection.set(
'fields',
fromJS(addLanguageFields(fields.toJS(), langs.toJS())),
fromJS(addLocaleFields(fields.toJS(), locales.toJS())),
);

// remove path for same or different folder config
Expand All @@ -83,7 +79,7 @@ export function applyDefaults(config) {
// add identifier config
collection = collection.set(
'identifier_field',
`${langs.first()}.${identifier_field}`,
`${locales.first()}.${identifier_field}`,
);
}

Expand All @@ -92,6 +88,10 @@ export function applyDefaults(config) {
collection = collection.set('media_folder', '');
}

if (collection.has('media_folder') && !collection.has('public_folder')) {
collection = collection.set('public_folder', collection.get('media_folder'));
}

return collection.set('folder', trimStart(folder, '/'));
}

Expand All @@ -112,8 +112,8 @@ export function applyDefaults(config) {
});
}

export function addLanguageFields(fields, langs) {
return langs.reduce((acc, item) => {
export function addLocaleFields(fields, locales) {
return locales.reduce((acc, item) => {
return [
...acc,
{
Expand Down
4 changes: 1 addition & 3 deletions packages/netlify-cms-core/src/actions/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,12 +712,10 @@ export function deleteEntry(collection: Collection, slug: string) {
return (dispatch: ThunkDispatch<State, {}, AnyAction>, getState: () => State) => {
const state = getState();
const backend = currentBackend(state.config);
const locales = state.config.get('locales');
const multiContent = DIFF_FILE_TYPES.includes(collection.get('multi_content'));

dispatch(entryDeleting(collection, slug));
return backend
.deleteEntry(state, collection, slug, locales, multiContent)
.deleteEntry(state, collection, slug)
.then(() => {
return dispatch(entryDeleted(collection, slug));
})
Expand Down
Loading

0 comments on commit 4a1dc92

Please sign in to comment.