module__amd.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global require, bender */
  6. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  7. import moduleTestUtils from '/tests/ckeditor5/_utils/module.js';
  8. testUtils.createSinonSandbox();
  9. describe( 'amdTestUtils', () => {
  10. const getModulePath = moduleTestUtils.getModulePath;
  11. describe( 'getModulePath()', () => {
  12. it( 'generates a path from a simple name', () => {
  13. const path = getModulePath( 'foo' );
  14. expect( path ).to.equal( '/ckeditor5/foo.js' );
  15. } );
  16. it( 'generates an absolute path from a simple path', () => {
  17. const path = getModulePath( 'engine/dataController' );
  18. expect( path ).to.equal( '/ckeditor5/engine/dataController.js' );
  19. } );
  20. it( 'does not process an absolute path', () => {
  21. const path = getModulePath( '/foo/bar/bom.js' );
  22. expect( path ).to.equal( '/foo/bar/bom.js' );
  23. } );
  24. } );
  25. describe( 'define()', () => {
  26. it( 'defines a module by using global define()', () => {
  27. const spy = testUtils.sinon.spy( window, 'define' );
  28. const expectedDeps = [ 'exports' ].concat( [ 'bar', 'ckeditor' ].map( getModulePath ) );
  29. moduleTestUtils.define( 'test1', [ 'bar', 'ckeditor' ], () => {} );
  30. expect( spy.calledOnce ).to.be.true;
  31. expect( spy.args[ 0 ][ 0 ] ).to.equal( getModulePath( 'test1' ) );
  32. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( expectedDeps );
  33. } );
  34. it( 'maps body args and returned value', () => {
  35. const spy = testUtils.sinon.spy( window, 'define' );
  36. const bodySpy = sinon.spy( () => 'ret' );
  37. moduleTestUtils.define( 'test2', [ 'bar', 'ckeditor' ], bodySpy );
  38. const realBody = spy.args[ 0 ][ 2 ];
  39. const exportsObj = {};
  40. expect( realBody ).to.be.a( 'function' );
  41. realBody( exportsObj, { default: 'arg' } );
  42. expect( exportsObj ).to.have.property( 'default', 'ret', 'it wraps the ret value with an ES6 module obj' );
  43. expect( bodySpy.calledOnce ).to.be.true;
  44. expect( bodySpy.args[ 0 ][ 0 ] ).to.equal( 'arg', 'it unwraps the args' );
  45. } );
  46. it( 'works with module name and body', () => {
  47. const spy = testUtils.sinon.spy( window, 'define' );
  48. moduleTestUtils.define( 'test1', () => {} );
  49. expect( spy.calledOnce ).to.be.true;
  50. expect( spy.args[ 0 ][ 0 ] ).to.equal( getModulePath( 'test1' ) );
  51. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( [ 'exports' ] );
  52. expect( spy.args[ 0 ][ 2 ] ).to.be.a( 'function' );
  53. } );
  54. // Note: this test only checks whether Require.JS doesn't totally fail when creating a circular dependency.
  55. // The value of dependencies are not available anyway inside the amdTestUtils.define() callbacks because
  56. // we lose the late-binding by immediately mapping modules to their default exports.
  57. it( 'works with circular dependencies', ( done ) => {
  58. moduleTestUtils.define( 'test-circular-a', [ 'test-circular-b' ], () => {
  59. return 'a';
  60. } );
  61. moduleTestUtils.define( 'test-circular-b', [ 'test-circular-a' ], () => {
  62. return 'b';
  63. } );
  64. require( [ 'test-circular-a', 'test-circular-b' ].map( moduleTestUtils.getModulePath ), ( a, b ) => {
  65. expect( a ).to.have.property( 'default', 'a' );
  66. expect( b ).to.have.property( 'default', 'b' );
  67. done();
  68. } );
  69. } );
  70. } );
  71. describe( 'require', () => {
  72. it( 'blocks Bender and loads modules through global require()', () => {
  73. let requireCb;
  74. const deferCbSpy = sinon.spy();
  75. testUtils.sinon.stub( bender, 'defer', () => deferCbSpy );
  76. testUtils.sinon.stub( window, 'require', ( deps, cb ) => {
  77. requireCb = cb;
  78. } );
  79. const modules = moduleTestUtils.require( { foo: 'foo/oof', bar: 'bar' } );
  80. expect( deferCbSpy.called ).to.be.false;
  81. requireCb( { default: 1 }, { default: 2 } );
  82. expect( deferCbSpy.calledOnce ).to.be.true;
  83. expect( modules ).to.have.property( 'foo', 1 );
  84. expect( modules ).to.have.property( 'bar', 2 );
  85. } );
  86. } );
  87. } );