Skip to content

Commit

Permalink
test: Add Typescript test [TSI-2115]
Browse files Browse the repository at this point in the history
  • Loading branch information
jablan committed Oct 11, 2023
1 parent ca1f575 commit 29daab0
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 3 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/test-typescript.yml
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
102 changes: 102 additions & 0 deletions clients/typescript/__tests__/BasicApiTest.ts
Original file line number Diff line number Diff line change
@@ -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`);
});
});
});
5 changes: 5 additions & 0 deletions openapi-generator/templates/typescript-fetch/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
9 changes: 7 additions & 2 deletions openapi-generator/templates/typescript-fetch/package.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 29daab0

Please sign in to comment.