You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
7.4 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. This file is part of Ext JS 3.4
  3. Copyright (c) 2011-2013 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. This file may be used under the terms of the GNU General Public License version 3.0 as
  7. published by the Free Software Foundation and appearing in the file LICENSE included in the
  8. packaging of this file.
  9. Please review the following information to ensure the GNU General Public License version 3.0
  10. requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  11. If you are unsure which license is appropriate for your use, please contact the sales department
  12. at http://www.sencha.com/contact.
  13. Build date: 2013-04-03 15:07:25
  14. */
  15. Ext.test.session.addTest( 'Ext', {
  16. name: 'Template',
  17. planned: 22,
  18. setUp: function() {
  19. this.es = [];
  20. },
  21. tearDown: function() {
  22. Ext.each( this.es, function( el ) {
  23. if ( el instanceof Ext.Element ) {
  24. el.remove();
  25. } else {
  26. Ext.removeNode( el );
  27. }
  28. });
  29. this.es.length = 0;
  30. },
  31. // 3
  32. test_from: function() {
  33. var id = Ext.id();
  34. var el = Ext.getBody().createChild({
  35. tag: 'textarea',
  36. cls: 'x-hidden',
  37. id: id,
  38. html: '{0} {1}'
  39. });
  40. this.es.push( el );
  41. var tpl1 = Ext.Template.from( el, { complied: true } );
  42. var tpl2 = Ext.Template.from( el.dom, { complied: true } );
  43. var tpl3 = Ext.Template.from( id, { complied: true } );
  44. Y.Assert.areEqual( 'Hello World', tpl1.apply( [ 'Hello', 'World' ] ), 'Test template compiled from textarea value (using Ext.element)' );
  45. Y.Assert.areEqual( 'Hello World', tpl2.apply( [ 'Hello', 'World' ] ), 'Test template compiled from textarea value (using Html element)' );
  46. Y.Assert.areEqual( 'Hello World', tpl3.apply( [ 'Hello', 'World' ] ), 'Test template compiled from textarea value (using element id)' );
  47. },
  48. // 5
  49. test_append: function() {
  50. var container = Ext.getBody().createChild({
  51. tag: 'div',
  52. cls: 'x-hidden'
  53. });
  54. this.es.push( container );
  55. var div = container.createChild({
  56. tag: 'div',
  57. html: 'foobar'
  58. });
  59. var tpl = new Ext.Template( '<div>{0} {1}</div>', {
  60. compiled: true,
  61. disableFormats: true
  62. });
  63. var newel = tpl.append( container, [ 'Hello', 'World' ] );
  64. Y.Assert.areEqual( 'Hello World', newel.innerHTML, 'Test if the new element\'s innerHTML matches the template' );
  65. Y.Assert.areSame( newel, div.dom.nextSibling, 'Test if nextSibling is the created element' );
  66. Y.Assert.areSame( newel, container.dom.lastChild, 'Test if container\'s lastChild is the created element' );
  67. Y.Assert.areEqual( 'Hello World', div.dom.nextSibling.innerHTML, 'Test if nextSibling\'s innerHTML is from the template' );
  68. Y.Assert.areEqual( 'Hello World', container.dom.lastChild.innerHTML, 'Test if the container\'s firstChild innerHTML is from the template' );
  69. },
  70. // 3
  71. test_apply: function() {
  72. var tpl1 = new Ext.Template( '{0}{hello} {1}{world}. How are you {2}{name}?', {
  73. compiled: true,
  74. disableFormats: true
  75. });
  76. var tpl2 = new Ext.Template( '{hello} {world}. How are you {name:ellipsis(8)}?', {
  77. compiled: true
  78. });
  79. Y.Assert.areEqual( 'Hello World. How are you TestyMcTester?', tpl1.apply( [ 'Hello', 'World', 'TestyMcTester' ] ), 'Test apply with an array, no formats' );
  80. Y.Assert.areEqual( 'Hello World. How are you TestyMcTester?', tpl1.apply( { hello: 'Hello', world: 'World', name: 'TestyMcTester' } ), 'Test apply with an object, no formats' );
  81. Y.Assert.areEqual( 'Hello World. How are you Testy...?', tpl2.apply( { hello: 'Hello', world: 'World', name: 'TestyMcTester' } ), 'Test apply with an object, with formats' );
  82. },
  83. // apply is an alias for applyTemplate
  84. // 3
  85. test_insertAfter: function() {
  86. var container = Ext.getBody().createChild({
  87. tag: 'div',
  88. cls: 'x-hidden'
  89. });
  90. this.es.push( container );
  91. var div = container.createChild({
  92. tag: 'div',
  93. html: 'foobar'
  94. });
  95. var tpl = new Ext.Template( '<div>{0} {1}</div>', {
  96. compiled: true,
  97. disableFormats: true
  98. });
  99. var newel = tpl.insertAfter( div, [ 'Hello', 'World' ] );
  100. Y.Assert.areEqual( 'Hello World', newel.innerHTML, 'Test if the new element\'s innerHTML matches the template' );
  101. Y.Assert.areSame( newel, div.dom.nextSibling, 'Test if nextSibling is the created element' );
  102. Y.Assert.areEqual( 'Hello World', div.dom.nextSibling.innerHTML, 'Test if nextSibling\'s innerHTML is from the template' );
  103. },
  104. // 3
  105. test_insertBefore: function() {
  106. var container = Ext.getBody().createChild({
  107. tag: 'div',
  108. cls: 'x-hidden'
  109. });
  110. this.es.push( container );
  111. var div = container.createChild({
  112. tag: 'div',
  113. html: 'foobar'
  114. });
  115. var tpl = new Ext.Template( '<div>{0} {1}</div>', {
  116. compiled: true,
  117. disableFormats: true
  118. });
  119. var newel = tpl.insertBefore( div, [ 'Hello', 'World' ] );
  120. Y.Assert.areEqual( 'Hello World', newel.innerHTML, 'Test if the new element\'s innerHTML matches the template' );
  121. Y.Assert.areSame( newel, container.dom.firstChild, 'Test if the container\'s firstChild is the created element' );
  122. Y.Assert.areEqual( 'Hello World', container.dom.firstChild.innerHTML, 'Test if the container\s firstChild innerHTML is from the template' );
  123. },
  124. // 3
  125. test_insertFirst: function() {
  126. var container = Ext.getBody().createChild({
  127. tag: 'div',
  128. cls: 'x-hidden'
  129. });
  130. this.es.push( container );
  131. container.createChild({
  132. tag: 'div',
  133. html: 'foobar'
  134. });
  135. var tpl = new Ext.Template( '<div>{0} {1}</div>', {
  136. compiled: true,
  137. disableFormats: true
  138. });
  139. var newel = tpl.insertFirst( container, [ 'Hello', 'World' ] );
  140. Y.Assert.areEqual( 'Hello World', newel.innerHTML, 'Test if the new element\'s innerHTML matches the template' );
  141. Y.Assert.areSame( newel, container.dom.firstChild, 'Test if the container\'s firstChild is the created element' );
  142. Y.Assert.areEqual( 'Hello World', container.dom.firstChild.innerHTML, 'Test if the container\s firstChild innerHTML is from the template' );
  143. },
  144. // 1
  145. test_overwrite: function() {
  146. var container = Ext.getBody().createChild({
  147. tag: 'div',
  148. cls: 'x-hidden'
  149. });
  150. this.es.push( container );
  151. container.createChild({
  152. tag: 'div',
  153. html: 'foobar'
  154. });
  155. var tpl = new Ext.Template( '{0} {1}', {
  156. compiled: true,
  157. disableFormats: true
  158. });
  159. var newel = tpl.overwrite( container, [ 'Hello', 'World' ] );
  160. Y.Assert.areEqual( 'Hello World', container.dom.innerHTML, 'Test if the container innerHTML matches the template' );
  161. },
  162. // 1
  163. test_set: function() {
  164. var foo = new Ext.Template( '{1}{0}' );
  165. foo.set( '{0}{1}', true );
  166. Y.Assert.areEqual( 'foobar', foo.apply( [ 'foo', 'bar' ] ), 'Test recompiled template using set' );
  167. }
  168. });