summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/aboutwelcome/MRColorways.test.jsx
blob: c8829d76a7fee544d9201b04dcbc7b2b46ccae83 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import React from "react";
import { shallow } from "enzyme";
import {
  Colorways,
  computeColorWay,
  ColorwayDescription,
  computeVariationIndex,
} from "content-src/aboutwelcome/components/MRColorways";
import { WelcomeScreen } from "content-src/aboutwelcome/components/MultiStageAboutWelcome";

describe("Multistage AboutWelcome module", () => {
  let sandbox;
  let COLORWAY_SCREEN_PROPS;
  beforeEach(() => {
    sandbox = sinon.createSandbox();
    COLORWAY_SCREEN_PROPS = {
      id: "test-colorway-screen",
      totalNumberofScreens: 1,
      content: {
        subtitle: "test subtitle",
        tiles: {
          type: "colorway",
          action: {
            theme: "<event>",
          },
          defaultVariationIndex: 0,
          systemVariations: ["automatic", "light"],
          variations: ["soft", "bold"],
          colorways: [
            {
              id: "default",
              label: "Default",
            },
            {
              id: "abstract",
              label: "Abstract",
            },
          ],
        },
        primary_button: {
          action: {},
          label: "test button",
        },
      },
      messageId: "test-mr-colorway-screen",
      activeTheme: "automatic",
    };
  });
  afterEach(() => {
    sandbox.restore();
  });

  describe("MRColorway component", () => {
    it("should render WelcomeScreen", () => {
      const wrapper = shallow(<WelcomeScreen {...COLORWAY_SCREEN_PROPS} />);

      assert.ok(wrapper.exists());
    });

    it("should use default when activeTheme is not set", () => {
      const wrapper = shallow(<Colorways {...COLORWAY_SCREEN_PROPS} />);
      wrapper.setProps({ activeTheme: null });

      const colorwaysOptionIcons = wrapper.find(
        ".tiles-theme-section .theme .icon"
      );
      assert.strictEqual(colorwaysOptionIcons.length, 2);

      // Default automatic theme is selected by default
      assert.strictEqual(
        colorwaysOptionIcons.first().prop("className").includes("selected"),
        true
      );

      assert.strictEqual(
        colorwaysOptionIcons.first().prop("className").includes("default"),
        true
      );
    });

    it("should use default when activeTheme is alpenglow", () => {
      const wrapper = shallow(<Colorways {...COLORWAY_SCREEN_PROPS} />);
      wrapper.setProps({ activeTheme: "alpenglow" });

      const colorwaysOptionIcons = wrapper.find(
        ".tiles-theme-section .theme .icon"
      );
      assert.strictEqual(colorwaysOptionIcons.length, 2);

      // Default automatic theme is selected when unsupported in colorway alpenglow theme is active
      assert.strictEqual(
        colorwaysOptionIcons.first().prop("className").includes("selected"),
        true
      );

      assert.strictEqual(
        colorwaysOptionIcons.first().prop("className").includes("default"),
        true
      );
    });

    it("should render colorways options", () => {
      const wrapper = shallow(<Colorways {...COLORWAY_SCREEN_PROPS} />);

      const colorwaysOptions = wrapper.find(
        ".tiles-theme-section .theme input[name='theme']"
      );

      const colorwaysOptionIcons = wrapper.find(
        ".tiles-theme-section .theme .icon"
      );

      const colorwaysLabels = wrapper.find(
        ".tiles-theme-section .theme span.sr-only"
      );

      assert.strictEqual(colorwaysOptions.length, 2);
      assert.strictEqual(colorwaysOptionIcons.length, 2);
      assert.strictEqual(colorwaysLabels.length, 2);

      // First colorway option
      // Default theme radio option is selected by default
      assert.strictEqual(
        colorwaysOptionIcons.first().prop("className").includes("selected"),
        true
      );

      //Colorway should be using id property
      assert.strictEqual(
        colorwaysOptions.first().prop("data-colorway"),
        "default"
      );

      // Second colorway option
      assert.strictEqual(
        colorwaysOptionIcons.last().prop("className").includes("selected"),
        false
      );

      //Colorway should be using id property
      assert.strictEqual(
        colorwaysOptions.last().prop("data-colorway"),
        "abstract"
      );

      //Colorway should be labelled for screen readers (parent label is for tooltip only, and does not describe the Colorway)
      assert.strictEqual(
        colorwaysOptions.last().prop("aria-labelledby"),
        "abstract-label"
      );
    });

    it("should handle colorway clicks", () => {
      sandbox.stub(React, "useEffect").callsFake((fn, vals) => {
        if (vals === undefined) {
          fn();
        } else if (vals[0] === "in") {
          fn();
        }
      });

      const handleAction = sandbox.stub();
      const wrapper = shallow(
        <Colorways handleAction={handleAction} {...COLORWAY_SCREEN_PROPS} />
      );
      const colorwaysOptions = wrapper.find(
        ".tiles-theme-section .theme input[name='theme']"
      );

      let props = wrapper.find(ColorwayDescription).props();
      assert.propertyVal(props.colorway, "label", "Default");

      const option = colorwaysOptions.last();
      assert.propertyVal(option.props(), "value", "abstract-soft");
      colorwaysOptions.last().simulate("click");
      assert.calledOnce(handleAction);
    });

    it("should render colorway description", () => {
      const wrapper = shallow(<Colorways {...COLORWAY_SCREEN_PROPS} />);

      let descriptionsWrapper = wrapper.find(ColorwayDescription);
      assert.ok(descriptionsWrapper.exists());

      let props = descriptionsWrapper.props();

      // Colorway  description should display Default theme desc by default
      assert.strictEqual(props.colorway.label, "Default");
    });

    it("ColorwayDescription should display active colorway desc", () => {
      let TEST_COLORWAY_PROPS = {
        colorway: {
          label: "Activist",
          description: "Test Activist",
        },
      };
      const descWrapper = shallow(
        <ColorwayDescription {...TEST_COLORWAY_PROPS} />
      );
      assert.ok(descWrapper.exists());
      const descText = descWrapper.find(".colorway-text");
      assert.equal(
        descText.props()["data-l10n-args"].includes("Activist"),
        true
      );
    });

    it("should computeColorWayId for default active theme", () => {
      let TEST_COLORWAY_PROPS = {
        ...COLORWAY_SCREEN_PROPS,
      };

      const colorwayId = computeColorWay(
        TEST_COLORWAY_PROPS.activeTheme,
        TEST_COLORWAY_PROPS.content.tiles.systemVariations
      );
      assert.strictEqual(colorwayId, "default");
    });

    it("should computeColorWayId for non-default active theme", () => {
      let TEST_COLORWAY_PROPS = {
        ...COLORWAY_SCREEN_PROPS,
        activeTheme: "abstract-soft",
      };

      const colorwayId = computeColorWay(
        TEST_COLORWAY_PROPS.activeTheme,
        TEST_COLORWAY_PROPS.content.tiles.systemVariations
      );
      assert.strictEqual(colorwayId, "abstract");
    });

    it("should computeVariationIndex for default active theme", () => {
      let TEST_COLORWAY_PROPS = {
        ...COLORWAY_SCREEN_PROPS,
      };

      const variationIndex = computeVariationIndex(
        TEST_COLORWAY_PROPS.activeTheme,
        TEST_COLORWAY_PROPS.content.tiles.systemVariations,
        TEST_COLORWAY_PROPS.content.tiles.variations,
        TEST_COLORWAY_PROPS.content.tiles.defaultVariationIndex
      );
      assert.strictEqual(
        variationIndex,
        TEST_COLORWAY_PROPS.content.tiles.defaultVariationIndex
      );
    });

    it("should computeVariationIndex for active theme", () => {
      let TEST_COLORWAY_PROPS = {
        ...COLORWAY_SCREEN_PROPS,
      };

      const variationIndex = computeVariationIndex(
        "light",
        TEST_COLORWAY_PROPS.content.tiles.systemVariations,
        TEST_COLORWAY_PROPS.content.tiles.variations,
        TEST_COLORWAY_PROPS.content.tiles.defaultVariationIndex
      );
      assert.strictEqual(variationIndex, 1);
    });

    it("should computeVariationIndex for colorway theme", () => {
      let TEST_COLORWAY_PROPS = {
        ...COLORWAY_SCREEN_PROPS,
      };

      const variationIndex = computeVariationIndex(
        "abstract-bold",
        TEST_COLORWAY_PROPS.content.tiles.systemVariations,
        TEST_COLORWAY_PROPS.content.tiles.variations,
        TEST_COLORWAY_PROPS.content.tiles.defaultVariationIndex
      );
      assert.strictEqual(variationIndex, 1);
    });

    describe("random colorways", () => {
      let test;
      beforeEach(() => {
        COLORWAY_SCREEN_PROPS.handleAction = sandbox.stub();
        sandbox.stub(window, "matchMedia");
        // eslint-disable-next-line max-nested-callbacks
        sandbox.stub(React, "useEffect").callsFake((fn, vals) => {
          if (vals?.length === 0) {
            fn();
          }
        });
        test = () => {
          shallow(<Colorways {...COLORWAY_SCREEN_PROPS} />);
          return COLORWAY_SCREEN_PROPS.handleAction.firstCall.firstArg
            .currentTarget;
        };
      });

      it("should select a random colorway", () => {
        const { value } = test();

        assert.strictEqual(value, "abstract-soft");
        assert.calledThrice(React.useEffect);
        assert.notCalled(window.matchMedia);
      });

      it("should select a random soft colorway when not dark", () => {
        window.matchMedia.returns({ matches: false });
        COLORWAY_SCREEN_PROPS.content.tiles.darkVariation = 1;

        const { value } = test();

        assert.strictEqual(value, "abstract-soft");
        assert.calledThrice(React.useEffect);
        assert.calledOnce(window.matchMedia);
      });

      it("should select a random bold colorway when dark", () => {
        window.matchMedia.returns({ matches: true });
        COLORWAY_SCREEN_PROPS.content.tiles.darkVariation = 1;

        const { value } = test();

        assert.strictEqual(value, "abstract-bold");
        assert.calledThrice(React.useEffect);
        assert.calledOnce(window.matchMedia);
      });
    });
  });
});