fieldname.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import {mount} from "@vue/test-utils";
  2. import {fieldNameDirective} from "@/fieldname";
  3. describe('field name', () => {
  4. it('update and v-on order', async () => {
  5. const form = new FormData()
  6. const Text1 = {
  7. directives: {
  8. "field-name": fieldNameDirective(form),
  9. },
  10. template: `
  11. <div>
  12. <input type="text" v-field-name='"Text1"'
  13. v-on:input="change2($event.target.value+'later')"
  14. v-on:input.trim="change2($event.target.value+'later2')"
  15. />
  16. </div>
  17. `,
  18. methods: {
  19. change2: function(val: any) {
  20. console.log("change2", val)
  21. form.set("Text1", val)
  22. }
  23. },
  24. data() {
  25. return { }
  26. }
  27. }
  28. const wrapper = mount(Text1)
  29. const input = wrapper.find("input[type=text]")
  30. const value = "12345"
  31. await input.setValue(value)
  32. expect(form.get("Text1")).toEqual("12345");
  33. })
  34. it('update value in form', async () => {
  35. const form = new FormData()
  36. const BaseInput = {
  37. template: `
  38. <label class="base-input">
  39. {{ label }}
  40. <input
  41. v-bind="$attrs"
  42. v-bind:value="value"
  43. v-on:input="$emit('input', $event.target.value)"
  44. >
  45. </label>
  46. `,
  47. data() {
  48. return { }
  49. },
  50. props: {
  51. label: String,
  52. value: String,
  53. }
  54. }
  55. const Text1 = {
  56. directives: {
  57. "field-name": fieldNameDirective(form),
  58. },
  59. components: {
  60. "base-input": BaseInput,
  61. },
  62. template: `
  63. <div class="Text1">
  64. <textarea v-field-name='"Textarea1"'>textarea1 value</textarea>
  65. <input type="text" v-field-name='"Text1"'/>
  66. <input type="radio" v-field-name='"Radio1"' value="Radio1 checked value" checked/>
  67. <input type="radio" v-field-name='"Radio1"' value="Radio1 not checked value"/>
  68. <input type="hidden" v-field-name='"Hidden1"' value="hidden1value"/>
  69. <input type="checkbox" v-field-name='"Checkbox1"' value="checkbox checked value" checked/>
  70. <input type="checkbox" v-field-name='"Checkbox2"' value="checkbox not checked value"/>
  71. <input type="number" v-field-name='"Number1"' value="123"/>
  72. <base-input v-field-name='"BaseInput1"' label="Label1"
  73. value="base input value"
  74. ></base-input>
  75. </div>
  76. `,
  77. methods: {
  78. },
  79. data() {
  80. return {
  81. Text1FieldChange: "",
  82. BaseInput1FieldChange: ""
  83. }
  84. }
  85. }
  86. const wrapper = mount(Text1)
  87. expect(form.get("Hidden1")).toEqual("hidden1value");
  88. expect(form.get("Radio1")).toEqual("Radio1 checked value");
  89. expect(form.get("Checkbox1")).toEqual("checkbox checked value");
  90. expect(form.get("Checkbox2")).toBeNull();
  91. expect(form.get("Textarea1")).toEqual("textarea1 value");
  92. expect(form.get("Number1")).toEqual("123");
  93. expect(form.get("BaseInput1")).toEqual("base input value");
  94. const input = wrapper.find("input[type=text]")
  95. const value = "12345"
  96. await input.setValue(value)
  97. expect(form.get("Text1")).toEqual(value);
  98. const baseInput = wrapper.find(".base-input")
  99. baseInput.vm.$emit("change", value)
  100. expect(form.get("BaseInput1")).toEqual(value);
  101. })
  102. })