feat: content in sub folders (#2897)
This commit is contained in:
committed by
Shawn Erquhart
parent
766ade1a3c
commit
afcfe5b6d5
@ -0,0 +1,160 @@
|
||||
import TestBackend from '../implementation';
|
||||
|
||||
describe('test backend implementation', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
});
|
||||
|
||||
describe('getEntry', () => {
|
||||
it('should get entry by path', async () => {
|
||||
window.repoFiles = {
|
||||
posts: {
|
||||
'some-post.md': {
|
||||
content: 'post content',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const backend = new TestBackend();
|
||||
|
||||
await expect(backend.getEntry(null, null, 'posts/some-post.md')).resolves.toEqual({
|
||||
file: { path: 'posts/some-post.md' },
|
||||
data: 'post content',
|
||||
});
|
||||
});
|
||||
|
||||
it('should get entry by nested path', async () => {
|
||||
window.repoFiles = {
|
||||
posts: {
|
||||
dir1: {
|
||||
dir2: {
|
||||
'some-post.md': {
|
||||
content: 'post content',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const backend = new TestBackend();
|
||||
|
||||
await expect(backend.getEntry(null, null, 'posts/dir1/dir2/some-post.md')).resolves.toEqual({
|
||||
file: { path: 'posts/dir1/dir2/some-post.md' },
|
||||
data: 'post content',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('persistEntry', () => {
|
||||
it('should persist entry', async () => {
|
||||
window.repoFiles = {};
|
||||
|
||||
const backend = new TestBackend();
|
||||
|
||||
const entry = { path: 'posts/some-post.md', raw: 'content', slug: 'some-post.md' };
|
||||
await backend.persistEntry(entry, [], { newEntry: true });
|
||||
|
||||
expect(window.repoFiles).toEqual({
|
||||
posts: {
|
||||
'some-post.md': {
|
||||
content: 'content',
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should persist entry and keep existing unrelated entries', async () => {
|
||||
window.repoFiles = {
|
||||
pages: {
|
||||
'other-page.md': {
|
||||
content: 'content',
|
||||
},
|
||||
},
|
||||
posts: {
|
||||
'other-post.md': {
|
||||
content: 'content',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const backend = new TestBackend();
|
||||
|
||||
const entry = { path: 'posts/new-post.md', raw: 'content', slug: 'new-post.md' };
|
||||
await backend.persistEntry(entry, [], { newEntry: true });
|
||||
|
||||
expect(window.repoFiles).toEqual({
|
||||
pages: {
|
||||
'other-page.md': {
|
||||
content: 'content',
|
||||
},
|
||||
},
|
||||
posts: {
|
||||
'new-post.md': {
|
||||
content: 'content',
|
||||
},
|
||||
'other-post.md': {
|
||||
content: 'content',
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should persist nested entry', async () => {
|
||||
window.repoFiles = {};
|
||||
|
||||
const backend = new TestBackend();
|
||||
|
||||
const slug = encodeURIComponent('dir1/dir2/some-post.md');
|
||||
const path = `posts/${decodeURIComponent(slug)}`;
|
||||
const entry = { path, raw: 'content', slug };
|
||||
await backend.persistEntry(entry, [], { newEntry: true });
|
||||
|
||||
expect(window.repoFiles).toEqual({
|
||||
posts: {
|
||||
dir1: {
|
||||
dir2: {
|
||||
'some-post.md': {
|
||||
content: 'content',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should update existing nested entry', async () => {
|
||||
window.repoFiles = {
|
||||
posts: {
|
||||
dir1: {
|
||||
dir2: {
|
||||
'some-post.md': {
|
||||
mediaFiles: ['file1'],
|
||||
content: 'content',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const backend = new TestBackend();
|
||||
|
||||
const slug = encodeURIComponent('dir1/dir2/some-post.md');
|
||||
const path = `posts/${decodeURIComponent(slug)}`;
|
||||
const entry = { path, raw: 'new content', slug };
|
||||
await backend.persistEntry(entry, [], { newEntry: false });
|
||||
|
||||
expect(window.repoFiles).toEqual({
|
||||
posts: {
|
||||
dir1: {
|
||||
dir2: {
|
||||
'some-post.md': {
|
||||
mediaFiles: ['file1'],
|
||||
content: 'new content',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@ -188,15 +188,18 @@ export default class TestBackend {
|
||||
}
|
||||
|
||||
const newEntry = options.newEntry || false;
|
||||
const folder = path.substring(0, path.lastIndexOf('/'));
|
||||
const fileName = path.substring(path.lastIndexOf('/') + 1);
|
||||
window.repoFiles[folder] = window.repoFiles[folder] || {};
|
||||
window.repoFiles[folder][fileName] = window.repoFiles[folder][fileName] || {};
|
||||
if (newEntry) {
|
||||
window.repoFiles[folder][fileName] = { content: raw };
|
||||
} else {
|
||||
window.repoFiles[folder][fileName].content = raw;
|
||||
|
||||
const segments = path.split('/');
|
||||
const entry = newEntry ? { content: raw } : { ...getFile(path), content: raw };
|
||||
|
||||
let obj = window.repoFiles;
|
||||
while (segments.length > 1) {
|
||||
const segment = segments.shift();
|
||||
obj[segment] = obj[segment] || {};
|
||||
obj = obj[segment];
|
||||
}
|
||||
obj[segments.shift()] = entry;
|
||||
|
||||
await Promise.all(mediaFiles.map(file => this.persistMedia(file)));
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
Reference in New Issue
Block a user