scope.spec.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {mount,} from "@vue/test-utils";
  2. import "@/setup"
  3. import {componentByTemplate} from "@/utils";
  4. describe('scope', () => {
  5. it('vars and form', async () => {
  6. const form = new FormData()
  7. const MyComp = componentByTemplate(`
  8. <div>
  9. <go-plaid-scope :init='{hello: "123"}' v-slot="{locals: vars}">
  10. <div id="l1">{{ vars.hello }}</div>
  11. <button id="l1Btn" @click='vars.hello = "456"'></button>
  12. <go-plaid-scope :init='{hello: "789"}' v-slot="{locals}">
  13. <div id="l2">{{ locals.hello }}</div>
  14. <button id="l2Btn" @click='locals.hello = "999"'></button>
  15. <go-plaid-scope v-slot="{plaidForm}">
  16. <div id="l3">{{ plaidForm.get("Name") }}</div>
  17. <input type="text" v-field-name='[plaidForm, "Name"]'
  18. value="AAA">
  19. <button id="l3Btn"
  20. @click='locals.hello = plaidForm.get("Name")'></button>
  21. <go-plaid-scope v-slot="{plaidForm}">
  22. <div id="l4">{{ plaidForm.get("Name") }}</div>
  23. <input id="input4" type="text"
  24. v-field-name='[plaidForm, "Name"]'
  25. value="BBB">
  26. </go-plaid-scope>
  27. </go-plaid-scope>
  28. </go-plaid-scope>
  29. </go-plaid-scope>
  30. <div class="globalForm">{{plaidForm.get("Name")}}</div>
  31. </div>
  32. `, form)
  33. const Root = {
  34. components: {
  35. "mycomp": MyComp,
  36. },
  37. template: `
  38. <mycomp></mycomp>`,
  39. provide() {
  40. return {
  41. vars: {},
  42. }
  43. },
  44. }
  45. const wrapper = await mount(Root)
  46. console.log(wrapper.html())
  47. const btn: any = wrapper.find("#l1Btn")
  48. await btn.trigger("click")
  49. const l1: any = wrapper.find("#l1")
  50. expect(l1.text()).toEqual(`456`);
  51. const btn2: any = wrapper.find("#l2Btn")
  52. await btn2.trigger("click")
  53. const l2: any = wrapper.find("#l2")
  54. expect(l2.text()).toEqual(`999`);
  55. const btn3: any = wrapper.find("#l3Btn")
  56. const input4: any = wrapper.find("#input4")
  57. await input4.setValue("CCC")
  58. await btn3.trigger("click")
  59. console.log(wrapper.html())
  60. const l3: any = wrapper.find("#l3")
  61. expect(l2.text()).toEqual(`AAA`);
  62. expect(l3.text()).toEqual(`AAA`);
  63. })
  64. })