summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/asrouter/templates/SimpleSnippet.test.jsx
blob: 7c169525e4e9ed5a87348ff01e4cd03dd56c84ec (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
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/SimpleSnippet/SimpleSnippet.schema.json";
import { SimpleSnippet } from "content-src/asrouter/templates/SimpleSnippet/SimpleSnippet.jsx";

const DEFAULT_CONTENT = { text: "foo" };

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

  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 SimpleSnippet 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 SimpleSnippet
   */
  function mountAndCheckProps(content = {}, provider = "test-provider") {
    const props = {
      content: Object.assign({}, DEFAULT_CONTENT, content),
      provider,
      onBlock: onBlockStub,
      sendUserActionTelemetry: sendUserActionTelemetryStub,
      onAction: sandbox.stub(),
    };
    assert.jsonSchema(props.content, schema);
    return mount(<SimpleSnippet {...props} />, mockL10nWrapper(props.content));
  }

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

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

  it("should have the correct defaults", () => {
    const wrapper = mountAndCheckProps();
    [["button", "title", "block_button_text"]].forEach(prop => {
      const props = wrapper.find(prop[0]).props();
      assert.propertyVal(props, prop[1], schema.properties[prop[2]].default);
    });
  });

  it("should render .text", () => {
    const wrapper = mountAndCheckProps({ 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({ title: "Foo" });
    assert.equal(wrapper.find(".title").text().trim(), "Foo");
  });
  it("should render a light theme variant .icon", () => {
    const wrapper = mountAndCheckProps({
      icon: "data:image/gif;base64,R0lGODl",
    });
    assert.equal(
      wrapper.find(".icon-light-theme").prop("src"),
      "data:image/gif;base64,R0lGODl"
    );
  });
  it("should render a dark theme variant .icon", () => {
    const wrapper = mountAndCheckProps({
      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 a light theme variant .icon as fallback", () => {
    const wrapper = mountAndCheckProps({
      icon_dark_theme: "",
      icon: "data:image/gif;base64,R0lGODp",
    });
    assert.equal(
      wrapper.find(".icon-dark-theme").prop("src"),
      "data:image/gif;base64,R0lGODp"
    );
  });
  it("should render .button_label and default className", () => {
    const wrapper = mountAndCheckProps({
      button_label: "Click here",
      button_action: "OPEN_APPLICATIONS_MENU",
      button_action_args: "appMenu",
    });

    const button = wrapper.find("button.ASRouterButton");
    button.simulate("click");

    assert.equal(button.text(), "Click here");
    assert.equal(button.prop("className"), "ASRouterButton secondary");
    assert.calledOnce(wrapper.props().onAction);
    assert.calledWithExactly(wrapper.props().onAction, {
      type: "OPEN_APPLICATIONS_MENU",
      data: { args: "appMenu" },
    });
  });
  it("should not wrap the main content if a section header is not present", () => {
    const wrapper = mountAndCheckProps({ text: "bar" });
    assert.lengthOf(wrapper.find(".innerContentWrapper"), 0);
  });
  it("should wrap the main content if a section header is present", () => {
    const wrapper = mountAndCheckProps({
      section_title_icon: "data:image/gif;base64,R0lGODl",
      section_title_text: "Messages from Mozilla",
    });

    assert.lengthOf(wrapper.find(".innerContentWrapper"), 1);
  });
  it("should render a section header if text and icon (light-theme) are specified", () => {
    const wrapper = mountAndCheckProps({
      section_title_icon: "data:image/gif;base64,R0lGODl",
      section_title_text: "Messages from Mozilla",
    });

    assert.equal(
      wrapper.find(".section-title .icon-light-theme").prop("style")
        .backgroundImage,
      'url("data:image/gif;base64,R0lGODl")'
    );
    assert.equal(
      wrapper.find(".section-title-text").text().trim(),
      "Messages from Mozilla"
    );
    // ensure there is no <a> when a section_title_url is not specified
    assert.lengthOf(wrapper.find(".section-title a"), 0);
  });
  it("should render a section header if text and icon (light-theme) are specified", () => {
    const wrapper = mountAndCheckProps({
      section_title_icon: "data:image/gif;base64,R0lGODl",
      section_title_icon_dark_theme: "data:image/gif;base64,R0lGODl",
      section_title_text: "Messages from Mozilla",
    });

    assert.equal(
      wrapper.find(".section-title .icon-dark-theme").prop("style")
        .backgroundImage,
      'url("data:image/gif;base64,R0lGODl")'
    );
    assert.equal(
      wrapper.find(".section-title-text").text().trim(),
      "Messages from Mozilla"
    );
    // ensure there is no <a> when a section_title_url is not specified
    assert.lengthOf(wrapper.find(".section-title a"), 0);
  });
  it("should render a section header wrapped in an <a> tag if a url is provided", () => {
    const wrapper = mountAndCheckProps({
      section_title_icon: "data:image/gif;base64,R0lGODl",
      section_title_text: "Messages from Mozilla",
      section_title_url: "https://www.mozilla.org",
    });

    assert.equal(
      wrapper.find(".section-title a").prop("href"),
      "https://www.mozilla.org"
    );
  });
  it("should send an OPEN_URL action when button_url is defined and button is clicked", () => {
    const wrapper = mountAndCheckProps({
      button_label: "Button",
      button_url: "https://mozilla.org",
    });

    const button = wrapper.find("button.ASRouterButton");
    button.simulate("click");

    assert.calledOnce(wrapper.props().onAction);
    assert.calledWithExactly(wrapper.props().onAction, {
      type: "OPEN_URL",
      data: { args: "https://mozilla.org" },
    });
  });
  it("should send an OPEN_ABOUT_PAGE action with entrypoint when the button is clicked", () => {
    const wrapper = mountAndCheckProps({
      button_label: "Button",
      button_action: "OPEN_ABOUT_PAGE",
      button_entrypoint_value: "snippet",
      button_entrypoint_name: "entryPoint",
      button_action_args: "logins",
    });

    const button = wrapper.find("button.ASRouterButton");
    button.simulate("click");

    assert.calledOnce(wrapper.props().onAction);
    assert.calledWithExactly(wrapper.props().onAction, {
      type: "OPEN_ABOUT_PAGE",
      data: { args: "logins", entrypoint: "entryPoint=snippet" },
    });
  });
  it("should send an OPEN_PREFERENCE_PAGE action with entrypoint when the button is clicked", () => {
    const wrapper = mountAndCheckProps({
      button_label: "Button",
      button_action: "OPEN_PREFERENCE_PAGE",
      button_entrypoint_value: "entry=snippet",
      button_action_args: "home",
    });

    const button = wrapper.find("button.ASRouterButton");
    button.simulate("click");

    assert.calledOnce(wrapper.props().onAction);
    assert.calledWithExactly(wrapper.props().onAction, {
      type: "OPEN_PREFERENCE_PAGE",
      data: { args: "home", entrypoint: "entry=snippet" },
    });
  });
  it("should call props.onBlock and sendUserActionTelemetry when CTA button is clicked", () => {
    const wrapper = mountAndCheckProps({ text: "bar" });

    wrapper.instance().onButtonClick();

    assert.calledOnce(onBlockStub);
    assert.calledOnce(sendUserActionTelemetryStub);
  });

  it("should not call props.onBlock if do_not_autoblock is true", () => {
    const wrapper = mountAndCheckProps({ text: "bar", do_not_autoblock: true });

    wrapper.instance().onButtonClick();

    assert.notCalled(onBlockStub);
  });

  it("should not call sendUserActionTelemetry for preview message when CTA button is clicked", () => {
    const wrapper = mountAndCheckProps({ text: "bar" }, "preview");

    wrapper.instance().onButtonClick();

    assert.calledOnce(onBlockStub);
    assert.notCalled(sendUserActionTelemetryStub);
  });
});