1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-01 05:16:10 +01:00

More tests for addMarks and Leaf.splitLeaves (#1953)

* Add tests

* Add failing test

* Start adding tests for Leaf.splitLeaves

* More tests for splitLeaves

* Fix test actually

* Lint and rename test
This commit is contained in:
Nicolas Gaborit 2018-07-05 17:59:51 +02:00 committed by Ian Storm Taylor
parent 4b15481f92
commit 910e983215
11 changed files with 277 additions and 1 deletions

View File

@ -239,7 +239,7 @@ class Text extends Record(DEFAULTS) {
/**
* Add a `set` of marks at `index` and `length`.
* Corner Cases:
* 1. If empty text, and if length === 0 and index === 0
* 1. If empty text, and if length === 0 and index === 0, will make sure the text contain an empty leaf with the given mark.
*
* @param {Number} index
* @param {Number} length

View File

@ -0,0 +1,42 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.addMarks(['underline'])
}
export const input = (
<value>
<document>
<paragraph>
Some{' '}
<b>
<anchor />bold
</b>{' '}
and some{' '}
<i>
ita<focus />lic
</i>
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<anchor />
Some{' '}
<b>
<u>bold</u>
</b>
<u> and some </u>
<i>
<u>ita</u>lic
</i>
</paragraph>
</document>
</value>
)

View File

@ -52,4 +52,5 @@ describe('models', () => {
})
require('./text/')
require('./leaf/')
})

View File

@ -0,0 +1,34 @@
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

@ -0,0 +1,35 @@
/** @jsx h */
import { List } from 'immutable'
import { Leaf } from '../../../..'
export const input = List([
Leaf.create({
text: 'Cat',
}),
Leaf.create({
text: 'is',
}),
Leaf.create({
text: 'Cute',
}),
])
export default function(leaves) {
return List(Leaf.splitLeaves(leaves, 'Cat is Cute'.length))
}
export const output = List([
List([
Leaf.create({
text: 'Cat',
}),
Leaf.create({
text: 'is',
}),
Leaf.create({
text: 'Cute',
}),
]),
List([]),
])

View File

@ -0,0 +1,35 @@
/** @jsx h */
import { List } from 'immutable'
import { Leaf } from '../../../..'
export const input = List([
Leaf.create({
text: 'Cat',
}),
Leaf.create({
text: 'is',
}),
Leaf.create({
text: 'Cute',
}),
])
export default function(leaves) {
return List(Leaf.splitLeaves(leaves, 'CatisCute'.length + 1))
}
export const output = List([
List([
Leaf.create({
text: 'Cat',
}),
Leaf.create({
text: 'is',
}),
Leaf.create({
text: 'Cute',
}),
]),
List([]),
])

View File

@ -0,0 +1,39 @@
/** @jsx h */
import { List } from 'immutable'
import { Leaf } from '../../../..'
export const input = List([
Leaf.create({
text: 'Cat',
}),
Leaf.create({
text: 'is',
}),
Leaf.create({
text: 'Cute',
}),
])
export default function(leaves) {
return List(Leaf.splitLeaves(leaves, 'CatisCute'.length))
}
export const output = List([
List([
Leaf.create({
text: 'Cat',
}),
Leaf.create({
text: 'is',
}),
Leaf.create({
text: 'Cute',
}),
]),
List([
Leaf.create({
text: '',
}),
]),
])

View File

@ -0,0 +1,39 @@
/** @jsx h */
import { List } from 'immutable'
import { Leaf } from '../../../..'
export const input = List([
Leaf.create({
text: 'Cat',
}),
Leaf.create({
text: 'is',
}),
Leaf.create({
text: 'Cute',
}),
])
export default function(leaves) {
return List(Leaf.splitLeaves(leaves, 4))
}
export const output = List([
List([
Leaf.create({
text: 'Cat',
}),
Leaf.create({
text: 'i',
}),
]),
List([
Leaf.create({
text: 's',
}),
Leaf.create({
text: 'Cute',
}),
]),
])

View File

@ -0,0 +1,35 @@
/** @jsx h */
import { List } from 'immutable'
import { Leaf } from '../../../..'
export const input = List([
Leaf.create({
text: 'Cat',
}),
Leaf.create({
text: 'is',
}),
Leaf.create({
text: 'Cute',
}),
])
export default function(leaves) {
return List(Leaf.splitLeaves(leaves, 0))
}
export const output = List([
List([Leaf.create()]),
List([
Leaf.create({
text: 'Cat',
}),
Leaf.create({
text: 'is',
}),
Leaf.create({
text: 'Cute',
}),
]),
])

View File

@ -0,0 +1,16 @@
/** @jsx h */
import h from '../../../../helpers/h'
import { Mark } from '../../../../..'
export const input = <text>Cat is Cute</text>[0]
export default function(t) {
return t.addMark(0, t.text.length, Mark.create('italic'))
}
export const output = (
<text>
<i>Cat is Cute</i>
</text>
)[0]