module__amd.js 3.8 KB

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