extensions.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* jshint node: false, browser: true, globalstrict: true, varstmt: false */
  6. /* globals bender, requirejs */
  7. 'use strict';
  8. ( function() {
  9. // This seems to be the only way to force Require.JS to load modules starting with '/' from a different path.
  10. var load = requirejs.load;
  11. requirejs.load = function( context, moduleId, url ) {
  12. var basePath = bender.getCKEditorModuleBasePath().replace( /\/$/, '' );
  13. if ( moduleId[ 0 ] == '/' ) {
  14. url = basePath + url;
  15. }
  16. return load( context, moduleId, url );
  17. };
  18. // Extend ChaiJS with custom exception check `expect().to.throwCKEditorError( [ msg ] )`.
  19. require( [ '/ckeditor5/utils/ckeditorerror.js', 'chai' ], function( CKEditorError, chai ) {
  20. CKEditorError = CKEditorError.default;
  21. // Load extension and bind class used to check for inheritance.
  22. chai.use( addThrowsCKEditorError.bind( null, CKEditorError ) );
  23. // Self-test of the extension. Function names will help find those tests in case of regression.
  24. chai.expect( function throwCKEditorErrorSelfTest1() {
  25. throw new CKEditorError();
  26. } ).to.throwCKEditorError();
  27. chai.expect( function throwCKEditorErrorSelfTest2() {
  28. throw new Error();
  29. } ).to.not.throwCKEditorError();
  30. chai.expect( function throwCKEditorErrorSelfTest3() {
  31. throw new CKEditorError( 'custom message' );
  32. } ).to.throwCKEditorError( 'message' );
  33. chai.expect( function throwCKEditorErrorSelfTest4() {
  34. throw new CKEditorError( 'another msg' );
  35. } ).to.throwCKEditorError( /msg/ );
  36. chai.expect( function throwCKEditorErrorSelfTest5() {
  37. throw new CKEditorError( 'another msg' );
  38. } ).to.throwCKEditorError( /^another/ );
  39. chai.expect( function throwCKEditorErrorSelfTest6() {
  40. throw new CKEditorError( 'another msg' );
  41. } ).to.throwCKEditorError( /msg$/ );
  42. } );
  43. // Reported: https://github.com/benderjs/benderjs/issues/248
  44. // Ugh... make some paths cleanup, because we need to combine these fragments and we don't want to
  45. // duplicate '/'. BTW. if you want to touch this make sure you haven't broken Bender jobs which
  46. // have different bender.basePaths (no trailing '/', unnecessary 'tests/' fragment).
  47. bender.getCKEditorModuleBasePath = function() {
  48. var appBasePath = bender.basePath;
  49. var ckeditorBasePath = bender.config.applications.ckeditor.basePath;
  50. var modulebasePath;
  51. modulebasePath = appBasePath
  52. .split( '/' )
  53. .filter( nonEmpty )
  54. // When running a job we need to drop the last parth of the base path, which is "tests".
  55. .slice( 0, -1 )
  56. .concat(
  57. ckeditorBasePath.split( '/' ).filter( nonEmpty )
  58. )
  59. .join( '/' );
  60. return '/' + modulebasePath;
  61. };
  62. function nonEmpty( str ) {
  63. return !!str.length;
  64. }
  65. // Add `throwCKEditorError` chainable method to ChaiJS.
  66. //
  67. // @param {Object} CKEditorError Constructor of class checking for CKEditorError membership.
  68. // @param {Object} _chai Chai instance.
  69. // @param {Object} utils Chai extension utils.
  70. // @returns {Object} Assertion
  71. function addThrowsCKEditorError( CKEditorError, _chai, utils ) {
  72. var Assertion = _chai.Assertion;
  73. Assertion.addMethod( 'throwCKEditorError', assertThrowCKEditorError );
  74. function assertThrowCKEditorError( errMsg ) {
  75. // jshint validthis: true
  76. // jscs:disable disallowMultipleVarDecl
  77. var obj = utils.flag( this, 'object' ),
  78. actuallyGot = '',
  79. thrown = false,
  80. thrownError = null,
  81. message;
  82. if ( arguments.length === 0 ) {
  83. errMsg = null;
  84. }
  85. try {
  86. obj();
  87. } catch ( err ) {
  88. this.assert(
  89. CKEditorError.isCKEditorError( err ),
  90. 'expected #{this} to throw #{exp} but #{act} was thrown',
  91. 'expected #{this} to not throw #{exp} but #{act} was thrown',
  92. 'CKEditorError',
  93. ( err instanceof Error ? err.toString() : err )
  94. );
  95. // Set subject of assertion.
  96. if ( !errMsg ) {
  97. utils.flag( this, 'object', err );
  98. return this;
  99. }
  100. // Check message of error.
  101. message = utils.type( err ) === 'object' && 'message' in err ? err.message : err.toString();
  102. if ( ( message !== null && message !== undefined ) && errMsg && errMsg instanceof RegExp ) {
  103. this.assert(
  104. errMsg.exec( message ),
  105. 'expected #{this} to throw CKEditorError matching #{exp} but got #{act}',
  106. 'expected #{this} to throw CKEditorError not matching #{exp}',
  107. errMsg,
  108. message
  109. );
  110. utils.flag( this, 'object', err );
  111. return this;
  112. } else if ( ( message !== null && message !== undefined ) && errMsg && typeof errMsg == 'string' ) {
  113. this.assert(
  114. message.indexOf( errMsg ) !== -1,
  115. 'expected #{this} to throw CKEditorError including #{exp} but got #{act}',
  116. 'expected #{this} to throw CKEditorError not including #{act}',
  117. errMsg,
  118. message
  119. );
  120. utils.flag( this, 'object', err );
  121. return this;
  122. } else {
  123. thrown = true;
  124. thrownError = err;
  125. }
  126. }
  127. if ( thrown ) {
  128. actuallyGot = ' but #{act} was thrown';
  129. }
  130. this.assert(
  131. thrown === true,
  132. 'expected #{this} to throw an CKEditorError' + actuallyGot,
  133. 'expected #{this} to not throw an CKEditorError' + actuallyGot,
  134. 'CKEditorError',
  135. ( thrownError instanceof Error ? thrownError.toString() : thrownError )
  136. );
  137. utils.flag( this, 'object', thrownError );
  138. return this;
  139. }
  140. }
  141. } )();