From 29daab0d90155ca81f58301c1404558198177843 Mon Sep 17 00:00:00 2001 From: Mladen Jablanovic Date: Wed, 11 Oct 2023 16:30:39 +0200 Subject: [PATCH] test: Add Typescript test [TSI-2115] --- .github/workflows/test-typescript.yml | 14 +++ .gitignore | 9 +- clients/typescript/__tests__/BasicApiTest.ts | 102 ++++++++++++++++++ .../templates/typescript-fetch/jest.config.js | 5 + .../typescript-fetch/package.mustache | 9 +- 5 files changed, 136 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/test-typescript.yml create mode 100644 clients/typescript/__tests__/BasicApiTest.ts create mode 100644 openapi-generator/templates/typescript-fetch/jest.config.js diff --git a/.github/workflows/test-typescript.yml b/.github/workflows/test-typescript.yml new file mode 100644 index 00000000..ae0e7c33 --- /dev/null +++ b/.github/workflows/test-typescript.yml @@ -0,0 +1,14 @@ +name: Run Typescipt Tests +on: [push] +jobs: + jest: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Build + run: | + npm install + npm run generate.typescript + cd ./clients/typescript + npm install + npm run test diff --git a/.gitignore b/.gitignore index e781fb6b..95f96271 100644 --- a/.gitignore +++ b/.gitignore @@ -34,7 +34,14 @@ clients/java/src/test/java/com/phrase/client/api/* !clients/java/src/test/java/com/phrase/client/api/LocalesApiTest.java !clients/java/src/test/java/com/phrase/client/api/UploadsApiTest.java -clients/typescript/ +clients/typescript/.* +clients/typescript/src +clients/typescript/package.json +clients/typescript/package-lock.json +clients/typescript/README.md +clients/typescript/jest.config.js +clients/typescript/tsconfig.json + clients/cli/cmd/api_*.go clients/cli/phrase-cli diff --git a/clients/typescript/__tests__/BasicApiTest.ts b/clients/typescript/__tests__/BasicApiTest.ts new file mode 100644 index 00000000..9b2b2319 --- /dev/null +++ b/clients/typescript/__tests__/BasicApiTest.ts @@ -0,0 +1,102 @@ +import { LocalesApi } from '../src/apis/LocalesApi'; +import { UploadsApi } from '../src/apis/UploadsApi'; +import { Configuration } from '../src/runtime'; +const FormData = require("form-data") +const fs = require("fs"); + +const globalAny: any = global; +globalAny.FormData = FormData; + +const getMockFetch = (jsonResponse, textResponse) => jest.fn(() => Promise.resolve({ + json: () => Promise.resolve(jsonResponse), + text: () => Promise.resolve(textResponse), + blob: () => Promise.resolve(textResponse), + status: 200, + ok: true +})) as jest.Mock; + +describe('LocalesApi', () => { + let mockFetch; + let configuration; + let api; + + describe('localesCreate', () => { + beforeEach(() => { + mockFetch = getMockFetch({}, 'foo'); + configuration = new Configuration( + { + apiKey: `token PHRASE_TOKEN`, + fetchApi: mockFetch + } + ); + api = new LocalesApi(configuration); + }); + + test('downloads a locale', async () => { + const projectId = 'my-project-id'; + const id = 'my-locale-id'; + + await api.localeDownload({id, projectId}).then((response) => { + expect(response).toBe('foo'); + }); + + expect(mockFetch.mock.calls.length).toBe(1); + expect(mockFetch.mock.calls[0][0]).toBe(`https://api.phrase.com/v2/projects/${projectId}/locales/${id}/download`); + }); + }); + + describe('localesList', () => { + beforeEach(() => { + mockFetch = getMockFetch([{id: 'locale_id_1'}], 'foo'); + configuration = new Configuration( + { + apiKey: `token PHRASE_TOKEN`, + fetchApi: mockFetch + } + ); + api = new LocalesApi(configuration); + }); + + test('lists locales', async () => { + const projectId = 'my-project-id'; + + await api.localesList({projectId}).then((response) => { + expect(response[0].id).toBe('locale_id_1'); + }); + + expect(mockFetch.mock.calls.length).toBe(1); + expect(mockFetch.mock.calls[0][0]).toBe(`https://api.phrase.com/v2/projects/${projectId}/locales`); + }); + }); +}); + +describe('UploadsApi', () => { + let mockFetch; + let configuration; + let api; + + describe('uploadCreate', () => { + beforeEach(() => { + mockFetch = getMockFetch({id: "upload_id"}, 'foo'); + configuration = new Configuration( + { + apiKey: `token PHRASE_TOKEN`, + fetchApi: mockFetch + } + ); + api = new UploadsApi(configuration); + }); + + test('uploads a file', async () => { + const projectId = 'my-project-id'; + const file = fs.createReadStream('package.json'); + + await api.uploadCreate({projectId, file}).then((response) => { + expect(response.id).toBe('upload_id'); + }); + + expect(mockFetch.mock.calls.length).toBe(1); + expect(mockFetch.mock.calls[0][0]).toBe(`https://api.phrase.com/v2/projects/${projectId}/uploads`); + }); + }); +}); diff --git a/openapi-generator/templates/typescript-fetch/jest.config.js b/openapi-generator/templates/typescript-fetch/jest.config.js new file mode 100644 index 00000000..b413e106 --- /dev/null +++ b/openapi-generator/templates/typescript-fetch/jest.config.js @@ -0,0 +1,5 @@ +/** @type {import('ts-jest').JestConfigWithTsJest} */ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node', +}; \ No newline at end of file diff --git a/openapi-generator/templates/typescript-fetch/package.mustache b/openapi-generator/templates/typescript-fetch/package.mustache index a0c513f9..2a38331c 100644 --- a/openapi-generator/templates/typescript-fetch/package.mustache +++ b/openapi-generator/templates/typescript-fetch/package.mustache @@ -8,10 +8,15 @@ "typings": "./dist/index.d.ts", "scripts": { "build": "tsc", - "prepare": "npm run build" + "prepare": "npm run build", + "test": "jest" }, "devDependencies": { - "typescript": "^{{#typescriptThreePlus}}3.6{{/typescriptThreePlus}}{{^typescriptThreePlus}}2.4{{/typescriptThreePlus}}" + "@types/jest": "^29.5.5", + "form-data": "^4.0.0", + "jest": "^29.7.0", + "ts-jest": "^29.1.1", + "typescript": "^4.9.5" }{{#npmRepository}},{{/npmRepository}} {{#npmRepository}} "publishConfig": {