UTEventReporting.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import {
  2. UTSessionPing,
  3. UTTrailheadEnrollPing,
  4. UTUserEventPing,
  5. } from "test/schemas/pings";
  6. import {GlobalOverrider} from "test/unit/utils";
  7. import {UTEventReporting} from "lib/UTEventReporting.jsm";
  8. const FAKE_EVENT_PING_PC = {
  9. "event": "CLICK",
  10. "source": "TOP_SITES",
  11. "addon_version": "123",
  12. "user_prefs": 63,
  13. "session_id": "abc",
  14. "page": "about:newtab",
  15. "action_position": 5,
  16. "locale": "en-US",
  17. };
  18. const FAKE_SESSION_PING_PC = {
  19. "session_duration": 1234,
  20. "addon_version": "123",
  21. "user_prefs": 63,
  22. "session_id": "abc",
  23. "page": "about:newtab",
  24. "locale": "en-US",
  25. };
  26. const FAKE_EVENT_PING_UT = [
  27. "activity_stream", "event", "CLICK", "TOP_SITES", {
  28. "addon_version": "123",
  29. "user_prefs": "63",
  30. "session_id": "abc",
  31. "page": "about:newtab",
  32. "action_position": "5",
  33. },
  34. ];
  35. const FAKE_SESSION_PING_UT = [
  36. "activity_stream", "end", "session", "1234", {
  37. "addon_version": "123",
  38. "user_prefs": "63",
  39. "session_id": "abc",
  40. "page": "about:newtab",
  41. },
  42. ];
  43. const FAKE_TRAILHEAD_ENROLL_EVENT = {
  44. experiment: "activity-stream-trailhead-firstrun-interrupts",
  45. type: "as-firstrun",
  46. branch: "supercharge",
  47. };
  48. const FAKE_TRAILHEAD_ENROLL_EVENT_UT = [
  49. "activity_stream", "enroll", "preference_study", "activity-stream-trailhead-firstrun-interrupts", {
  50. experimentType: "as-firstrun",
  51. branch: "supercharge",
  52. },
  53. ];
  54. describe("UTEventReporting", () => {
  55. let globals;
  56. let sandbox;
  57. let utEvents;
  58. beforeEach(() => {
  59. globals = new GlobalOverrider();
  60. sandbox = globals.sandbox;
  61. sandbox.stub(global.Services.telemetry, "setEventRecordingEnabled");
  62. sandbox.stub(global.Services.telemetry, "recordEvent");
  63. utEvents = new UTEventReporting();
  64. });
  65. afterEach(() => {
  66. globals.restore();
  67. });
  68. describe("#sendUserEvent()", () => {
  69. it("should queue up the correct data to send to Events Telemetry", async () => {
  70. utEvents.sendUserEvent(FAKE_EVENT_PING_PC);
  71. assert.calledWithExactly(global.Services.telemetry.recordEvent, ...FAKE_EVENT_PING_UT);
  72. let ping = global.Services.telemetry.recordEvent.firstCall.args;
  73. assert.validate(ping, UTUserEventPing);
  74. });
  75. });
  76. describe("#sendSessionEndEvent()", () => {
  77. it("should queue up the correct data to send to Events Telemetry", async () => {
  78. utEvents.sendSessionEndEvent(FAKE_SESSION_PING_PC);
  79. assert.calledWithExactly(global.Services.telemetry.recordEvent, ...FAKE_SESSION_PING_UT);
  80. let ping = global.Services.telemetry.recordEvent.firstCall.args;
  81. assert.validate(ping, UTSessionPing);
  82. });
  83. });
  84. describe("#sendTrailheadEnrollEvent()", () => {
  85. it("should queue up the correct data to send to Events Telemetry", async () => {
  86. utEvents.sendTrailheadEnrollEvent(FAKE_TRAILHEAD_ENROLL_EVENT);
  87. assert.calledWithExactly(global.Services.telemetry.recordEvent, ...FAKE_TRAILHEAD_ENROLL_EVENT_UT);
  88. let ping = global.Services.telemetry.recordEvent.firstCall.args;
  89. assert.validate(ping, UTTrailheadEnrollPing);
  90. });
  91. });
  92. describe("#uninit()", () => {
  93. it("should call setEventRecordingEnabled with a false value", () => {
  94. assert.equal(global.Services.telemetry.setEventRecordingEnabled.firstCall.args[0], "activity_stream");
  95. assert.equal(global.Services.telemetry.setEventRecordingEnabled.firstCall.args[1], true);
  96. utEvents.uninit();
  97. assert.equal(global.Services.telemetry.setEventRecordingEnabled.secondCall.args[0], "activity_stream");
  98. assert.equal(global.Services.telemetry.setEventRecordingEnabled.secondCall.args[1], false);
  99. });
  100. });
  101. });