Skip to content

Commit

Permalink
fix: add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
barthc committed Mar 16, 2020
1 parent e0d43a6 commit 23744e5
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 96 deletions.
89 changes: 78 additions & 11 deletions packages/netlify-cms-core/src/__tests__/backend.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Map, List, fromJS } from 'immutable';

jest.mock('Lib/registry');
jest.mock('netlify-cms-lib-util');
jest.mock('Formats/formats');
//jest.mock('Formats/formats');
jest.mock('../lib/urlHelper');

describe('Backend', () => {
Expand Down Expand Up @@ -179,7 +179,7 @@ describe('Backend', () => {
const slug = 'slug';

localForage.getItem.mockReturnValue({
raw: 'content',
raw: '---\ncontent: Post content',
});

const result = await backend.getLocalDraftBackup(collection, slug);
Expand All @@ -191,8 +191,8 @@ describe('Backend', () => {
slug: 'slug',
path: '',
partial: false,
raw: 'content',
data: {},
raw: '---\ncontent: Post content',
data: { content: 'Post content' },
label: null,
metaData: null,
isModification: null,
Expand All @@ -216,7 +216,7 @@ describe('Backend', () => {
const slug = 'slug';

localForage.getItem.mockReturnValue({
raw: 'content',
raw: '---\ncontent: Post content',
mediaFiles: [{ id: '1' }],
});

Expand All @@ -229,8 +229,8 @@ describe('Backend', () => {
slug: 'slug',
path: '',
partial: false,
raw: 'content',
data: {},
raw: '---\ncontent: Post content',
data: { content: 'Post content' },
label: null,
metaData: null,
isModification: null,
Expand Down Expand Up @@ -462,17 +462,19 @@ describe('Backend', () => {
const config = Map({});
const backend = new Backend(implementation, { config, backendName: 'github' });

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

Expand All @@ -488,7 +490,7 @@ describe('Backend', () => {
]);
});

it('should combine mutiple content different folder entries', () => {
it('should combine multiple content different folder entries', () => {
const entries = [
{
path: 'posts/en/post.md',
Expand Down Expand Up @@ -519,13 +521,16 @@ describe('Backend', () => {
});

describe('listAllMultipleEntires', () => {
beforeEach(() => {
jest.clearAllMocks();
});
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', async () => {
it('should combine multiple content same folder entries', async () => {
const entries = [
{
slug: 'post.en',
Expand Down Expand Up @@ -560,7 +565,7 @@ describe('Backend', () => {
});
});

it('should combine mutiple content different folder entries', async () => {
it('should combine multiple content different folder entries', async () => {
const entries = [
{
slug: 'en/post',
Expand Down Expand Up @@ -592,4 +597,66 @@ describe('Backend', () => {
});
});
});

describe('getMultipleEntries', () => {
beforeEach(() => {
jest.clearAllMocks();
});
const implementation = {
init: jest.fn(() => implementation),
};
const config = Map({});
const backend = new Backend(implementation, { config, backendName: 'github' });
const entryDraft = fromJS({
entry: {
data: {
en: { title: 'post', content: 'Content en' },
fr: { title: 'publier', content: 'Content fr' },
},
},
});
const entryObj = { path: 'posts/post.md', slug: 'post' };

it('should split multiple content into different locale file entries', async () => {
const collection = fromJS({
i18n_structure: 'locale_file_extensions',
fields: [{ name: 'title' }, { name: 'content' }],
extension: 'md',
});

expect(backend.getMultipleEntries(collection, entryDraft, entryObj)).toEqual([
{
slug: 'post',
path: 'posts/post.en.md',
raw: '---\ntitle: post\ncontent: Content en\n---\n',
},
{
slug: 'post',
path: 'posts/post.fr.md',
raw: '---\ntitle: publier\ncontent: Content fr\n---\n',
},
]);
});

it('should split multiple content into different locale folder entries', async () => {
const collection = fromJS({
i18n_structure: 'locale_folders',
fields: [{ name: 'title' }, { name: 'content' }],
extension: 'md',
});

expect(backend.getMultipleEntries(collection, entryDraft, entryObj)).toEqual([
{
slug: 'post',
path: 'posts/en/post.md',
raw: '---\ntitle: post\ncontent: Content en\n---\n',
},
{
slug: 'post',
path: 'posts/fr/post.md',
raw: '---\ntitle: publier\ncontent: Content fr\n---\n',
},
]);
});
});
});
77 changes: 41 additions & 36 deletions packages/netlify-cms-core/src/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
FilterRule,
Collections,
EntryDraft,
Entry,
CollectionFile,
State,
EntryField,
Expand Down Expand Up @@ -364,12 +365,11 @@ export class Backend {
multiEntries = entries
.filter(entry => locales.some(l => entry.slug.endsWith(`.${l}`)))
.map(entry => {
const path = entry.path.split('.');
const locale = path.splice(-2, 2)[0];
const locale = entry.slug.slice(-2);
return {
...entry,
slug: entry.slug.replace(`.${locale}`, ''),
contentKey: path.join('.'),
contentKey: entry.path.replace(`.${locale}`, ''),
i18nStructure,
slugWithLocale: entry.slug,
};
Expand Down Expand Up @@ -553,7 +553,7 @@ export class Backend {
loadedEntries = await Promise.all(
locales.map(l =>
this.implementation
.getEntry(path.replace(`${slug}`, `${l}/${slug}`))
.getEntry(path.replace(`/${slug}`, `/${l}/${slug}`))
.catch(() => undefined),
),
);
Expand Down Expand Up @@ -687,9 +687,8 @@ export class Backend {
let path;
entries.forEach(e => {
if (i18nStructure === LOCALE_FILE_EXTENSIONS) {
const entryPath = e.path.split('.');
const locale = entryPath.splice(-2, 1)[0];
!path && (path = entryPath.join('.'));
const locale = e.slugWithLocale.slice(-2);
!path && (path = e.path.replace(`.${locale}`, ''));
data[locale] = e.data;
} else if (i18nStructure === LOCALE_FOLDERS) {
const locale = e.slugWithLocale.slice(0, 2);
Expand Down Expand Up @@ -872,34 +871,7 @@ export class Backend {

let entriesObj = [entryObj];
if (MultiContentDiffFiles) {
const i18nStructure = collection.get('i18n_structure');
const extension = selectFolderEntryExtension(collection);
const data = entryDraft.getIn(['entry', 'data']).toJS();
const locales = Object.keys(data);
entriesObj = [];
if (i18nStructure === LOCALE_FILE_EXTENSIONS) {
locales.forEach(l => {
entriesObj.push({
path: entryObj.path.replace(extension, `${l}.${extension}`),
slug: entryObj.slug,
raw: this.entryToRaw(
collection,
entryDraft.get('entry').set('data', entryDraft.getIn(['entry', 'data', l])),
),
});
});
} else if (i18nStructure === LOCALE_FOLDERS) {
locales.forEach(l => {
entriesObj.push({
path: entryObj.path.replace(`${entryObj.slug}`, `${l}/${entryObj.slug}`),
slug: entryObj.slug,
raw: this.entryToRaw(
collection,
entryDraft.get('entry').set('data', entryDraft.getIn(['entry', 'data', l])),
),
});
});
}
entriesObj = this.getMultipleEntries(collection, entryDraft, entryObj);
}

const user = (await this.currentUser()) as User;
Expand Down Expand Up @@ -943,6 +915,39 @@ export class Backend {
return entryObj.slug;
}

getMultipleEntries(collection: Collection, entryDraft: EntryDraft, entryObj: Entry) {
const i18nStructure = collection.get('i18n_structure');
const extension = selectFolderEntryExtension(collection);
const data = entryDraft.getIn(['entry', 'data']).toJS();
const locales = Object.keys(data);
const entriesObj = [];
if (i18nStructure === LOCALE_FILE_EXTENSIONS) {
locales.forEach(l => {
entriesObj.push({
path: entryObj.path.replace(extension, `${l}.${extension}`),
slug: entryObj.slug,
raw: this.entryToRaw(
collection,
entryDraft.get('entry').set('data', entryDraft.getIn(['entry', 'data', l])),
),
});
});
} else if (i18nStructure === LOCALE_FOLDERS) {
locales.forEach(l => {
entriesObj.push({
path: entryObj.path.replace(`/${entryObj.slug}`, `/${l}/${entryObj.slug}`),
slug: entryObj.slug,
raw: this.entryToRaw(
collection,
entryDraft.get('entry').set('data', entryDraft.getIn(['entry', 'data', l])),
),
});
});
}

return entriesObj;
}

async invokeEventWithEntry(event: string, entry: EntryMap) {
const { login, name } = (await this.currentUser()) as User;
await invokeEvent({ name: event, data: { entry, author: { login, name } } });
Expand Down Expand Up @@ -1020,7 +1025,7 @@ export class Backend {
} else if (i18nStructure === LOCALE_FOLDERS) {
for (const l of locales) {
await this.implementation
.deleteFile(path.replace(`${slug}`, `${l}/${slug}`), commitMessage)
.deleteFile(path.replace(`/${slug}`, `/${l}/${slug}`), commitMessage)
.catch(() => undefined);
}
}
Expand Down
Loading

0 comments on commit 23744e5

Please sign in to comment.