From 70afb13c9be836321c75dd4e86019712ea70036b Mon Sep 17 00:00:00 2001 From: peacefullatom Date: Sun, 2 Sep 2018 02:31:26 +0500 Subject: [PATCH] #900 Add tests for events on Terminal --- src/Terminal.test.ts | 142 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 123 insertions(+), 19 deletions(-) diff --git a/src/Terminal.test.ts b/src/Terminal.test.ts index 83ab9a68..eb6c4723 100644 --- a/src/Terminal.test.ts +++ b/src/Terminal.test.ts @@ -26,7 +26,7 @@ describe('term.js addons', () => { beforeEach(() => { term = new TestTerminal(termOptions); - term.refresh = () => {}; + term.refresh = () => { }; (term).renderer = new MockRenderer(); term.viewport = new MockViewport(); (term)._compositionHelper = new MockCompositionHelper(); @@ -37,8 +37,8 @@ describe('term.js addons', () => { }; (term).element = { classList: { - toggle: () => {}, - remove: () => {} + toggle: () => { }, + remove: () => { } } }; }); @@ -68,24 +68,128 @@ describe('term.js addons', () => { }); }); + describe('on', () => { + beforeEach(() => { + term.on('key', () => { }); + term.on('keypress', () => { }); + term.on('keydown', () => { }); + }); + + describe('data', () => { + it('should emit a data event', (done) => { + term.on('data', () => { + done(); + }); + + term.handler('fake'); + }); + }); + + describe(`keypress (including 'key' event)`, () => { + it('should receive a string and event object', () => { + const evKeyPress = { + preventDefault: () => { }, + stopPropagation: () => { }, + type: 'keypress', + keyCode: 77 + }; + + term.on('keypress', (key, event) => { + assert.equal(typeof key, 'string'); + expect(event).to.be.an.instanceof(Object); + }); + + term.on('key', (key, event) => { + assert.equal(typeof key, 'string'); + expect(event).to.be.an.instanceof(Object); + }); + + term.keyPress(evKeyPress); + }); + }); + + describe(`keydown (including 'key' event)`, () => { + it('should receive a string and event object', () => { + const evKeyDown = { + preventDefault: () => { }, + stopPropagation: () => { }, + type: 'keydown', + keyCode: 77 + }; + + term.on('keydown', (key, event) => { + assert.equal(typeof key, 'string'); + expect(event).to.be.an.instanceof(Object); + }); + + term.on('key', (key, event) => { + assert.equal(typeof key, 'string'); + expect(event).to.be.an.instanceof(Object); + }); + + term.keyDown(evKeyDown); + }); + }); + + describe('refresh', () => { + it('should receive an object: {start: number, end: number}', () => { + term.on('refresh', (data) => { + expect(data).to.have.keys(['start', 'end']); + assert.equal(typeof data.start, 'number'); + assert.equal(typeof data.end, 'number'); + }); + term.refresh(0, term.rows - 1); + }); + }); + + describe('resize', () => { + it('should receive an object: {cols: number, rows: number}', () => { + term.on('resize', (data) => { + expect(data).to.have.keys(['cols', 'rows']); + assert.equal(typeof data.cols, 'number'); + assert.equal(typeof data.rows, 'number'); + }); + term.resize(1, 1); + }); + }); + + describe('scroll', () => { + it('should receive a number', () => { + term.on('scroll', (ydisp) => { + assert.equal(typeof ydisp, 'number'); + }); + term.scroll(); + }); + }); + + describe('title', () => { + it('should receive a string', () => { + term.on('title', (title) => { + assert.equal(typeof title, 'string'); + }); + term.handleTitle('title'); + }); + }); + }); + describe('attachCustomKeyEventHandler', () => { const evKeyDown = { - preventDefault: () => {}, - stopPropagation: () => {}, + preventDefault: () => { }, + stopPropagation: () => { }, type: 'keydown', keyCode: 77 }; const evKeyPress = { - preventDefault: () => {}, - stopPropagation: () => {}, + preventDefault: () => { }, + stopPropagation: () => { }, type: 'keypress', keyCode: 77 }; beforeEach(() => { - term.handler = () => {}; - term.showCursor = () => {}; - term.clearSelection = () => {}; + term.handler = () => { }; + term.showCursor = () => { }; + term.clearSelection = () => { }; }); it('should process the keydown/keypress event based on what the handler returns', () => { @@ -305,8 +409,8 @@ describe('term.js addons', () => { type: 'keydown', key: 'a', keyCode: 65, - preventDefault: () => {}, - stopPropagation: () => {} + preventDefault: () => { }, + stopPropagation: () => { } }; term.buffer.ydisp = 0; @@ -473,9 +577,9 @@ describe('term.js addons', () => { let evKeyPress: any; beforeEach(() => { - term.handler = () => {}; - term.showCursor = () => {}; - term.clearSelection = () => {}; + term.handler = () => { }; + term.showCursor = () => { }; + term.clearSelection = () => { }; // term.compositionHelper = { // isComposing: false, // keydown: { @@ -485,15 +589,15 @@ describe('term.js addons', () => { // } // }; evKeyDown = { - preventDefault: () => {}, - stopPropagation: () => {}, + preventDefault: () => { }, + stopPropagation: () => { }, type: 'keydown', altKey: null, keyCode: null }; evKeyPress = { - preventDefault: () => {}, - stopPropagation: () => {}, + preventDefault: () => { }, + stopPropagation: () => { }, type: 'keypress', altKey: null, charCode: null,