Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 0c69c1a

Browse files
committed
Merge pull request #232 from orthes/jasmine
Rewrite tests to use Jasmine insted of QUnit
2 parents ecce1bf + 38dcdf1 commit 0c69c1a

35 files changed

+1117
-9340
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ examples/uploads/*
77
server.js
88
.tm_properties
99
.project
10+
reports
11+
.grunt/
12+
_SpecRunner.html

.jshintrc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@
1111
"start": false,
1212
"sinon": false,
1313
"alert": false,
14-
"FB": false
14+
"FB": false,
15+
"describe": false,
16+
"it": false,
17+
"expect": false,
18+
"beforeEach": false,
19+
"afterEach": false,
20+
"MediumEditor": false,
21+
"jasmine": false,
22+
"spyOn": false,
23+
"placeCaret": false,
24+
"require": false
1525
},
1626

1727
"jquery" : true,
@@ -35,4 +45,4 @@
3545
"sub": true,
3646
"strict": false,
3747
"unused": true
38-
}
48+
}

Gruntfile.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function(grunt) {
1+
module.exports = function (grunt) {
22
// show elapsed time at the end
33
require('time-grunt')(grunt);
44
// load grunt tasks just in time
@@ -34,15 +34,37 @@ module.exports = function(grunt) {
3434
options: {
3535
jshintrc: true
3636
},
37-
files: ['src/js/*.js', '!src/js/templates.js', 'test/*.js']
37+
files: ['src/js/*.js', '!src/js/templates.js', 'spec/*.js']
3838
},
3939

40-
blanket_qunit: {
41-
options: {
42-
urls: ['test.html?coverage=true&gruntReport'],
43-
threshold: 70
44-
},
45-
unit: {}
40+
jasmine: {
41+
suite: {
42+
src: 'src/js/*.js',
43+
options: {
44+
specs: ['spec/*.spec.js'],
45+
helpers: 'spec/helpers/*.js',
46+
vendor: [
47+
'bower_components/jquery/dist/jquery.min.js',
48+
'bower_components/medium-editor/dist/js/medium-editor.min.js',
49+
'bower_components/handlebars/handlebars.runtime.min.js',
50+
'bower_components/blueimp-file-upload/js/vendor/jquery.ui.widget.js',
51+
'bower_components/blueimp-file-upload/js/jquery.iframe-transport.js',
52+
'bower_components/blueimp-file-upload/js/jquery.fileupload.js',
53+
'bower_components/jquery-sortable/source/js/jquery-sortable-min.js'
54+
],
55+
template: require('grunt-template-jasmine-istanbul'),
56+
templateOptions: {
57+
coverage: 'reports/jasmine/coverage.json',
58+
report: [{
59+
type: 'lcov',
60+
options: {
61+
dir: 'reports/jasmine/lcov'
62+
}
63+
}]
64+
},
65+
summary: true
66+
}
67+
}
4668
},
4769

4870
sass: {
@@ -121,7 +143,7 @@ module.exports = function(grunt) {
121143
}
122144
});
123145

124-
grunt.registerTask('test', ['jshint', 'blanket_qunit']);
146+
grunt.registerTask('test', ['jshint', 'jasmine']);
125147
grunt.registerTask('js', ['test', 'handlebars', 'uglify', 'concat']);
126148
grunt.registerTask('css', ['sass', 'autoprefixer', 'csso', 'usebanner']);
127149
grunt.registerTask('default', ['js', 'css']);

bower.json

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,19 @@
1414
"**/.*",
1515
"node_modules",
1616
"bower_components",
17-
"test",
18-
"test.html",
17+
"spec",
1918
"examples",
2019
"Gruntfile.js",
2120
"package.json"
2221
],
2322
"dependencies": {
2423
"jquery": ">=1.9.0",
25-
"medium-editor": "^5.7.0",
26-
"handlebars": "~3.0.0",
24+
"medium-editor": "~5.9.0",
25+
"handlebars": "~4.0.0",
2726
"blueimp-file-upload": "~9.11.1",
2827
"jquery-sortable": "~0.9.12"
2928
},
3029
"devDependencies": {
31-
"qunit": "~1.18.0",
32-
"sinon": "~1.15.0",
33-
"sinon-qunit": "~2.0.0",
34-
"jquery-cycle2": "~2.1.6",
35-
"blanket": "~1.1.5"
30+
"jquery-cycle2": "~2.1.6"
3631
}
3732
}

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
"devDependencies": {
1616
"grunt": "~0.4.4",
1717
"grunt-autoprefixer": "^3.0.1",
18-
"grunt-banner": "^0.5.0",
19-
"grunt-blanket-qunit": "^0.2.0",
18+
"grunt-banner": "^0.6.0",
2019
"grunt-contrib-concat": "^0.5.1",
21-
"grunt-contrib-handlebars": "^0.10.2",
20+
"grunt-contrib-handlebars": "^0.11.0",
21+
"grunt-contrib-jasmine": "^0.9.2",
2222
"grunt-contrib-jshint": "~0.11.0",
2323
"grunt-contrib-uglify": "~0.9.1",
2424
"grunt-contrib-watch": "~0.6.1",
2525
"grunt-csso": "^0.7.0",
26-
"grunt-sass": "~1.0.0",
26+
"grunt-sass": "~1.1.0",
27+
"grunt-template-jasmine-istanbul": "^0.3.4",
2728
"jit-grunt": "^0.9.1",
2829
"time-grunt": "^1.1.0"
2930
},

spec/core.spec.js

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
describe("Core", function () {
2+
beforeEach(function () {
3+
$('body').append('<div id="fixtures"><div class="editable"></div></div>');
4+
this.$el = $('.editable');
5+
6+
jasmine.clock().install();
7+
});
8+
9+
afterEach(function () {
10+
$('#fixtures').remove();
11+
12+
jasmine.clock().uninstall();
13+
});
14+
15+
it('initializes plugin', function () {
16+
this.$el.mediumInsert();
17+
18+
expect(this.$el.hasClass('medium-editor-insert-plugin')).toBe(true);
19+
});
20+
21+
it('initializes plugin on a textarea', function () {
22+
var textareaId;
23+
24+
$('#fixtures').html('<div medium-editor-textarea-id="123"></div><textarea id="textarea" medium-editor-textarea-id="123">test</textarea>');
25+
this.$el = $('#textarea');
26+
27+
this.$el.mediumInsert();
28+
textareaId = this.$el.attr('medium-editor-textarea-id');
29+
30+
expect(this.$el.hasClass('medium-editor-insert-plugin')).toBe(false);
31+
expect(this.$el.siblings('[medium-editor-textarea-id="' + textareaId + '"]').hasClass('medium-editor-insert-plugin')).toBe(true);
32+
});
33+
34+
it('initializes addons', function () {
35+
spyOn($.fn, 'mediumInsertImages');
36+
37+
this.$el.data('plugin_mediumInsertImages', {
38+
options: {}
39+
});
40+
this.$el.mediumInsert();
41+
42+
expect($.fn.mediumInsertImages.calls.count()).toEqual(1);
43+
});
44+
45+
it('does nothing if there is no addon selected', function () {
46+
this.$el.mediumInsert({
47+
addons: false
48+
});
49+
50+
expect(this.$el.html()).toBe('');
51+
});
52+
53+
it('adds empty paragraph if there is no content', function () {
54+
this.$el.mediumInsert();
55+
56+
expect(this.$el.find('p').length).toEqual(1);
57+
});
58+
59+
it('wraps <br> into paragraph', function () {
60+
this.$el.html('<br>');
61+
62+
this.$el.mediumInsert();
63+
64+
expect(this.$el.find('p').length).toEqual(1);
65+
expect(this.$el.find('br').length).toEqual(1);
66+
});
67+
68+
it('wraps text content into paragraph', function () {
69+
this.$el.html('text');
70+
71+
this.$el.mediumInsert();
72+
73+
expect(this.$el.find('p').length).toEqual(1);
74+
});
75+
76+
it('adds empty paragraph at the end if the last element is an addon element', function () {
77+
this.$el.html('<div class="medium-insert-images"></div>');
78+
79+
this.$el.mediumInsert();
80+
81+
expect(this.$el.find('.medium-insert-images').next().is('p')).toBe(true);
82+
});
83+
84+
it('adds plugin\'s buttons to the $el', function () {
85+
this.$el.mediumInsert();
86+
87+
expect(this.$el.find('.medium-insert-buttons').length).toEqual(1);
88+
});
89+
90+
it('shows plugin\'s buttons after clicking on empty paragraph', function () {
91+
this.$el.html('<p id="paragraph">&nbsp;</p><p id="paragraph2" class="medium-insert-active">test</p>');
92+
93+
this.$el.mediumInsert();
94+
95+
// Place caret at the beginning of #paragraph
96+
placeCaret(document.getElementById('paragraph'), 0);
97+
98+
this.$el.find('#paragraph').click();
99+
jasmine.clock().tick(1);
100+
101+
expect(this.$el.find('.medium-insert-buttons').css('display')).toBe('block');
102+
expect(this.$el.find('#paragraph').hasClass('medium-insert-active')).toBe(true);
103+
expect(this.$el.find('#paragraph2').hasClass('medium-insert-active')).toBe(false);
104+
});
105+
106+
it('shows only addon button after clicking on addon paragraph', function () {
107+
this.$el.html('<p id="paragraph" class="medium-insert-images">&nbsp;</p><p id="paragraph2" class="medium-insert-active">test</p>');
108+
109+
this.$el.mediumInsert();
110+
111+
// Place caret at the beginning of #paragraph
112+
placeCaret(document.getElementById('paragraph'), 0);
113+
114+
this.$el.find('#paragraph').click();
115+
jasmine.clock().tick(101);
116+
117+
expect(this.$el.find('.medium-insert-buttons').css('display')).toBe('block');
118+
expect(this.$el.find('.medium-insert-buttons a[data-addon="embeds"]').parent().css('display')).toBe('none');
119+
});
120+
121+
it('hides plugin\'s buttons after clicking on non-empty paragraph', function () {
122+
this.$el.html('<p>&nbsp;</p><p id="paragraph2">test</p>');
123+
124+
this.$el.mediumInsert();
125+
126+
// Place caret at the beginning of #paragraph
127+
placeCaret(document.getElementById('paragraph2'), 0);
128+
129+
this.$el.find('#paragraph2').click();
130+
131+
expect(this.$el.find('.medium-insert-buttons').css('display')).toBe('none');
132+
});
133+
134+
it('toggles addons buttons after clicking on show button', function () {
135+
this.$el.html('<p id="paragraph">&nbsp;</p><p>test</p>');
136+
137+
this.$el.mediumInsert();
138+
139+
// Place caret at the beginning of #paragraph
140+
placeCaret(document.getElementById('paragraph'), 0);
141+
142+
this.$el.find('#paragraph').click();
143+
this.$el.find('.medium-insert-buttons-show').click();
144+
145+
expect(this.$el.find('.medium-insert-buttons-addons').css('display')).toBe('block');
146+
expect(this.$el.find('.medium-insert-buttons-show').hasClass('medium-insert-buttons-rotate')).toBe(true);
147+
148+
this.$el.find('.medium-insert-buttons-show').click();
149+
150+
expect(!this.$el.find('.medium-insert-buttons-show').hasClass('medium-insert-buttons-rotate')).toBe(true);
151+
});
152+
153+
it('calls addon\'s add function if addon\'s button is clicked', function () {
154+
var addonInstance;
155+
156+
this.$el.html('<p id="paragraph">&nbsp;</p><p>test</p>');
157+
158+
this.$el.mediumInsert({
159+
addons: {
160+
embeds: false
161+
}
162+
});
163+
addonInstance = this.$el.data('plugin_mediumInsertImages');
164+
165+
spyOn(addonInstance, 'add');
166+
167+
// Place caret at the beginning of #paragraph
168+
placeCaret(document.getElementById('paragraph'), 0);
169+
170+
this.$el.find('#paragraph').click();
171+
this.$el.find('.medium-insert-buttons-show').click();
172+
this.$el.find('.medium-insert-action').click();
173+
174+
expect(addonInstance.add.calls.count()).toBe(1);
175+
});
176+
177+
it('removes also plugin buttons on serialization', function () {
178+
var editor = new MediumEditor('.editable');
179+
180+
this.$el.mediumInsert({
181+
editor: editor
182+
});
183+
184+
expect(editor.serialize()['element-0'].value).toBe('<p><br></p>');
185+
});
186+
187+
it('disables plugin on editor destroy', function () {
188+
var editor = new MediumEditor(this.$el.get(0));
189+
190+
this.$el.mediumInsert({
191+
editor: editor
192+
});
193+
194+
editor.destroy();
195+
196+
expect(this.$el.data('plugin_mediumInsert').options.enabled).toBe(false);
197+
});
198+
199+
it('enables plugin on editor setup', function () {
200+
var editor = new MediumEditor(this.$el.get(0));
201+
202+
this.$el.mediumInsert({
203+
editor: editor
204+
});
205+
206+
editor.setup();
207+
208+
expect(this.$el.data('plugin_mediumInsert').options.enabled).toBe(true);
209+
});
210+
211+
it('displays placeholder dispite of plugin buttons', function () {
212+
var editor = new MediumEditor(this.$el.get(0));
213+
214+
this.$el.mediumInsert({
215+
editor: editor
216+
});
217+
218+
editor.getExtensionByName('placeholder').updatePlaceholder(this.$el.get(0));
219+
220+
expect(this.$el.hasClass('medium-editor-placeholder')).toBe(true);
221+
});
222+
223+
it('hides placeholder when there is a text', function () {
224+
var editor = new MediumEditor(this.$el.get(0));
225+
226+
this.$el.mediumInsert({
227+
editor: editor
228+
});
229+
230+
this.$el.addClass('medium-editor-placeholder');
231+
this.$el.prepend('<p>asd</p>');
232+
233+
editor.getExtensionByName('placeholder').updatePlaceholder(this.$el.get(0));
234+
235+
expect(this.$el.hasClass('medium-editor-placeholder')).toBe(false);
236+
});
237+
});

0 commit comments

Comments
 (0)