Skip to content

Commit f151222

Browse files
committed
Add unit tests.
1 parent f184cdd commit f151222

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

spec/v2/providers/dataconnect.spec.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
// SOFTWARE.
2222

2323
import { expect } from "chai";
24+
import fs from "fs";
25+
import * as sinon from "sinon";
2426
import * as dataconnect from "../../../src/v2/providers/dataconnect";
2527
import { CloudEvent } from "../../../src/v2";
2628
import { onInit } from "../../../src/v2/core";
@@ -527,4 +529,100 @@ describe("dataconnect", () => {
527529
expect(hello).to.equal("world");
528530
});
529531
});
532+
533+
describe("initGraphqlServer", () => {
534+
let fsStub: sinon.SinonStub;
535+
536+
beforeEach(() => {
537+
fsStub = sinon.stub(fs, "readFileSync");
538+
});
539+
540+
afterEach(() => {
541+
sinon.restore();
542+
});
543+
544+
it("both `schema` and `schemaFilePath` set", async () => {
545+
const opts = {
546+
schema: "type Query { hello: String }",
547+
schemaFilePath: "schema.gql",
548+
resolvers: {
549+
query: {
550+
hello: () => "Hello, world!",
551+
},
552+
},
553+
};
554+
try {
555+
await dataconnect.initGraphqlServer(opts);
556+
} catch (err: any) {
557+
expect(err.message).to.equal(
558+
"Exactly one of 'schema' or 'schemaFilePath' must be provided."
559+
);
560+
}
561+
});
562+
it("neither `schema` nor `schemaFilePath` set", async () => {
563+
const opts = {
564+
resolvers: {
565+
query: {
566+
hello: () => "Hello, world!",
567+
},
568+
},
569+
};
570+
try {
571+
await dataconnect.initGraphqlServer(opts);
572+
} catch (err: any) {
573+
expect(err.message).to.equal(
574+
"Exactly one of 'schema' or 'schemaFilePath' must be provided."
575+
);
576+
}
577+
});
578+
it("cannot read file in `schemaFilePath`", async () => {
579+
fsStub.throws(new Error("test file not found error"));
580+
const opts = {
581+
schemaFilePath: "schema.gql",
582+
resolvers: {
583+
query: {
584+
hello: () => "Hello, world!",
585+
},
586+
},
587+
};
588+
try {
589+
await dataconnect.initGraphqlServer(opts);
590+
} catch (err: any) {
591+
expect(err.message).to.contain("test file not found error");
592+
}
593+
});
594+
it("no resolvers provided", async () => {
595+
const opts = {
596+
schema: "type Query { hello: String }",
597+
resolvers: {},
598+
};
599+
try {
600+
await dataconnect.initGraphqlServer(opts);
601+
} catch (err: any) {
602+
expect(err.message).to.equal("At least one query or mutation resolver must be provided.");
603+
}
604+
});
605+
});
606+
607+
describe("onGraphRequest", () => {
608+
it("returns callable function", () => {
609+
const opts = {
610+
schema: "type Query { hello: String }",
611+
schemaFilePath: "schema.gql",
612+
resolvers: {
613+
query: {
614+
hello: () => "Hello, world!",
615+
},
616+
},
617+
};
618+
const func = dataconnect.onGraphRequest(opts);
619+
expect(func.__trigger).to.deep.equal({
620+
platform: "gcfv2",
621+
httpsTrigger: {
622+
allowInsecure: false,
623+
},
624+
labels: {},
625+
});
626+
});
627+
});
530628
});

src/v2/providers/dataconnect.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ function onOperation<Variables, ResponseData, PathPatternOrOptions>(
374374
return func;
375375
}
376376

377-
async function initGraphqlServer(opts: GraphqlServerOptions): Promise<express.Express> {
377+
/** @hidden */
378+
export async function initGraphqlServer(opts: GraphqlServerOptions): Promise<express.Express> {
378379
if ((!opts.schema && !opts.schemaFilePath) || (opts.schema && opts.schemaFilePath)) {
379380
throw new Error("Exactly one of 'schema' or 'schemaFilePath' must be provided.");
380381
}

0 commit comments

Comments
 (0)