Skip to content

Commit

Permalink
Finish missing tests; remove redundant v1 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inlined committed Mar 2, 2024
1 parent 3365a38 commit 3ddc043
Show file tree
Hide file tree
Showing 23 changed files with 465 additions and 112 deletions.
2 changes: 1 addition & 1 deletion spec/v1/cloud-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("makeCloudFunction", () => {
legacyEventType: "providers/provider/eventTypes/event",
};

it("should call the onInit callback", async () => {
it("calls init function", async () => {
const test: Event = {
context: {
eventId: "00000",
Expand Down
3 changes: 0 additions & 3 deletions spec/v1/providers/analytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,13 @@ describe("Analytics Functions", () => {
},
};

let hello;
functions.onInit(() => hello = "world");
await expect(cloudFunction(event.data, event.context)).to.eventually.deep.equal({
params: {},
user: {
userId: "hi!",
userProperties: {},
},
});
expect(hello).equals("world");
});

it("should remove xValues", () => {
Expand Down
26 changes: 0 additions & 26 deletions spec/v1/providers/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,30 +339,4 @@ describe("Auth Functions", () => {
expect(cf.run).to.not.throw(Error);
});
});

describe("onInit", () => {
beforeEach(() => {
functions.onInit(() => {});
process.env.GCLOUD_PROJECT = "project";
});

after(() => {
functions.onInit(null);
delete process.env.GCLOUD_PROJECT;
})

it("initailizes before onCreate", async () => {
let hello;
functions.onInit(() => hello = "world");
await auth.user().onCreate(() => null)(event.data, event.context);
expect(hello).equals("world");
});

it("initailizes before onDelete", async () => {
let hello;
functions.onInit(() => hello = "world");
await auth.user().onDelete(() => null)(event.data, event.context);
expect(hello).equals("world");
});
});
});
32 changes: 8 additions & 24 deletions spec/v1/providers/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe("Database Functions", () => {
);
});

it("should return a handler that emits events with a proper DataSnapshot", async () => {
it("should return a handler that emits events with a proper DataSnapshot", () => {
const event = {
data: {
data: null,
Expand All @@ -133,11 +133,7 @@ describe("Database Functions", () => {
expect(change.after.val()).to.deep.equal({ foo: "bar" });
});

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await handler(event.data, event.context);
expect(hello).equals("world");
return handler(event.data, event.context);
});

it("Should have params of the correct type", () => {
Expand Down Expand Up @@ -181,7 +177,7 @@ describe("Database Functions", () => {
);
});

it("should return a handler that emits events with a proper DataSnapshot", async () => {
it("should return a handler that emits events with a proper DataSnapshot", () => {
const event = {
data: {
data: null,
Expand All @@ -199,11 +195,7 @@ describe("Database Functions", () => {
expect(data.val()).to.deep.equal({ foo: "bar" });
});

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await handler(event.data, event.context);
expect(hello).equals("world");
return handler(event.data, event.context);
});

it("Should have params of the correct type", () => {
Expand Down Expand Up @@ -247,7 +239,7 @@ describe("Database Functions", () => {
);
});

it("should return a handler that emits events with a proper DataSnapshot", async () => {
it("should return a handler that emits events with a proper DataSnapshot", () => {
const event = {
data: {
data: null,
Expand All @@ -265,11 +257,7 @@ describe("Database Functions", () => {
expect(change.after.val()).to.deep.equal({ foo: "bar" });
});

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await handler(event.data, event.context);
expect(hello).equals("world");
return handler(event.data, event.context);
});

it("Should have params of the correct type", () => {
Expand Down Expand Up @@ -313,7 +301,7 @@ describe("Database Functions", () => {
);
});

it("should return a handler that emits events with a proper DataSnapshot", async () => {
it("should return a handler that emits events with a proper DataSnapshot", () => {
const event = {
data: {
data: { foo: "bar" },
Expand All @@ -331,11 +319,7 @@ describe("Database Functions", () => {
expect(data.val()).to.deep.equal({ foo: "bar" });
});

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await handler(event.data, event.context);
expect(hello).equals("world");
return handler(event.data, event.context);
});

it("Should have params of the correct type", () => {
Expand Down
36 changes: 8 additions & 28 deletions spec/v1/providers/firestore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ describe("Firestore Functions", () => {
delete process.env.GCLOUD_PROJECT;
});

it('constructs appropriate fields and getters for event.data on "document.write" events', async () => {
it('constructs appropriate fields and getters for event.data on "document.write" events', () => {
const testFunction = firestore.document("path").onWrite((change) => {
expect(change.before.data()).to.deep.equal({
key1: false,
Expand All @@ -246,30 +246,20 @@ describe("Firestore Functions", () => {
return true; // otherwise will get warning about returning undefined
});
const event = constructEvent(createOldValue(), createValue());

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await testFunction(event.data, event.context);
expect(hello).equals("world");
return testFunction(event.data, event.context);
}).timeout(5000);

it('constructs appropriate fields and getters for event.data on "document.create" events', async () => {
it('constructs appropriate fields and getters for event.data on "document.create" events', () => {
const testFunction = firestore.document("path").onCreate((data) => {
expect(data.data()).to.deep.equal({ key1: true, key2: 123 });
expect(data.get("key1")).to.equal(true);
return true; // otherwise will get warning about returning undefined
});
const event = constructEvent({}, createValue());

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await testFunction(event.data, event.context);
expect(hello).equals("world");
return testFunction(event.data, event.context);
}).timeout(5000);

it('constructs appropriate fields and getters for event.data on "document.update" events', async () => {
it('constructs appropriate fields and getters for event.data on "document.update" events', () => {
const testFunction = firestore.document("path").onUpdate((change) => {
expect(change.before.data()).to.deep.equal({
key1: false,
Expand All @@ -281,27 +271,17 @@ describe("Firestore Functions", () => {
return true; // otherwise will get warning about returning undefined
});
const event = constructEvent(createOldValue(), createValue());

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await testFunction(event.data, event.context);
expect(hello).equals("world");
return testFunction(event.data, event.context);
}).timeout(5000);

it('constructs appropriate fields and getters for event.data on "document.delete" events', async () => {
it('constructs appropriate fields and getters for event.data on "document.delete" events', () => {
const testFunction = firestore.document("path").onDelete((data) => {
expect(data.data()).to.deep.equal({ key1: false, key2: 111 });
expect(data.get("key1")).to.equal(false);
return true; // otherwise will get warning about returning undefined
});
const event = constructEvent(createOldValue(), {});

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await testFunction(event.data, event.context);
expect(hello).equals("world");
return testFunction(event.data, event.context);
}).timeout(5000);
});

Expand Down
10 changes: 5 additions & 5 deletions spec/v1/providers/https.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,13 @@ describe("CloudHttpsBuilder", () => {
expect(fn.__endpoint.httpsTrigger.invoker).to.deep.equal(["private"]);
});

it("should call initializer", async () => {
it("calls init function", async () => {
let hello;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
const fn = functions.https.onRequest(() => null);
const fn = functions.https.onRequest((_req, res) => {
res.send(200);
});
const req = new MockRequest(
{
data: { foo: "bar" },
Expand All @@ -86,9 +88,7 @@ describe("CloudHttpsBuilder", () => {
}
);
req.method = "POST";
// We don't really have test infrastructure to fake requests. Luckily we
// don't touch much of the request in boilerplate, just trace context.
await fn({headers: []} as any, null as any);
await runHandler(fn, req as any);
expect(hello).to.equal("world");
});
});
Expand Down
8 changes: 2 additions & 6 deletions spec/v1/providers/pubsub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe("Pubsub Functions", () => {
expect(() => pubsub.topic("bad/topic/format")).to.throw(Error);
});

it("should properly handle a new-style event", async () => {
it("should properly handle a new-style event", () => {
const raw = new Buffer('{"hello":"world"}', "utf8").toString("base64");
const event: Event = {
data: {
Expand Down Expand Up @@ -151,15 +151,11 @@ describe("Pubsub Functions", () => {
};
});

let hello;
functions.onInit(() => (hello = "world"));
expect(hello).is.undefined;
await expect(result(event.data, event.context)).to.eventually.deep.equal({
return expect(result(event.data, event.context)).to.eventually.deep.equal({
raw,
json: { hello: "world" },
attributes: { foo: "bar" },
});
expect(hello).equals("world");
});
});

Expand Down
4 changes: 2 additions & 2 deletions spec/v2/providers/alerts/appDistribution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("appDistribution", () => {
expect(res).to.equal("input");
});

it("should call the initializer", async () => {
it("calls init function", async () => {
const func = appDistribution.onNewTesterIosDevicePublished(APPID, (event) => event);

let hello;
Expand Down Expand Up @@ -184,7 +184,7 @@ describe("appDistribution", () => {
expect(res).to.equal("input");
});

it("should call the initializer", async () => {
it("calls init function", async () => {
const func = appDistribution.onInAppFeedbackPublished(APPID, (event) => event);

let hello;
Expand Down
6 changes: 3 additions & 3 deletions spec/v2/providers/alerts/billing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("billing", () => {
});
});

it("should call the initializer", async () => {
it("calls init function", async () => {
const func = billing.onPlanAutomatedUpdatePublished((event) => event);

let hello;
Expand Down Expand Up @@ -88,7 +88,7 @@ describe("billing", () => {
});
});

it("should call the initializer", async () => {
it("calls init function", async () => {
const func = billing.onPlanAutomatedUpdatePublished((event) => event);

let hello;
Expand Down Expand Up @@ -141,7 +141,7 @@ describe("billing", () => {
expect(res).to.equal("input");
});

it("should call the initializer", async () => {
it("calls init function", async () => {
const func = billing.onOperation(ALERT_TYPE, (event) => event, undefined);

let hello;
Expand Down
2 changes: 1 addition & 1 deletion spec/v2/providers/alerts/crashlytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe("crashlytics", () => {
});
});

it("should call initializer", async () => {
it("calls init function", async () => {
const func = crashlytics[method](APPID, myHandler);

let hello;
Expand Down
18 changes: 18 additions & 0 deletions spec/v2/providers/alerts/performance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as alerts from "../../../../src/v2/providers/alerts";
import * as performance from "../../../../src/v2/providers/alerts/performance";
import { FULL_OPTIONS } from "../fixtures";
import { FULL_ENDPOINT, MINIMAL_V2_ENDPOINT } from "../../../fixtures";
import { CloudEvent, onInit } from "../../../../src/v2/core";

const APPID = "123456789";
const myHandler = () => 42;
Expand Down Expand Up @@ -45,6 +46,23 @@ describe("performance", () => {
retry: false,
},
});

it("calls init function", async () => {
const event: CloudEvent<string> = {
specversion: "1.0",
id: "id",
source: "source",
type: "type",
time: "now",
data: "data",
};

let hello: string;
onInit(() => (hello = "world"));
expect(hello).to.be.undefined;
await performance.onThresholdAlertPublished(() => null)(event);
expect(hello).to.equal("world");
});
});

it("should create a function with appid in opts", () => {
Expand Down

0 comments on commit 3ddc043

Please sign in to comment.