Skip to content

Commit 798c2dc

Browse files
committed
convert unit tests to use spread args
1 parent 6bdef60 commit 798c2dc

File tree

1 file changed

+76
-70
lines changed

1 file changed

+76
-70
lines changed

test/unit/data-connect/data-connect-api-client-internal.spec.ts

Lines changed: 76 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-len */ // TODO: REMOVE THIS
12
/*!
23
* @license
34
* Copyright 2024 Google LLC
@@ -27,8 +28,9 @@ import * as mocks from '../../resources/mocks';
2728
import { DATA_CONNECT_ERROR_CODE_MAPPING, DataConnectApiClient, FirebaseDataConnectError }
2829
from '../../../src/data-connect/data-connect-api-client-internal';
2930
import { FirebaseApp } from '../../../src/app/firebase-app';
30-
import { ConnectorConfig, GraphqlOptions } from '../../../src/data-connect';
31+
import { ConnectorConfig } from '../../../src/data-connect';
3132
import { getMetricsHeader, getSdkVersion } from '../../../src/utils';
33+
import { RefOptions } from '../../../src/data-connect/data-connect-api';
3234

3335
describe('DataConnectApiClient', () => {
3436

@@ -236,33 +238,30 @@ describe('DataConnectApiClient', () => {
236238
});
237239

238240
describe('executeQuery', () => {
239-
const unauthenticatedOptions: GraphqlOptions<unknown> =
240-
{ operationName: 'unauthenticatedQuery', impersonate: { unauthenticated: true } };
241-
const authenticatedOptions: GraphqlOptions<unknown> =
242-
{ operationName: 'authenticatedQuery', impersonate: { authClaims: { sub: 'authenticated-UUID' } } };
241+
const unauthenticatedOptions: RefOptions = { impersonate: { unauthenticated: true } };
242+
const authenticatedOptions: RefOptions = { impersonate: { authClaims: { sub: 'authenticated-UUID' } } };
243243

244244
it('should reject when no operationName is provided', () => {
245-
apiClient.executeQuery({ impersonate: { unauthenticated: true } })
246-
.should.eventually.be.rejectedWith('`query` must be a non-empty string.');
247-
apiClient.executeQuery({ operationName: undefined, impersonate: { unauthenticated: true } })
248-
.should.eventually.be.rejectedWith('`query` must be a non-empty string.');
249-
});
250-
it('should reject when no impersonate object is provided', () => {
251-
apiClient.executeQuery({ operationName: 'queryName' })
252-
.should.eventually.be.rejectedWith('GraphqlOptions must be a non-null object');
253-
apiClient.executeQuery({ operationName: 'queryName', impersonate: undefined })
254-
.should.eventually.be.rejectedWith('GraphqlOptions must be a non-null object');
245+
apiClient.executeQuery( '', undefined, unauthenticatedOptions)
246+
.should.eventually.be.rejectedWith('`name` must be a non-empty string.');
247+
apiClient.executeQuery(undefined as unknown as string, undefined, unauthenticatedOptions)
248+
.should.eventually.be.rejectedWith('`name` must be a non-empty string.');
255249
});
250+
256251
it('should reject when project id is not available', () => {
257-
clientWithoutProjectId.executeQuery(unauthenticatedOptions)
258-
.should.eventually.be.rejectedWith(noProjectId);
252+
clientWithoutProjectId.executeQuery(
253+
'unauthenticated query',
254+
undefined,
255+
unauthenticatedOptions
256+
).should.eventually.be.rejectedWith(noProjectId);
259257
});
258+
260259
it('should reject when no connectorId is provided', () => {
261260
apiClient = new DataConnectApiClient(
262261
{ location: connectorConfig.location, serviceId: connectorConfig.serviceId },
263262
app
264263
);
265-
apiClient.executeQuery({ impersonate: { unauthenticated: true } })
264+
apiClient.executeQuery('unauthenticated query', undefined, unauthenticatedOptions)
266265
.should.eventually.be.rejectedWith(
267266
`The 'connectorConfig.connector' field used to instantiate your Data Connect
268267
instance must be a non-empty string (the connectorId) when calling executeQuery or executeMutation.`);
@@ -273,7 +272,7 @@ describe('DataConnectApiClient', () => {
273272
.stub(HttpClient.prototype, 'send')
274273
.rejects(utils.errorFrom(ERROR_RESPONSE, 404));
275274
const expected = new FirebaseDataConnectError('not-found', 'Requested entity not found');
276-
return apiClient.executeQuery(unauthenticatedOptions)
275+
return apiClient.executeQuery('unauthenticated query', undefined, unauthenticatedOptions)
277276
.should.eventually.be.rejected.and.deep.include(expected);
278277
});
279278

@@ -282,7 +281,7 @@ describe('DataConnectApiClient', () => {
282281
.stub(HttpClient.prototype, 'send')
283282
.rejects(utils.errorFrom({}, 404));
284283
const expected = new FirebaseDataConnectError('unknown-error', 'Unknown server error: {}');
285-
return apiClient.executeQuery(unauthenticatedOptions)
284+
return apiClient.executeQuery('unauthenticated query', undefined, unauthenticatedOptions)
286285
.should.eventually.be.rejected.and.deep.include(expected);
287286
});
288287

@@ -292,7 +291,7 @@ describe('DataConnectApiClient', () => {
292291
.rejects(utils.errorFrom('not json', 404));
293292
const expected = new FirebaseDataConnectError(
294293
'unknown-error', 'Unexpected response with status: 404 and body: not json');
295-
return apiClient.executeQuery(unauthenticatedOptions)
294+
return apiClient.executeQuery('unauthenticated query', undefined, unauthenticatedOptions)
296295
.should.eventually.be.rejected.and.deep.include(expected);
297296
});
298297

@@ -301,7 +300,7 @@ describe('DataConnectApiClient', () => {
301300
sandbox
302301
.stub(HttpClient.prototype, 'send')
303302
.rejects(expected);
304-
return apiClient.executeQuery(unauthenticatedOptions)
303+
return apiClient.executeQuery('unauthenticated query', undefined, unauthenticatedOptions)
305304
.should.eventually.be.rejected.and.deep.include(expected);
306305
});
307306

@@ -319,7 +318,11 @@ describe('DataConnectApiClient', () => {
319318
const stub = sandbox
320319
.stub(HttpClient.prototype, 'send')
321320
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
322-
return apiClient.executeQuery<UsersResponse, unknown>(unauthenticatedOptions).then((resp) => {
321+
return apiClient.executeQuery<UsersResponse, undefined>(
322+
'unauthenticated query',
323+
undefined,
324+
unauthenticatedOptions
325+
).then((resp) => {
323326
expect(resp.data.users).to.be.not.empty;
324327
expect(resp.data.users[0].name).to.be.not.undefined;
325328
expect(resp.data.users[0].address).to.be.not.undefined;
@@ -332,8 +335,8 @@ describe('DataConnectApiClient', () => {
332335
url: `https://autopush-firebasedataconnect.sandbox.googleapis.com/v1/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateQuery`,
333336
headers: EXPECTED_HEADERS,
334337
data: {
335-
operationName: unauthenticatedOptions.operationName,
336-
extensions: { impersonate: unauthenticatedOptions.impersonate }
338+
operationName: 'unauthenticated query',
339+
extensions: unauthenticatedOptions
337340
}
338341
});
339342
});
@@ -342,7 +345,11 @@ describe('DataConnectApiClient', () => {
342345
const stub = sandbox
343346
.stub(HttpClient.prototype, 'send')
344347
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
345-
return apiClient.executeQuery<UsersResponse, unknown>(authenticatedOptions).then((resp) => {
348+
return apiClient.executeQuery<UsersResponse, undefined>(
349+
'authenticated query',
350+
undefined,
351+
authenticatedOptions
352+
).then((resp) => {
346353
expect(resp.data.users).to.be.not.empty;
347354
expect(resp.data.users[0].name).to.be.not.undefined;
348355
expect(resp.data.users[0].address).to.be.not.undefined;
@@ -355,7 +362,7 @@ describe('DataConnectApiClient', () => {
355362
url: `https://autopush-firebasedataconnect.sandbox.googleapis.com/v1/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateQuery`,
356363
headers: EXPECTED_HEADERS,
357364
data: {
358-
operationName: authenticatedOptions.operationName,
365+
operationName: 'authenticated query',
359366
extensions: { impersonate: authenticatedOptions.impersonate }
360367
}
361368
});
@@ -368,49 +375,48 @@ describe('DataConnectApiClient', () => {
368375
const stub = sandbox
369376
.stub(HttpClient.prototype, 'send')
370377
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
371-
return apiClient.executeQuery(unauthenticatedOptions)
372-
.then(() => {
373-
expect(stub).to.have.been.calledOnce.and.calledWith({
374-
method: 'POST',
375-
url: `http://localhost:9399/v1/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateQuery`,
376-
headers: EMULATOR_EXPECTED_HEADERS,
377-
data: {
378-
operationName: unauthenticatedOptions.operationName,
379-
extensions: { impersonate: unauthenticatedOptions.impersonate }
380-
}
381-
});
378+
return apiClient.executeQuery(
379+
'unauthenticated query',
380+
undefined,
381+
unauthenticatedOptions
382+
).then(() => {
383+
expect(stub).to.have.been.calledOnce.and.calledWith({
384+
method: 'POST',
385+
url: `http://localhost:9399/v1/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateQuery`,
386+
headers: EMULATOR_EXPECTED_HEADERS,
387+
data: {
388+
operationName: 'unauthenticated query',
389+
extensions: unauthenticatedOptions
390+
}
382391
});
392+
});
383393
});
384394
});
385395

386-
const unauthenticatedOptions: GraphqlOptions<unknown> =
387-
{ operationName: 'operationName', impersonate: { unauthenticated: true } };
388-
const authenticatedOptions: GraphqlOptions<unknown> =
389-
{ operationName: 'operationName', impersonate: { unauthenticated: true } };
396+
const unauthenticatedOptions: RefOptions =
397+
{ impersonate: { unauthenticated: true } };
398+
const authenticatedOptions: RefOptions =
399+
{ impersonate: { authClaims: { sub: 'authenticated-UUID' } } };
390400

391401
describe('executeMutation', () => {
392402
it('should reject when no operationName is provided', () => {
393-
apiClient.executeMutation({ impersonate: { unauthenticated: true } })
394-
.should.eventually.be.rejectedWith('`query` must be a non-empty string.');
395-
apiClient.executeMutation({ operationName: undefined, impersonate: { unauthenticated: true } })
396-
.should.eventually.be.rejectedWith('`query` must be a non-empty string.');
397-
});
398-
it('should reject when no impersonate object is provided', () => {
399-
apiClient.executeMutation({ operationName: 'queryName' })
400-
.should.eventually.be.rejectedWith('GraphqlOptions must be a non-null object');
401-
apiClient.executeMutation({ operationName: 'queryName', impersonate: undefined })
402-
.should.eventually.be.rejectedWith('GraphqlOptions must be a non-null object');
403+
apiClient.executeMutation('', undefined, unauthenticatedOptions)
404+
.should.eventually.be.rejectedWith('`name` must be a non-empty string.');
405+
apiClient.executeMutation(undefined as unknown as string, undefined, unauthenticatedOptions)
406+
.should.eventually.be.rejectedWith('`name` must be a non-empty string.');
403407
});
408+
404409
it('should reject when project id is not available', () => {
405-
clientWithoutProjectId.executeMutation(unauthenticatedOptions)
410+
clientWithoutProjectId.executeMutation('unauthenticated mutation', undefined, unauthenticatedOptions)
406411
.should.eventually.be.rejectedWith(noProjectId);
407412
});
413+
408414
it('should reject when no connectorId is provided', () => {
409415
apiClient = new DataConnectApiClient(
410416
{ location: connectorConfig.location, serviceId: connectorConfig.serviceId },
411417
app
412418
);
413-
apiClient.executeMutation({ impersonate: { unauthenticated: true } })
419+
apiClient.executeMutation('unauthenticated mutation', undefined, unauthenticatedOptions)
414420
.should.eventually.be.rejectedWith(
415421
`The 'connectorConfig.connector' field used to instantiate your Data Connect
416422
instance must be a non-empty string (the connectorId) when calling executeQuery or executeMutation.`);
@@ -421,7 +427,7 @@ describe('DataConnectApiClient', () => {
421427
.stub(HttpClient.prototype, 'send')
422428
.rejects(utils.errorFrom(ERROR_RESPONSE, 404));
423429
const expected = new FirebaseDataConnectError('not-found', 'Requested entity not found');
424-
return apiClient.executeMutation(unauthenticatedOptions)
430+
return apiClient.executeMutation('unauthenticated mutation', undefined, unauthenticatedOptions)
425431
.should.eventually.be.rejected.and.deep.include(expected);
426432
});
427433

@@ -430,7 +436,7 @@ describe('DataConnectApiClient', () => {
430436
.stub(HttpClient.prototype, 'send')
431437
.rejects(utils.errorFrom({}, 404));
432438
const expected = new FirebaseDataConnectError('unknown-error', 'Unknown server error: {}');
433-
return apiClient.executeMutation(unauthenticatedOptions)
439+
return apiClient.executeMutation('unauthenticated mutation', undefined, unauthenticatedOptions)
434440
.should.eventually.be.rejected.and.deep.include(expected);
435441
});
436442

@@ -440,7 +446,7 @@ describe('DataConnectApiClient', () => {
440446
.rejects(utils.errorFrom('not json', 404));
441447
const expected = new FirebaseDataConnectError(
442448
'unknown-error', 'Unexpected response with status: 404 and body: not json');
443-
return apiClient.executeMutation(unauthenticatedOptions)
449+
return apiClient.executeMutation('unauthenticated mutation', undefined, unauthenticatedOptions)
444450
.should.eventually.be.rejected.and.deep.include(expected);
445451
});
446452

@@ -449,7 +455,7 @@ describe('DataConnectApiClient', () => {
449455
sandbox
450456
.stub(HttpClient.prototype, 'send')
451457
.rejects(expected);
452-
return apiClient.executeMutation(unauthenticatedOptions)
458+
return apiClient.executeMutation('unauthenticated mutation', undefined, unauthenticatedOptions)
453459
.should.eventually.be.rejected.and.deep.include(expected);
454460
});
455461

@@ -467,7 +473,7 @@ describe('DataConnectApiClient', () => {
467473
const stub = sandbox
468474
.stub(HttpClient.prototype, 'send')
469475
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
470-
return apiClient.executeMutation<UsersResponse, unknown>(unauthenticatedOptions)
476+
return apiClient.executeMutation<UsersResponse, undefined>('unauthenticated mutation', undefined, unauthenticatedOptions)
471477
.then((resp) => {
472478
expect(resp.data.users).to.be.not.empty;
473479
expect(resp.data.users[0].name).to.be.not.undefined;
@@ -482,8 +488,8 @@ describe('DataConnectApiClient', () => {
482488
url: `https://autopush-firebasedataconnect.sandbox.googleapis.com/v1/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateMutation`,
483489
headers: EXPECTED_HEADERS,
484490
data: {
485-
operationName: unauthenticatedOptions.operationName,
486-
extensions: { impersonate: unauthenticatedOptions.impersonate }
491+
operationName: 'unauthenticated mutation',
492+
extensions: unauthenticatedOptions
487493
}
488494
});
489495
});
@@ -492,7 +498,7 @@ describe('DataConnectApiClient', () => {
492498
const stub = sandbox
493499
.stub(HttpClient.prototype, 'send')
494500
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
495-
return apiClient.executeMutation<UsersResponse, unknown>(authenticatedOptions)
501+
return apiClient.executeMutation<UsersResponse, undefined>('authenticated mutation', undefined, authenticatedOptions)
496502
.then((resp) => {
497503
expect(resp.data.users).to.be.not.empty;
498504
expect(resp.data.users[0].name).to.be.not.undefined;
@@ -507,8 +513,8 @@ describe('DataConnectApiClient', () => {
507513
url: `https://autopush-firebasedataconnect.sandbox.googleapis.com/v1/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateMutation`,
508514
headers: EXPECTED_HEADERS,
509515
data: {
510-
operationName: authenticatedOptions.operationName,
511-
extensions: { impersonate: authenticatedOptions.impersonate }
516+
operationName: 'authenticated mutation',
517+
extensions: authenticatedOptions
512518
}
513519
});
514520
});
@@ -528,7 +534,7 @@ describe('DataConnectApiClient', () => {
528534
const stub = sandbox
529535
.stub(HttpClient.prototype, 'send')
530536
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
531-
return apiClient.executeMutation<UsersResponse, unknown>(unauthenticatedOptions)
537+
return apiClient.executeMutation<UsersResponse, undefined>('authenticated mutation', undefined, authenticatedOptions)
532538
.then((resp) => {
533539
expect(resp.data.users).to.be.not.empty;
534540
expect(resp.data.users[0].name).to.be.not.undefined;
@@ -542,8 +548,8 @@ describe('DataConnectApiClient', () => {
542548
url: `https://autopush-firebasedataconnect.sandbox.googleapis.com/v1/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateMutation`,
543549
headers: EXPECTED_HEADERS,
544550
data: {
545-
operationName: unauthenticatedOptions.operationName,
546-
extensions: { impersonate: unauthenticatedOptions.impersonate }
551+
operationName: 'authenticated mutation',
552+
extensions: authenticatedOptions
547553
}
548554
});
549555
});
@@ -554,15 +560,15 @@ describe('DataConnectApiClient', () => {
554560
const stub = sandbox
555561
.stub(HttpClient.prototype, 'send')
556562
.resolves(utils.responseFrom(TEST_RESPONSE, 200));
557-
return apiClient.executeMutation(unauthenticatedOptions)
563+
return apiClient.executeMutation('unauthenticated mutation', undefined, unauthenticatedOptions)
558564
.then(() => {
559565
expect(stub).to.have.been.calledOnce.and.calledWith({
560566
method: 'POST',
561567
url: `http://localhost:9399/v1/projects/test-project/locations/${connectorConfig.location}/services/${connectorConfig.serviceId}/connectors/${connectorConfig.connector}:impersonateMutation`,
562568
headers: EMULATOR_EXPECTED_HEADERS,
563569
data: {
564-
operationName: unauthenticatedOptions.operationName,
565-
extensions: { impersonate: unauthenticatedOptions.impersonate }
570+
operationName: 'unauthenticated mutation',
571+
extensions: unauthenticatedOptions
566572
}
567573
});
568574
});

0 commit comments

Comments
 (0)