|
| 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"> </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"> </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> </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"> </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"> </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