2021-02-01 15:08:56 +00:00
|
|
|
import parseSourceFile from '~/static_site_editor/services/parse_source_file';
|
2020-08-26 09:10:16 +00:00
|
|
|
import {
|
2020-09-01 09:10:28 +00:00
|
|
|
sourceContentYAML as content,
|
|
|
|
sourceContentHeaderYAML as yamlFrontMatter,
|
2020-09-10 12:08:54 +00:00
|
|
|
sourceContentHeaderObjYAML as yamlFrontMatterObj,
|
2020-08-26 09:10:16 +00:00
|
|
|
sourceContentBody as body,
|
|
|
|
} from '../mock_data';
|
2020-06-10 15:08:07 +00:00
|
|
|
|
2020-09-01 09:10:28 +00:00
|
|
|
describe('static_site_editor/services/parse_source_file', () => {
|
2020-06-10 15:08:07 +00:00
|
|
|
const contentComplex = [content, content, content].join('');
|
2020-06-24 18:09:03 +00:00
|
|
|
const complexBody = [body, content, content].join('');
|
|
|
|
const edit = 'and more';
|
|
|
|
const newContent = `${content} ${edit}`;
|
|
|
|
const newContentComplex = `${contentComplex} ${edit}`;
|
2020-06-10 15:08:07 +00:00
|
|
|
|
2020-08-26 09:10:16 +00:00
|
|
|
describe('unmodified front matter', () => {
|
|
|
|
it.each`
|
2020-09-16 21:09:52 +00:00
|
|
|
parsedSource
|
|
|
|
${parseSourceFile(content)}
|
|
|
|
${parseSourceFile(contentComplex)}
|
|
|
|
`('returns $targetFrontMatter when frontMatter queried', ({ parsedSource }) => {
|
|
|
|
expect(parsedSource.matter()).toEqual(yamlFrontMatterObj);
|
|
|
|
});
|
2020-08-26 09:10:16 +00:00
|
|
|
});
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
describe('unmodified content', () => {
|
2020-06-10 15:08:07 +00:00
|
|
|
it.each`
|
2020-06-24 18:09:03 +00:00
|
|
|
parsedSource
|
|
|
|
${parseSourceFile(content)}
|
|
|
|
${parseSourceFile(contentComplex)}
|
|
|
|
`('returns false by default', ({ parsedSource }) => {
|
|
|
|
expect(parsedSource.isModified()).toBe(false);
|
2020-06-10 15:08:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
2020-06-24 18:09:03 +00:00
|
|
|
parsedSource | isBody | target
|
|
|
|
${parseSourceFile(content)} | ${undefined} | ${content}
|
|
|
|
${parseSourceFile(content)} | ${false} | ${content}
|
|
|
|
${parseSourceFile(content)} | ${true} | ${body}
|
|
|
|
${parseSourceFile(contentComplex)} | ${undefined} | ${contentComplex}
|
|
|
|
${parseSourceFile(contentComplex)} | ${false} | ${contentComplex}
|
|
|
|
${parseSourceFile(contentComplex)} | ${true} | ${complexBody}
|
|
|
|
`(
|
|
|
|
'returns only the $target content when the `isBody` parameter argument is $isBody',
|
|
|
|
({ parsedSource, isBody, target }) => {
|
|
|
|
expect(parsedSource.content(isBody)).toBe(target);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2020-06-10 15:08:07 +00:00
|
|
|
|
2020-08-26 09:10:16 +00:00
|
|
|
describe('modified front matter', () => {
|
2020-09-01 09:10:28 +00:00
|
|
|
const newYamlFrontMatter = '---\nnewKey: newVal\n---';
|
2020-09-10 12:08:54 +00:00
|
|
|
const newYamlFrontMatterObj = { newKey: 'newVal' };
|
2020-09-01 09:10:28 +00:00
|
|
|
const contentWithNewFrontMatter = content.replace(yamlFrontMatter, newYamlFrontMatter);
|
|
|
|
const contentComplexWithNewFrontMatter = contentComplex.replace(
|
|
|
|
yamlFrontMatter,
|
|
|
|
newYamlFrontMatter,
|
|
|
|
);
|
2020-08-26 09:10:16 +00:00
|
|
|
|
|
|
|
it.each`
|
|
|
|
parsedSource | targetContent
|
|
|
|
${parseSourceFile(content)} | ${contentWithNewFrontMatter}
|
|
|
|
${parseSourceFile(contentComplex)} | ${contentComplexWithNewFrontMatter}
|
|
|
|
`(
|
|
|
|
'returns the correct front matter and modified content',
|
|
|
|
({ parsedSource, targetContent }) => {
|
2020-09-16 21:09:52 +00:00
|
|
|
expect(parsedSource.matter()).toMatchObject(yamlFrontMatterObj);
|
2020-08-26 09:10:16 +00:00
|
|
|
|
2020-09-16 21:09:52 +00:00
|
|
|
parsedSource.syncMatter(newYamlFrontMatterObj);
|
2020-08-26 09:10:16 +00:00
|
|
|
|
2020-09-16 21:09:52 +00:00
|
|
|
expect(parsedSource.matter()).toMatchObject(newYamlFrontMatterObj);
|
2020-08-26 09:10:16 +00:00
|
|
|
expect(parsedSource.content()).toBe(targetContent);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
describe('modified content', () => {
|
|
|
|
const newBody = `${body} ${edit}`;
|
|
|
|
const newComplexBody = `${complexBody} ${edit}`;
|
2020-06-10 15:08:07 +00:00
|
|
|
|
|
|
|
it.each`
|
2020-09-16 21:09:52 +00:00
|
|
|
parsedSource | hasMatter | isModified | targetRaw | targetBody
|
|
|
|
${parseSourceFile(content)} | ${true} | ${false} | ${content} | ${body}
|
|
|
|
${parseSourceFile(content)} | ${true} | ${true} | ${newContent} | ${newBody}
|
|
|
|
${parseSourceFile(contentComplex)} | ${true} | ${false} | ${contentComplex} | ${complexBody}
|
|
|
|
${parseSourceFile(contentComplex)} | ${true} | ${true} | ${newContentComplex} | ${newComplexBody}
|
|
|
|
${parseSourceFile(body)} | ${false} | ${false} | ${body} | ${body}
|
|
|
|
${parseSourceFile(body)} | ${false} | ${true} | ${newBody} | ${newBody}
|
2020-06-24 18:09:03 +00:00
|
|
|
`(
|
|
|
|
'returns $isModified after a $targetRaw sync',
|
2020-09-16 21:09:52 +00:00
|
|
|
({ parsedSource, hasMatter, isModified, targetRaw, targetBody }) => {
|
2020-09-10 12:08:54 +00:00
|
|
|
parsedSource.syncContent(targetRaw);
|
2020-06-24 18:09:03 +00:00
|
|
|
|
2020-09-16 21:09:52 +00:00
|
|
|
expect(parsedSource.hasMatter()).toBe(hasMatter);
|
2020-06-24 18:09:03 +00:00
|
|
|
expect(parsedSource.isModified()).toBe(isModified);
|
|
|
|
expect(parsedSource.content()).toBe(targetRaw);
|
|
|
|
expect(parsedSource.content(true)).toBe(targetBody);
|
|
|
|
},
|
|
|
|
);
|
2020-06-10 15:08:07 +00:00
|
|
|
});
|
|
|
|
});
|