|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { ref, nextTick } from "vue"; |
| 3 | +import { buildMockComponent, buildMockCore } from "@/tests/mocks"; |
| 4 | +import { FieldType } from "@/writerTypes"; |
| 5 | +import { generateBuilderManager } from "./builderManager"; |
| 6 | +import { |
| 7 | + useComponentFieldViewModel, |
| 8 | + Dependencies, |
| 9 | +} from "./useComponentFieldViewModel"; |
| 10 | + |
| 11 | +describe(useComponentFieldViewModel.name, () => { |
| 12 | + const componentId = "TestComponent"; |
| 13 | + const fieldKey = "TestField"; |
| 14 | + |
| 15 | + let mockCore: ReturnType<typeof buildMockCore>; |
| 16 | + |
| 17 | + function setupMockDependencies( |
| 18 | + componentId: string, |
| 19 | + fieldKey: string, |
| 20 | + ): Dependencies { |
| 21 | + mockCore = buildMockCore(); |
| 22 | + mockCore.core.addComponent( |
| 23 | + buildMockComponent({ |
| 24 | + id: componentId, |
| 25 | + type: FieldType.Text, |
| 26 | + content: { |
| 27 | + [fieldKey]: undefined, |
| 28 | + }, |
| 29 | + }), |
| 30 | + ); |
| 31 | + |
| 32 | + vi.spyOn(mockCore.core, "getComponentDefinition").mockReturnValue({ |
| 33 | + name: "", |
| 34 | + description: "", |
| 35 | + fields: { |
| 36 | + [fieldKey]: { |
| 37 | + name: "", |
| 38 | + type: FieldType.Text, |
| 39 | + default: "default test", |
| 40 | + }, |
| 41 | + }, |
| 42 | + }); |
| 43 | + |
| 44 | + return { |
| 45 | + wf: mockCore.core, |
| 46 | + ssbm: generateBuilderManager(), |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + it("Default: uses field default from component definition when content is unset", async () => { |
| 51 | + const dependencies = setupMockDependencies(componentId, fieldKey); |
| 52 | + |
| 53 | + const vm = useComponentFieldViewModel( |
| 54 | + { componentId, fieldKey }, |
| 55 | + dependencies, |
| 56 | + ); |
| 57 | + |
| 58 | + await nextTick(); |
| 59 | + |
| 60 | + expect(vm.value).toBe("default test"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("Default update: reactive defaultValue overrides definition and updates value when changed", async () => { |
| 64 | + const dependencies = setupMockDependencies(componentId, fieldKey); |
| 65 | + |
| 66 | + const defaultValue = ref("ref default test"); |
| 67 | + |
| 68 | + const vm = useComponentFieldViewModel( |
| 69 | + { componentId, fieldKey, defaultValue }, |
| 70 | + dependencies, |
| 71 | + ); |
| 72 | + |
| 73 | + await nextTick(); |
| 74 | + |
| 75 | + expect(vm.value).toBe("ref default test"); |
| 76 | + |
| 77 | + defaultValue.value = "ref test"; |
| 78 | + |
| 79 | + await nextTick(); |
| 80 | + |
| 81 | + expect(vm.value).toBe("ref test"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("viewModel update: setting vm.value persists to component content and reflects in getter", async () => { |
| 85 | + const dependencies = setupMockDependencies(componentId, fieldKey); |
| 86 | + |
| 87 | + const vm = useComponentFieldViewModel( |
| 88 | + { componentId, fieldKey }, |
| 89 | + dependencies, |
| 90 | + ); |
| 91 | + |
| 92 | + await nextTick(); |
| 93 | + |
| 94 | + expect(vm.value).toBe("default test"); |
| 95 | + |
| 96 | + vm.value = "new value"; |
| 97 | + |
| 98 | + await nextTick(); |
| 99 | + |
| 100 | + expect( |
| 101 | + mockCore.core.getComponentById(componentId).content[fieldKey], |
| 102 | + ).toBe("new value"); |
| 103 | + expect(vm.value).toBe("new value"); |
| 104 | + |
| 105 | + vm.value = ""; |
| 106 | + |
| 107 | + await nextTick(); |
| 108 | + |
| 109 | + expect( |
| 110 | + mockCore.core.getComponentById(componentId).content[fieldKey], |
| 111 | + ).toBe(""); |
| 112 | + expect(vm.value).toBe(""); |
| 113 | + }); |
| 114 | +}); |
0 commit comments