Files
yjs/tests/y-text.tests.js

77 lines
2.6 KiB
JavaScript
Raw Normal View History

2019-03-12 01:22:06 +01:00
import { init, compare } from './testHelper.js'
2019-03-12 01:22:06 +01:00
import * as t from 'lib0/testing.js'
2018-02-26 02:18:39 +01:00
/**
* @param {t.TestCase} tc
*/
2019-03-12 01:22:06 +01:00
export const testBasicInsertAndDelete = tc => {
const { users, text0 } = init(tc, { users: 2 })
2018-02-26 02:18:39 +01:00
let delta
2018-11-25 03:17:00 +01:00
text0.observe(event => {
2018-02-26 02:18:39 +01:00
delta = event.delta
})
text0.delete(0, 0)
t.assert(true, 'Does not throw when deleting zero elements with position 0')
text0.insert(0, 'abc')
t.assert(text0.toString() === 'abc', 'Basic insert works')
t.compare(delta, [{ insert: 'abc' }])
text0.delete(0, 1)
t.assert(text0.toString() === 'bc', 'Basic delete works (position 0)')
t.compare(delta, [{ delete: 1 }])
text0.delete(1, 1)
t.assert(text0.toString() === 'b', 'Basic delete works (position 1)')
t.compare(delta, [{ retain: 1 }, { delete: 1 }])
users[0].transact(() => {
text0.insert(0, '1')
text0.delete(0, 1)
})
t.compare(delta, [])
2019-03-12 01:22:06 +01:00
compare(users)
}
2018-02-26 02:18:39 +01:00
/**
* @param {t.TestCase} tc
*/
2019-03-12 01:22:06 +01:00
export const testBasicFormat = tc => {
const { users, text0 } = init(tc, { users: 2 })
2018-02-26 02:18:39 +01:00
let delta
2018-11-25 03:17:00 +01:00
text0.observe(event => {
2018-02-26 02:18:39 +01:00
delta = event.delta
})
text0.insert(0, 'abc', { bold: true })
t.assert(text0.toString() === 'abc', 'Basic insert with attributes works')
t.compare(text0.toDelta(), [{ insert: 'abc', attributes: { bold: true } }])
t.compare(delta, [{ insert: 'abc', attributes: { bold: true } }])
text0.delete(0, 1)
t.assert(text0.toString() === 'bc', 'Basic delete on formatted works (position 0)')
t.compare(text0.toDelta(), [{ insert: 'bc', attributes: { bold: true } }])
t.compare(delta, [{ delete: 1 }])
text0.delete(1, 1)
t.assert(text0.toString() === 'b', 'Basic delete works (position 1)')
t.compare(text0.toDelta(), [{ insert: 'b', attributes: { bold: true } }])
t.compare(delta, [{ retain: 1 }, { delete: 1 }])
2019-03-01 23:26:40 +01:00
text0.insert(0, 'z', { bold: true })
2018-02-26 02:18:39 +01:00
t.assert(text0.toString() === 'zb')
t.compare(text0.toDelta(), [{ insert: 'zb', attributes: { bold: true } }])
t.compare(delta, [{ insert: 'z', attributes: { bold: true } }])
2019-04-03 03:08:10 +02:00
// @ts-ignore
t.assert(text0._start.right.right.right.string === 'b', 'Does not insert duplicate attribute marker')
2018-02-26 02:18:39 +01:00
text0.insert(0, 'y')
t.assert(text0.toString() === 'yzb')
t.compare(text0.toDelta(), [{ insert: 'y' }, { insert: 'zb', attributes: { bold: true } }])
t.compare(delta, [{ insert: 'y' }])
text0.format(0, 2, { bold: null })
t.assert(text0.toString() === 'yzb')
t.compare(text0.toDelta(), [{ insert: 'yz' }, { insert: 'b', attributes: { bold: true } }])
t.compare(delta, [{ retain: 1 }, { retain: 1, attributes: { bold: null } }])
2019-03-12 01:22:06 +01:00
compare(users)
}