-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// TS gets a but overloaded and will not address the parameters correctly | ||
// so we need to define these types here to trick it | ||
type ClientMethod = (...args: unknown[]) => Promise<unknown>; | ||
type GeneratorClientMethod = (...args: unknown[]) => AsyncGenerator<unknown>; | ||
|
||
/** | ||
* A helper function to get the results from a client method | ||
* | ||
* Since some functions are generators and some are not, we need to handle them differently. | ||
* This function will call the client method and return the results. | ||
* | ||
* @template T | ||
* @param {boolean} generator - Whether the client method is a generator | ||
* @param {T} client - The client to call the method on | ||
* @param {string} clientMethod - The method to call on the clientMethod | ||
* @param {Array<unknown> }args - The arguments to pass to the client method | ||
* | ||
* @return {Promise<unknown | Array<unknown>>} The results of the client method | ||
*/ | ||
export const getResults = async <T>( | ||
generator: boolean, | ||
client: T, | ||
clientMethod: keyof T, | ||
args: [unknown?, unknown?, unknown?] = [], | ||
): Promise<unknown | Array<unknown>> => { | ||
if (!client) { | ||
throw new Error('Client is not defined'); | ||
} | ||
|
||
if (client[clientMethod] === undefined) { | ||
throw new Error( | ||
`Method ${client.constructor.name}::${String( | ||
clientMethod, | ||
)} is not defined`, | ||
); | ||
} | ||
|
||
if (typeof client[clientMethod] !== 'function') { | ||
throw new Error( | ||
`Method ${client.constructor.name}::${String( | ||
clientMethod, | ||
)} is not a function`, | ||
); | ||
} | ||
|
||
if (!generator) { | ||
return await (client[clientMethod] as ClientMethod)(...args); | ||
} | ||
|
||
const results: Array<unknown> = []; | ||
|
||
for await (const result of (client[clientMethod] as GeneratorClientMethod)( | ||
...args, | ||
)) { | ||
results.push(result); | ||
} | ||
return results; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './types'; | ||
export * from './getResults'; | ||
export * from './key'; | ||
export * from './vonageTest'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export const testPrivateKey = `-----BEGIN PRIVATE KEY----- | ||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDTH8cEhJKsu2hB | ||
ucgs0Blf3tvgdXuIa5sMRI3zIZGok8jqaj6DC0WdM1eiJlQCWnVL0vR25MkopMZN | ||
MTphoaVpdK9wywMVx7PsgMCDXJjW77QFJFbbcbV9KG2xvMsjJGttmVrn+9aPpCnX | ||
yT27bWkTR0k/wCAvra/st5MDKg2kB3SMc+97zlO3GqIkJJKfKYQFAO8oRBcsZ1Bx | ||
ydPWTQvyLYW15Cjv30aIGLfceFwDeUOBgBsesGFtcXvVF6sVxd5D/USunX/9es95 | ||
Rdr4+9Qq4tIKZRkBz2KWD0eo256wmmq2lQoGaR9x6XgLHhUlsi6OXILcyChi2Qcc | ||
a01Hn7YvAgMBAAECggEAJsS+lIdNsddmIS+e4Q/DoRW49aJNMXNlEN8j2+Itr7GX | ||
ougom4K94UyUyotUQOxgfrB5wL1pbQO5AGLKUDRRPii1sLYu1liKIyNPdq/RxyJU | ||
Qd927awXQiji39EF0mm1KnaPOWtG7rCcGGp1Yg4Izgf4nPLIVkkENalOHzYhNB3u | ||
4W4OIT49iw/auBF4wnl1RmXWXjkxDuk2cYT28a8hWqyQjJqXTsO+u4BaXYxSf4nP | ||
Be2yoUEFRbcxvJrhEpfODhPP83I1EBipJkhUTc5WMb/vtH2b49+TYd2tPR0LOxom | ||
mcNUWF6++ae+vL6K8Dlfcvx+CA7g7KBHHcgFCzn7GQKBgQDzc2ow5LlQQ/VfXZTz | ||
n07V/QgVQ15sA5Cf/gsvmwnGPy06Qx/WRHsz6NG8nvW2mHZwfDIHuLjBW1gcssEx | ||
mLpqav5XLZfSyjjRO/AxLIfJDx/aARp3+7Ny5aY2e3wtNx8wz4J80i7P+eX3fETM | ||
70cWhc2PvYMDjG+O7cDW2FWAFwKBgQDeAcc/FBHLl9/HqiBvYf/Y/k0t1TUoHujO | ||
PSbP6SaN06JnvJmBANyED7sWeIPuoRFXXEr4Auu7y0C55Wlsno/ImTbJsopZ1rgU | ||
k5q4t9vcu7cGiOr7L7UkySNYZqRjwvKEJ610COexTThSwl0v3GNLP8r4AMdBaqdK | ||
uO6fVfxxqQKBgFc5ne2Atai9gJe3ltum0382FoRPy+/VYyb/xZA780eVcSXz0N9b | ||
T+0sWKFFLvJKM/1fcil0FLYqfSpjHXcgqoHgbdpcWo5KqArHd+qWctwl0Bqy1IHy | ||
q7vZ7jCNE9O7cTBy2OTSBbW8apm+a4Qlowm9zQXYN624zmueYb5YamHnAoGAZvJA | ||
KHnv/o7AkF/NhpjVARR7SYOSkLY0kl48/zBIVoAK0TvdmrqBhyOcR8E+vIsn9XCw | ||
uuzvzzdjHlDJYDruxcB2bXVDPoGY/sGrf3iSlXreVkNrY2st/o7euwFtvW0K9ElJ | ||
34K5nbgHJClI+QajbKN6RSJnQ2hnhvjWfkBrPXECgYEA4MCEm9EyrguEO51am8va | ||
OjIAiQMkj/iyjsMDL8eH0VM+OMdieHwbkKyajyrB9dFikvWfxiuo3dU1N5vJTzty | ||
LmzkB8M/rKlAYKD8iKA8cRun4tKzRepHT3JPMu0GYTfcP9ovs5F3aEjX+UuWOO7n | ||
doWDENAr/VU1RNCDwFdxYFg= | ||
-----END PRIVATE KEY-----`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import nock from 'nock'; | ||
|
||
/** | ||
* A Tuple that contains the status code of the response and the nock interceptor function | ||
*/ | ||
type TestResponse = [ | ||
/** | ||
* The status code of the response | ||
*/ | ||
number, | ||
|
||
/** | ||
* The nock interceptor body response | ||
* | ||
* This can either be the body of the response or a nock interceptor function | ||
*/ | ||
nock.InterceptFunction?, | ||
]; | ||
|
||
/** | ||
* A Tuple that contains the method, path, and body of a request nock will intercept. | ||
*/ | ||
type TestRequest = [ | ||
/** | ||
* The path of the request | ||
*/ | ||
string, | ||
|
||
/** | ||
* The method of the request | ||
*/ | ||
'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH', | ||
|
||
/** | ||
* The body of the request | ||
*/ | ||
nock.RequestBodyMatcher?, | ||
]; | ||
|
||
export type SDKTestCase<T> = { | ||
/** | ||
* The label for the test case | ||
*/ | ||
label: string; | ||
|
||
/** | ||
* The base URL for the API | ||
*/ | ||
baseUrl: string; | ||
|
||
/** | ||
* The headers that are expected to be sent with the request | ||
*/ | ||
reqHeaders?: Record<string, nock.RequestHeaderMatcher>; | ||
|
||
/** | ||
* List of Nock Responses that will be used to mock the API | ||
*/ | ||
responses: TestResponse[]; | ||
|
||
/** | ||
* List of Nock Requests that will be used to mock the API | ||
*/ | ||
requests: TestRequest[]; | ||
|
||
/** | ||
* The client that will be used to make the request | ||
*/ | ||
client: T; | ||
|
||
/** | ||
* The method that will be called on the client | ||
*/ | ||
clientMethod: keyof T; | ||
|
||
/** | ||
* The parameters that will be passed to the client method | ||
*/ | ||
parameters?: [unknown, unknown, unknown]; | ||
|
||
/** | ||
* Tell the test that the client method is a generator | ||
*/ | ||
generator: boolean; | ||
|
||
/** | ||
* Tell the test that the response is going to be an error | ||
*/ | ||
error: boolean; | ||
|
||
/** | ||
* The expected response from the call to the client method | ||
*/ | ||
expected: unknown; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { SDKTestCase } from './SDKTestCase'; | ||
|
||
/** | ||
* TestTuple is a tuple that contains the name of the test suite and an array of SDKTestCase objects. | ||
*/ | ||
export type TestTuple = { | ||
/** | ||
* The name of the test suite. (Used for the describe block in the test file) | ||
*/ | ||
name: string; | ||
|
||
/** | ||
* An array of SDKTestCase objects. | ||
*/ | ||
tests: Array<SDKTestCase<unknown>>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './SDKTestCase'; | ||
export * from './TestTuple'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* eslint-disable jest/no-export */ | ||
import nock from 'nock'; | ||
import { SDKTestCase, TestTuple } from './types'; | ||
import { getResults } from './getResults'; | ||
|
||
/** | ||
* A Wrapper function to build out tests for Vonage SDKs | ||
* | ||
* @param {TestTuple} testDataSets - An array of test data sets | ||
*/ | ||
export const VonageTest = <T>(testDataSets: TestTuple[]) => { | ||
describe.each<TestTuple>(testDataSets)('$label', ({ tests }) => { | ||
test.each<SDKTestCase<T>>(tests)( | ||
'Can $label', | ||
async ({ | ||
baseUrl, | ||
reqHeaders, | ||
generator, | ||
requests, | ||
responses, | ||
client, | ||
clientMethod, | ||
parameters, | ||
expected, | ||
}) => { | ||
const scope = nock(baseUrl, { | ||
reqheaders: reqHeaders, | ||
}); | ||
|
||
requests.forEach((request, index) => { | ||
(scope as nock.Scope) | ||
.intercept(...request) | ||
.reply(...responses[index]); | ||
}); | ||
|
||
const results = await getResults<T>( | ||
generator, | ||
client, | ||
clientMethod, | ||
parameters, | ||
); | ||
|
||
expect(results).toEqual(expected); | ||
expect(nock.isDone()).toBeTruthy(); | ||
|
||
expect(results).toEqual(expected); | ||
expect(nock.isDone()).toBeTruthy(); | ||
}, | ||
); | ||
}); | ||
}; |