forked from yabwe/medium-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextension.spec.js
143 lines (118 loc) · 4.53 KB
/
extension.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*global MediumEditor, describe, it, expect, spyOn,
afterEach, beforeEach, selectElementContents,
jasmine, fireEvent, tearDown*/
describe('Extensions TestCase', function () {
'use strict';
beforeEach(function () {
this.el = document.createElement('div');
this.el.className = 'editor';
this.el.innerHTML = 'lorem ipsum';
document.body.appendChild(this.el);
});
afterEach(function () {
tearDown(this.el);
});
describe('Editor', function () {
it('should accept a number of extensions as parameter', function () {
var extensions = {
'extension1': {},
'extension2': {}
},
editor = new MediumEditor('.editor', {
extensions: extensions
});
expect(editor.options.extensions).toBe(extensions);
});
it('should call methods on all extensions with callExtensions is used', function () {
var Extension = function () {
},
ext1 = new Extension(),
ext2 = new Extension(),
editor = new MediumEditor('.editor', {
extensions: {
'one': ext1,
'two': ext2
}
});
Extension.prototype.aMethod = function (param) {
};
spyOn(ext1, 'aMethod');
spyOn(ext2, 'aMethod');
editor.callExtensions('aMethod', 'theParam');
expect(ext1.aMethod).toHaveBeenCalledWith('theParam');
expect(ext2.aMethod).toHaveBeenCalledWith('theParam');
});
});
describe('Button integration', function () {
var ExtensionWithElement = {
getButton: function () {
var button = document.createElement('button');
button.className = 'extension-button';
button.innerText = 'XXX';
return button;
}
},
ExtensionWithString = {
getButton: function () {
return '<button class="extension-button">XXX</button>';
}
};
it('should include extensions button into toolbar', function () {
var editor = new MediumEditor('.editor', {
buttons: ['dummy'],
extensions: {
'dummy': ExtensionWithElement
}
});
expect(editor.toolbar.querySelectorAll('.extension-button').length).toBe(1);
});
it('should include extensions button by string into the toolbar', function () {
var editor = new MediumEditor('.editor', {
buttons: ['dummy'],
extensions: {
'dummy': ExtensionWithString
}
});
expect(editor.toolbar.querySelectorAll('.extension-button').length).toBe(1);
});
it('should not include extensions button into toolbar that are not in "buttons"', function () {
var editor = new MediumEditor('.editor', {
buttons: ['bold'],
extensions: {
'dummy': ExtensionWithElement
}
});
expect(editor.toolbar.querySelectorAll('.extension-button').length).toBe(0);
});
});
describe('Pass editor instance', function () {
var ExtensionOne = function () {
this.parent = true;
},
ExtensionTwo = function () {},
extOne = new ExtensionOne(),
extTwo = new ExtensionTwo();
it('should check if extension class has parent attribute', function () {
var editor = new MediumEditor('.editor', {
extensions: {
'one': extOne,
'two': extTwo
}
});
expect(editor instanceof MediumEditor).toBeTruthy();
expect(extOne.parent).toBeTruthy();
expect(extTwo.parent).toBeUndefined();
});
it('should set the base attribute to be an instance of editor', function () {
var editor = new MediumEditor('.editor', {
extensions: {
'one': extOne,
'two': extTwo
}
});
expect(editor instanceof MediumEditor).toBeTruthy();
expect(extOne.base instanceof MediumEditor).toBeTruthy();
expect(extTwo.base).toBeUndefined();
});
});
});