portal.spec.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import Vue from 'vue';
  2. import {mount,} from "@vue/test-utils";
  3. import {GoPlaidPortal} from "@/portal";
  4. import GoPlaidScope from "@/scope";
  5. // @ts-ignore
  6. import plaidFormTest from "./plaidFormTest";
  7. import "@/setup"
  8. import {inspectFormData} from "@/utils";
  9. describe('portal', () => {
  10. it('vars', async () => {
  11. const portal = GoPlaidPortal()
  12. const Father = Vue.extend({
  13. inject: ["vars"],
  14. template: `
  15. <div class="father" :value="vars.value"></div>
  16. `,
  17. methods: {
  18. change() {
  19. console.log("changed")
  20. // @ts-ignore
  21. this.vars.value = "123456"
  22. }
  23. },
  24. })
  25. const Son = Vue.extend({
  26. inject: ["vars"],
  27. created() {
  28. // @ts-ignore
  29. console.log("vars.value", this.vars.value)
  30. },
  31. template: `
  32. <div class="son" v-bind:value="vars.value"></div>
  33. `,
  34. methods: {
  35. change() {
  36. console.log("changed")
  37. // @ts-ignore
  38. this.vars.value = "son changed"
  39. }
  40. },
  41. })
  42. const Root = {
  43. components: {
  44. "son": Son,
  45. },
  46. template: `
  47. <div>
  48. <son></son>
  49. <go-plaid-portal :visible="true">
  50. <input type="text" v-init-context:vars='{value: "222"}'/>
  51. </go-plaid-portal>
  52. </div>
  53. `,
  54. methods: {
  55. change2: function (val: any) {
  56. console.log("change2", val)
  57. }
  58. },
  59. data() {
  60. return {
  61. vars: {},
  62. }
  63. },
  64. provide(): any {
  65. return {
  66. // @ts-ignore
  67. vars: this.vars,
  68. }
  69. },
  70. }
  71. const wrapper = await mount(Root)
  72. console.log(wrapper.html())
  73. const portalComp: any = wrapper.find(".go-plaid-portal")
  74. await portalComp.vm.changeCurrent(Father)
  75. const father: any = wrapper.find(".father")
  76. expect(father.attributes("value")).toEqual(`222`);
  77. await father.vm.change()
  78. console.log(wrapper.html())
  79. const son: any = wrapper.find(".son")
  80. expect(son.attributes("value")).toEqual(`123456`);
  81. })
  82. it('vars error', async () => {
  83. const Root = {
  84. template: `
  85. <div v-init-context:vars='{value: "222"}'>
  86. <input type="text" v-init-context:vars='{value: "333"}'/>
  87. </div>
  88. `,
  89. methods: {
  90. change2: function (val: any) {
  91. console.log("change2", val)
  92. }
  93. },
  94. data() {
  95. return {
  96. vars: {},
  97. }
  98. },
  99. provide(): any {
  100. return {
  101. // @ts-ignore
  102. vars: this.vars,
  103. }
  104. },
  105. }
  106. expect((mount(Root).vm as any).vars["value"]).toEqual("333")
  107. })
  108. it('plaidForm', async () => {
  109. const wrapper = await mount(plaidFormTest)
  110. console.log(wrapper.html())
  111. const portalComp: any = wrapper.find(".go-plaid-portal")
  112. await portalComp.vm.changeCurrentTemplate(`
  113. <input from="server" type="text" :value='555' v-field-name='[plaidForm, "Name"]'/>
  114. `)
  115. console.log(wrapper.html())
  116. const scope: any = wrapper.findComponent(GoPlaidScope)
  117. console.log(inspectFormData(scope.vm.plaidForm))
  118. expect(scope.vm.plaidForm.get("Name")).toEqual("555")
  119. })
  120. })