Skip to content

Commit

Permalink
test: custom formats and register function
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Feb 12, 2022
1 parent 988a06d commit 1dc43db
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
70 changes: 70 additions & 0 deletions packages/netlify-cms-core/src/formats/__tests__/formats.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { Map, List, fromJS } from 'immutable';

import { extensionFormatters, resolveFormat } from '../formats'
import { registerCustomFormat } from '../../lib/registry';

describe('custom formats', () => {
const testEntry = {
collection: 'testCollection',
data: { x: 1 },
isModification: false,
label: 'testLabel',
mediaFiles: [],
meta: {},
newRecord: true,
partial: false,
path: 'testPath1',
raw: 'testRaw',
slug: 'testSlug',
author: 'testAuthor',
updatedOn: 'testUpdatedOn'
}
it('resolves builtint formats', () => {
const collection = Map({
name: 'posts',
});
expect(resolveFormat(collection, { ...testEntry, path: 'test.yml' })).toEqual(extensionFormatters.yml)
expect(resolveFormat(collection, { ...testEntry, path: 'test.yaml' })).toEqual(extensionFormatters.yml)
expect(resolveFormat(collection, { ...testEntry, path: 'test.toml' })).toEqual(extensionFormatters.toml)
expect(resolveFormat(collection, { ...testEntry, path: 'test.json' })).toEqual(extensionFormatters.json)
expect(resolveFormat(collection, { ...testEntry, path: 'test.md' })).toEqual(extensionFormatters.md)
expect(resolveFormat(collection, { ...testEntry, path: 'test.markdown' })).toEqual(extensionFormatters.markdown)
expect(resolveFormat(collection, { ...testEntry, path: 'test.html' })).toEqual(extensionFormatters.html)
});

it('resolves custom format', () => {
registerCustomFormat('txt-querystring', 'txt', {
fromFile: (file) => Object.fromEntries(new URLSearchParams(file)),
toFile: (value) => new URLSearchParams(value).toString(),
})

const collection = Map({
name: 'posts',
format: 'txt-querystring',
});

const formatter = resolveFormat(collection, { ...testEntry, path: 'test.txt' })

expect(formatter.toFile({ foo: 'bar' })).toEqual('foo=bar')
expect(formatter.fromFile('foo=bar')).toEqual({ foo: 'bar' })
});

it('can override existing formatters', () => {
// simplified version of a more realistic use case: using a different yaml library like js-yaml
// to make netlify-cms play nice with other tools that edit content and spit out yaml
registerCustomFormat('bad-yaml', 'yml', {
fromFile: (file) => Object.fromEntries(file.split('\n').map(line => line.split(': '))),
toFile: (value) => Object.entries(value).map(([k, v]) => `${k}: ${v}`).join('\n'),
})

const collection = Map({
name: 'posts',
format: 'bad-yaml',
});

const formatter = resolveFormat(collection, { ...testEntry, path: 'test.txt' })

expect(formatter.toFile({ a: 'b', c: 'd' })).toEqual('a: b\nc: d')
expect(formatter.fromFile('a: b\nc: d')).toEqual({ a: 'b', c: 'd' })
});
});
15 changes: 15 additions & 0 deletions packages/netlify-cms-core/src/lib/__tests__/registry.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ describe('registry', () => {
});
});

describe('registerCustomFormat', () => {
it('can register a custom format', () => {
const { getCustomFormats, registerCustomFormat } = require('../registry');

expect(Object.keys(getCustomFormats())).not.toContain('querystring');

registerCustomFormat('querystring', 'qs', {
fromFile: content => Object.fromEntries(new URLSearchParams(content)),
toFile: obj => new URLSearchParams(obj).toString(),
});

expect(Object.keys(getCustomFormats())).toContain('querystring');
});
});

describe('eventHandlers', () => {
const events = [
'prePublish',
Expand Down

0 comments on commit 1dc43db

Please sign in to comment.