版博士V2.0程序
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

G.append.test.js 4.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const $ = require('../index');
  2. const config = require('./config');
  3. const jc1 = require('./code/simple1');
  4. const jc2 = require('./code/simple2');
  5. const hc1 = require('./code/simple1.html');
  6. test('$.append: empty code should not throw error', () => {
  7. expect(() => {
  8. const G = $('var a = 1;');
  9. G.append('');
  10. }).not.toThrow();
  11. })
  12. test('$.append: null should not throw error', () => {
  13. expect(() => {
  14. const G = $('var a = 1;');
  15. G.append(null);
  16. }).not.toThrow();
  17. })
  18. test('$.append: undefined should not throw error', () => {
  19. expect(() => {
  20. const G = $('var a = 1;');
  21. G.append(undefined);
  22. }).not.toThrow();
  23. })
  24. test('$.append: simple code should not throw error', () => {
  25. expect(() => {
  26. const CODE = `
  27. function a(){
  28. var a = 1;
  29. }
  30. `;
  31. $(CODE).append('var a = 1;');
  32. }).not.toThrow();
  33. })
  34. test('$.append: simple code with $ object should not throw error', () => {
  35. expect(() => {
  36. const CODE = `
  37. var a = 1;
  38. `;
  39. $(CODE).append($(` function a(){
  40. var a = 1;
  41. }`));
  42. }).not.toThrow();
  43. })
  44. test('$.append: simple code result should be ok', () => {
  45. const CODE = `
  46. var b = 1;
  47. `;
  48. const code = $(CODE).append($(` function a(){
  49. var a = 1;
  50. }`)).generate();
  51. const compareCode = '\n var b = 1;\n function a(){\n var a = 1;\n }\n ';
  52. expect(code).toBe(compareCode);
  53. })
  54. test('$.append: simple code result should be ok', () => {
  55. const CODE = `
  56. function a(){
  57. var a = 1;
  58. }
  59. `;
  60. const code = $(CODE).append($('var b = 1;').node).generate();
  61. const compareCode = '\n function a(){\n var a = 1;\n }\n var b = 1;\n ';
  62. expect(code).toBe(compareCode);
  63. })
  64. test('$.append: simple2 code result should to be passed', () => {
  65. const G = $(jc2).append($('{test:function(){}}').node);
  66. const result = G.node.program.body[G.node.program.body.length - 1].properties[0].key.name === 'test';
  67. expect(result).toBeTruthy();
  68. });
  69. test('$.append: simple code result should to be passed', () => {
  70. const code = $(`Page({
  71. onShow() { },
  72. data: { }
  73. })`).find(`Page({})`)
  74. .each(item => {
  75. // `init() {}` parse 抛异常
  76. $(item.attr('arguments.0')).append('properties', `init() {}`)
  77. // page的arguments[0]是第一个入参对象,通过attr获取到这个节点之后用$()转为AST实例,
  78. // 就可以链式调用进行后续操作,append第一个参数是第二个参数指定插入的位置
  79. })
  80. .root()
  81. .generate();
  82. const result = code.indexOf(`init()`) > -1;
  83. expect(result).toBeTruthy();
  84. });
  85. test('$.append: append simple code result should to be passed', () => {
  86. const C = `
  87. function create() {
  88. console.log('this is function')
  89. }
  90. `;
  91. const code = $(C)
  92. .find(`function create() {}`)
  93. .each(item => {
  94. $(item.attr('body')).append('body', `
  95. let type = 'success'
  96. console.log('success')
  97. `)
  98. })
  99. .root()
  100. .generate();
  101. const result = code.indexOf(`let type = 'success'`) > -1;
  102. expect(result).toBeTruthy();
  103. });
  104. test('$.append: simple html code', () => {
  105. const code = `<div>test</div>`;
  106. expect(() => {
  107. const G = $(hc1, config.html);
  108. G.append('<span>span</span>');
  109. }).not.toThrow();
  110. })
  111. test('$.append: simple1 html code', () => {
  112. expect(() => {
  113. const G = $(hc1, config.html);
  114. }).not.toThrow();
  115. })
  116. test('$.append: simple1 html code result should to be ok', () => {
  117. const CODE = '<div>test append</div>'
  118. const G = $(hc1, config.html);
  119. const result = G.find('<html>').append(CODE).generate();
  120. expect(result.indexOf(CODE) > -1).toBeTruthy();
  121. })
  122. test('$.append: append rest param', () => {
  123. const result =
  124. $(`function a(b) {}`)
  125. .append('params', `{ options = {}, ...rest }`)
  126. .generate();
  127. expect(result.indexOf('options') > -1).toBeTruthy();
  128. })
  129. test('$.empty: simple1 html code', () => {
  130. const G = $(`
  131. <view class="abc">
  132. <view> {{ message }} </view>
  133. <view> {{ message }} </view>
  134. <view> {{ message }} </view>
  135. <view> {{ message }} </view>
  136. </view>
  137. `, config.html);
  138. const newG = G.find('<$_$tag class="abc"></$_$tag>')
  139. .each(function (item) {
  140. var list =
  141. `<view>1111</view>
  142. <view>1111</view>
  143. <view>1111</view>`
  144. item.append(list)
  145. })
  146. .root()
  147. .generate()
  148. expect(newG.match(/1111/g).length == 3).toBeTruthy();
  149. })