2015-12-31 00:36:47 +08:00
|
|
|
import * as adapter from '../../src/extensions/composer-extension-adapter';
|
|
|
|
import {DOMUtils} from 'nylas-exports';
|
|
|
|
|
|
|
|
const selection = 'selection';
|
|
|
|
const node = 'node';
|
|
|
|
const event = 'event';
|
|
|
|
const extra = 'extra';
|
|
|
|
const editor = {
|
|
|
|
rootNode: node,
|
|
|
|
currentSelection() {
|
|
|
|
return selection;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('ComposerExtensionAdapter', ()=> {
|
|
|
|
describe('adaptOnInput', ()=> {
|
|
|
|
it('adapts correctly if onContentChanged already defined', ()=> {
|
|
|
|
const onInputSpy = jasmine.createSpy('onInput');
|
|
|
|
const extension = {
|
|
|
|
onContentChanged() {},
|
|
|
|
onInput(ev, editableNode, sel) {
|
|
|
|
onInputSpy(ev, editableNode, sel);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
adapter.adaptOnInput(extension);
|
|
|
|
extension.onContentChanged({editor, mutations: []});
|
|
|
|
expect(onInputSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adapts correctly when signature is (event, ...)', ()=> {
|
|
|
|
const onInputSpy = jasmine.createSpy('onInput');
|
|
|
|
const extension = {
|
|
|
|
onInput(ev, editableNode, sel) {
|
|
|
|
onInputSpy(ev, editableNode, sel);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
adapter.adaptOnInput(extension);
|
|
|
|
expect(extension.onContentChanged).toBeDefined();
|
|
|
|
extension.onContentChanged({editor, mutations: []});
|
|
|
|
expect(onInputSpy).toHaveBeenCalledWith([], node, selection);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adapts correctly when signature is (editableNode, selection, ...)', ()=> {
|
|
|
|
const onInputSpy = jasmine.createSpy('onInput');
|
|
|
|
const extension = {
|
|
|
|
onInput(editableNode, sel, ev) {
|
|
|
|
onInputSpy(editableNode, sel, ev);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
adapter.adaptOnInput(extension);
|
|
|
|
expect(extension.onContentChanged).toBeDefined();
|
|
|
|
extension.onContentChanged({editor, mutations: []});
|
|
|
|
expect(onInputSpy).toHaveBeenCalledWith(node, selection, []);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('adaptOnTabDown', ()=> {
|
|
|
|
it('adapts onTabDown correctly', ()=> {
|
|
|
|
const onTabDownSpy = jasmine.createSpy('onTabDownSpy');
|
|
|
|
const mockEvent = {key: 'Tab'};
|
|
|
|
const range = 'range';
|
|
|
|
spyOn(DOMUtils, 'getRangeInScope').andReturn(range);
|
|
|
|
const extension = {
|
|
|
|
onTabDown(editableNode, rn, ev) {
|
|
|
|
onTabDownSpy(editableNode, rn, ev);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
adapter.adaptOnTabDown(extension, 'method');
|
|
|
|
expect(extension.onKeyDown).toBeDefined();
|
|
|
|
extension.onKeyDown({editor, event: mockEvent});
|
|
|
|
expect(onTabDownSpy).toHaveBeenCalledWith(node, range, mockEvent);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-01-04 23:50:24 +08:00
|
|
|
describe('adaptContenteditableMethod', ()=> {
|
2015-12-31 00:36:47 +08:00
|
|
|
it('adapts correctly when signature is (editor, ...)', ()=> {
|
|
|
|
const methodSpy = jasmine.createSpy('methodSpy');
|
|
|
|
const extension = {
|
|
|
|
method(editor, ev, other) {
|
|
|
|
methodSpy(editor, ev, other);
|
|
|
|
},
|
|
|
|
};
|
2016-01-04 23:50:24 +08:00
|
|
|
adapter.adaptContenteditableMethod(extension, 'method');
|
2015-12-31 00:36:47 +08:00
|
|
|
extension.method({editor, event, extra});
|
|
|
|
expect(methodSpy).toHaveBeenCalledWith(editor, event, extra);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adapts correctly when signature is (event, ...)', ()=> {
|
|
|
|
const methodSpy = jasmine.createSpy('methodSpy');
|
|
|
|
const extension = {
|
|
|
|
method(ev, editableNode, sel, other) {
|
|
|
|
methodSpy(ev, editableNode, sel, other);
|
|
|
|
},
|
|
|
|
};
|
2016-01-04 23:50:24 +08:00
|
|
|
adapter.adaptContenteditableMethod(extension, 'method');
|
2015-12-31 00:36:47 +08:00
|
|
|
extension.method({editor, event, extra});
|
|
|
|
expect(methodSpy).toHaveBeenCalledWith(event, node, selection, extra);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adapts correctly when signature is (editableNode, selection, ...)', ()=> {
|
|
|
|
const methodSpy = jasmine.createSpy('methodSpy');
|
|
|
|
const extension = {
|
|
|
|
method(editableNode, sel, ev, other) {
|
|
|
|
methodSpy(editableNode, sel, ev, other);
|
|
|
|
},
|
|
|
|
};
|
2016-01-04 23:50:24 +08:00
|
|
|
adapter.adaptContenteditableMethod(extension, 'method');
|
2015-12-31 00:36:47 +08:00
|
|
|
extension.method({editor, event, extra});
|
|
|
|
expect(methodSpy).toHaveBeenCalledWith(node, selection, event, extra);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adapts correctly when using mutations instead of an event', ()=> {
|
|
|
|
const methodSpy = jasmine.createSpy('methodSpy');
|
|
|
|
const extension = {
|
|
|
|
method(editor, mutations) {
|
|
|
|
methodSpy(editor, mutations);
|
|
|
|
},
|
|
|
|
};
|
2016-01-04 23:50:24 +08:00
|
|
|
adapter.adaptContenteditableMethod(extension, 'method');
|
2015-12-31 00:36:47 +08:00
|
|
|
extension.method({editor, mutations: []});
|
|
|
|
expect(methodSpy).toHaveBeenCalledWith(editor, []);
|
|
|
|
});
|
|
|
|
});
|
2016-01-04 23:50:24 +08:00
|
|
|
|
|
|
|
describe('adaptComposerMethod', ()=> {
|
|
|
|
it('adapts correctly for finalizeSessionBeforeSending', ()=> {
|
|
|
|
const methodSpy = jasmine.createSpy('methodSpy');
|
|
|
|
const session = 'session';
|
|
|
|
const extension = {
|
|
|
|
finalizeSessionBeforeSending(sess) {
|
|
|
|
methodSpy(sess);
|
2016-01-05 02:23:24 +08:00
|
|
|
return 'result';
|
2016-01-04 23:50:24 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
adapter.adaptComposerMethod(extension, 'finalizeSessionBeforeSending');
|
2016-01-05 02:23:24 +08:00
|
|
|
const res = extension.finalizeSessionBeforeSending({session});
|
|
|
|
expect(res).toEqual('result');
|
2016-01-04 23:50:24 +08:00
|
|
|
expect(methodSpy).toHaveBeenCalledWith(session);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('adapts correctly for other composer extension methods', ()=> {
|
|
|
|
const methodSpy = jasmine.createSpy('methodSpy');
|
|
|
|
const draft = 'draft';
|
|
|
|
const extension = {
|
|
|
|
warningsForSending(dr) {
|
|
|
|
methodSpy(dr);
|
2016-01-05 02:23:24 +08:00
|
|
|
return 'result';
|
2016-01-04 23:50:24 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
adapter.adaptComposerMethod(extension, 'warningsForSending');
|
2016-01-05 02:23:24 +08:00
|
|
|
const res = extension.warningsForSending({draft});
|
|
|
|
expect(res).toEqual('result');
|
2016-01-04 23:50:24 +08:00
|
|
|
expect(methodSpy).toHaveBeenCalledWith(draft);
|
|
|
|
});
|
|
|
|
});
|
2015-12-31 00:36:47 +08:00
|
|
|
});
|