|
3 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | 4 |
|
5 | 5 | import assert from "assert"; |
| 6 | +import sinon from "sinon"; |
6 | 7 |
|
7 | 8 | import PingType from "../../../src/core/pings"; |
8 | 9 | import * as PingMaker from "../../../src/core/pings/maker"; |
9 | 10 | import Glean from "../../../src/core/glean"; |
| 11 | +import CoreEvents from "../../../src/core/events"; |
| 12 | +import Plugin from "../../../src/plugins"; |
| 13 | +import { PingPayload } from "../../../src/core/pings/database"; |
| 14 | +import { JSONObject } from "../../../src/core/utils"; |
| 15 | + |
| 16 | +const sandbox = sinon.createSandbox(); |
10 | 17 |
|
11 | 18 | describe("PingMaker", function() { |
12 | 19 | beforeEach(async function() { |
13 | 20 | await Glean.testResetGlean("something something"); |
14 | 21 | }); |
15 | 22 |
|
| 23 | + afterEach(function () { |
| 24 | + sandbox.restore(); |
| 25 | + }); |
| 26 | + |
16 | 27 | it("ping info must contain a non-empty start and end time", async function() { |
17 | 28 | const ping = new PingType({ |
18 | 29 | name: "custom", |
@@ -100,4 +111,38 @@ describe("PingMaker", function() { |
100 | 111 | assert.strictEqual(await PingMaker.getSequenceNumber(ping2), 11); |
101 | 112 | assert.strictEqual(await PingMaker.getSequenceNumber(ping1), 13); |
102 | 113 | }); |
| 114 | + |
| 115 | + it("collect and store triggers the AfterPingCollection and deals with possible result correctly", async function () { |
| 116 | + // Disable ping uploading for it not to interfere with this tests. |
| 117 | + sandbox.stub(Glean["pingUploader"], "triggerUpload").callsFake(() => Promise.resolve()); |
| 118 | + |
| 119 | + class MockPlugin extends Plugin<typeof CoreEvents["afterPingCollection"]> { |
| 120 | + constructor() { |
| 121 | + super(CoreEvents["afterPingCollection"].name, "mockPlugin"); |
| 122 | + } |
| 123 | + |
| 124 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 125 | + action(_payload: PingPayload): Promise<JSONObject> { |
| 126 | + return Promise.resolve({ "you": "got mocked!" }); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + await Glean.testUninitialize(); |
| 131 | + await Glean.testInitialize("something something", true, { plugins: [ new MockPlugin() ]}); |
| 132 | + const ping = new PingType({ |
| 133 | + name: "ping", |
| 134 | + includeClientId: true, |
| 135 | + sendIfEmpty: true, |
| 136 | + }); |
| 137 | + |
| 138 | + await PingMaker.collectAndStorePing("ident", ping); |
| 139 | + const recordedPing = (await Glean.pingsDatabase.getAllPings())["ident"]; |
| 140 | + assert.deepStrictEqual(recordedPing.payload, { "you": "got mocked!" }); |
| 141 | + |
| 142 | + await Glean.testUninitialize(); |
| 143 | + await Glean.testInitialize("something something", true); |
| 144 | + await PingMaker.collectAndStorePing("ident", ping); |
| 145 | + const recordedPingNoPlugin = (await Glean.pingsDatabase.getAllPings())["ident"]; |
| 146 | + assert.notDeepStrictEqual(recordedPingNoPlugin.payload, { "you": "got mocked!" }); |
| 147 | + }); |
103 | 148 | }); |
0 commit comments