Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ListItem.spec.js « integration « cypress - github.com/nextcloud/text.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a50936d827a1945dc30fd4b7188ca77a056fa83b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import BulletList from './../../src/nodes/BulletList'
import ListItem from './../../src/nodes/ListItem'
import Markdown from './../../src/extensions/Markdown'
import { createMarkdownSerializer } from './../../src/extensions/Markdown';
import { findChildren, findChildrenByType } from 'prosemirror-utils'
import createEditor from './../../src/tests/createEditor'

describe('ListItem extension integrated in the editor', () => {

	const editor = createEditor({
		content: '',
		extensions: [Markdown, BulletList, ListItem],
	})

	it('has attrs', () => {
		editor.commands.setContent('<p><ul><li>Test</li></ul></p>')
		const li = findListItem()
		expect(li.attrs).to.deep.eq({done: null, type: 0})
		expectMarkdown(`
			* Test`)
	})

	it('creates todo lists', () => {
		editor.commands.setContent('Test')
		editor.commands.todo_item()
		const li = findListItem()
		expect(li.attrs).to.deep.eq({done: false, type: 1})
		expectMarkdown(`* [ ] Test`)
	})

	it('removes the list when toggling todo off', () => {
		editor.commands.setContent('Test')
		editor.commands.todo_item()
		editor.commands.todo_item()
		expect(findListItem()).to.eq(undefined)
		expectMarkdown(`Test`)
	})

	it('creates a bullet list', () => {
		editor.commands.setContent('<p>Test</p>')
		editor.commands.bulletListItem()
		expectMarkdown(`* Test`)
	})

	it('turns a bullet list into a todo list', () => {
		editor.commands.setContent('<p>Test</p>')
		editor.commands.bulletListItem()
		editor.commands.todo_item()
		expectMarkdown(`* [ ] Test`)
	})

	it('only toggles one list item', () => {
		editor.commands.setContent('<p><ul><li>Todo</li><li>Not to do</li></ul></p>')
		for (const { pos } of findTexts('Todo')) {
			editor.commands.setTextSelection(pos)
			editor.commands.todo_item()
		}
		expectMarkdown(`
			* [ ] Todo
			* Not to do`)
	})

	it('toggles two separate list item', () => {
		editor.commands.setContent('<p><ul><li>Todo</li><li>Not to do</li><li>Todo</li></ul></p>')
		for (const { pos } of findTexts('Todo')) {
			editor.commands.setTextSelection(pos)
			editor.commands.todo_item()
		}
		expectMarkdown(`
			* [ ] Todo
			* Not to do
			* [ ] Todo`)
	})

	function findListItem(index = 0) {
		const doc = editor.state.doc
		const type = editor.schema.nodes.listItem
		return findChildrenByType(doc, type)[index]?.node;
	}

	function findTexts(text) {
		const doc = editor.state.doc
		return findChildren(doc, child => {
			return child.isText && child.text === text
		})
	}

	function expectMarkdown(markdown) {
		expect(getMarkdown()).to.equal(markdown.replace(/\t*/g, ''))
	}

	function getMarkdown() {
		const serializer = createMarkdownSerializer(editor.schema)
		return serializer.serialize(editor.state.doc)
	}
})