module__cjs.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global require, process */
  6. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  7. import moduleTestUtils from '/tests/ckeditor5/_utils/module.js';
  8. testUtils.createSinonSandbox();
  9. const path = require( 'path' );
  10. const cjsDir = path.join( process.cwd(), 'build', 'cjs' );
  11. describe( 'module utilities', () => {
  12. const getModulePath = moduleTestUtils.getModulePath;
  13. describe( 'getModulePath()', () => {
  14. it( 'generates absolute path from a plugin name', () => {
  15. const modulePath = getModulePath( 'foo' );
  16. expect( modulePath ).to.equal( path.join( cjsDir, '/ckeditor5/foo/foo.js' ) );
  17. } );
  18. it( 'generates an absolute path from a simple path', () => {
  19. const modulePath = getModulePath( 'core/editor' );
  20. expect( modulePath ).to.equal( path.join( cjsDir, '/ckeditor5/core/editor.js' ) );
  21. } );
  22. it( 'does not process an absolute path', () => {
  23. const modulePath = getModulePath( '/foo/bar/bom.js' );
  24. expect( modulePath ).to.equal( path.join( cjsDir, '/foo/bar/bom.js' ) );
  25. } );
  26. } );
  27. describe( 'define()', () => {
  28. it( 'defines a module using mockery', () => {
  29. const mockery = require( 'mockery' );
  30. const spy = testUtils.sinon.spy( mockery, 'registerMock' );
  31. moduleTestUtils.define( 'test1', [ '/ckeditor.js', 'bar' ], () => {} );
  32. expect( spy.calledOnce ).to.be.true;
  33. expect( spy.args[ 0 ][ 0 ] ).to.equal( getModulePath( 'test1' ) );
  34. } );
  35. it( 'works with module name and body', () => {
  36. const mockery = require( 'mockery' );
  37. const spy = testUtils.sinon.spy( mockery, 'registerMock' );
  38. const bodySpy = testUtils.sinon.spy( () => {} );
  39. moduleTestUtils.define( 'test1', bodySpy );
  40. expect( spy.calledOnce ).to.be.true;
  41. expect( spy.args[ 0 ][ 0 ] ).to.equal( getModulePath( 'test1' ) );
  42. expect( bodySpy.calledOnce ).to.be.true;
  43. // No dependencies are passed - check if no arguments were passed to the function.
  44. expect( bodySpy.args[ 0 ].length ).to.equal( 0 );
  45. } );
  46. // Note: this test only checks whether CommonJS version of `define()` doesn't totally fail when creating a
  47. // circular dependency. The value of dependencies are not available anyway inside the
  48. // amdTestUtils.define() callbacks because we lose the late-binding by immediately mapping modules to
  49. // their default exports.
  50. it( 'works with circular dependencies', () => {
  51. moduleTestUtils.define( 'test-circular-a', [ 'test-circular-b' ], () => {
  52. return 'a';
  53. } );
  54. moduleTestUtils.define( 'test-circular-b', [ 'test-circular-a' ], () => {
  55. return 'b';
  56. } );
  57. const a = require( moduleTestUtils.getModulePath( 'test-circular-a' ) );
  58. expect( a ).to.have.property( 'default', 'a' );
  59. const b = require( moduleTestUtils.getModulePath( 'test-circular-b' ) );
  60. expect( b ).to.have.property( 'default', 'b' );
  61. } );
  62. } );
  63. describe( 'require', () => {
  64. it( 'creates object with loaded modules', () => {
  65. // Create first module using mockery library.
  66. const mockery = require( 'mockery' );
  67. mockery.registerMock( moduleTestUtils.getModulePath( 'module1' ), { default: 'foo' } );
  68. // Create second module using define.
  69. moduleTestUtils.define( 'module2', () => 'bar' );
  70. const loadedModules = moduleTestUtils.require( { module1: 'module1', module2: 'module2' } );
  71. expect( loadedModules.module1 ).to.equal( 'foo' );
  72. expect( loadedModules.module2 ).to.equal( 'bar' );
  73. } );
  74. } );
  75. } );