版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

95 строки
2.5 KiB

  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. const CODE = `
  7. function parent(){
  8. let name = 'jerry';
  9. function eat(){
  10. console.log('do eat');
  11. }
  12. }
  13. `
  14. test('$.parent: simple code', () => {
  15. expect(()=>{
  16. const G = $('var a = 1;');
  17. G.parent();
  18. }).not.toThrow();
  19. })
  20. test('$.parent: simple code result should be ok', () => {
  21. const G = $('var a = 1;');
  22. const code = G.parent().generate();
  23. expect(code).toBe('var a = 1;');
  24. })
  25. test('$.parent: this[0] is null', () => {
  26. expect(()=>{
  27. const G = $('var a = 1;');
  28. G[0] = null
  29. G.parent();
  30. }).not.toThrow();
  31. })
  32. test('$.parent: parents find', () => {
  33. expect(()=>{
  34. const G = $(CODE).find('let $_$ = $_$;');
  35. const parent = G.parent();
  36. const psCode = parent.generate();
  37. }).not.toThrow();
  38. })
  39. test('$.parent: parents find result should be ok', () => {
  40. const G = $(CODE).find('let $_$ = $_$;');
  41. const parent = G.parent();
  42. const psCode = parent.generate();
  43. expect(psCode).toBe(
  44. `{
  45. let name = 'jerry';
  46. function eat(){
  47. console.log('do eat');
  48. }
  49. }`
  50. );
  51. })
  52. test('$.parent: simple2 code parents find result should be ok', () => {
  53. const G = $(jc2).find('this.render()');
  54. const parent = G.parent();
  55. // 做了处理 不生成大括号
  56. const psCode = parent.generate();
  57. expect(psCode).toBe(
  58. `this.render()`
  59. );
  60. })
  61. test('$.parent: simple html code', () => {
  62. const code = `<div>test</div>`;
  63. expect(() => {
  64. const G = $(code, config.html);
  65. G.parent();
  66. }).not.toThrow();
  67. })
  68. test('$.parent: simple1 html code', () => {
  69. expect(() => {
  70. const G = $(hc1, config.html);
  71. G.parent();
  72. }).not.toThrow();
  73. })
  74. test('$.parent: simple1 html code result should be ok', () => {
  75. const G = $(hc1, config.html);
  76. const parent = G.parent();
  77. const psCode = parent.generate();
  78. expect(psCode.indexOf('<html>' > -1)).toBeTruthy();
  79. })
  80. test('$.parent: simple1 html code result should be ok', () => {
  81. const G = $(hc1, config.html);
  82. const parent = G.find('<title>title</title>').parent();
  83. const psCode = parent.generate();
  84. expect(psCode.indexOf('<head>' > -1)).toBeTruthy();
  85. })
  86. test('$.parent: simple1 html code result should be ok', () => {
  87. let res = $(`that.fun('test').fun1().fun2()`)
  88. .find(`fun`)
  89. .parent({ type: 'MemberExpression'})
  90. expect(res.length == 3).toBeTruthy();
  91. })