Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

#900 Add tests for events on Terminal #1646

Merged
merged 6 commits into from Sep 8, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
142 changes: 123 additions & 19 deletions src/Terminal.test.ts
Expand Up @@ -26,7 +26,7 @@ describe('term.js addons', () => {

beforeEach(() => {
term = new TestTerminal(termOptions);
term.refresh = () => {};
term.refresh = () => { };
(<any>term).renderer = new MockRenderer();
term.viewport = new MockViewport();
(<any>term)._compositionHelper = new MockCompositionHelper();
Expand All @@ -37,8 +37,8 @@ describe('term.js addons', () => {
};
(<any>term).element = {
classList: {
toggle: () => {},
remove: () => {}
toggle: () => { },
remove: () => { }
}
};
});
Expand Down Expand Up @@ -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 = <KeyboardEvent>{
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test needs to be asynchronous and verify both events pass, as currently it will pass if key and keypress don't fire

});
});

describe(`keydown (including 'key' event)`, () => {
it('should receive a string and event object', () => {
const evKeyDown = <KeyboardEvent>{
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test needs to be asynchronous and verify both events pass, as currently it will pass if key and keydown don't fire

});
});

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test needs to be asynchronous and verify the event happens (add done to end of event listener)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After additional investigation, I've found out that refresh is a dummy function.

Next thing is that the MockRenderer also does not implement any functionality.

There are two ways:

  1. Extend mock functionality (because we're unable to use real renderers due to lack of window object, etc).
  2. Skip implementation of this test completely or implement a dummy test.

Don't know which one is preferable. 馃槂

});
});

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add done to end of event handler

});
});

describe('scroll', () => {
it('should receive a number', () => {
term.on('scroll', (ydisp) => {
assert.equal(typeof ydisp, 'number');
});
term.scroll();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add done to end of event handler

});
});

describe('title', () => {
it('should receive a string', () => {
term.on('title', (title) => {
assert.equal(typeof title, 'string');
});
term.handleTitle('title');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add done to end of event handler

});
});
});

describe('attachCustomKeyEventHandler', () => {
const evKeyDown = <KeyboardEvent>{
preventDefault: () => {},
stopPropagation: () => {},
preventDefault: () => { },
stopPropagation: () => { },
type: 'keydown',
keyCode: 77
};
const evKeyPress = <KeyboardEvent>{
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', () => {
Expand Down Expand Up @@ -305,8 +409,8 @@ describe('term.js addons', () => {
type: 'keydown',
key: 'a',
keyCode: 65,
preventDefault: () => {},
stopPropagation: () => {}
preventDefault: () => { },
stopPropagation: () => { }
};

term.buffer.ydisp = 0;
Expand Down Expand Up @@ -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: {
Expand All @@ -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,
Expand Down