module__cjs.js 3.5 KB

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