Skip to content

Commit

Permalink
add new failing e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
gquadrati committed Sep 5, 2024
1 parent 32c90d7 commit 1a801da
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
76 changes: 76 additions & 0 deletions __mocks__/api_test_defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
swagger: "2.0"
info:
version: 1.0.0
title: WithDefaults Test API
description: An Openapi for testing client default parameters
host: localhost
basePath: /api/v1
schemes:
- https

securityDefinitions:
bearerToken:
type: apiKey
name: Authorization
in: header
x-auth-scheme: bearer

security:
- Bearer: []

paths:
/test-1:
get:
operationId: "test1"
security:
- bearerToken: []
parameters:
- name: "qo"
in: "query"
required: true
type: "string"

responses:
"200":
description: "Will send `Authenticated`"
"403":
description: "You do not have necessary permissions for the resource"

/test-2:
post:
operationId: "test2"
security:
- bearerToken: []
parameters:
- name: "body"
in: body
required: true
schema:
$ref: "#/definitions/NewModel"

responses:
"200":
description: "Will send `Authenticated`"
"403":
description: "You do not have necessary permissions for the resource"

definitions:
NewModel:
title: NewModel
type: object
properties:
id:
type: string
name:
type: string
required:
- id
- name
responses:

parameters:

consumes:
- application/json
produces:
- application/json
41 changes: 41 additions & 0 deletions e2e/src/__tests__/test-api-withDefaults/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import nodeFetch from "node-fetch";

import * as E from "fp-ts/Either";

import config from "../../config";
import { createClient } from "../../generated/testapiWithDefaults/client";

// @ts-ignore because leaked-handles doesn't ship type defintions
import * as leaked from "leaked-handles";
leaked.set({ debugSockets: true });

// Use same config as
const { skipClient } = config;
const { isSpecEnabled, mockPort } = config.specs.testapiWithDefaults;

// if there's no need for this suite in this particular run, just skip it
const describeSuite = skipClient || !isSpecEnabled ? describe.skip : describe;

describeSuite("Http client generated from Test API spec", () => {
it("should be a valid module", async () => {
expect(createClient).toBeDefined();
expect(createClient).toEqual(expect.any(Function));
});

it("should make a call, with default parameters", async () => {
const client = createClient<"bearerToken">({
baseUrl: `http://localhost:${mockPort}`,
fetchApi: (nodeFetch as any) as typeof fetch,
basePath: "",
// cast op to any to make it compile
withDefaults: op => params => op({ ...params, bearerToken: "abc123" })
});

expect(client.test1).toEqual(expect.any(Function));

const result = await client.test1({
qo: "acb123"
});
expect(result).toMatchObject(E.right({ status: 200 }));
});
});
9 changes: 9 additions & 0 deletions e2e/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ export default {
isSpecEnabled: includeInList(process.env.INCLUDE_SPECS, "testapiV3"),
mockPort: 4103,
url: `${ROOT_DIRECTORY_FOR_E2E}/../__mocks__/openapi_v3/api.yaml`
},
testapiWithDefaults: {
generatedFilesDir: `${GENERATED_BASE_DIR}/testapiWithDefaults`,
isSpecEnabled: includeInList(
process.env.INCLUDE_SPECS,
"testapiWithDefaults"
),
mockPort: 4104,
url: `${ROOT_DIRECTORY_FOR_E2E}/../__mocks__/api_test_defaults.yaml`
}
}
};

0 comments on commit 1a801da

Please sign in to comment.