macos-defaults/build/write-pages.test.js

518 lines
16 KiB
JavaScript
Raw Normal View History

const fs = require('fs')
jest.mock('fs')
const writePages = require('./write-pages')
require('./handlebars-helpers')
const templatesPath = 'templates'
const destinationPath = 'dist'
describe('write-pages', () => {
afterEach(() => {
jest.clearAllMocks()
})
describe('one category, one page', () => {
describe('with text example', () => {
describe('with special chars key and values', () => {
beforeEach(() =>
callWritePages({
categories: [
{
folder: 'category',
name: 'Category',
description: 'This is a description of the category. It can contain "quotes" and markdown.\n\n- a list item',
keys: [
{
key: 'a page',
domain: 'com.apple.category',
title: 'Page',
description: 'This is a description of the page. It can contain "quotes" and markdown.\n\n- a list item',
param: { type: 'bool' },
examples: [
{
value: '~/Desktop',
default: true,
2020-11-27 12:45:16 +00:00
text: 'output when value is ~/Desktop',
},
{
value: '"EEE d MMM HH:mm:ss"',
text: 'output when value is "EEE d MMM HH:mm:ss"',
2020-11-27 12:45:16 +00:00
},
],
2020-11-27 12:45:16 +00:00
versions: ['Big Sur'],
},
],
},
],
})
)
it('should write a category/a-page.md file using the page template', () => {
const pageReadmeContent = readFile(
`${destinationPath}/category/a-page.md`
)
expect(pageReadmeContent).toMatchSnapshot()
})
})
describe('with requirements', () => {
beforeEach(() =>
callWritePages({
categories: [
{
folder: 'category',
name: 'Category',
description: 'Category description.',
keys: [
{
key: 'page',
domain: 'com.apple.category',
title: 'Page',
description: 'Page description.',
param: { type: 'string' },
requirements: [
{
folder: 'another-category',
key: 'another-key',
name: 'com.apple.category2 another-key',
2020-11-27 12:45:16 +00:00
value: true,
},
{
folder: 'a-third-category',
key: 'a-third-key',
name: 'com.apple.category2 a-third-key',
2020-11-27 12:45:16 +00:00
value: 0.5,
},
],
examples: [
{
value: true,
default: true,
2020-11-27 12:45:16 +00:00
text: 'output when value is true',
},
{
value: false,
2020-11-27 12:45:16 +00:00
text: 'output when value is false',
},
],
versions: ['Big Sur'],
2020-11-27 12:45:16 +00:00
after: 'killall App',
},
],
},
],
})
)
it('should write a category/page.md file using the page template', () => {
const pageReadmeContent = readFile(
`${destinationPath}/category/page.md`
)
expect(pageReadmeContent).toMatchSnapshot()
})
})
describe('with an after command', () => {
beforeEach(() =>
callWritePages({
categories: [
{
folder: 'category',
name: 'Category',
description: 'Category description.',
keys: [
{
key: 'page',
domain: 'com.apple.category',
title: 'Page',
description: 'Page description.',
param: { type: 'string' },
examples: [
{
value: '~/Desktop',
default: true,
2020-11-27 12:45:16 +00:00
text: 'output when value is ~/Desktop',
},
{
value: '~/Pictures',
2020-11-27 12:45:16 +00:00
text: 'output when value is ~/Pictures',
},
],
versions: ['Big Sur'],
2020-11-27 12:45:16 +00:00
after: 'killall App',
},
],
},
],
})
)
it('should write a category/page.md file using the page template', () => {
const pageReadmeContent = readFile(
`${destinationPath}/category/page.md`
)
expect(pageReadmeContent).toMatchSnapshot()
})
})
describe('with a deleteAlert section', () => {
beforeEach(() =>
callWritePages({
categories: [
{
folder: 'category',
name: 'Category',
description: 'Category description.',
keys: [
{
key: 'page',
domain: 'com.apple.category',
title: 'Page',
description: 'Page description.',
param: { type: 'bool' },
examples: [],
deleteAlert: {
type: 'danger',
2020-11-27 12:45:16 +00:00
message: 'This an alert message. <br> `defaults command`',
},
2020-11-27 12:45:16 +00:00
versions: ['Big Sur'],
},
],
},
],
})
)
it('should write a category/page.md file using the page template', () => {
const pageReadmeContent = readFile(
`${destinationPath}/category/page.md`
)
expect(pageReadmeContent).toMatchSnapshot()
})
})
})
describe('with text and possible values example', () => {
beforeEach(() =>
callWritePages({
categories: [
{
folder: 'category',
name: 'Category',
description: 'Category description.',
keys: [
{
key: 'page',
domain: 'com.apple.category',
title: 'Page',
description: 'Page description.',
param: { type: 'string', values: ['start', 'middle', 'end'] },
examples: [
{
value: 'start',
2020-11-27 12:45:16 +00:00
text: 'output when value is start',
},
{
value: 'middle',
default: true,
2020-11-27 12:45:16 +00:00
text: 'output when value is middle',
},
{
value: 'end',
2020-11-27 12:45:16 +00:00
text: 'output when value is end',
},
],
2020-11-27 12:45:16 +00:00
versions: ['Big Sur'],
},
],
},
],
})
)
it('should write a category/page.md file using the page template', () => {
const pageReadmeContent = readFile(
`${destinationPath}/category/page.md`
)
expect(pageReadmeContent).toMatchSnapshot()
})
})
describe('with image example', () => {
beforeEach(() =>
callWritePages({
categories: [
{
folder: 'category',
name: 'Category',
description: 'Category description.',
keys: [
{
key: 'page',
domain: 'com.apple.category',
title: 'Page',
description: 'Page description.',
param: { type: 'bool' },
examples: [
{
value: true,
default: true,
image: {
filename: 'true.png',
width: 600,
2020-11-27 12:45:16 +00:00
height: 400,
},
},
{
value: false,
image: {
filename: 'false.png',
width: 400,
2020-11-27 12:45:16 +00:00
height: 200,
},
},
],
2020-11-27 12:45:16 +00:00
versions: ['Big Sur'],
},
],
},
],
})
)
it('should write a category/page.md file using the page template', () => {
const pageReadmeContent = readFile(
`${destinationPath}/category/page.md`
)
expect(pageReadmeContent).toMatchSnapshot()
})
it('should copy the true.png example image to a category/category-page-true folder', () => {
const originImagePath = readFile(
`${destinationPath}/category/category-page-true.png`
)
expect(originImagePath).toMatchInlineSnapshot(
`"copied:../images/category/page/true.png"`
)
})
it('should copy the false.png example image to a category/category-page-false folder', () => {
const originImagePath = readFile(
`${destinationPath}/category/category-page-false.png`
)
expect(originImagePath).toMatchInlineSnapshot(
`"copied:../images/category/page/false.png"`
)
})
})
describe('with video example', () => {
beforeEach(() =>
callWritePages({
categories: [
{
folder: 'category',
name: 'Category',
description: 'Category description.',
keys: [
{
key: 'page',
domain: 'com.apple.category',
title: 'Page',
description: 'Page description.',
param: { type: 'float' },
examples: [
{
value: '0',
default: true,
video: {
filename: '0.mp4',
width: 750,
2020-11-27 12:45:16 +00:00
height: 400,
},
},
{
value: '0.5',
video: {
filename: '0.5.mp4',
width: 720,
2020-11-27 12:45:16 +00:00
height: 390,
},
},
],
2020-11-27 12:45:16 +00:00
versions: ['Big Sur'],
},
],
},
],
})
)
it('should write a category/page.md file using the page template', () => {
const pageReadmeContent = readFile(
`${destinationPath}/category/page.md`
)
expect(pageReadmeContent).toMatchSnapshot()
})
it('should copy the 0.mp4 example video to a category/category-page-0 folder', () => {
const originVideoPath = readFile(
`${destinationPath}/category/category-page-0.mp4`
)
expect(originVideoPath).toMatchInlineSnapshot(
`"copied:../images/category/page/0.mp4"`
)
})
it('should copy the 0.5.mp4 example video to a category/category-page-0.5 folder', () => {
const originVideoPath = readFile(
`${destinationPath}/category/category-page-0.5.mp4`
)
expect(originVideoPath).toMatchInlineSnapshot(
`"copied:../images/category/page/0.5.mp4"`
)
})
})
})
describe('one category, two pages', () => {
beforeEach(() =>
callWritePages({
categories: [
{
folder: 'category',
name: 'Category',
description: 'Category description.',
keys: [
{
key: 'page1',
domain: 'com.apple.category',
title: 'Page 1',
description: 'Page 1 description.',
param: { type: 'bool' },
examples: [
{
value: true,
default: true,
2020-11-27 12:45:16 +00:00
text: 'output when value is true',
},
{
value: false,
2020-11-27 12:45:16 +00:00
text: 'output when value is false',
},
],
2020-11-27 12:45:16 +00:00
versions: ['Big Sur'],
},
{
key: 'page2',
domain: 'com.apple.category',
title: 'Page 2',
description: 'Page 2 description.',
param: { type: 'bool' },
examples: [
{
value: true,
2020-11-27 12:45:16 +00:00
text: 'output when value is true',
},
{
value: false,
default: true,
2020-11-27 12:45:16 +00:00
text: 'output when value is false',
},
],
2020-11-27 12:45:16 +00:00
versions: ['Big Sur'],
},
],
},
],
})
)
it('should write a category/page1.md file using the page template', () => {
const pageReadmeContent = readFile(`${destinationPath}/category/page1.md`)
expect(pageReadmeContent).toMatchSnapshot()
})
it('should write a category/page2.md file using the page template', () => {
const pageReadmeContent = readFile(`${destinationPath}/category/page2.md`)
expect(pageReadmeContent).toMatchSnapshot()
})
})
describe('two categories, one page in each', () => {
beforeEach(() =>
callWritePages({
categories: [
{
folder: 'category1',
name: 'Category 1',
description: 'Category 1 description.',
keys: [
{
key: 'page',
domain: 'com.apple.category1',
title: 'Page',
description: 'Page description.',
param: { type: 'bool' },
examples: [
{
value: true,
default: true,
2020-11-27 12:45:16 +00:00
text: 'output when value is true',
},
{
value: false,
2020-11-27 12:45:16 +00:00
text: 'output when value is false',
},
],
2020-11-27 12:45:16 +00:00
versions: ['Big Sur'],
},
],
},
{
folder: 'category2',
name: 'Category 2',
description: 'Category 2 description.',
keys: [
{
key: 'page',
domain: 'com.apple.category2',
title: 'Page',
description: 'Page description.',
param: { type: 'bool' },
examples: [
{
value: true,
2020-11-27 12:45:16 +00:00
text: 'output when value is true',
},
{
value: false,
default: true,
2020-11-27 12:45:16 +00:00
text: 'output when value is false',
},
],
2020-11-27 12:45:16 +00:00
versions: ['Big Sur'],
},
],
},
],
})
)
it('should write a category1/page.md file using the page template', () => {
const pageReadmeContent = readFile(`${destinationPath}/category1/page.md`)
expect(pageReadmeContent).toMatchSnapshot()
})
it('should write a category2/page.md file using the page template', () => {
const pageReadmeContent = readFile(`${destinationPath}/category2/page.md`)
expect(pageReadmeContent).toMatchSnapshot()
})
})
})
2020-11-27 12:45:16 +00:00
const callWritePages = (defaults) =>
writePages({ defaults, url: '/' }, templatesPath, destinationPath)
const readFile = (file) => fs.readFakeFileSync(file, 'utf8')