Skip to content

Commit 07ac4b2

Browse files
committed
PR feedback
1 parent 075dab0 commit 07ac4b2

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

packages-exp/auth-exp/src/api/index.test.ts

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { FirebaseError } from '@firebase/util';
1919
import { expect, use } from 'chai';
2020
import * as chaiAsPromised from 'chai-as-promised';
2121
import { SinonStub, stub, useFakeTimers } from 'sinon';
22-
import { DEFAULT_API_TIMEOUT, Endpoint, HttpMethod, performApiRequest } from '.';
22+
import { DEFAULT_API_TIMEOUT_MS, Endpoint, HttpMethod, performApiRequest } from '.';
2323
import { mockEndpoint } from '../../test/api/helper';
2424
import { mockAuth } from '../../test/mock_auth';
2525
import * as mockFetch from '../../test/mock_fetch';
@@ -43,13 +43,19 @@ describe('performApiRequest', () => {
4343

4444
it('should set the correct request, method and HTTP Headers', async () => {
4545
const mock = mockEndpoint(Endpoint.SIGN_UP, serverResponse);
46-
const response = await performApiRequest<typeof request, typeof serverResponse>(mockAuth, HttpMethod.POST, Endpoint.SIGN_UP, request);
46+
const response = await performApiRequest<typeof request, typeof serverResponse>(
47+
mockAuth,
48+
HttpMethod.POST,
49+
Endpoint.SIGN_UP,
50+
request
51+
);
4752
expect(response).to.eql(serverResponse);
4853
expect(mock.calls.length).to.eq(1);
4954
expect(mock.calls[0].method).to.eq(HttpMethod.POST);
5055
expect(mock.calls[0].request).to.eql(request);
5156
expect(mock.calls[0].headers).to.eql({
52-
'Content-Type': 'application/json'
57+
'Content-Type': 'application/json',
58+
'X-Client-Version': 'testSDK/0.0.0'
5359
});
5460
});
5561

@@ -69,7 +75,12 @@ describe('performApiRequest', () => {
6975
},
7076
400
7177
);
72-
const promise = performApiRequest<typeof request, typeof serverResponse>(mockAuth, HttpMethod.POST, Endpoint.SIGN_UP, request);
78+
const promise = performApiRequest<typeof request, typeof serverResponse>(
79+
mockAuth,
80+
HttpMethod.POST,
81+
Endpoint.SIGN_UP,
82+
request
83+
);
7384
await expect(promise).to.be.rejectedWith(
7485
FirebaseError,
7586
'Firebase: The email address is already in use by another account. (auth/email-already-in-use).'
@@ -93,7 +104,12 @@ describe('performApiRequest', () => {
93104
},
94105
400
95106
);
96-
const promise = performApiRequest<typeof request, typeof serverResponse>(mockAuth, HttpMethod.POST, Endpoint.SIGN_UP, request);
107+
const promise = performApiRequest<typeof request, typeof serverResponse>(
108+
mockAuth,
109+
HttpMethod.POST,
110+
Endpoint.SIGN_UP,
111+
request
112+
);
97113
await expect(promise).to.be.rejectedWith(
98114
FirebaseError,
99115
'Firebase: An internal AuthError has occurred. (auth/internal-error).'
@@ -117,7 +133,14 @@ describe('performApiRequest', () => {
117133
},
118134
400
119135
);
120-
const promise = performApiRequest<typeof request, typeof serverResponse>(mockAuth, HttpMethod.POST, Endpoint.SIGN_UP, request, { [ServerError.EMAIL_EXISTS]: AuthErrorCode.ARGUMENT_ERROR });
136+
const promise = performApiRequest<typeof request, typeof serverResponse>(
137+
mockAuth,
138+
HttpMethod.POST,
139+
Endpoint.SIGN_UP,
140+
request,
141+
{
142+
[ServerError.EMAIL_EXISTS]: AuthErrorCode.ARGUMENT_ERROR
143+
});
121144
await expect(promise).to.be.rejectedWith(
122145
FirebaseError,
123146
'Firebase: Error (auth/argument-error).'
@@ -142,8 +165,13 @@ describe('performApiRequest', () => {
142165
fetchStub.callsFake(() => {
143166
return new Promise<never>(() => null);
144167
});
145-
const promise = performApiRequest<typeof request, never>(mockAuth, HttpMethod.POST, Endpoint.SIGN_UP, request);
146-
clock.tick(DEFAULT_API_TIMEOUT + 1);
168+
const promise = performApiRequest<typeof request, never>(
169+
mockAuth,
170+
HttpMethod.POST,
171+
Endpoint.SIGN_UP,
172+
request
173+
);
174+
clock.tick(DEFAULT_API_TIMEOUT_MS + 1);
147175
await expect(promise).to.be.rejectedWith(FirebaseError, 'Firebase: The operation has timed out. (auth/timeout).');
148176
clock.restore();
149177
});
@@ -152,7 +180,12 @@ describe('performApiRequest', () => {
152180
fetchStub.callsFake(() => {
153181
return new Promise<never>((_, reject) => reject(new Error('network error')));
154182
});
155-
const promise = performApiRequest<typeof request, never>(mockAuth, HttpMethod.POST, Endpoint.SIGN_UP, request);
183+
const promise = performApiRequest<typeof request, never>(
184+
mockAuth,
185+
HttpMethod.POST,
186+
Endpoint.SIGN_UP,
187+
request
188+
);
156189
await expect(promise).to.be.rejectedWith(FirebaseError, 'Firebase: A network AuthError (such as timeout]: interrupted connection or unreachable host) has occurred. (auth/network-request-failed).');
157190
});
158191
});

packages-exp/auth-exp/src/api/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export enum Endpoint {
5353
WITHDRAW_MFA = '/v2/accounts/mfaEnrollment:withdraw'
5454
}
5555

56-
export const DEFAULT_API_TIMEOUT = 30_000;
56+
export const DEFAULT_API_TIMEOUT_MS = 30_000;
5757

5858
export async function performApiRequest<T, V>(
5959
auth: Auth,
@@ -97,7 +97,9 @@ export async function performApiRequest<T, V>(
9797
...body
9898
}
9999
), new Promise((_, reject) =>
100-
setTimeout(() => reject(AUTH_ERROR_FACTORY.create(AuthErrorCode.TIMEOUT, { appName: auth.name })), DEFAULT_API_TIMEOUT)
100+
setTimeout(() => {
101+
return reject(AUTH_ERROR_FACTORY.create(AuthErrorCode.TIMEOUT, { appName: auth.name }));
102+
}, DEFAULT_API_TIMEOUT_MS)
101103
)]);
102104
if (response.ok) {
103105
return response.json();

0 commit comments

Comments
 (0)