view.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, HTMLElement */
  6. /* bender-tags: core, ui */
  7. 'use strict';
  8. const modules = bender.amd.require( 'ckeditor', 'ui/view', 'ui/region', 'ckeditorerror', 'model', 'eventinfo' );
  9. let View, TestView;
  10. let view;
  11. bender.tools.createSinonSandbox();
  12. beforeEach( updateModuleReference );
  13. describe( 'constructor', function() {
  14. beforeEach( function() {
  15. setTestViewClass();
  16. setTestViewInstance();
  17. } );
  18. it( 'accepts the model', function() {
  19. setTestViewInstance( { a: 'foo', b: 42 } );
  20. expect( view.model ).to.be.an.instanceof( modules.model );
  21. expect( view ).to.have.deep.property( 'model.a', 'foo' );
  22. expect( view ).to.have.deep.property( 'model.b', 42 );
  23. } );
  24. } );
  25. describe( 'instance', function() {
  26. beforeEach( function() {
  27. setTestViewClass();
  28. setTestViewInstance();
  29. } );
  30. it( 'has no default element', function() {
  31. expect( () => view.el ).to.throw( modules.ckeditorerror );
  32. } );
  33. it( 'has no default template', function() {
  34. expect( view.template ).to.be.undefined();
  35. } );
  36. it( 'has no default regions', function() {
  37. expect( view.regions ).to.have.length( 0 );
  38. } );
  39. } );
  40. describe( 'bind', function() {
  41. beforeEach( createViewInstanceWithTemplate );
  42. it( 'returns a function that passes arguments', function() {
  43. setTestViewInstance( { a: 'foo' } );
  44. let spy = bender.sinon.spy();
  45. let callback = view.bind( 'a', spy );
  46. expect( spy.called ).to.be.false;
  47. callback( 'el', 'updater' );
  48. sinon.assert.calledOnce( spy );
  49. sinon.assert.calledWithExactly( spy, 'el', 'foo' );
  50. view.model.a = 'bar';
  51. sinon.assert.calledTwice( spy );
  52. expect( spy.secondCall.calledWithExactly( 'el', 'bar' ) ).to.be.true;
  53. } );
  54. it( 'allows binding attribute to the model', function() {
  55. setTestViewClass( function() {
  56. return {
  57. tag: 'p',
  58. attrs: {
  59. 'class': this.bind( 'foo' )
  60. },
  61. text: 'abc'
  62. };
  63. } );
  64. setTestViewInstance( { foo: 'bar' } );
  65. expect( view.el.outerHTML ).to.be.equal( '<p class="bar">abc</p>' );
  66. view.model.foo = 'baz';
  67. expect( view.el.outerHTML ).to.be.equal( '<p class="baz">abc</p>' );
  68. } );
  69. it( 'allows binding "text" to the model', function() {
  70. setTestViewClass( function() {
  71. return {
  72. tag: 'p',
  73. children: [
  74. {
  75. tag: 'b',
  76. text: 'baz'
  77. }
  78. ],
  79. text: this.bind( 'foo' )
  80. };
  81. } );
  82. setTestViewInstance( { foo: 'bar' } );
  83. expect( view.el.outerHTML ).to.be.equal( '<p>bar<b>baz</b></p>' );
  84. // TODO: A solution to avoid nuking the children?
  85. view.model.foo = 'qux';
  86. expect( view.el.outerHTML ).to.be.equal( '<p>qux</p>' );
  87. } );
  88. it( 'allows binding to the model with value processing', function() {
  89. let callback = ( el, value ) =>
  90. ( value > 0 ? 'positive' : 'negative' );
  91. setTestViewClass( function() {
  92. return {
  93. tag: 'p',
  94. attrs: {
  95. 'class': this.bind( 'foo', callback )
  96. },
  97. text: this.bind( 'foo', callback )
  98. };
  99. } );
  100. setTestViewInstance( { foo: 3 } );
  101. expect( view.el.outerHTML ).to.be.equal( '<p class="positive">positive</p>' );
  102. view.model.foo = -7;
  103. expect( view.el.outerHTML ).to.be.equal( '<p class="negative">negative</p>' );
  104. } );
  105. it( 'allows binding to the model with custom callback', function() {
  106. setTestViewClass( function() {
  107. return {
  108. tag: 'p',
  109. attrs: {
  110. 'class': this.bind( 'foo', ( el, value ) => {
  111. el.innerHTML = value;
  112. if ( value == 'changed' ) {
  113. return value;
  114. }
  115. } )
  116. },
  117. text: 'bar'
  118. };
  119. } );
  120. setTestViewInstance( { foo: 'moo' } );
  121. expect( view.el.outerHTML ).to.be.equal( '<p>moo</p>' );
  122. view.model.foo = 'changed';
  123. expect( view.el.outerHTML ).to.be.equal( '<p class="changed">changed</p>' );
  124. } );
  125. } );
  126. describe( 'on', function() {
  127. it( 'accepts plain binding', function() {
  128. let spy = bender.sinon.spy();
  129. setTestViewClass( function() {
  130. return {
  131. tag: 'p',
  132. on: {
  133. x: 'a',
  134. }
  135. };
  136. } );
  137. setTestViewInstance();
  138. view.on( 'a', spy );
  139. dispatchEvent( view.el, 'x' );
  140. sinon.assert.calledWithExactly( spy,
  141. sinon.match.has( 'name', 'a' ),
  142. sinon.match.has( 'target', view.el )
  143. );
  144. } );
  145. it( 'accepts an array of event bindings', function() {
  146. let spy1 = bender.sinon.spy();
  147. let spy2 = bender.sinon.spy();
  148. setTestViewClass( function() {
  149. return {
  150. tag: 'p',
  151. on: {
  152. x: [ 'a', 'b' ]
  153. }
  154. };
  155. } );
  156. setTestViewInstance();
  157. view.on( 'a', spy1 );
  158. view.on( 'b', spy2 );
  159. dispatchEvent( view.el, 'x' );
  160. sinon.assert.calledWithExactly( spy1,
  161. sinon.match.has( 'name', 'a' ),
  162. sinon.match.has( 'target', view.el )
  163. );
  164. sinon.assert.calledWithExactly( spy2,
  165. sinon.match.has( 'name', 'b' ),
  166. sinon.match.has( 'target', view.el )
  167. );
  168. } );
  169. it( 'accepts DOM selectors', function() {
  170. let spy1 = bender.sinon.spy();
  171. let spy2 = bender.sinon.spy();
  172. let spy3 = bender.sinon.spy();
  173. setTestViewClass( function() {
  174. return {
  175. tag: 'p',
  176. children: [
  177. {
  178. tag: 'span',
  179. attrs: {
  180. 'class': 'y',
  181. },
  182. on: {
  183. 'test@p': 'c'
  184. }
  185. },
  186. {
  187. tag: 'div',
  188. children: [
  189. {
  190. tag: 'span',
  191. attrs: {
  192. 'class': 'y',
  193. }
  194. }
  195. ],
  196. }
  197. ],
  198. on: {
  199. 'test@.y': 'a',
  200. 'test@div': 'b'
  201. }
  202. };
  203. } );
  204. setTestViewInstance();
  205. view.on( 'a', spy1 );
  206. view.on( 'b', spy2 );
  207. view.on( 'c', spy3 );
  208. // Test "test@p".
  209. dispatchEvent( view.el, 'test' );
  210. sinon.assert.callCount( spy1, 0 );
  211. sinon.assert.callCount( spy2, 0 );
  212. sinon.assert.callCount( spy3, 0 );
  213. // Test "test@.y".
  214. dispatchEvent( view.el.firstChild, 'test' );
  215. expect( spy1.firstCall.calledWithExactly(
  216. sinon.match.has( 'name', 'a' ),
  217. sinon.match.has( 'target', view.el.firstChild )
  218. ) ).to.be.true;
  219. sinon.assert.callCount( spy2, 0 );
  220. sinon.assert.callCount( spy3, 0 );
  221. // Test "test@div".
  222. dispatchEvent( view.el.lastChild, 'test' );
  223. sinon.assert.callCount( spy1, 1 );
  224. expect( spy2.firstCall.calledWithExactly(
  225. sinon.match.has( 'name', 'b' ),
  226. sinon.match.has( 'target', view.el.lastChild )
  227. ) ).to.be.true;
  228. sinon.assert.callCount( spy3, 0 );
  229. // Test "test@.y".
  230. dispatchEvent( view.el.lastChild.firstChild, 'test' );
  231. expect( spy1.secondCall.calledWithExactly(
  232. sinon.match.has( 'name', 'a' ),
  233. sinon.match.has( 'target', view.el.lastChild.firstChild )
  234. ) ).to.be.true;
  235. sinon.assert.callCount( spy2, 1 );
  236. sinon.assert.callCount( spy3, 0 );
  237. } );
  238. it( 'accepts function callbacks', function() {
  239. let spy1 = bender.sinon.spy();
  240. let spy2 = bender.sinon.spy();
  241. setTestViewClass( function() {
  242. return {
  243. tag: 'p',
  244. children: [
  245. {
  246. tag: 'span'
  247. }
  248. ],
  249. on: {
  250. x: spy1,
  251. 'y@span': [ spy2, 'c' ],
  252. }
  253. };
  254. } );
  255. setTestViewInstance();
  256. dispatchEvent( view.el, 'x' );
  257. dispatchEvent( view.el.firstChild, 'y' );
  258. sinon.assert.calledWithExactly( spy1,
  259. sinon.match.has( 'target', view.el )
  260. );
  261. sinon.assert.calledWithExactly( spy2,
  262. sinon.match.has( 'target', view.el.firstChild )
  263. );
  264. } );
  265. it( 'supports event delegation', function() {
  266. let spy = bender.sinon.spy();
  267. setTestViewClass( function() {
  268. return {
  269. tag: 'p',
  270. children: [
  271. {
  272. tag: 'span'
  273. }
  274. ],
  275. on: {
  276. x: 'a',
  277. }
  278. };
  279. } );
  280. setTestViewInstance();
  281. view.on( 'a', spy );
  282. dispatchEvent( view.el.firstChild, 'x' );
  283. sinon.assert.calledWithExactly( spy,
  284. sinon.match.has( 'name', 'a' ),
  285. sinon.match.has( 'target', view.el.firstChild )
  286. );
  287. } );
  288. it( 'works for future elements', function() {
  289. let spy = bender.sinon.spy();
  290. setTestViewClass( function() {
  291. return {
  292. tag: 'p',
  293. on: {
  294. 'test@div': 'a'
  295. }
  296. };
  297. } );
  298. setTestViewInstance();
  299. view.on( 'a', spy );
  300. let div = document.createElement( 'div' );
  301. view.el.appendChild( div );
  302. dispatchEvent( div, 'test' );
  303. sinon.assert.calledWithExactly( spy, sinon.match.has( 'name', 'a' ), sinon.match.has( 'target', div ) );
  304. } );
  305. } );
  306. describe( 'render', function() {
  307. beforeEach( createViewInstanceWithTemplate );
  308. it( 'creates an element from template', function() {
  309. setTestViewInstance( { a: 1 } );
  310. expect( view.el ).to.be.an.instanceof( HTMLElement );
  311. expect( view.el.nodeName ).to.be.equal( 'A' );
  312. } );
  313. } );
  314. describe( 'destroy', function() {
  315. beforeEach( createViewInstanceWithTemplate );
  316. it( 'detaches the model', function() {
  317. expect( view.model ).to.be.an.instanceof( modules.model );
  318. view.destroy();
  319. expect( view.model ).to.be.null;
  320. } );
  321. it( 'detaches the element', function() {
  322. // Append the views's element to some container.
  323. let container = document.createElement( 'div' );
  324. container.appendChild( view.el );
  325. expect( view.el.nodeName ).to.be.equal( 'A' );
  326. expect( view.el ).to.be.an.instanceof( HTMLElement );
  327. expect( view.el.parentNode ).to.be.equal( container );
  328. view.destroy();
  329. expect( view.el ).to.be.an.instanceof( HTMLElement );
  330. expect( view.el.parentNode ).to.be.null;
  331. } );
  332. it( 'destroys child regions', function() {
  333. const Region = modules[ 'ui/region' ];
  334. let region = new Region( 'test' );
  335. let spy = bender.sinon.spy( region, 'destroy' );
  336. view.regions.add( region );
  337. view.destroy();
  338. expect( view.regions ).to.have.length( 0 );
  339. expect( spy.calledOnce ).to.be.true;
  340. } );
  341. it( 'detaches bound model listeners', function() {
  342. setTestViewClass( function() {
  343. return {
  344. tag: 'p',
  345. text: this.bind( 'foo' )
  346. };
  347. } );
  348. setTestViewInstance( { foo: 'bar' } );
  349. // Keep the reference after the view is destroyed.
  350. let model = view.model;
  351. expect( view.el.outerHTML ).to.be.equal( '<p>bar</p>' );
  352. model.foo = 'baz';
  353. expect( view.el.outerHTML ).to.be.equal( '<p>baz</p>' );
  354. view.destroy();
  355. model.foo = 'abc';
  356. expect( view.el.outerHTML ).to.be.equal( '<p>baz</p>' );
  357. } );
  358. } );
  359. function updateModuleReference() {
  360. View = modules[ 'ui/view' ];
  361. }
  362. function createViewInstanceWithTemplate() {
  363. setTestViewClass( () => ( { tag: 'a' } ) );
  364. setTestViewInstance();
  365. }
  366. function setTestViewClass( template ) {
  367. TestView = class V extends View {
  368. constructor( model ) {
  369. super( model );
  370. if ( template ) {
  371. this.template = template.call( this );
  372. }
  373. }
  374. };
  375. }
  376. function setTestViewInstance( model ) {
  377. view = new TestView( model );
  378. if ( view.template ) {
  379. document.body.appendChild( view.el );
  380. }
  381. }
  382. function dispatchEvent( el, domEvtName ) {
  383. if ( !el.parentNode ) {
  384. throw new Error( 'To dispatch an event, element must be in DOM. Otherwise #target is null.' );
  385. }
  386. el.dispatchEvent( new Event( domEvtName, {
  387. bubbles: true
  388. } ) );
  389. }