summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/lib/UTEventReporting.test.js
blob: ad0e3063a17910286b1f6ab9f25f5b3ef1d8d765 (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
import { UTSessionPing, UTUserEventPing } from "test/schemas/pings";
import { GlobalOverrider } from "test/unit/utils";
import { UTEventReporting } from "lib/UTEventReporting.jsm";

const FAKE_EVENT_PING_PC = {
  event: "CLICK",
  source: "TOP_SITES",
  addon_version: "123",
  user_prefs: 63,
  session_id: "abc",
  page: "about:newtab",
  action_position: 5,
  locale: "en-US",
};
const FAKE_SESSION_PING_PC = {
  session_duration: 1234,
  addon_version: "123",
  user_prefs: 63,
  session_id: "abc",
  page: "about:newtab",
  locale: "en-US",
};
const FAKE_EVENT_PING_UT = [
  "activity_stream",
  "event",
  "CLICK",
  "TOP_SITES",
  {
    addon_version: "123",
    user_prefs: "63",
    session_id: "abc",
    page: "about:newtab",
    action_position: "5",
  },
];
const FAKE_SESSION_PING_UT = [
  "activity_stream",
  "end",
  "session",
  "1234",
  {
    addon_version: "123",
    user_prefs: "63",
    session_id: "abc",
    page: "about:newtab",
  },
];

describe("UTEventReporting", () => {
  let globals;
  let sandbox;
  let utEvents;

  beforeEach(() => {
    globals = new GlobalOverrider();
    sandbox = globals.sandbox;
    sandbox.stub(global.Services.telemetry, "setEventRecordingEnabled");
    sandbox.stub(global.Services.telemetry, "recordEvent");

    utEvents = new UTEventReporting();
  });

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

  describe("#sendUserEvent()", () => {
    it("should queue up the correct data to send to Events Telemetry", async () => {
      utEvents.sendUserEvent(FAKE_EVENT_PING_PC);
      assert.calledWithExactly(
        global.Services.telemetry.recordEvent,
        ...FAKE_EVENT_PING_UT
      );

      let ping = global.Services.telemetry.recordEvent.firstCall.args;
      assert.validate(ping, UTUserEventPing);
    });
  });

  describe("#sendSessionEndEvent()", () => {
    it("should queue up the correct data to send to Events Telemetry", async () => {
      utEvents.sendSessionEndEvent(FAKE_SESSION_PING_PC);
      assert.calledWithExactly(
        global.Services.telemetry.recordEvent,
        ...FAKE_SESSION_PING_UT
      );

      let ping = global.Services.telemetry.recordEvent.firstCall.args;
      assert.validate(ping, UTSessionPing);
    });
  });

  describe("#uninit()", () => {
    it("should call setEventRecordingEnabled with a false value", () => {
      assert.equal(
        global.Services.telemetry.setEventRecordingEnabled.firstCall.args[0],
        "activity_stream"
      );
      assert.equal(
        global.Services.telemetry.setEventRecordingEnabled.firstCall.args[1],
        true
      );

      utEvents.uninit();
      assert.equal(
        global.Services.telemetry.setEventRecordingEnabled.secondCall.args[0],
        "activity_stream"
      );
      assert.equal(
        global.Services.telemetry.setEventRecordingEnabled.secondCall.args[1],
        false
      );
    });
  });
});