summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/asrouter/templates/EOYSnippet.test.jsx
blob: bd4ab004684061ade9b888bca5926972ab3e38c7 (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
import { EOYSnippet } from "content-src/asrouter/templates/EOYSnippet/EOYSnippet";
import { GlobalOverrider } from "test/unit/utils";
import { mount } from "enzyme";
import React from "react";
import { FluentBundle, FluentResource } from "@fluent/bundle";
import { LocalizationProvider, ReactLocalization } from "@fluent/react";
import schema from "content-src/asrouter/templates/EOYSnippet/EOYSnippet.schema.json";

const DEFAULT_CONTENT = {
  text: "foo",
  donation_amount_first: 50,
  donation_amount_second: 25,
  donation_amount_third: 10,
  donation_amount_fourth: 5,
  donation_form_url: "https://submit.form",
  button_label: "Donate",
};

describe("EOYSnippet", () => {
  let sandbox;
  let wrapper;

  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 EOYSnippet 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 EOYSnippet
   */
  function mountAndCheckProps(content = {}, provider = "test-provider") {
    const props = {
      content: Object.assign({}, DEFAULT_CONTENT, content),
      provider,
      onAction: sandbox.stub(),
      onBlock: sandbox.stub(),
      sendClick: sandbox.stub(),
    };
    const comp = mount(
      <EOYSnippet {...props} />,
      mockL10nWrapper(props.content)
    );
    // Check schema with the final props the component receives (including defaults)
    assert.jsonSchema(comp.children().get(0).props.content, schema);
    return comp;
  }

  beforeEach(() => {
    sandbox = sinon.createSandbox();
    wrapper = mountAndCheckProps();
  });

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

  it("should have the correct defaults", () => {
    wrapper = mountAndCheckProps();
    // SendToDeviceSnippet is a wrapper around SubmitFormSnippet
    const { props } = wrapper.children().get(0);

    const defaultProperties = Object.keys(schema.properties).filter(
      prop => schema.properties[prop].default
    );
    assert.lengthOf(defaultProperties, 4);
    defaultProperties.forEach(prop =>
      assert.propertyVal(props.content, prop, schema.properties[prop].default)
    );
  });

  it("should render 4 donation options", () => {
    assert.lengthOf(wrapper.find("input[type='radio']"), 4);
  });

  it("should have a data-metric field", () => {
    assert.ok(wrapper.find("form[data-metric='EOYSnippetForm']").exists());
  });

  it("should select the second donation option", () => {
    wrapper = mountAndCheckProps({ selected_button: "donation_amount_second" });

    assert.propertyVal(
      wrapper.find("input[type='radio']").get(1).props,
      "defaultChecked",
      true
    );
  });

  it("should set frequency value to monthly", () => {
    const form = wrapper.find("form").instance();
    assert.equal(form.querySelector("[name='frequency']").value, "single");

    form.querySelector("#monthly-checkbox").checked = true;
    wrapper.find("form").simulate("submit");

    assert.equal(form.querySelector("[name='frequency']").value, "monthly");
  });

  it("should block after submitting the form", () => {
    const onBlockStub = sandbox.stub();
    wrapper.setProps({ onBlock: onBlockStub });

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

    assert.calledOnce(onBlockStub);
  });

  it("should not block if do_not_autoblock is true", () => {
    const onBlockStub = sandbox.stub();
    wrapper = mountAndCheckProps({ do_not_autoblock: true });
    wrapper.setProps({ onBlock: onBlockStub });

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

    assert.notCalled(onBlockStub);
  });

  it("should report form submissions", () => {
    wrapper = mountAndCheckProps();
    const { sendClick } = wrapper.props();

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

    assert.calledOnce(sendClick);
    assert.equal(
      sendClick.firstCall.args[0].target.dataset.metric,
      "EOYSnippetForm"
    );
  });

  it("it should preserve URL GET params as hidden inputs", () => {
    wrapper = mountAndCheckProps({
      donation_form_url:
        "https://donate.mozilla.org/pl/?utm_source=desktop-snippet&amp;utm_medium=snippet&amp;utm_campaign=donate&amp;utm_term=7556",
    });

    const hiddenInputs = wrapper.find("input[type='hidden']");

    assert.propertyVal(
      hiddenInputs.find("[name='utm_source']").props(),
      "value",
      "desktop-snippet"
    );
    assert.propertyVal(
      hiddenInputs.find("[name='amp;utm_medium']").props(),
      "value",
      "snippet"
    );
    assert.propertyVal(
      hiddenInputs.find("[name='amp;utm_campaign']").props(),
      "value",
      "donate"
    );
    assert.propertyVal(
      hiddenInputs.find("[name='amp;utm_term']").props(),
      "value",
      "7556"
    );
  });

  describe("locale", () => {
    let stub;
    let globals;
    beforeEach(() => {
      globals = new GlobalOverrider();
      stub = sandbox.stub().returns({ format: () => {} });

      globals = new GlobalOverrider();
      globals.set({ Intl: { NumberFormat: stub } });
    });
    afterEach(() => {
      globals.restore();
    });

    it("should use content.locale for Intl", () => {
      // triggers component rendering and calls the function we're testing
      wrapper.setProps({
        content: {
          locale: "locale-foo",
          donation_form_url: DEFAULT_CONTENT.donation_form_url,
        },
      });

      assert.calledOnce(stub);
      assert.calledWithExactly(stub, "locale-foo", sinon.match.object);
    });

    it("should use navigator.language as locale fallback", () => {
      // triggers component rendering and calls the function we're testing
      wrapper.setProps({
        content: {
          locale: null,
          donation_form_url: DEFAULT_CONTENT.donation_form_url,
        },
      });

      assert.calledOnce(stub);
      assert.calledWithExactly(stub, navigator.language, sinon.match.object);
    });
  });
});