utils-amd.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global require */
  6. 'use strict';
  7. import testUtils from '/tests/_utils/utils.js';
  8. import amdTestUtils from '/tests/_utils/amd.js';
  9. testUtils.createSinonSandbox();
  10. describe( 'bender.amd', () => {
  11. const getModulePath = amdTestUtils.getModulePath;
  12. describe( 'getModulePath()', () => {
  13. it( 'generates an absolute path', () => {
  14. const path = getModulePath( 'ckeditor' );
  15. expect( path ).to.match( /\/ckeditor.js$/, 'ends with /ckeditor.js' );
  16. expect( path ).to.match( /^\//, 'is absolute' );
  17. } );
  18. } );
  19. describe( 'define()', () => {
  20. it( 'defines a module by using global define()', () => {
  21. const spy = testUtils.sinon.spy( window, 'define' );
  22. const expectedDeps = [ 'exports' ].concat( [ 'bar', 'ckeditor' ].map( getModulePath ) );
  23. amdTestUtils.define( 'test1', [ 'bar', 'ckeditor' ], () => {} );
  24. expect( spy.calledOnce ).to.be.true;
  25. expect( spy.args[ 0 ][ 0 ] ).to.equal( getModulePath( 'test1' ) );
  26. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( expectedDeps );
  27. } );
  28. it( 'maps body args and returned value', () => {
  29. const spy = testUtils.sinon.spy( window, 'define' );
  30. const bodySpy = sinon.spy( () => 'ret' );
  31. amdTestUtils.define( 'test2', [ 'bar', 'ckeditor' ], bodySpy );
  32. const realBody = spy.args[ 0 ][ 2 ];
  33. const exportsObj = {};
  34. expect( realBody ).to.be.a( 'function' );
  35. realBody( exportsObj, { default: 'arg' } );
  36. expect( exportsObj ).to.have.property( 'default', 'ret', 'it wraps the ret value with an ES6 module obj' );
  37. expect( bodySpy.calledOnce ).to.be.true;
  38. expect( bodySpy.args[ 0 ][ 0 ] ).to.equal( 'arg', 'it unwraps the args' );
  39. } );
  40. it( 'works with module name and body', () => {
  41. const spy = testUtils.sinon.spy( window, 'define' );
  42. amdTestUtils.define( 'test1', () => {} );
  43. expect( spy.calledOnce ).to.be.true;
  44. expect( spy.args[ 0 ][ 0 ] ).to.equal( getModulePath( 'test1' ) );
  45. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( [ 'exports' ] );
  46. expect( spy.args[ 0 ][ 2 ] ).to.be.a( 'function' );
  47. } );
  48. // Note: this test only checks whether Require.JS doesn't totally fail when creating a circular dependency.
  49. // The value of dependencies are not available anyway inside the amdTestUtils.define() callbacks because
  50. // we lose the late-binding by immediately mapping modules to their default exports.
  51. it( 'works with circular dependencies', ( done ) => {
  52. amdTestUtils.define( 'test-circular-a', [ 'test-circular-b' ], () => {
  53. return 'a';
  54. } );
  55. amdTestUtils.define( 'test-circular-b', [ 'test-circular-a' ], () => {
  56. return 'b';
  57. } );
  58. require( [ 'test-circular-a', 'test-circular-b' ].map( amdTestUtils.getModulePath ), ( a, b ) => {
  59. expect( a ).to.have.property( 'default', 'a' );
  60. expect( b ).to.have.property( 'default', 'b' );
  61. done();
  62. } );
  63. } );
  64. } );
  65. } );