amd.js 3.6 KB

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