amd.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. bender.tools.createSinonSandbox();
  7. describe( 'bender.amd', () => {
  8. const getModulePath = bender.amd.getModulePath;
  9. describe( 'getModulePath()', () => {
  10. // Thanks to this we'll check whether all paths are relative to ckeditor.js path.
  11. const basePath = getModulePath( 'ckeditor' ).replace( /\/ckeditor\.js$/, '/' );
  12. it( 'generates path for the main file', () => {
  13. const path = getModulePath( 'ckeditor' );
  14. expect( path ).to.match( /\/ckeditor.js$/, 'ends with /ckeditor.js' );
  15. expect( path ).to.match( /^\//, 'is absolute' );
  16. } );
  17. it( 'generates path for modules within ckeditor5 package', () => {
  18. const path = getModulePath( 'ckeditor5/foo' );
  19. expect( path ).to.equal( basePath + 'ckeditor5/foo.js' );
  20. } );
  21. it( 'generates path for modules within the core package', () => {
  22. const path = getModulePath( 'core/ui/controller' );
  23. expect( path ).to.equal( basePath + 'ckeditor5-core/ui/controller.js' );
  24. } );
  25. it( 'generates path for modules within some package', () => {
  26. const path = getModulePath( 'some/ba' );
  27. expect( path ).to.equal( basePath + 'ckeditor5-some/ba.js' );
  28. } );
  29. it( 'generates path from simplified feature name', () => {
  30. const path = getModulePath( 'foo' );
  31. expect( path ).to.equal( basePath + 'ckeditor5-foo/foo.js' );
  32. } );
  33. } );
  34. describe( 'define()', () => {
  35. it( 'defines a module by using global define()', () => {
  36. const spy = bender.sinon.spy( window, 'define' );
  37. bender.amd.define( 'test1', [ 'bar', 'ckeditor' ], () => {} );
  38. expect( spy.calledOnce ).to.be.true;
  39. expect( spy.args[ 0 ][ 0 ] ).to.equal( getModulePath( 'test1' ) );
  40. expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( [ 'bar', 'ckeditor' ].map( getModulePath ) );
  41. } );
  42. it( 'maps body args and returned value', () => {
  43. const spy = bender.sinon.spy( window, 'define' );
  44. const bodySpy = sinon.spy( () => 'ret' );
  45. bender.amd.define( 'test2', [ 'bar', 'ckeditor' ], bodySpy );
  46. const realBody = spy.args[ 0 ][ 2 ];
  47. expect( realBody ).to.be.a( 'function' );
  48. const ret = realBody( { default: 'arg' } );
  49. expect( ret ).to.have.property( 'default', 'ret', 'it wraps the ret value with an ES6 module obj' );
  50. expect( bodySpy.calledOnce ).to.be.true;
  51. expect( bodySpy.args[ 0 ][ 0 ] ).to.equal( 'arg', 'it unwraps the args' );
  52. } );
  53. it( 'works with module name and body', () => {
  54. const spy = bender.sinon.spy( window, 'define' );
  55. bender.amd.define( 'test1', () => {} );
  56. expect( spy.calledOnce ).to.be.true;
  57. expect( spy.args[ 0 ][ 0 ] ).to.equal( getModulePath( 'test1' ) );
  58. expect( spy.args[ 0 ][ 1 ] ).to.be.empty;
  59. expect( spy.args[ 0 ][ 2 ] ).to.be.a( 'function' );
  60. } );
  61. } );
  62. describe( 'require', () => {
  63. it( 'blocks Bender and loads modules through global require()', () => {
  64. let requireCb;
  65. const deferCbSpy = sinon.spy();
  66. bender.sinon.stub( bender, 'defer', () => deferCbSpy );
  67. bender.sinon.stub( window, 'require', ( deps, cb ) => {
  68. requireCb = cb;
  69. } );
  70. const modules = bender.amd.require( 'foo', 'bar' );
  71. expect( deferCbSpy.called ).to.be.false;
  72. requireCb( { default: 1 }, { default: 2 } );
  73. expect( deferCbSpy.calledOnce ).to.be.true;
  74. expect( modules ).to.have.property( 'foo', 1 );
  75. expect( modules ).to.have.property( 'bar', 2 );
  76. } );
  77. } );
  78. } );