Skip to content

Commit 7ef18cc

Browse files
authored
Merge pull request #1646 from peacefullatom/add-tests-for-events
#900 Add tests for events on Terminal
2 parents 0f28ccb + c291ab1 commit 7ef18cc

File tree

1 file changed

+137
-19
lines changed

1 file changed

+137
-19
lines changed

src/Terminal.test.ts

Lines changed: 137 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('term.js addons', () => {
2626

2727
beforeEach(() => {
2828
term = new TestTerminal(termOptions);
29-
term.refresh = () => {};
29+
term.refresh = () => { };
3030
(<any>term).renderer = new MockRenderer();
3131
term.viewport = new MockViewport();
3232
(<any>term)._compositionHelper = new MockCompositionHelper();
@@ -37,8 +37,8 @@ describe('term.js addons', () => {
3737
};
3838
(<any>term).element = {
3939
classList: {
40-
toggle: () => {},
41-
remove: () => {}
40+
toggle: () => { },
41+
remove: () => { }
4242
}
4343
};
4444
});
@@ -68,24 +68,142 @@ describe('term.js addons', () => {
6868
});
6969
});
7070

71+
describe('on', () => {
72+
beforeEach(() => {
73+
term.on('key', () => { });
74+
term.on('keypress', () => { });
75+
term.on('keydown', () => { });
76+
});
77+
78+
describe('data', () => {
79+
it('should emit a data event', (done) => {
80+
term.on('data', () => {
81+
done();
82+
});
83+
84+
term.handler('fake');
85+
});
86+
});
87+
88+
describe(`keypress (including 'key' event)`, () => {
89+
it('should receive a string and event object', (done) => {
90+
let steps = 0;
91+
92+
const finish = () => {
93+
if ((++steps) === 2) {
94+
done();
95+
}
96+
};
97+
98+
const evKeyPress = <KeyboardEvent>{
99+
preventDefault: () => { },
100+
stopPropagation: () => { },
101+
type: 'keypress',
102+
keyCode: 13
103+
};
104+
105+
term.on('keypress', (key, event) => {
106+
assert.equal(typeof key, 'string');
107+
expect(event).to.be.an.instanceof(Object);
108+
finish();
109+
});
110+
111+
term.on('key', (key, event) => {
112+
assert.equal(typeof key, 'string');
113+
expect(event).to.be.an.instanceof(Object);
114+
finish();
115+
});
116+
117+
term.keyPress(evKeyPress);
118+
});
119+
});
120+
121+
describe(`keydown (including 'key' event)`, () => {
122+
it(`should receive an event object for 'keydown' and a string and event object for 'key'`, (done) => {
123+
let steps = 0;
124+
125+
const finish = () => {
126+
if ((++steps) === 2) {
127+
done();
128+
}
129+
};
130+
131+
const evKeyDown = <KeyboardEvent>{
132+
preventDefault: () => { },
133+
stopPropagation: () => { },
134+
type: 'keydown',
135+
keyCode: 13
136+
};
137+
138+
term.on('keydown', (event) => {
139+
expect(event).to.be.an.instanceof(Object);
140+
finish();
141+
});
142+
143+
term.on('key', (key, event) => {
144+
assert.equal(typeof key, 'string');
145+
expect(event).to.be.an.instanceof(Object);
146+
finish();
147+
});
148+
149+
term.keyDown(evKeyDown);
150+
});
151+
});
152+
153+
describe('resize', () => {
154+
it('should receive an object: {cols: number, rows: number}', (done) => {
155+
term.on('resize', (data) => {
156+
expect(data).to.have.keys(['cols', 'rows']);
157+
assert.equal(typeof data.cols, 'number');
158+
assert.equal(typeof data.rows, 'number');
159+
done();
160+
});
161+
162+
term.resize(1, 1);
163+
});
164+
});
165+
166+
describe('scroll', () => {
167+
it('should receive a number', (done) => {
168+
term.on('scroll', (ydisp) => {
169+
assert.equal(typeof ydisp, 'number');
170+
done();
171+
});
172+
173+
term.scroll();
174+
});
175+
});
176+
177+
describe('title', () => {
178+
it('should receive a string', (done) => {
179+
term.on('title', (title) => {
180+
assert.equal(typeof title, 'string');
181+
done();
182+
});
183+
184+
term.handleTitle('title');
185+
});
186+
});
187+
});
188+
71189
describe('attachCustomKeyEventHandler', () => {
72190
const evKeyDown = <KeyboardEvent>{
73-
preventDefault: () => {},
74-
stopPropagation: () => {},
191+
preventDefault: () => { },
192+
stopPropagation: () => { },
75193
type: 'keydown',
76194
keyCode: 77
77195
};
78196
const evKeyPress = <KeyboardEvent>{
79-
preventDefault: () => {},
80-
stopPropagation: () => {},
197+
preventDefault: () => { },
198+
stopPropagation: () => { },
81199
type: 'keypress',
82200
keyCode: 77
83201
};
84202

85203
beforeEach(() => {
86-
term.handler = () => {};
87-
term.showCursor = () => {};
88-
term.clearSelection = () => {};
204+
term.handler = () => { };
205+
term.showCursor = () => { };
206+
term.clearSelection = () => { };
89207
});
90208

91209
it('should process the keydown/keypress event based on what the handler returns', () => {
@@ -305,8 +423,8 @@ describe('term.js addons', () => {
305423
type: 'keydown',
306424
key: 'a',
307425
keyCode: 65,
308-
preventDefault: () => {},
309-
stopPropagation: () => {}
426+
preventDefault: () => { },
427+
stopPropagation: () => { }
310428
};
311429

312430
term.buffer.ydisp = 0;
@@ -473,9 +591,9 @@ describe('term.js addons', () => {
473591
let evKeyPress: any;
474592

475593
beforeEach(() => {
476-
term.handler = () => {};
477-
term.showCursor = () => {};
478-
term.clearSelection = () => {};
594+
term.handler = () => { };
595+
term.showCursor = () => { };
596+
term.clearSelection = () => { };
479597
// term.compositionHelper = {
480598
// isComposing: false,
481599
// keydown: {
@@ -485,15 +603,15 @@ describe('term.js addons', () => {
485603
// }
486604
// };
487605
evKeyDown = {
488-
preventDefault: () => {},
489-
stopPropagation: () => {},
606+
preventDefault: () => { },
607+
stopPropagation: () => { },
490608
type: 'keydown',
491609
altKey: null,
492610
keyCode: null
493611
};
494612
evKeyPress = {
495-
preventDefault: () => {},
496-
stopPropagation: () => {},
613+
preventDefault: () => { },
614+
stopPropagation: () => { },
497615
type: 'keypress',
498616
altKey: null,
499617
charCode: null,

0 commit comments

Comments
 (0)