Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { useChoices, LabelStudio, Textarea, ImageView, Sidebar, Choices } from "@humansignal/frontend-test/helpers/LSF";
import {
  multipleChainedViewsVisibilityConfig,
  perRegionVisibilityConfig,
  perRegionVisibilityResult,
  simpleVisibleWhenVisibilityConfig,
  textareaVisibilityConfig,
  visibilityImageData,
  visibilityTextData,
} from "../../data/control_tags/visibility";
 
const checkVisibility = (visibleIndexes: number[], totalIndexes: number) => {
  Array.from({ length: totalIndexes }).forEach((_, i) => {
    cy.get(`.lsf-choices:eq(${i})`).should(visibleIndexes.includes(i) ? "be.visible" : "not.be.visible");
  });
};
 
describe("Visibility", () => {
  it("Ensure correct visibility of conditionally selected choices", () => {
    LabelStudio.params().config(simpleVisibleWhenVisibilityConfig).data(visibilityTextData).withResult([]).init();
 
    const l1Choice = useChoices("&:eq(0)");
    const l2Choice = useChoices("&:eq(1)");
    const l3Choice = useChoices("&:eq(2)");
    checkVisibility([0], 3);
 
    l1Choice.findChoice("1A").click();
    checkVisibility([0, 1], 3);
 
    l2Choice.findChoice("2A").click();
    l3Choice.findChoice("3A").click();
    checkVisibility([0, 1, 2], 3);
 
    LabelStudio.serialize().then((result) => {
      expect(result).to.have.lengthOf(3);
      const expected = [
        { from_name: "level1", value: { choices: ["1A"] } },
        { from_name: "level2", value: { choices: ["2A"] } },
        { from_name: "level3", value: { choices: ["3A"] } },
      ];
      expected.forEach((item, index) => {
        expect(result[index]).to.deep.include(item);
      });
    });
 
    l1Choice.findChoice("1A").click();
    checkVisibility([0], 3);
 
    LabelStudio.serialize().then((result) => {
      expect(result).to.have.lengthOf(0);
    });
 
    l1Choice.findChoice("1B").click();
    checkVisibility([0], 3);
 
    LabelStudio.serialize().then((result) => {
      expect(result).to.have.lengthOf(1);
      expect(result[0]).to.deep.include({ from_name: "level1", value: { choices: ["1B"] } });
    });
  });
 
  it("Ensure correct visibility of conditionally selected choices with parent visibility restrictions", () => {
    LabelStudio.params().config(multipleChainedViewsVisibilityConfig).data(visibilityTextData).withResult([]).init();
 
    const l1Choice = useChoices("&:eq(0)");
    const l2Choice = useChoices("&:eq(1)");
    const l3AChoice = useChoices("&:eq(2)");
    checkVisibility([0], 4);
 
    l1Choice.findChoice("1A").click();
    checkVisibility([0, 1], 4);
 
    l2Choice.findChoice("2A").click();
    checkVisibility([0, 1, 2], 4);
 
    LabelStudio.serialize().then((result) => {
      expect(result).to.have.lengthOf(2);
 
      const expected = [
        { from_name: "level1", value: { choices: ["1A"] } },
        { from_name: "level2", value: { choices: ["2A"] } },
      ];
 
      expected.forEach((item, index) => {
        expect(result[index]).to.deep.include(item);
      });
    });
 
    l3AChoice.findChoice("3X").click();
    checkVisibility([0, 1, 2], 4);
 
    l1Choice.findChoice("1A").click();
    checkVisibility([0], 4);
 
    LabelStudio.serialize().then((result) => {
      expect(result).to.have.lengthOf(0);
    });
  });
 
  it("Ensure correct visibility of conditionally selected and submitted textarea", () => {
    LabelStudio.params().config(textareaVisibilityConfig).data(visibilityTextData).withResult([]).init();
 
    const l1Choice = useChoices("&:eq(0)");
    checkVisibility([0], 1);
 
    l1Choice.findChoice("choice1").click();
 
    LabelStudio.serialize().then((result) => {
      expect(result).to.have.lengthOf(1);
      expect(result[0]).to.deep.include({ from_name: "author", value: { choices: ["choice1"] } });
    });
 
    Textarea.type("text1{enter}");
 
    LabelStudio.serialize().then((result) => {
      expect(result).to.have.lengthOf(2);
 
      const expected = [
        { from_name: "author", value: { choices: ["choice1"] } },
        { from_name: "new_author", value: { text: ["text1"] } },
      ];
 
      expected.forEach((item, index) => {
        expect(result[index]).to.deep.include(item);
      });
    });
 
    l1Choice.findChoice("choice1").click();
    checkVisibility([0], 1);
 
    LabelStudio.serialize().then((result) => {
      expect(result).to.have.lengthOf(0);
    });
  });
 
  it("Ensure correct visibility and data submission of simple perRegion controls", () => {
    LabelStudio.params()
      .config(perRegionVisibilityConfig)
      .data(visibilityImageData)
      .withResult(perRegionVisibilityResult)
      .init();
 
    ImageView.waitForImage();
 
    Sidebar.hasRegions(1);
    Sidebar.toggleRegionSelection(0);
    Sidebar.hasSelectedRegions(1);
 
    Choices.findChoice("Benign").click();
 
    LabelStudio.serialize().then((result) => {
      expect(result).to.have.lengthOf(2);
 
      const expected = [
        { from_name: "label", value: { rectanglelabels: ["Tumor"] } },
        { from_name: "classification", value: { choices: ["Benign"] } },
      ];
 
      expected.forEach((item, index) => {
        expect(result[index]).to.include({ from_name: item.from_name });
        expect(result[index].value).to.deep.include(item.value);
      });
    });
 
    ImageView.clickAtRelative(0.5, 0.5);
    Sidebar.hasSelectedRegions(0);
 
    LabelStudio.serialize().then((result) => {
      expect(result).to.have.lengthOf(2);
 
      const expected = [
        { from_name: "label", value: { rectanglelabels: ["Tumor"] } },
        { from_name: "classification", value: { choices: ["Benign"] } },
      ];
 
      expected.forEach((item, index) => {
        expect(result[index]).to.include({ from_name: item.from_name });
        expect(result[index].value).to.deep.include(item.value);
      });
    });
  });
});