|
| 1 | +import { VNodeData, VNodeDirective } from "vue" |
| 2 | +import { mergeData } from "../src/index" |
| 3 | + |
| 4 | +it("should handle multiple arguments", () => { |
| 5 | + // Pre-define functions so they compare equal |
| 6 | + function click() {} |
| 7 | + function mouseup() {} |
| 8 | + |
| 9 | + let expected: VNodeData = { |
| 10 | + staticClass: "btn ml-auto", |
| 11 | + class: [{ "text-center": true }, "btn-block", { "btn-primary": true }], |
| 12 | + on: { |
| 13 | + click: [click, click], |
| 14 | + mouseup: [mouseup, mouseup], |
| 15 | + }, |
| 16 | + } |
| 17 | + |
| 18 | + let actual = mergeData( |
| 19 | + { staticClass: "ml-auto" }, |
| 20 | + { staticClass: "btn", class: { "btn-primary": true } }, |
| 21 | + { class: ["btn-block"] }, |
| 22 | + { on: { click, mouseup } }, |
| 23 | + { on: { click, mouseup } }, |
| 24 | + { class: { "text-center": true } } |
| 25 | + ) |
| 26 | + |
| 27 | + expect(actual).toEqual(expected) |
| 28 | +}) |
| 29 | + |
| 30 | +it("should work like in the example", () => { |
| 31 | + let onClick1 = e => alert("💥") |
| 32 | + let onClick2 = e => alert("👍") |
| 33 | + |
| 34 | + let componentData: VNodeData = { |
| 35 | + staticClass: "fn-component", // concatenates all static classes |
| 36 | + class: { |
| 37 | + active: true, |
| 38 | + "special-class": false, |
| 39 | + }, |
| 40 | + attrs: { |
| 41 | + id: "my-functional-component", |
| 42 | + }, |
| 43 | + on: { |
| 44 | + click: onClick1, |
| 45 | + }, |
| 46 | + } |
| 47 | + // <my-btn variant="primary" type="submit" id="form-submit-btn" @click="onClick">Submit</my-btn> |
| 48 | + let templateData: VNodeData = { |
| 49 | + attrs: { |
| 50 | + id: "form-submit-btn", |
| 51 | + type: "submit", |
| 52 | + }, |
| 53 | + on: { click: onClick2 }, |
| 54 | + } |
| 55 | + |
| 56 | + expect(mergeData(templateData, componentData)).toEqual({ |
| 57 | + staticClass: "fn-component", |
| 58 | + class: expect.arrayContaining([ |
| 59 | + { |
| 60 | + active: true, |
| 61 | + "special-class": false, |
| 62 | + }, |
| 63 | + ]), |
| 64 | + attrs: { |
| 65 | + id: "my-functional-component", |
| 66 | + type: "submit", |
| 67 | + }, |
| 68 | + on: { click: expect.arrayContaining([onClick1, onClick2]) }, |
| 69 | + }) |
| 70 | +}) |
0 commit comments