summaryrefslogtreecommitdiffstats
path: root/browser/components/aboutwelcome/tests/unit/MultiSelect.test.jsx
blob: b42964f90607ecc43bf62190f87a3c721280d433 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import React from "react";
import { mount } from "enzyme";
import { MultiSelect } from "content-src/components/MultiSelect";

describe("MultiSelect component", () => {
  let sandbox;
  let MULTISELECT_SCREEN_PROPS;
  let setScreenMultiSelects;
  let setActiveMultiSelect;
  beforeEach(() => {
    sandbox = sinon.createSandbox();
    setScreenMultiSelects = sandbox.stub();
    setActiveMultiSelect = sandbox.stub();
    MULTISELECT_SCREEN_PROPS = {
      id: "multiselect-screen",
      content: {
        position: "split",
        split_narrow_bkg_position: "-60px",
        image_alt_text: {
          string_id: "mr2022-onboarding-default-image-alt",
        },
        background:
          "url('chrome://activity-stream/content/data/content/assets/mr-settodefault.svg') var(--mr-secondary-position) no-repeat var(--mr-screen-background-color)",
        progress_bar: true,
        logo: {},
        title: "Test Title",
        tiles: {
          type: "multiselect",
          label: "Test Subtitle",
          data: [
            {
              id: "checkbox-1",
              defaultValue: true,
              label: {
                string_id: "mr2022-onboarding-set-default-primary-button-label",
              },
              action: {
                type: "SET_DEFAULT_BROWSER",
              },
            },
            {
              id: "checkbox-2",
              defaultValue: true,
              label: "Test Checkbox 2",
              action: {
                type: "SHOW_MIGRATION_WIZARD",
                data: {},
              },
            },
            {
              id: "checkbox-3",
              defaultValue: false,
              label: "Test Checkbox 3",
              action: {
                type: "SHOW_MIGRATION_WIZARD",
                data: {},
              },
            },
          ],
        },
        primary_button: {
          label: "Save and Continue",
          action: {
            type: "MULTI_ACTION",
            collectSelect: true,
            navigate: true,
            data: { actions: [] },
          },
        },
        secondary_button: {
          label: "Skip",
          action: {
            navigate: true,
          },
          has_arrow_icon: true,
        },
      },
      setScreenMultiSelects,
      setActiveMultiSelect,
    };
  });
  afterEach(() => {
    sandbox.restore();
  });

  it("should call setScreenMultiSelects with all ids of checkboxes", () => {
    mount(<MultiSelect {...MULTISELECT_SCREEN_PROPS} />);

    assert.calledOnce(setScreenMultiSelects);
    assert.calledWith(setScreenMultiSelects, [
      "checkbox-1",
      "checkbox-2",
      "checkbox-3",
    ]);
  });

  it("should not call setScreenMultiSelects if it's already set", () => {
    let map = sandbox
      .stub()
      .returns(MULTISELECT_SCREEN_PROPS.content.tiles.data);

    mount(
      <MultiSelect screenMultiSelects={{ map }} {...MULTISELECT_SCREEN_PROPS} />
    );

    assert.notCalled(setScreenMultiSelects);
    assert.calledOnce(map);
    assert.calledWith(map, sinon.match.func);
  });

  it("should call setActiveMultiSelect with ids of checkboxes with defaultValue true", () => {
    const wrapper = mount(<MultiSelect {...MULTISELECT_SCREEN_PROPS} />);

    wrapper.setProps({ activeMultiSelect: null });
    assert.calledOnce(setActiveMultiSelect);
    assert.calledWith(setActiveMultiSelect, ["checkbox-1", "checkbox-2"]);
  });

  it("should use activeMultiSelect ids to set checked state for respective checkbox", () => {
    const wrapper = mount(<MultiSelect {...MULTISELECT_SCREEN_PROPS} />);

    wrapper.setProps({ activeMultiSelect: ["checkbox-1", "checkbox-2"] });
    const checkBoxes = wrapper.find(".checkbox-container input");
    assert.strictEqual(checkBoxes.length, 3);

    assert.strictEqual(checkBoxes.first().props().checked, true);
    assert.strictEqual(checkBoxes.at(1).props().checked, true);
    assert.strictEqual(checkBoxes.last().props().checked, false);
  });

  it("cover the randomize property", async () => {
    MULTISELECT_SCREEN_PROPS.content.tiles.data.forEach(
      item => (item.randomize = true)
    );

    const wrapper = mount(<MultiSelect {...MULTISELECT_SCREEN_PROPS} />);

    const checkBoxes = wrapper.find(".checkbox-container input");
    assert.strictEqual(checkBoxes.length, 3);

    // We don't want to actually test the randomization, just that it doesn't
    // throw. We _could_ render the component until we get a different order,
    // and that should work the vast majority of the time, but it's
    // theoretically possible that we get the same order over and over again
    // until we hit the 2 second timeout. That would be an extremely low failure
    // rate, but we already know Math.random() works, so we don't really need to
    // test it anyway. It's not worth the added risk of false failures.
  });

  it("should filter out id when checkbox is unchecked", () => {
    const wrapper = mount(<MultiSelect {...MULTISELECT_SCREEN_PROPS} />);
    wrapper.setProps({ activeMultiSelect: ["checkbox-1", "checkbox-2"] });

    const ckbx1 = wrapper.find(".checkbox-container input").at(0);
    assert.strictEqual(ckbx1.prop("value"), "checkbox-1");
    ckbx1.getDOMNode().checked = false;
    ckbx1.simulate("change");
    assert.calledWith(setActiveMultiSelect, ["checkbox-2"]);
  });

  it("should add id when checkbox is checked", () => {
    const wrapper = mount(<MultiSelect {...MULTISELECT_SCREEN_PROPS} />);
    wrapper.setProps({ activeMultiSelect: ["checkbox-1", "checkbox-2"] });

    const ckbx3 = wrapper.find(".checkbox-container input").at(2);
    assert.strictEqual(ckbx3.prop("value"), "checkbox-3");
    ckbx3.getDOMNode().checked = true;
    ckbx3.simulate("change");
    assert.calledWith(setActiveMultiSelect, [
      "checkbox-1",
      "checkbox-2",
      "checkbox-3",
    ]);
  });

  it("should render radios and checkboxes with correct styles", async () => {
    const SCREEN_PROPS = { ...MULTISELECT_SCREEN_PROPS };
    SCREEN_PROPS.content.tiles.style = { flexDirection: "row", gap: "24px" };
    SCREEN_PROPS.content.tiles.data = [
      {
        id: "checkbox-1",
        defaultValue: true,
        label: { raw: "Test1" },
        action: { type: "OPEN_PROTECTION_REPORT" },
        style: { color: "red" },
        icon: { style: { color: "blue" } },
      },
      {
        id: "radio-1",
        type: "radio",
        group: "radios",
        defaultValue: true,
        label: { raw: "Test3" },
        action: { type: "OPEN_PROTECTION_REPORT" },
        style: { color: "purple" },
        icon: { style: { color: "yellow" } },
      },
    ];
    const wrapper = mount(<MultiSelect {...SCREEN_PROPS} />);

    // wait for effect hook
    await new Promise(resolve => queueMicrotask(resolve));
    // activeMultiSelect was called on effect hook with default values
    assert.calledWith(setActiveMultiSelect, ["checkbox-1", "radio-1"]);

    const container = wrapper.find(".multi-select-container");
    assert.strictEqual(container.prop("style").flexDirection, "row");
    assert.strictEqual(container.prop("style").gap, "24px");

    // checkboxes/radios are rendered with correct styles
    const checkBoxes = wrapper.find(".checkbox-container");
    assert.strictEqual(checkBoxes.length, 2);
    assert.strictEqual(checkBoxes.first().prop("style").color, "red");
    assert.strictEqual(checkBoxes.at(1).prop("style").color, "purple");

    const checks = wrapper.find(".checkbox-container input");
    assert.strictEqual(checks.length, 2);
    assert.strictEqual(checks.first().prop("style").color, "blue");
    assert.strictEqual(checks.at(1).prop("style").color, "yellow");
  });
});