utils.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global describe, it, beforeEach, afterEach */
  6. 'use strict';
  7. const chai = require( 'chai' );
  8. const expect = chai.expect;
  9. const sinon = require( 'sinon' );
  10. const path = require( 'path' );
  11. const fs = require( 'fs' );
  12. const gzipSize = require( 'gzip-size' );
  13. describe( 'bundle-utils', () => {
  14. const utils = require( '../../tasks/bundle/utils' );
  15. let sandbox;
  16. beforeEach( () => {
  17. sandbox = sinon.sandbox.create();
  18. } );
  19. afterEach( () => {
  20. sandbox.restore();
  21. } );
  22. describe( 'getModuleFullPath', () => {
  23. it( 'should return full path when passed path not relative', () => {
  24. expect( utils.getModuleFullPath( 'editor-classic/classic' ) ).to.equal( './build/esnext/ckeditor5/editor-classic/classic.js' );
  25. } );
  26. it( 'should return unmodified path when passed path is a relative', () => {
  27. expect( utils.getModuleFullPath( './path/to/editor-classic/classic' ) ).to.equal( './path/to/editor-classic/classic.js' );
  28. expect( utils.getModuleFullPath( '../path/to/editor-classic/classic' ) ).to.equal( '../path/to/editor-classic/classic.js' );
  29. } );
  30. } );
  31. describe( 'getPluginPath()', () => {
  32. it( 'should resolve a simple plugin name to the full path', () => {
  33. expect( utils.getPluginPath( 'typing' ) ).to.equal( './build/esnext/ckeditor5/typing/typing.js' );
  34. } );
  35. it( 'should return full path if passed argument is a relative path', () => {
  36. expect( utils.getPluginPath( 'typing/typing' ) ).to.equal( './build/esnext/ckeditor5/typing/typing.js' );
  37. } );
  38. it( 'should return unmodified plugin path if passed argument is a relative path', () => {
  39. expect( utils.getPluginPath( './typing' ) ).to.equal( './typing.js' );
  40. expect( utils.getPluginPath( '../typing' ) ).to.equal( '../typing.js' );
  41. } );
  42. } );
  43. describe( 'capitalize()', () => {
  44. it( 'should transform first letter of the passed string to uppercase', () => {
  45. expect( utils.capitalize( 'string' ) ).to.equal( 'String' );
  46. expect( utils.capitalize( 'multi word string' ) ).to.equal( 'Multi word string' );
  47. } );
  48. } );
  49. describe( 'renderEntryFileContent()', () => {
  50. it( 'should render file content with proper data', () => {
  51. const result = utils.renderEntryFileContent( './bundle/tmp', {
  52. moduleName: 'MyCKEditor',
  53. editor: 'editor-classic/classic',
  54. features: [
  55. 'delete',
  56. 'path/to/default',
  57. './path/to/custom'
  58. ]
  59. } );
  60. const expected = `
  61. 'use strict';
  62. // Babel helpers.
  63. import '../../node_modules/regenerator-runtime/runtime.js';
  64. import Classic from '../../build/esnext/ckeditor5/editor-classic/classic.js';
  65. import Delete from '../../build/esnext/ckeditor5/delete/delete.js';
  66. import Default from '../../build/esnext/ckeditor5/path/to/default.js';
  67. import Custom from '../../path/to/custom.js';
  68. export default class MyCKEditor extends Classic {
  69. static create( element, config = {} ) {
  70. if ( !config.features ) {
  71. config.features = [];
  72. }
  73. config.features = [ ...config.features, Delete, Default, Custom ];
  74. return Classic.create( element, config );
  75. }
  76. }
  77. `;
  78. expect( result ).to.equal( expected );
  79. } );
  80. it( 'should render file content with unique plugin names', () => {
  81. const result = utils.renderEntryFileContent( './bundle/tmp', {
  82. moduleName: 'MyCKEditor',
  83. editor: 'editor-classic/classic',
  84. features: [
  85. 'plugin',
  86. 'path/to/plugin',
  87. 'other/path/to/plugin'
  88. ]
  89. } );
  90. const expected = `
  91. 'use strict';
  92. // Babel helpers.
  93. import '../../node_modules/regenerator-runtime/runtime.js';
  94. import Classic from '../../build/esnext/ckeditor5/editor-classic/classic.js';
  95. import Plugin from '../../build/esnext/ckeditor5/plugin/plugin.js';
  96. import Plugin1 from '../../build/esnext/ckeditor5/path/to/plugin.js';
  97. import Plugin2 from '../../build/esnext/ckeditor5/other/path/to/plugin.js';
  98. export default class MyCKEditor extends Classic {
  99. static create( element, config = {} ) {
  100. if ( !config.features ) {
  101. config.features = [];
  102. }
  103. config.features = [ ...config.features, Plugin, Plugin1, Plugin2 ];
  104. return Classic.create( element, config );
  105. }
  106. }
  107. `;
  108. expect( result ).to.equal( expected );
  109. } );
  110. it( 'should render file content with proper without features', () => {
  111. const result = utils.renderEntryFileContent( './bundle/tmp', {
  112. moduleName: 'MyCKEditor',
  113. editor: 'editor-classic/classic'
  114. } );
  115. const expected = `
  116. 'use strict';
  117. // Babel helpers.
  118. import '../../node_modules/regenerator-runtime/runtime.js';
  119. import Classic from '../../build/esnext/ckeditor5/editor-classic/classic.js';
  120. export default class MyCKEditor extends Classic {
  121. static create( element, config = {} ) {
  122. if ( !config.features ) {
  123. config.features = [];
  124. }
  125. config.features = [ ...config.features, ];
  126. return Classic.create( element, config );
  127. }
  128. }
  129. `;
  130. expect( result ).to.equal( expected );
  131. } );
  132. } );
  133. describe( 'getFileSize', () => {
  134. it( 'should return file size in bytes', () => {
  135. const filePath = 'path/to/file';
  136. const size = 1337;
  137. const statSyncMock = sandbox.stub( fs, 'statSync', () => {
  138. return { size };
  139. } );
  140. expect( utils.getFileSize( filePath ) ).to.be.equal( size );
  141. sinon.assert.calledWithExactly( statSyncMock, filePath );
  142. } );
  143. } );
  144. describe( 'getGzippedFileSize', () => {
  145. it( 'should return file size in bytes', () => {
  146. const filePath = 'path/to/file';
  147. const size = 1337;
  148. const fileContent = 'some string';
  149. const readFileSyncMock = sandbox.stub( fs, 'readFileSync', () => fileContent );
  150. const gzipSizeMock = sandbox.stub( gzipSize, 'sync', () => 1337 );
  151. expect( utils.getGzippedFileSize( filePath ) ).to.be.equal( size );
  152. sinon.assert.calledWithExactly( readFileSyncMock, filePath );
  153. sinon.assert.calledWithExactly( gzipSizeMock, fileContent );
  154. } );
  155. } );
  156. describe( 'getFilesSizeStats', () => {
  157. let size, gzippedSize;
  158. beforeEach( () => {
  159. size = 1337;
  160. gzippedSize = 543;
  161. sandbox.stub( utils, 'getFileSize', () => size );
  162. sandbox.stub( utils, 'getGzippedFileSize', () => gzippedSize );
  163. } );
  164. it( 'should returns an array with two elements', () => {
  165. const result = utils.getFilesSizeStats( [ 'sub/dir/file.js', 'other/sub/dir/file.css' ], 'root/path' );
  166. expect( result ).to.be.an( 'array' );
  167. expect( result ).to.have.length( 2 );
  168. } );
  169. it( 'should returns list of object with files stats', () => {
  170. const result = utils.getFilesSizeStats( [ 'sub/dir/file.js', 'other/sub/dir/file.css' ], 'root/path' );
  171. expect( result ).to.be.deep.equal( [
  172. { name: 'file.js', size, gzippedSize },
  173. { name: 'file.css', size, gzippedSize }
  174. ] );
  175. } );
  176. it( 'should get files from root directory', () => {
  177. let basenameSpy = sandbox.spy( path, 'basename' );
  178. const result = utils.getFilesSizeStats( [ 'sub/dir/file.js', 'other/sub/dir/file.css' ], 'root/path' );
  179. expect( result[ 0 ] ).to.have.property( 'name', 'file.js' );
  180. expect( result[ 1 ] ).to.have.property( 'name', 'file.css' );
  181. sinon.assert.calledWithExactly( basenameSpy.firstCall, 'root/path/sub/dir/file.js' );
  182. sinon.assert.calledWithExactly( basenameSpy.secondCall, 'root/path/other/sub/dir/file.css' );
  183. } );
  184. it( 'should get files if root directory is not specified', () => {
  185. let basenameSpy = sandbox.spy( path, 'basename' );
  186. const result = utils.getFilesSizeStats( [ 'sub/dir/file.js', 'file.css' ] );
  187. expect( result[ 0 ] ).to.have.property( 'name', 'file.js' );
  188. expect( result[ 1 ] ).to.have.property( 'name', 'file.css' );
  189. sinon.assert.calledWithExactly( basenameSpy.firstCall, 'sub/dir/file.js' );
  190. sinon.assert.calledWithExactly( basenameSpy.secondCall, 'file.css' );
  191. } );
  192. } );
  193. } );