Skip to content

Commit 27bccc5

Browse files
committed
javascript
1 parent b3d950d commit 27bccc5

File tree

16 files changed

+24
-31
lines changed

16 files changed

+24
-31
lines changed

generators/src/main/java/com/algolia/codegen/cts/tests/TestsClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
6868
CodegenOperation ope = null;
6969
if (step.type.equals("createClient")) {
7070
stepOut.put("stepTemplate", "tests/client/createClient.mustache");
71-
stepOut.put("isCreateClient", true); // TODO: remove once dart and kotlin are converted
71+
stepOut.put("isCreateClient", true); // TODO: remove once kotlin is converted
7272

7373
boolean hasCustomHosts = step.parameters != null && step.parameters.containsKey("customHosts");
7474

@@ -94,7 +94,7 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
9494
}
9595
stepOut.put("stepTemplate", "tests/client/method.mustache");
9696
stepOut.put("isMethod", true); // TODO: remove once kotlin is converted
97-
stepOut.put("hasOperationParams", ope.hasParams);
97+
stepOut.put("hasParams", ope.hasParams);
9898

9999
// set on testOut because we need to wrap everything for java.
100100
testOut.put("isHelper", (boolean) ope.vendorExtensions.getOrDefault("x-helper", false));

generators/src/main/java/com/algolia/codegen/cts/tests/TestsRequest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,7 @@ public void run(Map<String, CodegenModel> models, Map<String, CodegenOperation>
183183

184184
test.put("request", req.request);
185185
test.put("isAsync", true);
186-
test.put("hasParameters", req.parameters.size() != 0);
187-
test.put("hasOperationParams", ope.hasParams);
186+
test.put("hasParams", ope.hasParams);
188187
test.put("isHelper", isHelper);
189188

190189
if (req.requestOptions != null) {

templates/go/tests/method.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
client.{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}({{#hasOperationParams}}{{^isHelper}}client.NewApi{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}Request({{/isHelper}}
1+
client.{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}({{#hasParams}}{{^isHelper}}client.NewApi{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}Request({{/isHelper}}
22
{{#parametersWithDataType}}{{#required}}{{> tests/generateParams}},{{/required}}{{/parametersWithDataType}}
3-
{{^isHelper}}){{#parametersWithDataType}}{{^required}}.With{{#lambda.pascalcase}}{{{key}}}{{/lambda.pascalcase}}({{> tests/generateParams}}){{/required}}{{/parametersWithDataType}}{{/isHelper}}{{#isHelper}}{{#parametersWithDataType}}{{^required}}{{> tests/generateParams}},{{/required}}{{/parametersWithDataType}}{{/isHelper}}{{/hasOperationParams}}{{#requestOptions}}{{#hasOperationParams}},{{/hasOperationParams}}
3+
{{^isHelper}}){{#parametersWithDataType}}{{^required}}.With{{#lambda.pascalcase}}{{{key}}}{{/lambda.pascalcase}}({{> tests/generateParams}}){{/required}}{{/parametersWithDataType}}{{/isHelper}}{{#isHelper}}{{#parametersWithDataType}}{{^required}}{{> tests/generateParams}},{{/required}}{{/parametersWithDataType}}{{/isHelper}}{{/hasParams}}{{#requestOptions}}{{#hasParams}},{{/hasParams}}
44
{{#queryParameters.parametersWithDataType}}{{clientPrefix}}.WithQueryParam("{{{key}}}", {{> tests/generateInnerParams}}),{{/queryParameters.parametersWithDataType}}{{#headers.parametersWithDataType}}{{clientPrefix}}.WithHeaderParam("{{{key}}}", {{> tests/generateInnerParams}}),{{/headers.parametersWithDataType}}
55
{{/requestOptions}})

templates/javascript/snippets/method.mustache

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@ export async function snippetFor{{#lambda.pascalcase}}{{method}}{{/lambda.pascal
1616
// Initialize the client
1717
const client = {{client}}("YOUR_APP_ID", "YOUR_API_KEY", {{#hasRegionalHost}}'YOUR_APP_ID_REGION', {{/hasRegionalHost}});
1818

19-
{{#hasRequestOptions}}const requestOptions: RequestOptions = {
20-
{{#requestOptions.queryParameters.parameters}}queryParameters: {{{.}}},{{/requestOptions.queryParameters.parameters}}
21-
{{#requestOptions.headers.parameters}}headers: {{{.}}}{{/requestOptions.headers.parameters}}
22-
};{{/hasRequestOptions}}
23-
2419
// Call the API
25-
const response = await client.{{method}}({{#hasParameters}}{{{parameters}}}{{/hasParameters}}{{#hasRequestOptions}}, requestOptions{{/hasRequestOptions}});
20+
const response = {{> tests/method}};
2621

2722
// use typed response
2823
console.log(response);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{^autoCreateClient}}const $client = {{/autoCreateClient}}{{client}}('{{parametersWithDataTypeMap.appId.value}}','{{parametersWithDataTypeMap.apiKey.value}}',{{#hasRegionalHost}}'{{parametersWithDataTypeMap.region.value}}',{{/hasRegionalHost}}{ {{#useEchoRequester}}requester: echoRequester(),{{/useEchoRequester}}{{#hasCustomHosts}}hosts:[{{#customHosts}}{ url: '{{host}}', port: {{port}}, accept: 'readWrite', protocol: 'http' },{{/customHosts}}]{{/hasCustomHosts}} });
1+
{{^autoCreateClient}}const client = {{/autoCreateClient}}{{client}}('{{parametersWithDataTypeMap.appId.value}}','{{parametersWithDataTypeMap.apiKey.value}}',{{#hasRegionalHost}}'{{parametersWithDataTypeMap.region.value}}',{{/hasRegionalHost}}{ {{#useEchoRequester}}requester: echoRequester(),{{/useEchoRequester}}{{#hasCustomHosts}}hosts:[{{#customHosts}}{ url: '{{host}}', port: {{port}}, accept: 'readWrite', protocol: 'http' },{{/customHosts}}]{{/hasCustomHosts}} });
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
const result = {{#isAsync}}await {{/isAsync}}(client{{method}}({{{parameters}}})){{#useEchoRequester}} as unknown as EchoResponse{{/useEchoRequester}};
1+
const result = {{> tests/method}}{{#useEchoRequester}} as unknown as EchoResponse{{/useEchoRequester}};

templates/javascript/tests/client/suite.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('{{testType}}', () => {
1717
{{#tests}}
1818
test('{{{testName}}}', async () => {
1919
{{#autoCreateClient}}
20-
const $client = createClient();
20+
const client = createClient();
2121
{{/autoCreateClient}}
2222

2323
{{#steps}}

templates/javascript/tests/e2e/e2e.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('{{operationId}}', () => {
2323
{{#tests}}
2424
test('{{{testName}}}', async () => {
2525
{{#response}}
26-
{{#body}}const resp = {{/body}}await client.{{method}}({{#hasParameters}}{{{parameters}}}{{/hasParameters}}{{#hasRequestOptions}}, requestOptions{{/hasRequestOptions}});
26+
{{#body}}const resp = {{/body}}{{> tests/method}};
2727

2828
{{#body}}
2929
const expectedBody = {{{.}}};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{#isAsync}}await {{/isAsync}}client.{{method}}({{#hasParams}}{{{parameters}}}{{/hasParams}}{{#hasRequestOptions}}, {
2+
{{#requestOptions.queryParameters.parameters}}queryParameters: {{{.}}},{{/requestOptions.queryParameters.parameters}}
3+
{{#requestOptions.headers.parameters}}headers: {{{.}}}{{/requestOptions.headers.parameters}}
4+
}{{/hasRequestOptions}})

templates/javascript/tests/requests/requests.mustache

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ const client = {{client}}(appId, apiKey, {{#hasRegionalHost}}'{{{defaultRegion}}
1313
describe('{{operationId}}', () => {
1414
{{#tests}}
1515
test('{{{testName}}}', async () => {
16-
{{#hasRequestOptions}}const requestOptions: RequestOptions = {
17-
{{#requestOptions.queryParameters.parameters}}queryParameters: {{{.}}},{{/requestOptions.queryParameters.parameters}}
18-
{{#requestOptions.headers.parameters}}headers: {{{.}}}{{/requestOptions.headers.parameters}}
19-
};{{/hasRequestOptions}}
20-
21-
const req = (await client.{{method}}({{#hasParameters}}{{{parameters}}}{{/hasParameters}}{{#hasRequestOptions}}, requestOptions{{/hasRequestOptions}})) as unknown as EchoResponse;
16+
const req = ({{> tests/method}}) as unknown as EchoResponse;
2217

2318
expect(req.path).toEqual('{{{request.path}}}');
2419
expect(req.method).toEqual('{{{request.method}}}');

templates/php/snippets/method.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Snippet{{client}}
3939
{{/hasRequestOptions}}
4040

4141
// Call the API
42-
$response = $client->{{^hasParameters}}{{{method}}}();{{/hasParameters}}{{#hasParameters}}{{{method}}}({{#parametersWithDataType}}{{> tests/generateParams}}{{/parametersWithDataType}}{{#hasRequestOptions}}$requestOptions{{/hasRequestOptions}});{{/hasParameters}}
42+
$response = $client->{{^hasParams}}{{{method}}}();{{/hasParams}}{{#hasParams}}{{{method}}}({{#parametersWithDataType}}{{> tests/generateParams}}{{/parametersWithDataType}}{{#hasRequestOptions}}$requestOptions{{/hasRequestOptions}});{{/hasParams}}
4343

4444
// play with the response
4545
var_dump($response);
@@ -48,4 +48,4 @@ class Snippet{{client}}
4848

4949
{{/snippets}}
5050
{{/blocksRequests}}
51-
}
51+
}

templates/php/tests/e2e/e2e.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ class {{clientPrefix}}Test extends TestCase
5353
public function test{{#lambda.titlecase}}{{method}}{{/lambda.titlecase}}{{testIndex}}()
5454
{
5555
$client = $this->getClient();
56-
$resp = $client->{{^hasParameters}}{{{method}}}();{{/hasParameters}}{{#hasParameters}}{{{method}}}(
56+
$resp = $client->{{^hasParams}}{{{method}}}();{{/hasParams}}{{#hasParams}}{{{method}}}(
5757
{{#parametersWithDataType}}{{> tests/generateParams}}{{/parametersWithDataType}}{{#hasRequestOptions}}$requestOptions{{/hasRequestOptions}}
58-
);{{/hasParameters}}
58+
);{{/hasParams}}
5959

6060
{{#response}}
6161
{{#body}}

templates/php/tests/requests/requests.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ class {{clientPrefix}}Test extends TestCase implements HttpClientInterface
9797
]
9898
];
9999
{{/hasRequestOptions}}
100-
$client->{{^hasParameters}}{{{method}}}();{{/hasParameters}}{{#hasParameters}}{{{method}}}(
100+
$client->{{^hasParams}}{{{method}}}();{{/hasParams}}{{#hasParams}}{{{method}}}(
101101
{{#parametersWithDataType}}{{> tests/generateParams}}{{/parametersWithDataType}}{{#hasRequestOptions}}$requestOptions{{/hasRequestOptions}}
102-
);{{/hasParameters}}
102+
);{{/hasParams}}
103103

104104
$this->assertRequests([
105105
[

templates/swift/tests/e2e/e2e.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ final class {{client}}RequestsTestsE2E: XCTestCase {
6161
}
6262

6363
{{#response}}
64-
let e2eResponse{{#isGeneric}}: Response<{{{returnType}}}<Hit>>{{/isGeneric}} = try await e2eClient.{{method}}WithHTTPInfo({{#hasParameters}}{{#parametersWithDataType}}{{> tests/generateParams }}{{^-last}}, {{/-last}}{{/parametersWithDataType}}{{/hasParameters}}{{#hasRequestOptions}}, requestOptions: requestOptions{{/hasRequestOptions}})
64+
let e2eResponse{{#isGeneric}}: Response<{{{returnType}}}<Hit>>{{/isGeneric}} = try await e2eClient.{{method}}WithHTTPInfo({{#hasParams}}{{#parametersWithDataType}}{{> tests/generateParams }}{{^-last}}, {{/-last}}{{/parametersWithDataType}}{{/hasParams}}{{#hasRequestOptions}}, requestOptions: requestOptions{{/hasRequestOptions}})
6565
{{#body}}
6666
let e2eResponseBody = try XCTUnwrap(e2eResponse.body)
6767
let e2eResponseBodyData = try CodableHelper.jsonEncoder.encode(e2eResponseBody)

templates/swift/tests/requests/requests.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class {{client}}RequestsTests: XCTestCase {
2929
)
3030
{{/hasRequestOptions}}
3131

32-
let response{{#isGeneric}}: Response<{{{returnType}}}<Hit>>{{/isGeneric}} = try await client.{{method}}WithHTTPInfo({{#hasParameters}}{{#parametersWithDataType}}{{> tests/generateParams }}{{^-last}}, {{/-last}}{{/parametersWithDataType}}{{/hasParameters}}{{#hasRequestOptions}}, requestOptions: requestOptions{{/hasRequestOptions}})
32+
let response{{#isGeneric}}: Response<{{{returnType}}}<Hit>>{{/isGeneric}} = try await client.{{method}}WithHTTPInfo({{#hasParams}}{{#parametersWithDataType}}{{> tests/generateParams }}{{^-last}}, {{/-last}}{{/parametersWithDataType}}{{/hasParams}}{{#hasRequestOptions}}, requestOptions: requestOptions{{/hasRequestOptions}})
3333
let responseBodyData = try XCTUnwrap(response.bodyData)
3434
let echoResponse = try CodableHelper.jsonDecoder.decode(EchoResponse.self, from: responseBodyData)
3535

website/docs/contributing/testing/common-test-suite.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ When writing your template, here is a list of variables accessible from `mustach
140140
"method": "the method to call on the API Client",
141141
"testName": "the descriptive name test (default to `method`)",
142142
"testIndex": "the index of the test to avoid duplicate function name",
143-
"hasParameters": "true if the method has parameters, useful for `GET` requests",
143+
"hasParams": "true if the method has parameters, useful for `GET` requests",
144144
"assertNullBody": "true if the method does not have a body, useful to assert if `GET` and `DELETE` requests are correctly parsed",
145145
"parameters": {
146146
// Object of all parameters with their name, to be used for languages that require the parameter name

0 commit comments

Comments
 (0)