import { GeneralSteps, Pipeline, Step } from '@ephox/agar'; import { UnitTest } from '@ephox/bedrock'; import { TinyApis, TinyLoader } from '@ephox/mcagar'; import { Uint8Array, Window } from '@ephox/sand'; import Actions from 'tinymce/themes/inlite/core/Actions'; import Theme from 'tinymce/themes/inlite/Theme'; UnitTest.asynctest('browser/core/ActionsTest', function () { const success = arguments[arguments.length - 2]; const failure = arguments[arguments.length - 1]; Theme(); const wrap = function (f, args) { return function () { const currentArgs = Array.prototype.slice.call(arguments); return Step.sync(function () { f.apply(null, [].concat(args).concat(currentArgs)); }); }; }; const sInsertTableTests = function (editor, tinyApis) { const sInsertTableTest = function (cols, rows, expectedHtml, message) { const sInsertTable: any = wrap(Actions.insertTable, editor); return GeneralSteps.sequence([ tinyApis.sSetContent(''), sInsertTable(cols, rows), tinyApis.sAssertContent(expectedHtml, message) ]); }; return GeneralSteps.sequence([ sInsertTableTest(2, 3, [ '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '
  
  
  
' ].join('\n'), 'Should be a 2x3 table' ), sInsertTableTest(3, 2, [ '', '', '', '', '', '', '', '', '', '', '', '', '', '
   
   
' ].join('\n'), 'Should be a 3x2 table' ) ]); }; const sFormatBlockTests = function (editor, tinyApis) { const sFormatBlockTest = function (name) { const sFormatBlock: any = wrap(Actions.formatBlock, editor); return GeneralSteps.sequence([ tinyApis.sSetContent('

a

'), tinyApis.sSetCursor([0], 0), sFormatBlock(name), tinyApis.sAssertContent('<' + name + '>a', 'Should be a ' + name + ' block') ]); }; return GeneralSteps.sequence([ sFormatBlockTest('h1'), sFormatBlockTest('h2'), sFormatBlockTest('pre') ]); }; const sCreateLinkTests = function (editor, tinyApis) { const sCreateLinkTest = function (inputHtml, url, sPath, sOffset, fPath, fOffset, expectedHtml) { const sCreateLink: any = wrap(Actions.createLink, editor); return GeneralSteps.sequence([ tinyApis.sSetContent(inputHtml), tinyApis.sSetSelection(sPath, sOffset, fPath, fOffset), sCreateLink(url), tinyApis.sAssertContent(expectedHtml, 'Should have a link') ]); }; return GeneralSteps.sequence([ sCreateLinkTest('

a

', '#1', [0, 0], 0, [0, 0], 1, '

a

'), sCreateLinkTest('

a

', '#2', [0, 0], 0, [0, 0], 1, '

a

'), sCreateLinkTest('

a

', '#2', [0, 0, 0], 0, [0, 0, 0], 1, '

a

') ]); }; const sUnlinkTests = function (editor, tinyApis) { const sUnlinkTest = function (inputHtml, sPath, sOffset, fPath, fOffset, expectedHtml) { const sUnlink = wrap(Actions.unlink, editor); return GeneralSteps.sequence([ tinyApis.sSetContent(inputHtml), tinyApis.sSetSelection(sPath, sOffset, fPath, fOffset), sUnlink(), tinyApis.sAssertContent(expectedHtml, 'Should not have a link') ]); }; return GeneralSteps.sequence([ sUnlinkTest('

a

', [0, 0], 0, [0, 0], 1, '

a

'), sUnlinkTest('

a

', [0, 0, 0], 0, [0, 0, 0], 1, '

a

'), sUnlinkTest('

a

', [0, 0, 0], 0, [0, 0, 0], 1, '

a

'), sUnlinkTest('

ab

', [0, 0, 0], 0, [0, 1], 1, '

ab

') ]); }; const base64ToBlob = function (base64, type) { const buff = Window.atob(base64); const bytes = new Uint8Array(buff.length); for (let i = 0; i < bytes.length; i++) { bytes[i] = buff.charCodeAt(i); } return new Blob([bytes], { type }); }; const sInsertBlobTests = function (editor, tinyApis) { const sInsertBlobTest = function (inputHtml, path, offset, blob, base64, expectedHtml) { const sInsertBlob: any = wrap(Actions.insertBlob, editor); return GeneralSteps.sequence([ tinyApis.sSetContent(inputHtml), tinyApis.sSetCursor(path, offset), sInsertBlob(blob, base64), tinyApis.sAssertContent(expectedHtml, 'Should have a image') ]); }; const base64 = 'R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'; const blob = base64ToBlob(base64, 'image/gif'); return GeneralSteps.sequence([ sInsertBlobTest('

a

', [0, 0], 0, base64, blob, '

a

') ]); }; TinyLoader.setup(function (editor, onSuccess, onFailure) { const tinyApis = TinyApis(editor); Pipeline.async({}, [ sInsertTableTests(editor, tinyApis), sFormatBlockTests(editor, tinyApis), sInsertBlobTests(editor, tinyApis), sCreateLinkTests(editor, tinyApis), sUnlinkTests(editor, tinyApis) ], onSuccess, onFailure); }, { inline: true, theme: 'inlite', skin_url: '/project/js/tinymce/skins/lightgray' }, success, failure); });