|
| 1 | +import { Map } from 'immutable'; |
| 2 | + |
| 3 | +import { extensionFormatters, resolveFormat } from '../formats'; |
| 4 | +import { registerCustomFormat } from '../../lib/registry'; |
| 5 | + |
| 6 | +describe('custom formats', () => { |
| 7 | + const testEntry = { |
| 8 | + collection: 'testCollection', |
| 9 | + data: { x: 1 }, |
| 10 | + isModification: false, |
| 11 | + label: 'testLabel', |
| 12 | + mediaFiles: [], |
| 13 | + meta: {}, |
| 14 | + newRecord: true, |
| 15 | + partial: false, |
| 16 | + path: 'testPath1', |
| 17 | + raw: 'testRaw', |
| 18 | + slug: 'testSlug', |
| 19 | + author: 'testAuthor', |
| 20 | + updatedOn: 'testUpdatedOn', |
| 21 | + }; |
| 22 | + it('resolves builtint formats', () => { |
| 23 | + const collection = Map({ |
| 24 | + name: 'posts', |
| 25 | + }); |
| 26 | + expect(resolveFormat(collection, { ...testEntry, path: 'test.yml' })).toEqual( |
| 27 | + extensionFormatters.yml, |
| 28 | + ); |
| 29 | + expect(resolveFormat(collection, { ...testEntry, path: 'test.yaml' })).toEqual( |
| 30 | + extensionFormatters.yml, |
| 31 | + ); |
| 32 | + expect(resolveFormat(collection, { ...testEntry, path: 'test.toml' })).toEqual( |
| 33 | + extensionFormatters.toml, |
| 34 | + ); |
| 35 | + expect(resolveFormat(collection, { ...testEntry, path: 'test.json' })).toEqual( |
| 36 | + extensionFormatters.json, |
| 37 | + ); |
| 38 | + expect(resolveFormat(collection, { ...testEntry, path: 'test.md' })).toEqual( |
| 39 | + extensionFormatters.md, |
| 40 | + ); |
| 41 | + expect(resolveFormat(collection, { ...testEntry, path: 'test.markdown' })).toEqual( |
| 42 | + extensionFormatters.markdown, |
| 43 | + ); |
| 44 | + expect(resolveFormat(collection, { ...testEntry, path: 'test.html' })).toEqual( |
| 45 | + extensionFormatters.html, |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it('resolves custom format', () => { |
| 50 | + registerCustomFormat('txt-querystring', 'txt', { |
| 51 | + fromFile: file => Object.fromEntries(new URLSearchParams(file)), |
| 52 | + toFile: value => new URLSearchParams(value).toString(), |
| 53 | + }); |
| 54 | + |
| 55 | + const collection = Map({ |
| 56 | + name: 'posts', |
| 57 | + format: 'txt-querystring', |
| 58 | + }); |
| 59 | + |
| 60 | + const formatter = resolveFormat(collection, { ...testEntry, path: 'test.txt' }); |
| 61 | + |
| 62 | + expect(formatter.toFile({ foo: 'bar' })).toEqual('foo=bar'); |
| 63 | + expect(formatter.fromFile('foo=bar')).toEqual({ foo: 'bar' }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('can override existing formatters', () => { |
| 67 | + // simplified version of a more realistic use case: using a different yaml library like js-yaml |
| 68 | + // to make netlify-cms play nice with other tools that edit content and spit out yaml |
| 69 | + registerCustomFormat('bad-yaml', 'yml', { |
| 70 | + fromFile: file => Object.fromEntries(file.split('\n').map(line => line.split(': '))), |
| 71 | + toFile: value => |
| 72 | + Object.entries(value) |
| 73 | + .map(([k, v]) => `${k}: ${v}`) |
| 74 | + .join('\n'), |
| 75 | + }); |
| 76 | + |
| 77 | + const collection = Map({ |
| 78 | + name: 'posts', |
| 79 | + format: 'bad-yaml', |
| 80 | + }); |
| 81 | + |
| 82 | + const formatter = resolveFormat(collection, { ...testEntry, path: 'test.txt' }); |
| 83 | + |
| 84 | + expect(formatter.toFile({ a: 'b', c: 'd' })).toEqual('a: b\nc: d'); |
| 85 | + expect(formatter.fromFile('a: b\nc: d')).toEqual({ a: 'b', c: 'd' }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments