This repository was archived by the owner on May 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathCodeMirror.spec.ts
More file actions
75 lines (68 loc) · 2.46 KB
/
CodeMirror.spec.ts
File metadata and controls
75 lines (68 loc) · 2.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
jest.mock('codemirror', () => ({
Pos: (line: number, ch: number) => ({ line, ch }),
}))
jest.mock('codemirror/addon/runmode/runmode', () => undefined)
jest.mock('codemirror/mode/markdown/markdown', () => undefined)
jest.mock('codemirror/mode/javascript/javascript', () => undefined)
jest.mock('codemirror/mode/css/css', () => undefined)
jest.mock('codemirror/mode/diff/diff', () => undefined)
jest.mock('codemirror/lib/codemirror.css', () => undefined)
jest.mock('codemirror/addon/edit/continuelist', () => undefined)
jest.mock('codemirror/keymap/vim', () => undefined)
jest.mock('codemirror/addon/hint/show-hint', () => undefined)
jest.mock('codemirror/addon/hint/show-hint.css', () => undefined)
jest.mock('codemirror/keymap/sublime', () => undefined)
jest.mock('codemirror/keymap/emacs', () => undefined)
jest.mock('codemirror/addon/scroll/scrollpastend', () => undefined)
jest.mock('../../../design/lib/codemirror/util', () => ({
loadMode: jest.fn(),
}))
import {
getCodeBlockHintContext,
getCodeBlockModeSuggestions,
} from './CodeMirror'
describe('CodeMirror code block mode hints', () => {
test('detects backtick and tilde code fences', () => {
expect(getCodeBlockHintContext('```', 3)).toEqual({
fenceMarker: '```',
language: '',
})
expect(getCodeBlockHintContext('~~~js', 5)).toEqual({
fenceMarker: '~~~',
language: 'js',
})
})
test('offers a plain code block option before initial language hints', () => {
const suggestions = getCodeBlockModeSuggestions('', '```')
expect(suggestions[0].text).toEqual('')
expect(suggestions[0].displayText).toEqual('Plain code block')
expect(suggestions.map((suggestion) => suggestion.text)).toContain('js')
expect(suggestions.map((suggestion) => suggestion.text)).toContain('python')
})
test('selected language hint completes the code block', () => {
const jsSuggestion = getCodeBlockModeSuggestions('j', '```').find(
(suggestion) => suggestion.text === 'js'
)
const cm = {
replaceRange: jest.fn(),
setCursor: jest.fn(),
}
expect(jsSuggestion).toBeDefined()
jsSuggestion!.hint!(
cm as any,
{
from: { line: 4, ch: 0 },
to: { line: 4, ch: 4 },
list: [],
},
jsSuggestion!
)
expect(cm.replaceRange).toHaveBeenCalledWith(
'```js\n\n```',
{ line: 4, ch: 0 },
{ line: 4, ch: 4 },
'complete'
)
expect(cm.setCursor).toHaveBeenCalledWith({ line: 5, ch: 0 })
})
})