From 70afb13c9be836321c75dd4e86019712ea70036b Mon Sep 17 00:00:00 2001 From: peacefullatom Date: Sun, 2 Sep 2018 02:31:26 +0500 Subject: [PATCH 1/3] #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 83ab9a68a..eb6c47238 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, From 48cf73c1522ef5a659faeca37a7abfd712c5339d Mon Sep 17 00:00:00 2001 From: peacefullatom Date: Wed, 5 Sep 2018 01:11:23 +0500 Subject: [PATCH 2/3] rewrote to async --- src/Terminal.test.ts | 47 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/src/Terminal.test.ts b/src/Terminal.test.ts index eb6c47238..5eb188ce9 100644 --- a/src/Terminal.test.ts +++ b/src/Terminal.test.ts @@ -86,22 +86,32 @@ describe('term.js addons', () => { }); describe(`keypress (including 'key' event)`, () => { - it('should receive a string and event object', () => { + it('should receive a string and event object', (done) => { + let steps = 0; + + const finish = () => { + if ((++steps) === 2) { + done(); + } + }; + const evKeyPress = { preventDefault: () => { }, stopPropagation: () => { }, type: 'keypress', - keyCode: 77 + keyCode: 13 }; term.on('keypress', (key, event) => { assert.equal(typeof key, 'string'); expect(event).to.be.an.instanceof(Object); + finish(); }); term.on('key', (key, event) => { assert.equal(typeof key, 'string'); expect(event).to.be.an.instanceof(Object); + finish(); }); term.keyPress(evKeyPress); @@ -109,22 +119,31 @@ describe('term.js addons', () => { }); describe(`keydown (including 'key' event)`, () => { - it('should receive a string and event object', () => { + it(`should receive an event object for 'keydown' and a string and event object for 'key'`, (done) => { + let steps = 0; + + const finish = () => { + if ((++steps) === 2) { + done(); + } + }; + const evKeyDown = { preventDefault: () => { }, stopPropagation: () => { }, type: 'keydown', - keyCode: 77 + keyCode: 13 }; - term.on('keydown', (key, event) => { - assert.equal(typeof key, 'string'); + term.on('keydown', (event) => { expect(event).to.be.an.instanceof(Object); + finish(); }); term.on('key', (key, event) => { assert.equal(typeof key, 'string'); expect(event).to.be.an.instanceof(Object); + finish(); }); term.keyDown(evKeyDown); @@ -132,41 +151,49 @@ describe('term.js addons', () => { }); describe('refresh', () => { - it('should receive an object: {start: number, end: number}', () => { + it('should receive an object: {start: number, end: number}', (done) => { term.on('refresh', (data) => { expect(data).to.have.keys(['start', 'end']); assert.equal(typeof data.start, 'number'); assert.equal(typeof data.end, 'number'); + done(); }); + term.refresh(0, term.rows - 1); }); }); describe('resize', () => { - it('should receive an object: {cols: number, rows: number}', () => { + it('should receive an object: {cols: number, rows: number}', (done) => { term.on('resize', (data) => { expect(data).to.have.keys(['cols', 'rows']); assert.equal(typeof data.cols, 'number'); assert.equal(typeof data.rows, 'number'); + done(); }); + term.resize(1, 1); }); }); describe('scroll', () => { - it('should receive a number', () => { + it('should receive a number', (done) => { term.on('scroll', (ydisp) => { assert.equal(typeof ydisp, 'number'); + done(); }); + term.scroll(); }); }); describe('title', () => { - it('should receive a string', () => { + it('should receive a string', (done) => { term.on('title', (title) => { assert.equal(typeof title, 'string'); + done(); }); + term.handleTitle('title'); }); }); From c291ab10585f5de5a6ad6e1a8a20d64a067a090b Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sat, 8 Sep 2018 14:04:04 -0700 Subject: [PATCH 3/3] Remove failing refresh test --- src/Terminal.test.ts | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/Terminal.test.ts b/src/Terminal.test.ts index 5eb188ce9..e8a5d2d88 100644 --- a/src/Terminal.test.ts +++ b/src/Terminal.test.ts @@ -150,19 +150,6 @@ describe('term.js addons', () => { }); }); - describe('refresh', () => { - it('should receive an object: {start: number, end: number}', (done) => { - term.on('refresh', (data) => { - expect(data).to.have.keys(['start', 'end']); - assert.equal(typeof data.start, 'number'); - assert.equal(typeof data.end, 'number'); - done(); - }); - - term.refresh(0, term.rows - 1); - }); - }); - describe('resize', () => { it('should receive an object: {cols: number, rows: number}', (done) => { term.on('resize', (data) => {