|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | + |
| 3 | +import { ComponentState, PlotState } from "../state/component"; |
| 4 | +import { ContribPoint } from "../model/extension"; |
| 5 | +import { ContributionState } from "../state/contribution"; |
| 6 | +import { ChangeRequest } from "../model/callback"; |
| 7 | +import { |
| 8 | + applyComponentStateChange, |
| 9 | + applyContributionChangeRequests, |
| 10 | + getComponentStateValue, |
| 11 | +} from "./applyPropertyChange"; |
| 12 | + |
| 13 | +const componentState: ComponentState = { |
| 14 | + type: "Box", |
| 15 | + id: "b1", |
| 16 | + components: [ |
| 17 | + { type: "Plot", id: "p1", figure: null } as PlotState, |
| 18 | + { |
| 19 | + type: "Box", |
| 20 | + id: "b2", |
| 21 | + components: [ |
| 22 | + { type: "Checkbox", id: "cb1", value: true }, |
| 23 | + { type: "Dropdown", id: "dd1", value: 13 }, |
| 24 | + ], |
| 25 | + }, |
| 26 | + ], |
| 27 | +}; |
| 28 | + |
| 29 | +describe("Test that applyContributionChangeRequests()", () => { |
| 30 | + const contributionStatesRecord: Record<ContribPoint, ContributionState[]> = { |
| 31 | + panels: [ |
| 32 | + { componentStateResult: { status: "ok" }, visible: true, componentState }, |
| 33 | + ], |
| 34 | + }; |
| 35 | + |
| 36 | + const changeRequest1: ChangeRequest = { |
| 37 | + contribPoint: "panels", |
| 38 | + contribIndex: 0, |
| 39 | + changes: [ |
| 40 | + { |
| 41 | + kind: "Component", |
| 42 | + id: "dd1", |
| 43 | + property: "value", |
| 44 | + value: 14, |
| 45 | + }, |
| 46 | + ], |
| 47 | + }; |
| 48 | + |
| 49 | + const changeRequest2: ChangeRequest = { |
| 50 | + contribPoint: "panels", |
| 51 | + contribIndex: 0, |
| 52 | + changes: [ |
| 53 | + { |
| 54 | + kind: "Component", |
| 55 | + id: "dd1", |
| 56 | + property: "value", |
| 57 | + value: 13, |
| 58 | + }, |
| 59 | + ], |
| 60 | + }; |
| 61 | + |
| 62 | + it("changes state if values are different", () => { |
| 63 | + const newState = applyContributionChangeRequests(contributionStatesRecord, [ |
| 64 | + changeRequest1, |
| 65 | + ]); |
| 66 | + expect(newState).not.toBe(contributionStatesRecord); |
| 67 | + expect( |
| 68 | + newState["panels"][0].componentState!.components![1].components![1].value, |
| 69 | + ).toEqual(14); |
| 70 | + }); |
| 71 | + |
| 72 | + it("doesn't change the state if value stays the same", () => { |
| 73 | + const newState = applyContributionChangeRequests(contributionStatesRecord, [ |
| 74 | + changeRequest2, |
| 75 | + ]); |
| 76 | + expect(newState).toBe(contributionStatesRecord); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe("Test that applyComponentStateChange()", () => { |
| 81 | + it("changes state if values are different", () => { |
| 82 | + const newState = applyComponentStateChange(componentState, { |
| 83 | + kind: "Component", |
| 84 | + id: "cb1", |
| 85 | + property: "value", |
| 86 | + value: false, |
| 87 | + }); |
| 88 | + expect(newState).not.toBe(componentState); |
| 89 | + expect(newState.components![1].components![0].value).toEqual(false); |
| 90 | + }); |
| 91 | + |
| 92 | + it("doesn't change the state if value stays the same", () => { |
| 93 | + const newState = applyComponentStateChange(componentState, { |
| 94 | + kind: "Component", |
| 95 | + id: "cb1", |
| 96 | + property: "value", |
| 97 | + value: true, |
| 98 | + }); |
| 99 | + expect(newState).toBe(componentState); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe("Test that getComponentStateValue()", () => { |
| 104 | + it("works on 1st level", () => { |
| 105 | + expect( |
| 106 | + getComponentStateValue(componentState, { |
| 107 | + kind: "Component", |
| 108 | + id: "b1", |
| 109 | + property: "value", |
| 110 | + }), |
| 111 | + ).toBeUndefined(); |
| 112 | + }); |
| 113 | + |
| 114 | + it("works on 2nd level", () => { |
| 115 | + expect( |
| 116 | + getComponentStateValue(componentState, { |
| 117 | + kind: "Component", |
| 118 | + id: "p1", |
| 119 | + property: "figure", |
| 120 | + }), |
| 121 | + ).toEqual(null); |
| 122 | + }); |
| 123 | + |
| 124 | + it("works on 3rd level", () => { |
| 125 | + expect( |
| 126 | + getComponentStateValue(componentState, { |
| 127 | + kind: "Component", |
| 128 | + id: "cb1", |
| 129 | + property: "value", |
| 130 | + }), |
| 131 | + ).toEqual(true); |
| 132 | + |
| 133 | + expect( |
| 134 | + getComponentStateValue(componentState, { |
| 135 | + kind: "Component", |
| 136 | + id: "dd1", |
| 137 | + property: "value", |
| 138 | + }), |
| 139 | + ).toEqual(13); |
| 140 | + }); |
| 141 | +}); |
0 commit comments