Skip to content

Commit

Permalink
test: Add api tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjholm committed Nov 8, 2022
1 parent 19cd158 commit c5bfbb0
Showing 1 changed file with 78 additions and 6 deletions.
84 changes: 78 additions & 6 deletions src/resources/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.
import * as faas from "../faas/index";
import { api, ApiWorkerOptions } from '.';
import { ResourceServiceClient } from "@nitric/api/proto/resource/v1/resource_grpc_pb";
import { ApiResourceDetails, ResourceDetailsResponse } from "@nitric/api/proto/resource/v1/resource_pb";

jest.mock('../faas/index');

Expand All @@ -39,12 +41,12 @@ describe('Api', () => {
it("should create a new FaasClient", () => {
expect(faas.Faas).toBeCalledTimes(1);
});

it("should provide Faas with ApiWorkerOptions", () => {
const expectedOpts = new ApiWorkerOptions("main", "/newroute/", ['GET', 'POST', 'PATCH', 'PUT', 'DELETE', 'OPTIONS']);
expect(faas.Faas).toBeCalledWith(expectedOpts)
});

it("should call FaasClient::start()", () => {
expect(startSpy).toBeCalledTimes(1);
});
Expand All @@ -61,25 +63,95 @@ describe('Api', () => {
},
})
});

afterAll(() => {
jest.resetAllMocks();
});

it("should create a new FaasClient", () => {
expect(faas.Faas).toBeCalledTimes(1);
});

it("should provide Faas with ApiWorkerOptions", () => {
const expectedOpts = new ApiWorkerOptions("main", "/test/", [method.toUpperCase() as any], {
security: { "test": [] }
});
expect(faas.Faas).toBeCalledWith(expectedOpts)
});

it("should call FaasClient::start()", () => {
expect(startSpy).toBeCalledTimes(1);
});
});
});

describe("when getting the url", () => {

describe("when api details are returned", () => {
let a;
let detailsSpy;

beforeAll(async () => {
// mock the details api
detailsSpy = jest
.spyOn(ResourceServiceClient.prototype, 'details')
.mockImplementationOnce((request, callback: any) => {
const resp = new ResourceDetailsResponse();
resp.setId("mock-id");
resp.setProvider("mock-provider");
resp.setService("mock-service");


const api = new ApiResourceDetails();
api.setUrl("http://localhost:9001/test");
resp.setApi(api);

callback(null, resp);

return null as any;
});

a = await api("main");
});

afterAll(() => {
jest.resetAllMocks();
});

it("should return the url", async () => {
await expect(a.url()).resolves.toBe("http://localhost:9001/test");
});
});

describe("when non api details are returned", () => {
let a;
let detailsSpy;

beforeAll(async () => {
// mock the details api
detailsSpy = jest
.spyOn(ResourceServiceClient.prototype, 'details')
.mockImplementationOnce((request, callback: any) => {
const resp = new ResourceDetailsResponse();
resp.setId("mock-id");
resp.setProvider("mock-provider");
resp.setService("mock-service");

callback(null, resp);

return null as any;
});

a = await api("main");
});

afterAll(() => {
jest.resetAllMocks();
});

it("should throw an error", async () => {
await expect(a.url()).rejects.toThrowError("Unexpected details in response. Expected API details");
});
});
});
});

0 comments on commit c5bfbb0

Please sign in to comment.