forked from yabwe/medium-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanchor.spec.js
executable file
·147 lines (134 loc) · 6.29 KB
/
anchor.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
144
145
146
147
/*global MediumEditor, describe, it, expect, spyOn,
afterEach, beforeEach, selectElementContents,
jasmine, fireEvent, console, tearDown*/
describe('Anchor Button TestCase', function () {
'use strict';
beforeEach(function () {
jasmine.clock().install();
this.el = document.createElement('div');
this.el.className = 'editor';
this.el.innerHTML = 'lorem ipsum';
document.body.appendChild(this.el);
});
afterEach(function () {
tearDown(this.el);
jasmine.clock().uninstall();
});
describe('Click', function () {
it('should display the anchor form when toolbar is visible', function () {
spyOn(MediumEditor.prototype, 'showAnchorForm').and.callThrough();
var button,
editor = new MediumEditor('.editor');
selectElementContents(editor.elements[0]);
fireEvent(editor.elements[0], 'mouseup');
jasmine.clock().tick(1);
button = editor.toolbar.querySelector('[data-element="a"]');
fireEvent(button, 'click');
expect(editor.toolbarActions.style.display).toBe('none');
expect(editor.anchorForm.style.display).toBe('block');
expect(editor.showAnchorForm).toHaveBeenCalled();
});
it('should display the toolbar actions when anchor form is visible', function () {
spyOn(MediumEditor.prototype, 'showToolbarActions').and.callThrough();
var button,
editor = new MediumEditor('.editor');
selectElementContents(editor.elements[0]);
fireEvent(editor.elements[0], 'mouseup');
jasmine.clock().tick(1);
button = editor.toolbar.querySelector('[data-element="a"]');
editor.anchorForm.style.display = 'block';
fireEvent(button, 'click');
expect(editor.toolbarActions.style.display).toBe('block');
expect(editor.anchorForm.style.display).toBe('none');
expect(editor.showToolbarActions).toHaveBeenCalled();
});
it('should unlink when selection is a link', function () {
spyOn(document, 'execCommand').and.callThrough();
this.el.innerHTML = '<a href="#">link</a>';
var button,
editor = new MediumEditor('.editor');
selectElementContents(editor.elements[0]);
fireEvent(editor.elements[0], 'mouseup');
jasmine.clock().tick(1);
button = editor.toolbar.querySelector('[data-element="a"]');
fireEvent(button, 'click');
expect(this.el.innerHTML, 'link');
expect(document.execCommand).toHaveBeenCalled();
});
});
describe('Link Creation', function () {
it('should create a link when user presses enter', function () {
spyOn(MediumEditor.prototype, 'createLink').and.callThrough();
var editor = new MediumEditor('.editor'),
button,
input;
selectElementContents(editor.elements[0]);
button = editor.toolbar.querySelector('[data-element="a"]');
fireEvent(button, 'click');
input = editor.anchorForm.querySelector('input');
input.value = 'test';
fireEvent(input, 'keyup', 13);
expect(editor.createLink).toHaveBeenCalled();
});
it('shouldn\'t create a link when user presses enter without value', function () {
spyOn(MediumEditor.prototype, 'createLink').and.callThrough();
var editor = new MediumEditor('.editor'),
button,
input;
selectElementContents(editor.elements[0]);
button = editor.toolbar.querySelector('[data-element="a"]');
fireEvent(button, 'click');
input = editor.anchorForm.querySelector('input');
input.value = '';
fireEvent(input, 'keyup', 13);
expect(editor.elements[0].querySelector('a')).toBeNull();
});
it('should add http:// if need be and checkLinkFormat option is set to true', function () {
var editor = new MediumEditor('.editor', {
checkLinkFormat: true
}),
input = editor.anchorForm.querySelector('input');
selectElementContents(editor.elements[0]);
input.value = 'test.com';
editor.createLink(input);
expect(editor.elements[0].querySelector('a').href).toBe('http://test.com/');
});
it('should not change protocol when a valid one is included', function () {
var editor = new MediumEditor('.editor', {
checkLinkFormat: true
}),
input = editor.anchorForm.querySelector('input'),
validUrl = 'mailto:test.com';
selectElementContents(editor.elements[0]);
input.value = validUrl;
editor.createLink(input);
expect(editor.elements[0].querySelector('a').href).toBe(validUrl);
});
it('should add target="_blank" when respective option is set to true', function () {
var editor = new MediumEditor('.editor', {
targetBlank: true
}),
input = editor.anchorForm.querySelector('input');
selectElementContents(editor.elements[0]);
input.value = 'http://test.com';
editor.createLink(input);
expect(editor.elements[0].querySelector('a').target).toBe('_blank');
});
});
describe('Cancel', function () {
it('should close the link form when user clicks on cancel', function () {
spyOn(MediumEditor.prototype, 'showToolbarActions').and.callThrough();
var editor = new MediumEditor('.editor'),
button,
cancel;
selectElementContents(editor.elements[0]);
button = editor.toolbar.querySelector('[data-element="a"]');
cancel = editor.anchorForm.querySelector('a.medium-editor-toobar-anchor-close');
fireEvent(button, 'click');
expect(editor.anchorForm.style.display).toBe('block');
fireEvent(cancel, 'click');
expect(editor.showToolbarActions).toHaveBeenCalled();
expect(editor.anchorForm.style.display).toBe('none');
});
});
});