- Working Angular tests

This commit is contained in:
Ferdi Koomen 2022-01-26 19:03:06 +01:00
parent 53241c1ba3
commit de4d115329
11 changed files with 177 additions and 96 deletions

View File

@ -8,7 +8,7 @@ Before working on a Pull Request, create an issue explaining what you want to co
This ensures that your pull request won't go unnoticed, and that you are not contributing
something that is not suitable for the project.
If you are unfamiliar with Github Pull Requests, please read the following documentation:
If you are unfamiliar with GitHub Pull Requests, please read the following documentation:
https://help.github.com/articles/using-pull-requests
**Your Pull Request must:**

View File

@ -33,6 +33,7 @@ const config: Config.InitialOptions = {
'<rootDir>/test/e2e/client.axios.spec.ts',
'<rootDir>/test/e2e/client.babel.spec.ts',
],
testPathIgnorePatterns: ['<rootDir>/test/e2e/generated'],
},
],
collectCoverageFrom: ['<rootDir>/src/**/*.ts', '!<rootDir>/src/**/*.d.ts', '!<rootDir>/bin', '!<rootDir>/dist'],

View File

@ -4,7 +4,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
const password = await resolve(options, config.PASSWORD);
const additionalHeaders = await resolve(options, config.HEADERS);
const defaultHeaders = Object.entries({
const headers = Object.entries({
Accept: 'application/json',
...additionalHeaders,
...options.headers,
@ -15,28 +15,26 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
[key]: String(value),
}), {} as Record<string, string>);
const headers = new HttpHeaders(defaultHeaders);
if (isStringWithValue(token)) {
headers.append('Authorization', `Bearer ${token}`);
headers['Authorization'] = `Bearer ${token}`;
}
if (isStringWithValue(username) && isStringWithValue(password)) {
const credentials = base64(`${username}:${password}`);
headers.append('Authorization', `Basic ${credentials}`);
headers['Authorization'] = `Basic ${credentials}`;
}
if (options.body) {
if (options.mediaType) {
headers.append('Content-Type', options.mediaType);
headers['Content-Type'] = options.mediaType;
} else if (isBlob(options.body)) {
headers.append('Content-Type', options.body.type || 'application/octet-stream');
headers['Content-Type'] = options.body.type || 'application/octet-stream';
} else if (isString(options.body)) {
headers.append('Content-Type', 'text/plain');
headers['Content-Type'] = 'text/plain';
} else if (!isFormData(options.body)) {
headers.append('Content-Type', 'application/json');
headers['Content-Type'] = 'application/json';
}
}
return headers;
return new HttpHeaders(headers);
};

View File

@ -4,7 +4,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
const password = await resolve(options, config.PASSWORD);
const additionalHeaders = await resolve(options, config.HEADERS);
const defaultHeaders = Object.entries({
const headers = Object.entries({
Accept: 'application/json',
...additionalHeaders,
...options.headers,
@ -15,28 +15,28 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
[key]: String(value),
}), {} as Record<string, string>);
const headers = new Headers(defaultHeaders);
const headers =
if (isStringWithValue(token)) {
headers.append('Authorization', `Bearer ${token}`);
headers['Authorization'] = `Bearer ${token}`;
}
if (isStringWithValue(username) && isStringWithValue(password)) {
const credentials = base64(`${username}:${password}`);
headers.append('Authorization', `Basic ${credentials}`);
headers['Authorization'] = `Basic ${credentials}`;
}
if (options.body) {
if (options.mediaType) {
headers.append('Content-Type', options.mediaType);
headers['Content-Type'] = options.mediaType;
} else if (isBlob(options.body)) {
headers.append('Content-Type', options.body.type || 'application/octet-stream');
headers['Content-Type'] = options.body.type || 'application/octet-stream';
} else if (isString(options.body)) {
headers.append('Content-Type', 'text/plain');
headers['Content-Type'] = 'text/plain';
} else if (!isFormData(options.body)) {
headers.append('Content-Type', 'application/json');
headers['Content-Type'] = 'application/json';
}
}
return headers;
return new Headers(headers);
};

View File

@ -15,27 +15,26 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
[key]: String(value),
}), {} as Record<string, string>);
const headers = new Headers(defaultHeaders);
if (isStringWithValue(token)) {
headers.append('Authorization', `Bearer ${token}`);
headers['Authorization'] = `Bearer ${token}`;
}
if (isStringWithValue(username) && isStringWithValue(password)) {
const credentials = base64(`${username}:${password}`);
headers.append('Authorization', `Basic ${credentials}`);
headers['Authorization'] = `Basic ${credentials}`;
}
if (options.body) {
if (options.mediaType) {
headers.append('Content-Type', options.mediaType);
headers['Content-Type'] = options.mediaType;
} else if (isBlob(options.body)) {
headers.append('Content-Type', 'application/octet-stream');
headers['Content-Type'] = 'application/octet-stream';
} else if (isString(options.body)) {
headers.append('Content-Type', 'text/plain');
headers['Content-Type'] = 'text/plain';
} else if (!isFormData(options.body)) {
headers.append('Content-Type', 'application/json');
headers['Content-Type'] = 'application/json';
}
}
return headers;
return new Headers(headers);
};

View File

@ -15,27 +15,26 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
[key]: String(value),
}), {} as Record<string, string>);
const headers = new Headers(defaultHeaders);
if (isStringWithValue(token)) {
headers.append('Authorization', `Bearer ${token}`);
headers['Authorization'] = `Bearer ${token}`;
}
if (isStringWithValue(username) && isStringWithValue(password)) {
const credentials = base64(`${username}:${password}`);
headers.append('Authorization', `Basic ${credentials}`);
headers['Authorization'] = `Basic ${credentials}`;
}
if (options.body) {
if (options.mediaType) {
headers.append('Content-Type', options.mediaType);
headers['Content-Type'] = options.mediaType;
} else if (isBlob(options.body)) {
headers.append('Content-Type', options.body.type || 'application/octet-stream');
headers['Content-Type'] = options.body.type || 'application/octet-stream';
} else if (isString(options.body)) {
headers.append('Content-Type', 'text/plain');
headers['Content-Type'] = 'text/plain';
} else if (!isFormData(options.body)) {
headers.append('Content-Type', 'application/json');
headers['Content-Type'] = 'application/json';
}
}
return headers;
return new Headers(headers);
};

View File

@ -365,7 +365,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
const password = await resolve(options, config.PASSWORD);
const additionalHeaders = await resolve(options, config.HEADERS);
const defaultHeaders = Object.entries({
const headers = Object.entries({
Accept: 'application/json',
...additionalHeaders,
...options.headers,
@ -376,30 +376,30 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
[key]: String(value),
}), {} as Record<string, string>);
const headers = new Headers(defaultHeaders);
const headers =
if (isStringWithValue(token)) {
headers.append('Authorization', \`Bearer \${token}\`);
headers['Authorization'] = \`Bearer \${token}\`;
}
if (isStringWithValue(username) && isStringWithValue(password)) {
const credentials = base64(\`\${username}:\${password}\`);
headers.append('Authorization', \`Basic \${credentials}\`);
headers['Authorization'] = \`Basic \${credentials}\`;
}
if (options.body) {
if (options.mediaType) {
headers.append('Content-Type', options.mediaType);
headers['Content-Type'] = options.mediaType;
} else if (isBlob(options.body)) {
headers.append('Content-Type', options.body.type || 'application/octet-stream');
headers['Content-Type'] = options.body.type || 'application/octet-stream';
} else if (isString(options.body)) {
headers.append('Content-Type', 'text/plain');
headers['Content-Type'] = 'text/plain';
} else if (!isFormData(options.body)) {
headers.append('Content-Type', 'application/json');
headers['Content-Type'] = 'application/json';
}
}
return headers;
return new Headers(headers);
};
const getRequestBody = (options: ApiRequestOptions): any => {
@ -3268,7 +3268,7 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
const password = await resolve(options, config.PASSWORD);
const additionalHeaders = await resolve(options, config.HEADERS);
const defaultHeaders = Object.entries({
const headers = Object.entries({
Accept: 'application/json',
...additionalHeaders,
...options.headers,
@ -3279,30 +3279,30 @@ const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Pr
[key]: String(value),
}), {} as Record<string, string>);
const headers = new Headers(defaultHeaders);
const headers =
if (isStringWithValue(token)) {
headers.append('Authorization', \`Bearer \${token}\`);
headers['Authorization'] = \`Bearer \${token}\`;
}
if (isStringWithValue(username) && isStringWithValue(password)) {
const credentials = base64(\`\${username}:\${password}\`);
headers.append('Authorization', \`Basic \${credentials}\`);
headers['Authorization'] = \`Basic \${credentials}\`;
}
if (options.body) {
if (options.mediaType) {
headers.append('Content-Type', options.mediaType);
headers['Content-Type'] = options.mediaType;
} else if (isBlob(options.body)) {
headers.append('Content-Type', options.body.type || 'application/octet-stream');
headers['Content-Type'] = options.body.type || 'application/octet-stream';
} else if (isString(options.body)) {
headers.append('Content-Type', 'text/plain');
headers['Content-Type'] = 'text/plain';
} else if (!isFormData(options.body)) {
headers.append('Content-Type', 'application/json');
headers['Content-Type'] = 'application/json';
}
}
return headers;
return new Headers(headers);
};
const getRequestBody = (options: ApiRequestOptions): any => {

View File

@ -1,10 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script type="module" src="js/main.js"></script>
</head>
<body>
</body>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<script type="module" src="js/main.js"></script>
</head>
<body>
</body>
</html>

View File

@ -3,6 +3,7 @@ import { Component, NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { OpenAPI } from './core/OpenAPI';
import { CollectionFormatService } from './services/CollectionFormatService';
import { ComplexService } from './services/ComplexService';
import { DefaultService } from './services/DefaultService';
@ -21,42 +22,43 @@ import { TypesService } from './services/TypesService';
@Component({
selector: 'app-root',
template: `<div>Angular</div>`,
template: `<div>Angular is ready</div>`,
})
export class AppComponent {
constructor(
private readonly collectionFormatService: CollectionFormatService,
private readonly complexServiceService: ComplexService,
private readonly defaultServiceService: DefaultService,
private readonly defaultsServiceService: DefaultsService,
private readonly duplicateServiceService: DuplicateService,
private readonly errorServiceService: ErrorService,
private readonly headerServiceService: HeaderService,
private readonly multipleTags1ServiceService: MultipleTags1Service,
private readonly multipleTags2ServiceService: MultipleTags2Service,
private readonly multipleTags3ServiceService: MultipleTags3Service,
private readonly noContentServiceService: NoContentService,
private readonly parametersServiceService: ParametersService,
private readonly responseServiceService: ResponseService,
private readonly simpleServiceService: SimpleService,
private readonly typesServiceService: TypesService
private readonly complexService: ComplexService,
private readonly defaultService: DefaultService,
private readonly defaultsService: DefaultsService,
private readonly duplicateService: DuplicateService,
private readonly errorService: ErrorService,
private readonly headerService: HeaderService,
private readonly multipleTags1Service: MultipleTags1Service,
private readonly multipleTags2Service: MultipleTags2Service,
private readonly multipleTags3Service: MultipleTags3Service,
private readonly noContentService: NoContentService,
private readonly parametersService: ParametersService,
private readonly responseService: ResponseService,
private readonly simpleService: SimpleService,
private readonly typesService: TypesService
) {
(window as any).api = {
collectionFormatService,
complexServiceService,
defaultServiceService,
defaultsServiceService,
duplicateServiceService,
errorServiceService,
headerServiceService,
multipleTags1ServiceService,
multipleTags2ServiceService,
multipleTags3ServiceService,
noContentServiceService,
parametersServiceService,
responseServiceService,
simpleServiceService,
typesServiceService,
OpenAPI,
CollectionFormatService: collectionFormatService,
ComplexService: complexService,
DefaultService: defaultService,
DefaultsService: defaultsService,
DuplicateService: duplicateService,
ErrorService: errorService,
HeaderService: headerService,
MultipleTags1Service: multipleTags1Service,
MultipleTags2Service: multipleTags2Service,
MultipleTags3Service: multipleTags3Service,
NoContentService: noContentService,
ParametersService: parametersService,
ResponseService: responseService,
SimpleService: simpleService,
TypesService: typesService,
};
}
}

View File

@ -25,6 +25,30 @@ describe('v2.angular', () => {
});
it('requests token', async () => {
expect(true).toBe(true);
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
const result = await browser.evaluate(async () => {
return await new Promise<any>(resolve => {
const { OpenAPI, SimpleService } = (window as any).api;
OpenAPI.TOKEN = (window as any).tokenRequest;
SimpleService.getCallWithoutParametersAndResponse().subscribe(resolve);
});
});
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
});
it('supports complex params', async () => {
const result = await browser.evaluate(async () => {
return await new Promise<any>(resolve => {
const { ComplexService } = (window as any).api;
ComplexService.complexTypes({
first: {
second: {
third: 'Hello World!',
},
},
}).subscribe(resolve);
});
});
expect(result).toBeDefined();
});
});

View File

@ -23,6 +23,64 @@ describe('v3.angular', () => {
});
it('requests token', async () => {
expect(true).toBe(true);
await browser.exposeFunction('tokenRequest', jest.fn().mockResolvedValue('MY_TOKEN'));
const result = await browser.evaluate(async () => {
return await new Promise<any>(resolve => {
const { OpenAPI, SimpleService } = (window as any).api;
OpenAPI.TOKEN = (window as any).tokenRequest;
OpenAPI.USERNAME = undefined;
OpenAPI.PASSWORD = undefined;
SimpleService.getCallWithoutParametersAndResponse().subscribe(resolve);
});
});
expect(result.headers.authorization).toBe('Bearer MY_TOKEN');
});
it('uses credentials', async () => {
const result = await browser.evaluate(async () => {
return await new Promise<any>(resolve => {
const { OpenAPI, SimpleService } = (window as any).api;
OpenAPI.TOKEN = undefined;
OpenAPI.USERNAME = 'username';
OpenAPI.PASSWORD = 'password';
SimpleService.getCallWithoutParametersAndResponse().subscribe(resolve);
});
});
expect(result.headers.authorization).toBe('Basic dXNlcm5hbWU6cGFzc3dvcmQ=');
});
it('supports complex params', async () => {
const result = await browser.evaluate(async () => {
return await new Promise<any>(resolve => {
const { ComplexService } = (window as any).api;
ComplexService.complexTypes({
first: {
second: {
third: 'Hello World!',
},
},
}).subscribe(resolve);
});
});
expect(result).toBeDefined();
});
it('support form data', async () => {
const result = await browser.evaluate(async () => {
return await new Promise<any>(resolve => {
const { ParametersService } = (window as any).api;
ParametersService.callWithParameters(
'valueHeader',
'valueQuery',
'valueForm',
'valueCookie',
'valuePath',
{
prop: 'valueBody',
}
).subscribe(resolve);
});
});
expect(result).toBeDefined();
});
});