testUtils.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {
  2. NodeTypes,
  3. CallExpression,
  4. ElementNode,
  5. locStub,
  6. Namespaces,
  7. ElementTypes
  8. } from '../src'
  9. import { CREATE_VNODE } from '../src/runtimeConstants'
  10. const leadingBracketRE = /^\[/
  11. const bracketsRE = /^\[|\]$/g
  12. // Create a matcher for an object
  13. // where non-static expressions should be wrapped in []
  14. // e.g.
  15. // - createObjectMatcher({ 'foo': '[bar]' }) matches { foo: bar }
  16. // - createObjectMatcher({ '[foo]': 'bar' }) matches { [foo]: "bar" }
  17. export function createObjectMatcher(obj: any) {
  18. return {
  19. type: NodeTypes.JS_OBJECT_EXPRESSION,
  20. properties: Object.keys(obj).map(key => ({
  21. type: NodeTypes.JS_PROPERTY,
  22. key: {
  23. type: NodeTypes.SIMPLE_EXPRESSION,
  24. content: key.replace(bracketsRE, ''),
  25. isStatic: !leadingBracketRE.test(key)
  26. },
  27. value: {
  28. type: NodeTypes.SIMPLE_EXPRESSION,
  29. content: obj[key].replace(bracketsRE, ''),
  30. isStatic: !leadingBracketRE.test(obj[key])
  31. }
  32. }))
  33. }
  34. }
  35. export function createElementWithCodegen(
  36. args: CallExpression['arguments']
  37. ): ElementNode {
  38. return {
  39. type: NodeTypes.ELEMENT,
  40. loc: locStub,
  41. ns: Namespaces.HTML,
  42. tag: 'div',
  43. tagType: ElementTypes.ELEMENT,
  44. isSelfClosing: false,
  45. props: [],
  46. children: [],
  47. codegenNode: {
  48. type: NodeTypes.JS_CALL_EXPRESSION,
  49. loc: locStub,
  50. callee: CREATE_VNODE,
  51. arguments: args
  52. }
  53. }
  54. }