-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
codeMirrorExt.spec.js
166 lines (127 loc) 路 4.74 KB
/
codeMirrorExt.spec.js
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
/**
* @fileoverview test codemirror extension
* @author NHN Ent. FE Development Lab <dl_javascript@nhnent.com>
*/
import $ from 'jquery';
import CodeMirrorExt from '../../src/js/codeMirrorExt';
describe('CodeMirrorExt', () => {
let codeMirrorExt, container;
beforeEach(() => {
jasmine.getStyleFixtures().fixturesPath = '/base';
loadStyleFixtures('src/css/tui-editor.css');
loadStyleFixtures('node_modules/codemirror/lib/codemirror.css');
container = document.createElement('div');
document.body.appendChild(container);
codeMirrorExt = new CodeMirrorExt(container);
});
afterEach(() => {
container.parentNode.removeChild(container);
});
describe('Initialize', () => {
it('make codemirror context', () => {
expect($('.CodeMirror').length).toEqual(1);
});
});
describe('replaceSelection', () => {
it('replace selection content with passed content', () => {
const selection = {
from: {
line: 0,
ch: 0
},
to: {
line: 0,
ch: 0
}
};
codeMirrorExt.replaceSelection('test', selection);
expect(codeMirrorExt.getValue()).toEqual('test');
});
it('if replace selection without selection, use current selection', () => {
codeMirrorExt.replaceSelection('test');
expect(codeMirrorExt.getValue()).toEqual('test');
});
it('replace with current cursor\'s containers offset', () => {
codeMirrorExt.replaceSelection('t');
codeMirrorExt.replaceSelection('e');
codeMirrorExt.replaceSelection('s');
codeMirrorExt.replaceSelection('t');
codeMirrorExt.replaceRelativeOffset('123', -2, 1);
expect(codeMirrorExt.getValue()).toEqual('te123t');
});
});
describe('move cursor to end or start', () => {
it('move cursor to end', () => {
codeMirrorExt.setValue('test\ntest\ntest\n');
codeMirrorExt.moveCursorToEnd();
expect(codeMirrorExt.getEditor().getCursor().line).toEqual(3);
expect(codeMirrorExt.getEditor().getCursor().ch).toEqual(0);
});
it('move cursor to start', () => {
codeMirrorExt.setValue('test\ntest\ntest\n');
codeMirrorExt.moveCursorToStart();
expect(codeMirrorExt.getEditor().getCursor().line).toEqual(0);
expect(codeMirrorExt.getEditor().getCursor().ch).toEqual(0);
});
});
describe('setValue', () => {
it('move cursor to end after setValue', () => {
codeMirrorExt.setValue('test\ntest\ntest\n');
expect(codeMirrorExt.getEditor().getCursor().line).toEqual(3);
expect(codeMirrorExt.getEditor().getCursor().ch).toEqual(0);
});
it('should not move cursor to end after setValue if false to `cursorToEnd` param', () => {
codeMirrorExt.setValue('test\ntest\ntest\n', false);
expect(codeMirrorExt.getEditor().getCursor().line).not.toEqual(3);
});
});
it('getRange() returns current selection range', () => {
const start = codeMirrorExt.getEditor().getCursor('from');
const end = codeMirrorExt.getEditor().getCursor('to');
const range = codeMirrorExt.getRange();
expect(range.start).toBeDefined();
expect(range.start.line).toEqual(start.line);
expect(range.start.ch).toEqual(start.ch);
expect(range.end).toBeDefined();
expect(range.end.line).toEqual(end.line);
expect(range.end.ch).toEqual(end.ch);
});
it('getCursor() returns current cursor', () => {
const cursor = codeMirrorExt.getCursor();
expect(cursor.line).toEqual(jasmine.any(Number));
expect(cursor.ch).toEqual(jasmine.any(Number));
});
it('getWrapperElement() returns wrapper element', () => {
const wrapper = codeMirrorExt.getWrapperElement();
expect(wrapper).toEqual(jasmine.any(HTMLElement));
expect($(wrapper.classList).hasClass('CodeMirror'));
});
describe('get, set scrollTop', () => {
it('get scrollTop', done => {
codeMirrorExt.setHeight(50);
codeMirrorExt.setValue('1\n2\n3\n4\n5\n1\n2\n3\n4\n5\n');
codeMirrorExt.scrollTop(10);
setTimeout(() => {
expect(codeMirrorExt.scrollTop()).not.toEqual(0);
done();
}, 500);
});
});
describe('event', () => {
it('on() add event handler for codemirror', () => {
const handler = jasmine.createSpy('handler');
codeMirrorExt.on('change', handler);
codeMirrorExt.setValue('changed');
expect(handler).toHaveBeenCalled();
});
it('off() remove event handler for codemirror', () => {
const handler = jasmine.createSpy('handler');
codeMirrorExt.on('change', handler);
codeMirrorExt.setValue('changed');
expect(handler.calls.count()).toEqual(1);
codeMirrorExt.off('change', handler);
codeMirrorExt.setValue('changed again');
expect(handler.calls.count()).toEqual(1);
});
});
});