summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/asrouter/templates/SubmitFormSnippet.test.jsx
blob: 12e4f968636d7e9192816a90431a6710d90911d6 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import { mount } from "enzyme";
import React from "react";
import { FluentBundle, FluentResource } from "@fluent/bundle";
import { LocalizationProvider, ReactLocalization } from "@fluent/react";
import { RichText } from "content-src/asrouter/components/RichText/RichText.jsx";
import schema from "content-src/asrouter/templates/SubmitFormSnippet/SubmitFormSnippet.schema.json";
import { SubmitFormSnippet } from "content-src/asrouter/templates/SubmitFormSnippet/SubmitFormSnippet.jsx";

const DEFAULT_CONTENT = {
  scene1_text: "foo",
  scene2_text: "bar",
  scene1_button_label: "Sign Up",
  retry_button_label: "Try again",
  form_action: "foo.com",
  hidden_inputs: { foo: "foo" },
  error_text: "error",
  success_text: "success",
};

describe("SubmitFormSnippet", () => {
  let sandbox;
  let onBlockStub;

  function mockL10nWrapper(content) {
    const bundle = new FluentBundle("en-US");
    for (const [id, value] of Object.entries(content)) {
      if (typeof value === "string") {
        bundle.addResource(new FluentResource(`${id} = ${value}`));
      }
    }
    const l10n = new ReactLocalization([bundle]);
    return {
      wrappingComponent: LocalizationProvider,
      wrappingComponentProps: { l10n },
    };
  }

  /**
   * mountAndCheckProps - Mounts a SubmitFormSnippet with DEFAULT_CONTENT extended with any props
   *                      passed in the content param and validates props against the schema.
   * @param {obj} content Object containing custom message content (e.g. {text, icon, title})
   * @returns enzyme wrapper for SubmitFormSnippet
   */
  function mountAndCheckProps(content = {}) {
    const props = {
      content: Object.assign({}, DEFAULT_CONTENT, content),
      onBlock: onBlockStub,
      onDismiss: sandbox.stub(),
      sendUserActionTelemetry: sandbox.stub(),
      onAction: sandbox.stub(),
      form_method: "POST",
    };
    assert.jsonSchema(props.content, schema);
    return mount(
      <SubmitFormSnippet {...props} />,
      mockL10nWrapper(props.content)
    );
  }

  beforeEach(() => {
    sandbox = sinon.createSandbox();
    onBlockStub = sandbox.stub();
  });

  afterEach(() => {
    sandbox.restore();
  });

  it("should render .text", () => {
    const wrapper = mountAndCheckProps({ scene1_text: "bar" });
    assert.equal(wrapper.find(".body").text(), "bar");
  });
  it("should not render title element if no .title prop is supplied", () => {
    const wrapper = mountAndCheckProps();
    assert.lengthOf(wrapper.find(".title"), 0);
  });
  it("should render .title", () => {
    const wrapper = mountAndCheckProps({ scene1_title: "Foo" });
    assert.equal(wrapper.find(".title").text().trim(), "Foo");
  });
  it("should render light-theme .icon", () => {
    const wrapper = mountAndCheckProps({
      scene1_icon: "data:image/gif;base64,R0lGODl",
    });
    assert.equal(
      wrapper.find(".icon-light-theme").prop("src"),
      "data:image/gif;base64,R0lGODl"
    );
  });
  it("should render dark-theme .icon", () => {
    const wrapper = mountAndCheckProps({
      scene1_icon_dark_theme: "data:image/gif;base64,R0lGODl",
    });
    assert.equal(
      wrapper.find(".icon-dark-theme").prop("src"),
      "data:image/gif;base64,R0lGODl"
    );
  });
  it("should render .button_label and default className", () => {
    const wrapper = mountAndCheckProps({ scene1_button_label: "Click here" });

    const button = wrapper.find("button.ASRouterButton");
    assert.equal(button.text(), "Click here");
    assert.equal(button.prop("className"), "ASRouterButton secondary");
  });

  describe("#SignupView", () => {
    let wrapper;
    const fetchOk = { json: () => Promise.resolve({ status: "ok" }) };
    const fetchFail = { json: () => Promise.resolve({ status: "fail" }) };

    beforeEach(() => {
      wrapper = mountAndCheckProps({
        scene1_text: "bar",
        scene2_email_placeholder_text: "Email",
        scene2_text: "signup",
      });
    });

    it("should set the input type if provided through props.inputType", () => {
      wrapper.setProps({ inputType: "number" });
      wrapper.setState({ expanded: true });
      assert.equal(wrapper.find(".mainInput").instance().type, "number");
    });

    it("should validate via props.validateInput if provided", () => {
      function validateInput(value, content) {
        if (content.country === "CA" && value === "poutine") {
          return "";
        }
        return "Must be poutine";
      }
      const setCustomValidity = sandbox.stub();
      wrapper.setProps({
        validateInput,
        content: { ...DEFAULT_CONTENT, country: "CA" },
      });
      wrapper.setState({ expanded: true });
      const input = wrapper.find(".mainInput");
      input.instance().value = "poutine";
      input.simulate("change", {
        target: { value: "poutine", setCustomValidity },
      });
      assert.calledWith(setCustomValidity, "");

      input.instance().value = "fried chicken";
      input.simulate("change", {
        target: { value: "fried chicken", setCustomValidity },
      });
      assert.calledWith(setCustomValidity, "Must be poutine");
    });

    it("should show the signup form if state.expanded is true", () => {
      wrapper.setState({ expanded: true });

      assert.isTrue(wrapper.find("form").exists());
    });
    it("should dismiss the snippet", () => {
      wrapper.setState({ expanded: true });

      wrapper.find(".ASRouterButton.secondary").simulate("click");

      assert.calledOnce(wrapper.props().onDismiss);
    });
    it("should send a DISMISS event ping", () => {
      wrapper.setState({ expanded: true });

      wrapper.find(".ASRouterButton.secondary").simulate("click");

      assert.equal(
        wrapper.props().sendUserActionTelemetry.firstCall.args[0].event,
        "DISMISS"
      );
    });
    it("should render hidden inputs + email input", () => {
      wrapper.setState({ expanded: true });

      assert.lengthOf(wrapper.find("input[type='hidden']"), 1);
    });
    it("should open the SignupView when the action button is clicked", () => {
      assert.isFalse(wrapper.find("form").exists());

      wrapper.find(".ASRouterButton").simulate("click");

      assert.isTrue(wrapper.state().expanded);
      assert.isTrue(wrapper.find("form").exists());
    });
    it("should submit telemetry when the action button is clicked", () => {
      assert.isFalse(wrapper.find("form").exists());

      wrapper.find(".ASRouterButton").simulate("click");

      assert.equal(
        wrapper.props().sendUserActionTelemetry.firstCall.args[0].event_context,
        "scene1-button-learn-more"
      );
    });
    it("should submit form data when submitted", () => {
      sandbox.stub(window, "fetch").resolves(fetchOk);
      wrapper.setState({ expanded: true });

      wrapper.find("form").simulate("submit");
      assert.calledOnce(window.fetch);
    });
    it("should send user telemetry when submitted", () => {
      wrapper.setState({ expanded: true });

      wrapper.find("form").simulate("submit");

      assert.equal(
        wrapper.props().sendUserActionTelemetry.firstCall.args[0].event_context,
        "conversion-subscribe-activation"
      );
    });
    it("should set signupSuccess when submission status is ok", async () => {
      sandbox.stub(window, "fetch").resolves(fetchOk);
      wrapper.setState({ expanded: true });
      await wrapper.instance().handleSubmit({ preventDefault: sandbox.stub() });

      assert.equal(wrapper.state().signupSuccess, true);
      assert.equal(wrapper.state().signupSubmitted, true);
      assert.calledOnce(onBlockStub);
      assert.calledWithExactly(onBlockStub, { preventDismiss: true });
    });
    it("should send user telemetry when submission status is ok", async () => {
      sandbox.stub(window, "fetch").resolves(fetchOk);
      wrapper.setState({ expanded: true });
      await wrapper.instance().handleSubmit({ preventDefault: sandbox.stub() });

      assert.equal(
        wrapper.props().sendUserActionTelemetry.secondCall.args[0]
          .event_context,
        "subscribe-success"
      );
    });
    it("should not block the snippet if submission failed", async () => {
      sandbox.stub(window, "fetch").resolves(fetchFail);
      wrapper.setState({ expanded: true });
      await wrapper.instance().handleSubmit({ preventDefault: sandbox.stub() });

      assert.equal(wrapper.state().signupSuccess, false);
      assert.equal(wrapper.state().signupSubmitted, true);
      assert.notCalled(onBlockStub);
    });
    it("should not block if do_not_autoblock is true", async () => {
      sandbox.stub(window, "fetch").resolves(fetchOk);
      wrapper = mountAndCheckProps({
        scene1_text: "bar",
        scene2_email_placeholder_text: "Email",
        scene2_text: "signup",
        do_not_autoblock: true,
      });
      wrapper.setState({ expanded: true });
      await wrapper.instance().handleSubmit({ preventDefault: sandbox.stub() });

      assert.equal(wrapper.state().signupSuccess, true);
      assert.equal(wrapper.state().signupSubmitted, true);
      assert.notCalled(onBlockStub);
    });
    it("should send user telemetry if submission failed", async () => {
      sandbox.stub(window, "fetch").resolves(fetchFail);
      wrapper.setState({ expanded: true });
      await wrapper.instance().handleSubmit({ preventDefault: sandbox.stub() });

      assert.equal(
        wrapper.props().sendUserActionTelemetry.secondCall.args[0]
          .event_context,
        "subscribe-error"
      );
    });
    it("should render the signup success message", () => {
      wrapper.setProps({ content: { success_text: "success" } });
      wrapper.setState({ signupSuccess: true, signupSubmitted: true });

      assert.isTrue(wrapper.find(".submissionStatus").exists());
      assert.propertyVal(
        wrapper.find(RichText).props(),
        "localization_id",
        "success_text"
      );
      assert.propertyVal(
        wrapper.find(RichText).props(),
        "success_text",
        "success"
      );
      assert.isFalse(wrapper.find(".ASRouterButton").exists());
    });
    it("should render the signup error message", () => {
      wrapper.setProps({ content: { error_text: "trouble" } });
      wrapper.setState({ signupSuccess: false, signupSubmitted: true });

      assert.isTrue(wrapper.find(".submissionStatus").exists());
      assert.propertyVal(
        wrapper.find(RichText).props(),
        "localization_id",
        "error_text"
      );
      assert.propertyVal(
        wrapper.find(RichText).props(),
        "error_text",
        "trouble"
      );
      assert.isTrue(wrapper.find(".ASRouterButton").exists());
    });
    it("should render the button to return to the signup form if there was an error", () => {
      wrapper.setState({ signupSubmitted: true, signupSuccess: false });

      const button = wrapper.find("button.ASRouterButton");
      assert.equal(button.text(), "Try again");
      wrapper.find(".ASRouterButton").simulate("click");

      assert.equal(wrapper.state().signupSubmitted, false);
    });
    it("should not render the privacy notice checkbox if prop is missing", () => {
      wrapper.setState({ expanded: true });

      assert.isFalse(wrapper.find(".privacyNotice").exists());
    });
    it("should render the privacy notice checkbox if prop is provided", () => {
      wrapper.setProps({
        content: { ...DEFAULT_CONTENT, scene2_privacy_html: "privacy notice" },
      });
      wrapper.setState({ expanded: true });

      assert.isTrue(wrapper.find(".privacyNotice").exists());
    });
    it("should not call fetch if form_method is GET", async () => {
      sandbox.stub(window, "fetch").resolves(fetchOk);
      wrapper.setProps({ form_method: "GET" });
      wrapper.setState({ expanded: true });

      await wrapper.instance().handleSubmit({ preventDefault: sandbox.stub() });

      assert.notCalled(window.fetch);
    });
    it("should block the snippet when form_method is GET", () => {
      wrapper.setProps({ form_method: "GET" });
      wrapper.setState({ expanded: true });

      wrapper.instance().handleSubmit({ preventDefault: sandbox.stub() });

      assert.calledOnce(onBlockStub);
      assert.calledWithExactly(onBlockStub, { preventDismiss: true });
    });
    it("should return to scene 2 alt when clicking the retry button", async () => {
      wrapper.setState({ signupSubmitted: true });
      wrapper.setProps({ expandedAlt: true });

      wrapper.find(".ASRouterButton").simulate("click");

      assert.isTrue(wrapper.find(".scene2Alt").exists());
    });
  });
});