forked from ucfopen/Obojobo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-plugin.test.js
More file actions
148 lines (125 loc) · 4.48 KB
/
Copy pathformat-plugin.test.js
File metadata and controls
148 lines (125 loc) · 4.48 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
import Common from 'src/scripts/common/index'
import FormatPlugin from 'src/scripts/oboeditor/plugins/format-plugin'
// const LIST_NODE = 'ObojoboDraft.Chunks.List'
const TEXT_NODE = 'ObojoboDraft.Chunks.Text'
const HEADING_NODE = 'ObojoboDraft.Chunks.Heading'
const CODE_NODE = 'ObojoboDraft.Chunks.Code'
describe('FormatPlugin', () => {
test('onKeyDown does not toggle mark if wrong key is pressed', () => {
const editor = {
changeToType: jest.fn()
}
FormatPlugin.onKeyDown({ key: 'q' }, editor, jest.fn())
expect(editor.changeToType).not.toHaveBeenCalled()
})
test('onKeyDown does not toggle mark if CTRL/CMD + wrong key is pressed', () => {
const editor = {
changeToType: jest.fn()
}
FormatPlugin.onKeyDown({ ctrlKey: true, key: 'f' }, editor, jest.fn())
expect(editor.changeToType).not.toHaveBeenCalled()
})
test('onKeyDown toggles marks if CTRL/CMD + key is pressed', () => {
const editor = {
changeToType: jest.fn()
}
const mockEvent = {
metaKey: true,
shiftKey: true,
key: 'k',
preventDefault: jest.fn()
}
mockEvent.key = 'c'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(CODE_NODE)
mockEvent.key = ' '
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(TEXT_NODE)
mockEvent.key = '1'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(HEADING_NODE, { headingLevel: 1 })
mockEvent.key = '!'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(HEADING_NODE, { headingLevel: 1 })
mockEvent.key = '2'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(HEADING_NODE, { headingLevel: 2 })
mockEvent.key = '@'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(HEADING_NODE, { headingLevel: 2 })
mockEvent.key = '3'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(HEADING_NODE, { headingLevel: 3 })
mockEvent.key = '#'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(HEADING_NODE, { headingLevel: 3 })
mockEvent.key = '4'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(HEADING_NODE, { headingLevel: 4 })
mockEvent.key = '$'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(HEADING_NODE, { headingLevel: 4 })
mockEvent.key = '5'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(HEADING_NODE, { headingLevel: 5 })
mockEvent.key = '%'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(HEADING_NODE, { headingLevel: 5 })
mockEvent.key = '6'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(HEADING_NODE, { headingLevel: 6 })
mockEvent.key = '^'
FormatPlugin.onKeyDown(mockEvent, editor, jest.fn())
expect(editor.changeToType).toHaveBeenCalledWith(HEADING_NODE, { headingLevel: 6 })
})
test('changeToType calls item.switchToType', () => {
const editor = {
children: [
{
type: 'mock-node',
children: [{ text: 'mockText' }]
}
],
selection: {
anchor: { path: [0, 0], offset: 1 },
focus: { path: [1, 0], offset: 1 }
},
isInline: () => false,
isVoid: () => false
}
const item = {
switchType: {
'mock-type': jest.fn()
}
}
const spy = jest.spyOn(Common.Registry, 'getItemForType')
spy.mockReturnValueOnce(item)
FormatPlugin.commands.changeToType(editor, 'mock-type')
expect(item.switchType['mock-type']).toHaveBeenCalled()
})
test('changeToType does not call item.switchToType when the value doent exist', () => {
const editor = {
children: [
{
type: 'mock-node',
children: [{ text: 'mockText' }]
}
],
selection: {
anchor: { path: [0, 0], offset: 1 },
focus: { path: [1, 0], offset: 1 }
},
isInline: () => false,
isVoid: () => false
}
const item = {
switchType: {
'mock-type': jest.fn()
}
}
const spy = jest.spyOn(Common.Registry, 'getItemForType')
spy.mockReturnValueOnce(item)
FormatPlugin.commands.changeToType(editor, 'mock-other-type')
expect(item.switchType['mock-type']).not.toHaveBeenCalled()
})
})