1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-11 09:43:58 +02:00

Refactor tests (#2021)

* add slate-dev-test-utils, refactor tests to use fixtures util

* cleanup top-level test files

* tweaks
This commit is contained in:
Ian Storm Taylor
2018-08-01 09:16:02 -07:00
committed by GitHub
parent f7fd0b7a4e
commit 1748a4b0c1
39 changed files with 465 additions and 812 deletions

View File

@@ -155,5 +155,13 @@
{ "blankLine": "always", "prev": "*", "next": "multiline-block-like" }, { "blankLine": "always", "prev": "*", "next": "multiline-block-like" },
{ "blankLine": "any", "prev": "case", "next": "case" } { "blankLine": "any", "prev": "case", "next": "case" }
] ]
},
"overrides": [
{
"files": "**/test/**/*.js",
"rules": {
"import/no-extraneous-dependencies": false
} }
}
]
} }

View File

@@ -66,6 +66,7 @@
"rollup-plugin-uglify": "^3.0.0", "rollup-plugin-uglify": "^3.0.0",
"slate-collapse-on-escape": "^0.6.0", "slate-collapse-on-escape": "^0.6.0",
"slate-dev-benchmark": "*", "slate-dev-benchmark": "*",
"slate-dev-test-utils": "*",
"slate-soft-break": "^0.6.0", "slate-soft-break": "^0.6.0",
"source-map-loader": "^0.2.3", "source-map-loader": "^0.2.3",
"source-map-support": "^0.4.0", "source-map-support": "^0.4.0",

View File

@@ -1,6 +1,6 @@
{ {
"name": "slate-dev-benchmark", "name": "slate-dev-benchmark",
"description": "A simple, development-only benchmark for Slate", "description": "INTERNAL: A development-only benchmark tool for Slate's core.",
"version": "0.0.3", "version": "0.0.3",
"license": "MIT", "license": "MIT",
"repository": "git://github.com/ianstormtaylor/slate.git", "repository": "git://github.com/ianstormtaylor/slate.git",
@@ -10,8 +10,5 @@
}, },
"scripts": { "scripts": {
"clean": "rm -rf ./dist ./lib ./node_modules" "clean": "rm -rf ./dist ./lib ./node_modules"
}, }
"keywords": [
"slate"
]
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "slate-dev-environment", "name": "slate-dev-environment",
"description": "Detect browser and OS environments in core Slate packages", "description": "INTERNAL: A set of environment-related constants for Slate's core.",
"version": "0.1.3", "version": "0.1.3",
"license": "MIT", "license": "MIT",
"repository": "git://github.com/ianstormtaylor/slate.git", "repository": "git://github.com/ianstormtaylor/slate.git",
@@ -12,13 +12,10 @@
"dist/", "dist/",
"lib/" "lib/"
], ],
"scripts": {
"clean": "rm -rf ./dist ./lib ./node_modules"
},
"keywords": [
"slate"
],
"dependencies": { "dependencies": {
"is-in-browser": "^1.1.3" "is-in-browser": "^1.1.3"
},
"scripts": {
"clean": "rm -rf ./dist ./lib ./node_modules"
} }
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "slate-dev-logger", "name": "slate-dev-logger",
"description": "A simple, development-only logger for Slate.", "description": "INTERNAL: A simple, development-only logger for Slate.",
"version": "0.1.41", "version": "0.1.41",
"license": "MIT", "license": "MIT",
"repository": "git://github.com/ianstormtaylor/slate.git", "repository": "git://github.com/ianstormtaylor/slate.git",
@@ -17,8 +17,5 @@
}, },
"scripts": { "scripts": {
"clean": "rm -rf ./dist ./lib ./node_modules" "clean": "rm -rf ./dist ./lib ./node_modules"
}, }
"keywords": [
"slate"
]
} }

View File

@@ -0,0 +1 @@
This package contains a set of testing utilities used by Slate's other core packages for writing their tests.

View File

@@ -0,0 +1,18 @@
{
"name": "slate-dev-test-utils",
"description": "INTERNAL: A set of utils for Slate's core tests.",
"version": "0.0.0",
"license": "MIT",
"repository": "git://github.com/ianstormtaylor/slate.git",
"main": "src/index.js",
"files": [
"dist/",
"lib/"
],
"peerDependencies": {
"slate": ">=0.35.0"
},
"scripts": {
"clean": "rm -rf ./dist ./lib ./node_modules"
}
}

View File

@@ -0,0 +1,59 @@
import fs from 'fs'
import { basename, extname, resolve } from 'path'
import { KeyUtils } from 'slate'
export const fixtures = (...args) => {
let fn = args.pop()
let options = { skip: false }
if (typeof fn !== 'function') {
options = fn
fn = args.pop()
}
const path = resolve(...args)
const files = fs.readdirSync(path)
const dir = basename(path)
const d = options.skip ? describe.skip : describe
d(dir, () => {
for (const file of files) {
const p = resolve(path, file)
const stat = fs.statSync(p)
if (stat.isDirectory()) {
fixtures(path, file, fn)
}
if (
stat.isFile() &&
file.endsWith('.js') &&
!file.startsWith('.') &&
// Ignoring `index.js` files allows us to use the fixtures directly
// from the top-level directory itself, instead of only children.
file !== 'index.js'
) {
const name = basename(file, extname(file))
// This needs to be a non-arrow function to use `this.skip()`.
it(name, function() {
// Ensure that the key generator is reset. We have to do this here
// because the `require` call will create the Slate objects.
KeyUtils.resetGenerator()
const module = require(p)
if (module.skip) {
this.skip()
return
}
fn({ name, path, module })
})
}
}
})
}
fixtures.skip = (...args) => {
fixtures(...args, { skip: true })
}

View File

@@ -1,37 +1,11 @@
/**
* Dependencies.
*/
import Html from '..' import Html from '..'
import assert from 'assert' import assert from 'assert'
import fs from 'fs' import { JSDOM } from 'jsdom'
import { JSDOM } from 'jsdom' // eslint-disable-line import/no-extraneous-dependencies import { Value } from 'slate'
import { Value, KeyUtils } from 'slate' import { fixtures } from 'slate-dev-test-utils'
import { basename, extname, resolve } from 'path'
/**
* Reset Slate's internal key generator state before each text.
*/
beforeEach(() => {
KeyUtils.resetGenerator()
})
/**
* Tests.
*/
describe('slate-html-serializer', () => { describe('slate-html-serializer', () => {
describe('deserialize()', () => { fixtures(__dirname, 'deserialize', ({ module }) => {
const dir = resolve(__dirname, './deserialize')
const tests = fs
.readdirSync(dir)
.filter(t => t[0] != '.')
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(dir, test))
const { input, output, config, options } = module const { input, output, config, options } = module
const html = new Html({ parseHtml: JSDOM.fragment, ...config }) const html = new Html({ parseHtml: JSDOM.fragment, ...config })
const value = html.deserialize(input, options) const value = html.deserialize(input, options)
@@ -39,19 +13,8 @@ describe('slate-html-serializer', () => {
const expected = Value.isValue(output) ? output.toJSON() : output const expected = Value.isValue(output) ? output.toJSON() : output
assert.deepEqual(actual, expected) assert.deepEqual(actual, expected)
}) })
}
})
describe('serialize()', () => { fixtures(__dirname, 'serialize', ({ module }) => {
const dir = resolve(__dirname, './serialize')
const tests = fs
.readdirSync(dir)
.filter(t => t[0] != '.')
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(dir, test))
const { input, output, rules, options } = module const { input, output, rules, options } = module
const html = new Html({ rules, parseHtml: JSDOM.fragment }) const html = new Html({ rules, parseHtml: JSDOM.fragment })
const string = html.serialize(input, options) const string = html.serialize(input, options)
@@ -59,6 +22,4 @@ describe('slate-html-serializer', () => {
const expected = output const expected = output
assert.deepEqual(actual, expected) assert.deepEqual(actual, expected)
}) })
}
})
}) })

View File

@@ -1,46 +1,21 @@
import assert from 'assert' import assert from 'assert'
import fs from 'fs' import { Value } from 'slate'
import { Value, KeyUtils } from 'slate' import { fixtures } from 'slate-dev-test-utils'
import { basename, extname, resolve } from 'path'
beforeEach(KeyUtils.resetGenerator)
describe('slate-hyperscript', () => { describe('slate-hyperscript', () => {
const dir = resolve(__dirname, './fixtures') fixtures(__dirname, 'fixtures', ({ module }) => {
const tests = fs
.readdirSync(dir)
.filter(t => t[0] != '.')
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(dir, test))
const { input, output, options } = module const { input, output, options } = module
const actual = input.toJSON(options) const actual = input.toJSON(options)
const expected = Value.isValue(output) ? output.toJSON() : output const expected = Value.isValue(output) ? output.toJSON() : output
assert.deepEqual(actual, expected) assert.deepEqual(actual, expected)
}) })
}
describe.skip('decorations', () => { fixtures.skip(__dirname, 'decorations', ({ module }) => {
const decDir = resolve(__dirname, './decorations')
const decTests = fs
.readdirSync(decDir)
.filter(t => t[0] != '.')
.map(t => basename(t, extname(t)))
for (const test of decTests) {
it(test, async () => {
const module = require(resolve(decDir, test))
const { input, output, expectDecorations } = module const { input, output, expectDecorations } = module
// ensure deserialization was okay
const actual = input.toJSON() const actual = input.toJSON()
const expected = Value.isValue(output) ? output.toJSON() : output const expected = Value.isValue(output) ? output.toJSON() : output
assert.deepEqual(actual, expected) assert.deepEqual(actual, expected)
// ensure expected properties of decorations match
// note: they are expected to match order in test result
expectDecorations.forEach((decoration, i) => { expectDecorations.forEach((decoration, i) => {
Object.keys(decoration).forEach(prop => { Object.keys(decoration).forEach(prop => {
assert.deepEqual( assert.deepEqual(
@@ -51,6 +26,4 @@ describe('slate-hyperscript', () => {
}) })
}) })
}) })
}
})
}) })

View File

@@ -1,47 +1,22 @@
import Plain from '..' import Plain from '..'
import assert from 'assert' import assert from 'assert'
import fs from 'fs' import { Value } from 'slate'
import { Value, KeyUtils } from 'slate' import { fixtures } from 'slate-dev-test-utils'
import { basename, extname, resolve } from 'path'
beforeEach(KeyUtils.resetGenerator)
describe('slate-plain-serializer', () => { describe('slate-plain-serializer', () => {
describe('deserialize()', () => { fixtures(__dirname, 'deserialize', ({ module }) => {
const dir = resolve(__dirname, './deserialize')
const tests = fs
.readdirSync(dir)
.filter(t => t[0] != '.')
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(dir, test))
const { input, output, options } = module const { input, output, options } = module
const value = Plain.deserialize(input, options) const value = Plain.deserialize(input, options)
const actual = Value.isValue(value) ? value.toJSON() : value const actual = Value.isValue(value) ? value.toJSON() : value
const expected = Value.isValue(output) ? output.toJSON() : output const expected = Value.isValue(output) ? output.toJSON() : output
assert.deepEqual(actual, expected) assert.deepEqual(actual, expected)
}) })
}
})
describe('serialize()', () => { fixtures(__dirname, 'serialize', ({ module }) => {
const dir = resolve(__dirname, './serialize')
const tests = fs
.readdirSync(dir)
.filter(t => t[0] != '.')
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(dir, test))
const { input, output, options } = module const { input, output, options } = module
const string = Plain.serialize(input, options) const string = Plain.serialize(input, options)
const actual = string const actual = string
const expected = output const expected = output
assert.deepEqual(actual, expected) assert.deepEqual(actual, expected)
}) })
}
})
}) })

View File

@@ -1,9 +1,42 @@
import { KeyUtils } from 'slate' import AfterPlugin from '../src/plugins/after'
import assert from 'assert'
beforeEach(KeyUtils.resetGenerator) import BeforePlugin from '../src/plugins/before'
import clean from './helpers/clean'
import React from 'react'
import ReactDOM from 'react-dom/server'
import Simulator from 'slate-simulator'
import { Editor } from '..'
import { fixtures } from 'slate-dev-test-utils'
import { JSDOM } from 'jsdom'
describe('slate-react', () => { describe('slate-react', () => {
require('./plugins') fixtures.skip(__dirname, 'plugins', ({ module }) => {
require('./rendering') const { input, output, props = {} } = module
require('./utils') const fn = module.default
const plugins = [BeforePlugin(props), AfterPlugin(props)]
const simulator = new Simulator({ plugins, value: input })
fn(simulator)
const actual = simulator.value.toJSON({ preserveSelection: true })
const expected = output.toJSON({ preserveSelection: true })
assert.deepEqual(actual, expected)
})
fixtures(__dirname, 'rendering/fixtures', ({ module }) => {
const { value, output, props } = module
const p = {
value,
onChange() {},
...(props || {}),
}
const string = ReactDOM.renderToStaticMarkup(<Editor {...p} />)
const dom = JSDOM.fragment(output)
const expected = dom.firstChild.outerHTML
.trim()
.replace(/\n/gm, '')
.replace(/>\s*</g, '><')
assert.equal(clean(string), expected)
})
}) })

View File

@@ -1,45 +0,0 @@
import AfterPlugin from '../../src/plugins/after'
import BeforePlugin from '../../src/plugins/before'
import Simulator from 'slate-simulator'
import assert from 'assert'
import fs from 'fs'
import toCamel from 'to-camel-case' // eslint-disable-line import/no-extraneous-dependencies
import { basename, extname, resolve } from 'path'
/**
* Tests.
*/
describe('plugins', () => {
describe.skip('core', () => {
const dir = resolve(__dirname, 'core')
const events = fs
.readdirSync(dir)
.filter(e => e[0] != '.' && e != 'index.js')
for (const event of events) {
describe(`${toCamel(event)}`, () => {
const testDir = resolve(dir, event)
const tests = fs
.readdirSync(testDir)
.filter(t => t[0] != '.' && !!~t.indexOf('.js'))
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(testDir, test))
const { input, output, props = {} } = module
const fn = module.default
const plugins = [BeforePlugin(props), AfterPlugin(props)]
const simulator = new Simulator({ plugins, value: input })
fn(simulator)
const actual = simulator.value.toJSON({ preserveSelection: true })
const expected = output.toJSON({ preserveSelection: true })
assert.deepEqual(actual, expected)
})
}
})
}
})
})

View File

@@ -1,41 +0,0 @@
import React from 'react'
import ReactDOM from 'react-dom/server'
import assert from 'assert'
import clean from '../helpers/clean'
import fs from 'fs-promise' // eslint-disable-line import/no-extraneous-dependencies
import { JSDOM } from 'jsdom' // eslint-disable-line import/no-extraneous-dependencies
import { Editor } from '../..'
import { basename, extname, resolve } from 'path'
/**
* Tests.
*/
describe('rendering', () => {
const dir = resolve(__dirname, './fixtures')
const tests = fs
.readdirSync(dir)
.filter(t => t[0] != '.' && !!~t.indexOf('.js'))
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(dir, test))
const { value, output, props } = module
const p = {
value,
onChange() {},
...(props || {}),
}
const string = ReactDOM.renderToStaticMarkup(<Editor {...p} />)
const dom = JSDOM.fragment(output)
const expected = dom.firstChild.outerHTML
.trim()
.replace(/\n/gm, '')
.replace(/>\s*</g, '><')
assert.equal(clean(string), expected)
})
}
})

View File

@@ -1,93 +0,0 @@
/** @jsx h */
import { List } from 'immutable'
import assert from 'assert'
import h from '../helpers/h'
import getChildrenDecorations from '../../src/utils/get-children-decorations'
const value = (
<value>
<document key="a">
<paragraph key="b">
<text key="c">First line</text>
</paragraph>
<paragraph key="d">
<text key="e">Second line</text>
</paragraph>
</document>
</value>
)
const { document } = value
const [paragraphB] = document.nodes.toArray()
describe('getChildrenDecorations', () => {
it('should return the child list when no decorations are given', () => {
const actual = getChildrenDecorations(document, List())
const expected = [[], []]
assert.deepEqual(actual.map(l => l.toArray()), expected)
})
it('should wrap a block with the range it contains', () => {
const decoration1 = {
startKey: 'c',
startOffset: 1,
endKey: 'c',
endOffset: 2,
decoration: 'd1',
}
const actual = getChildrenDecorations(document, List([decoration1]))
const expected = [[decoration1], []]
assert.deepEqual(actual.map(l => l.toArray()), expected)
})
it('should sort two decorations inside a node', () => {
const decoration1 = {
startKey: 'c',
startOffset: 1,
endKey: 'c',
endOffset: 2,
decoration: 'd1',
}
const decoration2 = {
startKey: 'c',
startOffset: 1,
endKey: 'e',
endOffset: 2,
decoration: 'd2',
}
const actual = getChildrenDecorations(
document,
List([decoration1, decoration2])
)
const expected = [[decoration1, decoration2], [decoration2]]
assert.deepEqual(actual.map(l => l.toArray()), expected)
})
it('should sort decorations outside the node', () => {
const decoration1 = {
startKey: 'c',
startOffset: 1,
endKey: 'e',
endOffset: 2,
decoration: 'd1',
}
const actual = getChildrenDecorations(paragraphB, List([decoration1]))
const expected = [[decoration1]]
assert.deepEqual(actual.map(l => l.toArray()), expected)
})
})

View File

@@ -1,7 +0,0 @@
import { KeyUtils } from 'slate'
beforeEach(KeyUtils.resetGenerator)
describe('utils', () => {
// require('./get-children-decorations')
})

View File

@@ -28,7 +28,8 @@
"react": ">=0.14.0" "react": ">=0.14.0"
}, },
"devDependencies": { "devDependencies": {
"slate-hyperscript": "^0.6.2" "slate-hyperscript": "^0.6.2",
"slate-dev-test-utils": "*"
}, },
"scripts": { "scripts": {
"clean": "rm -rf ./dist ./lib ./node_modules" "clean": "rm -rf ./dist ./lib ./node_modules"

View File

@@ -1,48 +0,0 @@
import assert from 'assert'
import fs from 'fs-promise' // eslint-disable-line import/no-extraneous-dependencies
import toCamel from 'to-camel-case' // eslint-disable-line import/no-extraneous-dependencies
import { basename, extname, resolve } from 'path'
/**
* Tests.
*/
describe('changes', async () => {
const dir = resolve(__dirname)
const categories = fs
.readdirSync(dir)
.filter(c => c[0] != '.' && c != 'index.js')
for (const category of categories) {
describe(category, () => {
const categoryDir = resolve(dir, category)
const methods = fs.readdirSync(categoryDir).filter(c => c[0] != '.')
for (const method of methods) {
describe(toCamel(method), () => {
const testDir = resolve(categoryDir, method)
const tests = fs
.readdirSync(testDir)
.filter(t => t[0] != '.' && !!~t.indexOf('.js'))
.map(t => basename(t, extname(t)))
for (const test of tests) {
const module = require(resolve(testDir, test))
const { input, output, skip } = module
const fn = module.default
const t = skip ? it.skip : it
t(test, async () => {
const change = input.change()
fn(change)
const opts = { preserveSelection: true, preserveData: true }
const actual = change.value.toJSON(opts)
const expected = output.toJSON(opts)
assert.deepEqual(actual, expected)
})
}
})
}
})
}
})

View File

@@ -1,11 +1,5 @@
import { createHyperscript } from 'slate-hyperscript' import { createHyperscript } from 'slate-hyperscript'
/**
* Define a hyperscript.
*
* @type {Function}
*/
const h = createHyperscript({ const h = createHyperscript({
blocks: { blocks: {
line: 'line', line: 'line',
@@ -39,10 +33,4 @@ const h = createHyperscript({
}, },
}) })
/**
* Export.
*
* @type {Function}
*/
export default h export default h

View File

@@ -1,39 +0,0 @@
import assert from 'assert'
import fs from 'fs'
import { basename, extname, resolve } from 'path'
/**
* Tests.
*/
describe('history', async () => {
const dir = resolve(__dirname)
const methods = fs
.readdirSync(dir)
.filter(d => d[0] != '.' && d != 'index.js')
for (const method of methods) {
describe(method, () => {
const testDir = resolve(dir, method)
const tests = fs
.readdirSync(testDir)
.filter(f => f[0] != '.' && !!~f.indexOf('.js'))
.map(f => basename(f, extname(f)))
for (const test of tests) {
const module = require(resolve(testDir, test))
const { input, output, skip } = module
const fn = module.default
const t = skip ? it.skip : it
t(test, async () => {
const next = fn(input)
const opts = { preserveSelection: true, preserveData: true }
const actual = next.toJSON(opts)
const expected = output.toJSON(opts)
assert.deepEqual(actual, expected)
})
}
})
}
})

View File

@@ -1,26 +1,116 @@
/** import assert from 'assert'
* Dependencies. import { fixtures } from 'slate-dev-test-utils'
*/ import { Node, Schema, Value } from '..'
import { KeyUtils } from '..'
/**
* Tests.
*/
describe('slate', () => { describe('slate', () => {
require('./serializers') fixtures(__dirname, 'models/leaf', ({ module }) => {
require('./schema') const { input, output } = module
require('./changes') const fn = module.default
require('./history') const actual = fn(input).toJSON()
require('./operations') const expected = output.toJSON()
require('./models') assert.deepEqual(actual, expected)
}) })
/** fixtures(__dirname, 'models/text', ({ module }) => {
* Reset Slate's internal key generator state before each text. const { input, output } = module
*/ const fn = module.default
const actual = fn(input).toJSON()
const expected = output.toJSON()
assert.deepEqual(actual, expected)
})
beforeEach(() => { fixtures(__dirname, 'models/node', ({ module }) => {
KeyUtils.resetGenerator() const { input, output } = module
const fn = module.default
let actual = fn(input)
let expected = output
if (Node.isNode(actual)) {
actual = actual.toJSON()
}
if (Node.isNode(expected)) {
expected = expected.toJSON()
}
assert.deepEqual(actual, expected)
})
fixtures(__dirname, 'models/change', ({ module }) => {
const { input, output, schema, flags, customChange } = module
const s = Schema.create(schema)
const expected = output.toJSON()
const actual = input
.change(flags)
.setValue({ schema: s })
.withoutNormalization(customChange)
.value.toJSON()
assert.deepEqual(actual, expected)
})
fixtures(__dirname, 'serializers/raw/deserialize', ({ module }) => {
const { input, output, options } = module
const actual = Value.fromJSON(input, options).toJSON()
const expected = output.toJSON()
assert.deepEqual(actual, expected)
})
fixtures(__dirname, 'serializers/raw/serialize', ({ module }) => {
const { input, output, options } = module
const actual = input.toJSON(options)
const expected = output
assert.deepEqual(actual, expected)
})
fixtures(__dirname, 'operations', ({ module }) => {
const { input, output } = module
const operations = module.default
const change = input.change()
change.applyOperations(operations)
const opts = {
preserveSelection: true,
preserveDecorations: true,
preserveData: true,
}
const actual = change.value.toJSON(opts)
const expected = output.toJSON(opts)
assert.deepEqual(actual, expected)
})
fixtures(__dirname, 'changes', ({ module }) => {
const { input, output } = module
const fn = module.default
const change = input.change()
fn(change)
const opts = { preserveSelection: true, preserveData: true }
const actual = change.value.toJSON(opts)
const expected = output.toJSON(opts)
assert.deepEqual(actual, expected)
})
fixtures(__dirname, 'schema', ({ module }) => {
const { input, output, schema } = module
const s = Schema.create(schema)
let expected = output
let actual = input
.change()
.setValue({ schema: s })
.normalize().value
if (Value.isValue(actual)) actual = actual.toJSON()
if (Value.isValue(expected)) expected = expected.toJSON()
assert.deepEqual(actual, expected)
})
fixtures(__dirname, 'history', ({ module }) => {
const { input, output } = module
const fn = module.default
const next = fn(input)
const opts = { preserveSelection: true, preserveData: true }
const actual = next.toJSON(opts)
const expected = output.toJSON(opts)
assert.deepEqual(actual, expected)
})
}) })

View File

@@ -1,56 +0,0 @@
import assert from 'assert'
import fs from 'fs'
import { Schema } from '../..'
import { basename, extname, resolve } from 'path'
/**
* Tests.
*/
describe('models', () => {
describe('change', () => {
describe('withoutNormalization', () => {
const testsDir = resolve(__dirname, 'change')
const tests = fs
.readdirSync(testsDir)
.filter(t => t[0] != '.')
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(testsDir, test))
const { input, output, schema, flags, customChange } = module
const s = Schema.create(schema)
const expected = output.toJSON()
const actual = input
.change(flags)
.setValue({ schema: s })
.withoutNormalization(customChange)
.value.toJSON()
assert.deepEqual(actual, expected)
})
}
})
})
describe('node', () => {
describe('node', () => {
const testsDir = resolve(__dirname, 'node')
const tests = fs
.readdirSync(testsDir)
.filter(t => t[0] != '.')
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const run = require(resolve(testsDir, test)).default
run()
})
}
})
})
require('./text/')
require('./leaf/')
})

View File

@@ -1,34 +0,0 @@
import assert from 'assert'
import fs from 'fs'
import { resolve } from 'path'
describe('leaf', () => {
const dir = resolve(__dirname)
const methods = fs
.readdirSync(dir)
.filter(c => c[0] != '.' && c != 'index.js')
for (const method of methods) {
describe(method, () => {
const testDir = resolve(dir, method)
const tests = fs
.readdirSync(testDir)
.filter(t => t[0] != '.' && t.includes('.js'))
for (const test of tests) {
const module = require(resolve(testDir, test))
const { input, output, skip } = module
const fn = module.default
const t = skip ? it.skip : it
t(test.replace('.js', ''), () => {
const actual = fn(input)
const opts = { preserveData: true }
const expected = output.toJSON(opts)
assert.deepEqual(actual.toJSON(opts), expected)
})
}
})
}
})

View File

@@ -1,28 +0,0 @@
/** @jsx h */
import h from '../../helpers/h'
import assert from 'assert'
export default function() {
const value = (
<value>
<document>
<paragraph>
one <anchor />two<focus /> three
</paragraph>
</document>
</value>
)
const { document, selection } = value
const actual = document.getFragmentAtRange(selection)
const expected = (
<document>
<paragraph>two</paragraph>
</document>
)
const a = actual.toJSON()
const e = expected.toJSON()
assert.deepEqual(a, e)
}

View File

@@ -0,0 +1,29 @@
/** @jsx h */
import h from '../../../helpers/h'
export const input = (
<value>
<document>
<paragraph>
on<anchor />e
</paragraph>
<paragraph>two</paragraph>
<paragraph>
th<focus /> ree
</paragraph>
</document>
</value>
)
export default function({ document, selection }) {
return document.getFragmentAtRange(selection)
}
export const output = (
<document>
<paragraph>e</paragraph>
<paragraph>two</paragraph>
<paragraph>th</paragraph>
</document>
)

View File

@@ -0,0 +1,23 @@
/** @jsx h */
import h from '../../../helpers/h'
export const input = (
<value>
<document>
<paragraph>
one <anchor />two<focus /> three
</paragraph>
</document>
</value>
)
export default function({ document, selection }) {
return document.getFragmentAtRange(selection)
}
export const output = (
<document>
<paragraph>two</paragraph>
</document>
)

View File

@@ -1,21 +0,0 @@
/** @jsx h */
import h from '../../helpers/h'
import assert from 'assert'
export default function() {
const { document } = (
<value>
<document>
<paragraph>Some Text</paragraph>
</document>
</value>
)
const paragraph = document.nodes.first()
const text = paragraph.getFirstText()
assert.equal(document.getFurthestOnlyChildAncestor(paragraph.key), null)
assert.equal(paragraph.getFurthestOnlyChildAncestor(text.key), null)
assert.equal(document.getFurthestOnlyChildAncestor(text.key), paragraph)
}

View File

@@ -0,0 +1,24 @@
/** @jsx h */
import h from '../../../helpers/h'
export const input = (
<value>
<document>
<quote>
<paragraph key="a">word</paragraph>
</quote>
</document>
</value>
)
export default function(value) {
const { document } = value
return document.getFurthestOnlyChildAncestor('a')
}
export const output = (
<quote>
<paragraph>word</paragraph>
</quote>
)

View File

@@ -0,0 +1,17 @@
/** @jsx h */
import h from '../../../helpers/h'
export const input = (
<value>
<document>
<paragraph key="a">word</paragraph>
</document>
</value>
)
export default function({ document }) {
return document.getFurthestOnlyChildAncestor('a')
}
export const output = null

View File

@@ -0,0 +1,22 @@
/** @jsx h */
import h from '../../../helpers/h'
export const input = (
<value>
<document>
<paragraph>
one<link>
<text key="a">two</text>
</link>three
</paragraph>
</document>
</value>
)
export default function(value) {
const { document } = value
return document.getFurthestOnlyChildAncestor('a')
}
export const output = <link>two</link>

View File

@@ -0,0 +1,26 @@
/** @jsx h */
import h from '../../../helpers/h'
export const input = (
<value>
<document>
<quote>
<paragraph>
<text key="a">word</text>
</paragraph>
</quote>
</document>
</value>
)
export default function(value) {
const { document } = value
return document.getFurthestOnlyChildAncestor('a')
}
export const output = (
<quote>
<paragraph>word</paragraph>
</quote>
)

View File

@@ -0,0 +1,20 @@
/** @jsx h */
import h from '../../../helpers/h'
export const input = (
<value>
<document>
<paragraph>
<text key="a">word</text>
</paragraph>
</document>
</value>
)
export default function(value) {
const { document } = value
return document.getFurthestOnlyChildAncestor('a')
}
export const output = <paragraph>word</paragraph>

View File

@@ -1,43 +0,0 @@
import assert from 'assert'
import fs from 'fs'
import { resolve } from 'path'
describe('texts', () => {
const dir = resolve(__dirname)
const categories = fs
.readdirSync(dir)
.filter(c => c[0] != '.' && c != 'index.js')
for (const category of categories) {
describe(category, () => {
const categoryDir = resolve(dir, category)
const methods = fs
.readdirSync(categoryDir)
.filter(c => c[0] != '.' && !c.includes('.js'))
for (const method of methods) {
describe(method, () => {
const testDir = resolve(categoryDir, method)
const tests = fs
.readdirSync(testDir)
.filter(t => t[0] != '.' && t.includes('.js'))
for (const test of tests) {
const module = require(resolve(testDir, test))
const { input, output, skip } = module
const fn = module.default
const t = skip ? it.skip : it
t(test.replace('.js', ''), () => {
const actual = fn(input)
const opts = { preserveData: true }
const expected = output.toJSON(opts)
assert.deepEqual(actual.toJSON(opts), expected)
})
}
})
}
})
}
})

View File

@@ -1,50 +0,0 @@
import assert from 'assert'
import fs from 'fs-promise' // eslint-disable-line import/no-extraneous-dependencies
import toSnake from 'to-snake-case' // eslint-disable-line import/no-extraneous-dependencies
import { basename, extname, resolve } from 'path'
/**
* Tests.
*/
describe('operations', async () => {
const dir = resolve(__dirname)
const categories = fs
.readdirSync(dir)
.filter(c => c[0] != '.' && c != 'index.js')
for (const category of categories) {
describe(category, () => {
const categoryDir = resolve(dir, category)
const methods = fs.readdirSync(categoryDir).filter(c => c[0] != '.')
for (const method of methods) {
describe(toSnake(method), () => {
const testDir = resolve(categoryDir, method)
const tests = fs
.readdirSync(testDir)
.filter(t => t[0] != '.' && !!~t.indexOf('.js'))
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(testDir, test))
const { input, output } = module
const operations = module.default
const change = input.change()
change.applyOperations(operations)
const opts = {
preserveSelection: true,
preserveDecorations: true,
preserveData: true,
}
const actual = change.value.toJSON(opts)
const expected = output.toJSON(opts)
assert.deepEqual(actual, expected)
})
}
})
}
})
}
})

View File

@@ -1,58 +0,0 @@
import assert from 'assert'
import fs from 'fs'
import { Schema } from '../..'
import { basename, extname, resolve } from 'path'
/**
* Tests.
*/
describe('schema', () => {
describe('core', () => {
const testsDir = resolve(__dirname, 'core')
const tests = fs
.readdirSync(testsDir)
.filter(t => t[0] != '.')
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(testsDir, test))
const { input, output, schema } = module
const s = Schema.create(schema)
const expected = output
const actual = input
.change()
.setValue({ schema: s })
.normalize()
.value.toJSON()
assert.deepEqual(actual, expected)
})
}
})
describe('custom', () => {
const testsDir = resolve(__dirname, 'custom')
const tests = fs
.readdirSync(testsDir)
.filter(t => t[0] != '.')
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(testsDir, test))
const { input, output, schema } = module
const s = Schema.create(schema)
const expected = output.toJSON()
const actual = input
.change()
.setValue({ schema: s })
.normalize()
.value.toJSON()
assert.deepEqual(actual, expected)
})
}
})
})

View File

@@ -1,48 +0,0 @@
import assert from 'assert'
import fs from 'fs'
import { Value } from '../..'
import { basename, extname, resolve } from 'path'
/**
* Tests.
*/
describe('serializers', () => {
describe('raw', () => {
describe('deserialize()', () => {
const dir = resolve(__dirname, './raw/deserialize')
const tests = fs
.readdirSync(dir)
.filter(t => t[0] != '.')
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(dir, test))
const { input, output, options } = module
const actual = Value.fromJSON(input, options).toJSON()
const expected = output.toJSON()
assert.deepEqual(actual, expected)
})
}
})
describe('serialize()', () => {
const dir = resolve(__dirname, './raw/serialize')
const tests = fs
.readdirSync(dir)
.filter(t => t[0] != '.')
.map(t => basename(t, extname(t)))
for (const test of tests) {
it(test, async () => {
const module = require(resolve(dir, test))
const { input, output, options } = module
const actual = input.toJSON(options)
const expected = output
assert.deepEqual(actual, expected)
})
}
})
})
})

View File

@@ -2,6 +2,8 @@
import h from '../../../helpers/h' import h from '../../../helpers/h'
export const skip = true
export const input = ( export const input = (
<value> <value>
<document> <document>

View File

@@ -2,6 +2,8 @@
import h from '../../../helpers/h' import h from '../../../helpers/h'
export const skip = true
export const input = ( export const input = (
<value> <value>
<document> <document>