Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add Typescript test [TSI-2115] #421

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Typescript 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 test
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ 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/tsconfig.json

clients/cli/cmd/api_*.go
clients/cli/phrase-cli

Expand Down
101 changes: 101 additions & 0 deletions clients/typescript/__tests__/BasicApiTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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");

global.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 clients/typescript/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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"exclude": [
"dist",
"node_modules"
"node_modules",
"__tests__"
]
}