-
Notifications
You must be signed in to change notification settings - Fork 26
chore: add client tests #88
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
Changes from all commits
0f2050b
edeed9f
62eec79
5079f25
2c2a49e
0ce50ab
3b3dfa8
823b3dd
4f1cd88
38c6dd4
935f934
f784d23
29bc84c
65ab886
bb63439
5f31dd3
587a222
46b535e
180817d
a7c9514
93b385b
04d4e4a
26cc9ca
2b5871b
971cebf
794326f
69e2983
575d88c
c78aadc
39f8503
7a0358d
169d190
d7a6955
b0fca35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||
[ | ||||||
{ | ||||||
"testName": "does not throw when region is not given", | ||||||
"autoCreateClient": false, | ||||||
"steps": [ | ||||||
{ | ||||||
"type": "createClient", | ||||||
"parameters": { | ||||||
"appId": "my-app-id", | ||||||
"apiKey": "my-api-key", | ||||||
"region": "" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Testing the region can be tricky, because in typescript this won't even compile because of the union but in other languages it's just a string so it's fine, or enum. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, but if I were to write this test case manually, I'd do something like: expect(() => {
// @ts-expect-error it's expecting `region` but intentionally miss it to make it throw
createClient({ appId: '...', apiKey: '...' });
}).toThrow(....); So I think it's alright to have that test, but we need to way to tell TS that it's intentional. Maybe we could update the test template like api-clients-automation/tests/CTS/client/templates/javascript/suite.mustache Lines 30 to 31 in 04d4e4a
await expect(new Promise((resolve, reject) => {
+ // @ts-ignore
{{> step}} What do you think? edit: but // @ts-expect-error this is expected
const $client = new AnalyticsApi(
'app-id',
'api-key',
'', // <- region
{
requester: new EchoRequester(),
}
); because we need to ignore the whole block, but it's not supported by TS: microsoft/TypeScript#19573 And I'm not sure how much we need to invest on types in test code. To pass the CI, I put this. We can go with this for now, and improve it progressively. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can remove some code by removing the async await, for example: describe('basic', () => {
test('does not throw when region is not given', () => {
expect(() => {
// @ts-expect-error
// eslint-disable-next-line no-new
new AnalyticsApi('my-app-id', 'my-api-key', '', {
requester: new EchoRequester(),
});
}).not.toThrow();
});
test('getAverageClickPosition throws without index', () => {
const $client = createClient();
// @ts-expect-error
expect(() => $client.getClickPositions({})).toThrow(
'Parameter `index` is required when calling `getClickPositions`.'
);
});
}); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah but
For example,
We could do this, but this However,
|
||||||
}, | ||||||
"expected": { | ||||||
"error": false | ||||||
} | ||||||
} | ||||||
] | ||||||
}, | ||||||
{ | ||||||
"testName": "getAverageClickPosition throws without index", | ||||||
"steps": [ | ||||||
{ | ||||||
"type": "method", | ||||||
"object": "$client", | ||||||
"path": "getClickPositions", | ||||||
"parameters": [{}], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here for typescript, this won't compile, but will in some languages |
||||||
"expected": { | ||||||
"error": "Parameter `index` is required when calling `getClickPositions`." | ||||||
} | ||||||
} | ||||||
] | ||||||
} | ||||||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[ | ||
{ | ||
"testName": "client throws with invalid parameters", | ||
"autoCreateClient": false, | ||
"steps": [ | ||
{ | ||
"type": "createClient", | ||
"parameters": { | ||
"apiKey": "blah" | ||
}, | ||
"expected": { | ||
"error": "`appId` is missing." | ||
} | ||
} | ||
] | ||
} | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
new {{client}}( | ||
'{{parameters.appId}}', | ||
'{{parameters.apiKey}}', | ||
{{#hasRegionalHost}}'{{parameters.region}}',{{/hasRegionalHost}} | ||
{ | ||
requester: new EchoRequester() | ||
} | ||
) | ||
eunjae-lee marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{{#length}} | ||
expect(actual).toHaveLength({{length}}); | ||
{{/length}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{object}}{{#path}}.{{.}}{{/path}}({{{parameters}}}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{{#isCreateClient}} | ||
const $client = {{> createClient}} | ||
actual = $client; | ||
{{/isCreateClient}} | ||
|
||
{{#isVariable}} | ||
actual = {{> variable}} | ||
{{/isVariable}} | ||
|
||
{{#isMethod}} | ||
actual = {{> method}} | ||
{{/isMethod}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
/* eslint-disable require-await */ | ||
/* eslint-disable @typescript-eslint/explicit-function-return-type */ | ||
// @ts-nocheck | ||
import { {{client}}, EchoRequester } from '{{{import}}}'; | ||
|
||
const appId = process.env.ALGOLIA_APPLICATION_ID || 'Algolia-API-Key'; | ||
const apiKey = process.env.ALGOLIA_SEARCH_KEY || 'Algolia-Application-Id'; | ||
|
||
function createClient(): {{client}} { | ||
return new {{client}}(appId, apiKey, {{#hasRegionalHost}}'us', {{/hasRegionalHost}}{ requester: new EchoRequester() }); | ||
} | ||
|
||
{{#blocks}} | ||
describe('{{operationId}}', () => { | ||
{{#tests}} | ||
test('{{testName}}', async () => { | ||
{{#autoCreateClient}} | ||
const $client = createClient(); | ||
{{/autoCreateClient}} | ||
|
||
let actual; | ||
{{#steps}} | ||
{{#expectedError}} | ||
await expect(new Promise((resolve, reject) => { | ||
{{> step}} | ||
if (actual instanceof Promise) { | ||
actual.then(resolve).catch(reject); | ||
} else { | ||
resolve(); | ||
} | ||
})).rejects.toThrow("{{{expectedError}}}") | ||
{{/expectedError}} | ||
Comment on lines
+24
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We promisify the return of the step and expects it to throw. |
||
|
||
{{^expectedError}} | ||
{{#expectedNoError}} | ||
await expect(new Promise((resolve, reject) => { | ||
{{> step}} | ||
if (actual instanceof Promise) { | ||
actual.then(resolve).catch(reject); | ||
} else { | ||
resolve(); | ||
} | ||
})).resolves.not.toThrow(); | ||
{{/expectedNoError}} | ||
|
||
{{^expectedNoError}} | ||
{{> step}} | ||
|
||
if (actual instanceof Promise) { | ||
actual = await actual; | ||
} | ||
Comment on lines
+50
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return of the step can be a promise, so we resolve it and then move onto validating the result. |
||
|
||
{{#expected}} | ||
{{> expected}} | ||
{{/expected}} | ||
{{/expectedNoError}} | ||
{{/expectedError}} | ||
{{/steps}} | ||
}); | ||
|
||
{{/tests}} | ||
}) | ||
|
||
{{/blocks}} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{object}}{{#path}}.{{.}}{{/path}}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// @ts-nocheck | ||
import { AnalyticsApi, EchoRequester } from '@algolia/client-analytics'; | ||
|
||
const appId = process.env.ALGOLIA_APPLICATION_ID || 'Algolia-API-Key'; | ||
const apiKey = process.env.ALGOLIA_SEARCH_KEY || 'Algolia-Application-Id'; | ||
|
||
function createClient(): AnalyticsApi { | ||
return new AnalyticsApi(appId, apiKey, 'us', { | ||
requester: new EchoRequester(), | ||
}); | ||
} | ||
|
||
describe('basic', () => { | ||
test('does not throw when region is not given', async () => { | ||
let actual; | ||
|
||
await expect( | ||
new Promise((resolve, reject) => { | ||
const $client = new AnalyticsApi('my-app-id', 'my-api-key', '', { | ||
requester: new EchoRequester(), | ||
}); | ||
actual = $client; | ||
|
||
if (actual instanceof Promise) { | ||
actual.then(resolve).catch(reject); | ||
} else { | ||
resolve(); | ||
} | ||
}) | ||
).resolves.not.toThrow(); | ||
}); | ||
|
||
test('getAverageClickPosition throws without index', async () => { | ||
const $client = createClient(); | ||
|
||
let actual; | ||
await expect( | ||
new Promise((resolve, reject) => { | ||
actual = $client.getClickPositions({}); | ||
if (actual instanceof Promise) { | ||
actual.then(resolve).catch(reject); | ||
} else { | ||
resolve(); | ||
} | ||
}) | ||
).rejects.toThrow( | ||
'Parameter `index` is required when calling `getClickPositions`.' | ||
); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only
analytics
client falls back tous
region.