MiniScript.extra.test.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { MiniScript } from './MiniScript';
  2. describe('MiniScriptExtra', () => {
  3. const ms = () => new MiniScript();
  4. test('registerFunction 对象扩展', () => {
  5. const m = new MiniScript();
  6. m.registerFunction('uni.getSystemInfo', () => {
  7. return {
  8. platform: 'ios',
  9. version: '1.0.0',
  10. system: '1.0.0',
  11. };
  12. });
  13. expect(m.execute('return uni.getSystemInfo()')).toEqual({
  14. platform: 'ios',
  15. version: '1.0.0',
  16. system: '1.0.0',
  17. });
  18. });
  19. test('数组调用', () => {
  20. expect(ms().execute('return list[0].value + list[1].array[0].value', {
  21. list: [
  22. {
  23. name: 'a',
  24. value: 1,
  25. },
  26. {
  27. name: 'b',
  28. value: 2,
  29. array: [
  30. {
  31. name: 'c',
  32. value: 3,
  33. },
  34. {
  35. name: 'd',
  36. value: 4,
  37. },
  38. ]
  39. },
  40. ]
  41. })).toEqual(4);
  42. });
  43. test('数组筛选', () => {
  44. expect(ms().execute('return list.filter(item => item.type === "fruit").length', {
  45. list: [
  46. {
  47. name: 'apple',
  48. type: 'fruit',
  49. },
  50. {
  51. name: 'banana',
  52. type: 'fruit',
  53. },
  54. {
  55. name: 'carrot',
  56. type: 'vegetable',
  57. },
  58. ]
  59. })).toEqual(2);
  60. });
  61. });