-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathsearchQuery.spec.js
More file actions
101 lines (80 loc) · 2.24 KB
/
searchQuery.spec.js
File metadata and controls
101 lines (80 loc) · 2.24 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
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import { EditorState, Plugin } from '@tiptap/pm/state'
import { schema } from 'prosemirror-schema-basic'
import {
nextMatch,
previousMatch,
searchQuery,
setSearchQuery,
} from '../../plugins/searchQuery.js'
describe('searchQuery plugin', () => {
it('can set up plugin and state', () => {
const { plugin, state } = pluginSetup()
expect(plugin).toBeInstanceOf(Plugin)
expect(state.plugins).toContain(plugin)
})
it('has default search query state', () => {
const { plugin, state } = pluginSetup()
const defaultState = {
query: '',
matchAll: true,
index: 0,
}
expect(plugin.getState(state)).toEqual(defaultState)
})
it('can accept new search query state', () => {
const { plugin, state } = pluginSetup()
const setSearch = setSearchQuery('lorem')(state)
const newState = state.apply(setSearch)
expect(plugin.getState(newState)).toEqual({
query: 'lorem',
matchAll: true,
index: 0,
})
})
it('can accept next match state', () => {
const { plugin, state } = pluginSetup()
const setSearch = setSearchQuery('lorem')(state)
const nextSearch = nextMatch()(state)
let newState = state.apply(setSearch)
expect(plugin.getState(newState)).toEqual({
query: 'lorem',
matchAll: true,
index: 0,
})
newState = newState.apply(nextSearch)
expect(plugin.getState(newState)).toEqual({
query: 'lorem', // search query should be the same
matchAll: false, // matchAll is set to false
index: 1, // index is incremented to the next match
})
})
it('can accept previous match state', () => {
const { plugin, state } = pluginSetup()
const setSearch = setSearchQuery('lorem')(state)
const previousSearch = previousMatch()(state)
let newState = state.apply(setSearch)
expect(plugin.getState(newState)).toEqual({
query: 'lorem',
matchAll: true,
index: 0,
})
newState = newState.apply(previousSearch)
expect(plugin.getState(newState)).toEqual({
query: 'lorem',
matchAll: false,
index: -1,
})
})
})
const pluginSetup = () => {
const plugin = searchQuery()
const state = EditorState.create({
schema,
plugins: [plugin],
})
return { plugin, state }
}