summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/content-src/components/addUtmParams.test.js
blob: 953fc60d790576c4d1a2ac46b56cdebd6a1e6c30 (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
import {
  addUtmParams,
  BASE_PARAMS,
} from "content-src/asrouter/templates/FirstRun/addUtmParams";

describe("addUtmParams", () => {
  it("should convert a string URL", () => {
    const result = addUtmParams("https://foo.com", "foo");
    assert.equal(result.hostname, "foo.com");
  });
  it("should add all base params", () => {
    assert.match(
      addUtmParams(new URL("https://foo.com"), "foo").toString(),
      /utm_source=activity-stream&utm_campaign=firstrun&utm_medium=referral/
    );
  });
  it("should allow updating base params utm values", () => {
    BASE_PARAMS.utm_campaign = "firstrun-default";
    assert.match(
      addUtmParams(new URL("https://foo.com"), "foo", "default").toString(),
      /utm_source=activity-stream&utm_campaign=firstrun-default&utm_medium=referral/
    );
  });
  it("should add utm_term", () => {
    const params = addUtmParams(new URL("https://foo.com"), "foo").searchParams;
    assert.equal(params.get("utm_term"), "foo", "utm_term");
  });
  it("should not override the URL's existing utm param values", () => {
    const url = new URL("https://foo.com/?utm_source=foo&utm_campaign=bar");
    const params = addUtmParams(url, "foo").searchParams;
    assert.equal(params.get("utm_source"), "foo", "utm_source");
    assert.equal(params.get("utm_campaign"), "bar", "utm_campaign");
    assert.equal(params.get("utm_medium"), "referral", "utm_medium");
  });
});