-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathplainText.spec.ts
More file actions
166 lines (148 loc) · 5.46 KB
/
Copy pathplainText.spec.ts
File metadata and controls
166 lines (148 loc) · 5.46 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { vi, describe, it, expect, beforeEach } from 'vitest'
import { ref } from 'vue'
import { Editor } from '@tiptap/vue-3'
import { getSchema } from '@tiptap/core'
import type { TextEditorLinkPanelRequest, TextEditorState } from '../../../../src/editor/types'
import { useStrategyPlainText } from '../../../../src/editor/composables/strategies/plainText'
import { createTestingPinia } from '@opencloud-eu/web-test-helpers'
vi.mock('vue3-gettext', () => ({
useGettext: () => ({ $gettext: (text: string) => text })
}))
function createStrategy() {
const state: TextEditorState = {
sourceMode: ref(false),
linkPanel: ref<TextEditorLinkPanelRequest | null>(null),
editorZoom: ref(100)
}
return useStrategyPlainText(state)
}
describe('useStrategyPlainText', () => {
beforeEach(() => {
createTestingPinia()
})
describe('extensions', () => {
it('returns the starter kit, find and replace and clipboard extensions', () => {
const strategy = createStrategy()
const names = strategy.extensions().map((e) => e.name)
expect(names).toEqual(['starterKit', 'findAndReplace', 'plainTextClipboard'])
expect(names).not.toContain('link')
})
it('does not register the link mark bundled in the starter kit', () => {
const strategy = createStrategy()
const editor = new Editor({ extensions: strategy.extensions() })
expect(editor.schema.marks.link).toBeUndefined()
editor.destroy()
})
it('does not autolink pasted URLs', () => {
const strategy = createStrategy()
const editor = new Editor({ extensions: strategy.extensions() })
editor.view.pasteText('https://opencloud.eu ')
expect(editor.state.doc.firstChild?.firstChild?.marks).toHaveLength(0)
editor.destroy()
})
})
describe('editorActionGroups', () => {
it('returns navigation, emoji, search and export groups', () => {
const strategy = createStrategy()
const groups = strategy.editorActionGroups()
expect(groups).toHaveLength(4)
const navigationGroup = groups.find((group) => group.id === 'navigation')
const emojiGroup = groups.find((group) => group.id === 'emoji')
const searchGroup = groups.find((group) => group.id === 'search')
const exportGroup = groups.find((group) => group.id === 'export')
expect(navigationGroup).toMatchObject({
id: 'navigation',
title: 'Navigation'
})
expect(navigationGroup?.actions.map((action) => action.id)).toEqual([
'undo',
'redo',
'menu-zoom'
])
expect(emojiGroup).toMatchObject({
id: 'emoji',
title: 'Emoji'
})
expect(emojiGroup?.actions.map((action) => action.id)).toEqual(['menu-emoji'])
expect(searchGroup).toMatchObject({
id: 'search',
title: 'Search'
})
expect(searchGroup?.actions.map((action) => action.id)).toEqual(['menu-search-and-replace'])
expect(exportGroup).toMatchObject({
id: 'export',
title: 'Export'
})
expect(exportGroup?.actions.map((action) => action.id)).toEqual(['print'])
expect(groups.at(-1)?.id).toBe('export')
expect(
strategy.editorActionGroups().flatMap(({ actions }) => actions.map(({ id }) => id))
).not.toContain('link')
})
})
describe('serialize', () => {
it('joins blocks with a single newline', () => {
const strategy = createStrategy()
const schema = getSchema(strategy.extensions())
const doc = schema.nodeFromJSON({
type: 'doc',
content: [
{ type: 'paragraph', content: [{ type: 'text', text: 'line1' }] },
{ type: 'paragraph', content: [{ type: 'text', text: 'line2' }] }
]
})
expect(strategy.serialize(doc)).toBe('line1\nline2')
})
})
describe('deserialize', () => {
it('wraps multiline text in paragraph nodes', () => {
const strategy = createStrategy()
const result = strategy.deserialize('line1\nline2')
expect(result).toEqual({
type: 'doc',
content: [
{ type: 'paragraph', content: [{ type: 'text', text: 'line1' }] },
{ type: 'paragraph', content: [{ type: 'text', text: 'line2' }] }
]
})
})
it('handles empty string', () => {
const strategy = createStrategy()
const result = strategy.deserialize('')
expect(result).toEqual({
type: 'doc',
content: [{ type: 'paragraph' }]
})
})
it('handles single-line content', () => {
const strategy = createStrategy()
const result = strategy.deserialize('just one line')
expect(result).toEqual({
type: 'doc',
content: [{ type: 'paragraph', content: [{ type: 'text', text: 'just one line' }] }]
})
})
it('handles trailing newline', () => {
const strategy = createStrategy()
const result = strategy.deserialize('line1\n')
expect(result).toEqual({
type: 'doc',
content: [
{ type: 'paragraph', content: [{ type: 'text', text: 'line1' }] },
{ type: 'paragraph' }
]
})
})
it('handles Windows line endings (\\r\\n) — trailing \\r remains', () => {
const strategy = createStrategy()
const result = strategy.deserialize('line1\r\nline2')
expect(result).toEqual({
type: 'doc',
content: [
{ type: 'paragraph', content: [{ type: 'text', text: 'line1\r' }] },
{ type: 'paragraph', content: [{ type: 'text', text: 'line2' }] }
]
})
})
})
})