2020-07-02 20:46:54 -04:00
|
|
|
/**
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
2018-09-07 21:36:47 -04:00
|
|
|
import fetchMock from 'fetch-mock';
|
|
|
|
|
2022-01-07 20:16:24 -05:00
|
|
|
import callApiAndParseWithTimeout from '../../../src/connection/callApi/callApiAndParseWithTimeout';
|
2018-09-07 21:36:47 -04:00
|
|
|
|
|
|
|
// we import these via * so that we can spy on the 'default' property of the object
|
2022-01-07 20:16:24 -05:00
|
|
|
import * as callApi from '../../../src/connection/callApi/callApi';
|
|
|
|
import * as parseResponse from '../../../src/connection/callApi/parseResponse';
|
|
|
|
import * as rejectAfterTimeout from '../../../src/connection/callApi/rejectAfterTimeout';
|
2018-09-07 21:36:47 -04:00
|
|
|
|
|
|
|
import { LOGIN_GLOB } from '../fixtures/constants';
|
|
|
|
|
|
|
|
describe('callApiAndParseWithTimeout()', () => {
|
|
|
|
beforeAll(() => {
|
2021-03-01 16:46:25 -05:00
|
|
|
fetchMock.get(LOGIN_GLOB, { result: '1234' });
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(fetchMock.restore);
|
|
|
|
|
|
|
|
const mockGetUrl = '/mock/get/url';
|
|
|
|
const mockGetPayload = { get: 'payload' };
|
|
|
|
fetchMock.get(mockGetUrl, mockGetPayload);
|
|
|
|
|
2020-01-28 18:15:31 -05:00
|
|
|
afterEach(() => {
|
|
|
|
fetchMock.reset();
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2018-09-07 21:36:47 -04:00
|
|
|
|
|
|
|
describe('callApi', () => {
|
|
|
|
it('calls callApi()', () => {
|
|
|
|
const callApiSpy = jest.spyOn(callApi, 'default');
|
|
|
|
callApiAndParseWithTimeout({ url: mockGetUrl, method: 'GET' });
|
|
|
|
|
|
|
|
expect(callApiSpy).toHaveBeenCalledTimes(1);
|
|
|
|
callApiSpy.mockClear();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('parseResponse', () => {
|
2021-12-08 15:14:30 -05:00
|
|
|
it('calls parseResponse()', async () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
const parseSpy = jest.spyOn(parseResponse, 'default');
|
2021-12-08 15:14:30 -05:00
|
|
|
|
|
|
|
await callApiAndParseWithTimeout({
|
|
|
|
url: mockGetUrl,
|
|
|
|
method: 'GET',
|
|
|
|
});
|
2018-09-07 21:36:47 -04:00
|
|
|
|
|
|
|
expect(parseSpy).toHaveBeenCalledTimes(1);
|
|
|
|
parseSpy.mockClear();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('timeout', () => {
|
|
|
|
it('does not create a rejection timer if no timeout passed', () => {
|
|
|
|
const rejectionSpy = jest.spyOn(rejectAfterTimeout, 'default');
|
|
|
|
callApiAndParseWithTimeout({ url: mockGetUrl, method: 'GET' });
|
|
|
|
|
|
|
|
expect(rejectionSpy).toHaveBeenCalledTimes(0);
|
|
|
|
rejectionSpy.mockClear();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('creates a rejection timer if a timeout passed', () => {
|
|
|
|
jest.useFakeTimers(); // prevents the timeout from rejecting + failing test
|
|
|
|
const rejectionSpy = jest.spyOn(rejectAfterTimeout, 'default');
|
2021-11-09 07:42:28 -05:00
|
|
|
callApiAndParseWithTimeout({
|
|
|
|
url: mockGetUrl,
|
|
|
|
method: 'GET',
|
|
|
|
timeout: 10,
|
|
|
|
});
|
2018-09-07 21:36:47 -04:00
|
|
|
|
|
|
|
expect(rejectionSpy).toHaveBeenCalledTimes(1);
|
|
|
|
rejectionSpy.mockClear();
|
|
|
|
});
|
|
|
|
|
2020-07-02 20:46:54 -04:00
|
|
|
it('rejects if the request exceeds the timeout', async () => {
|
|
|
|
expect.assertions(2);
|
|
|
|
jest.useFakeTimers();
|
2020-02-05 15:47:10 -05:00
|
|
|
|
2020-07-02 20:46:54 -04:00
|
|
|
const mockTimeoutUrl = '/mock/timeout/url';
|
|
|
|
const unresolvingPromise = new Promise(() => {});
|
2021-01-13 01:09:58 -05:00
|
|
|
let error;
|
2020-07-02 20:46:54 -04:00
|
|
|
fetchMock.get(mockTimeoutUrl, () => unresolvingPromise);
|
2020-02-05 15:47:10 -05:00
|
|
|
|
2020-07-02 20:46:54 -04:00
|
|
|
try {
|
|
|
|
const promise = callApiAndParseWithTimeout({
|
|
|
|
url: mockTimeoutUrl,
|
|
|
|
method: 'GET',
|
|
|
|
timeout: 1,
|
|
|
|
});
|
2020-02-05 15:47:10 -05:00
|
|
|
jest.advanceTimersByTime(2);
|
2020-07-02 20:46:54 -04:00
|
|
|
await promise;
|
2021-01-13 01:09:58 -05:00
|
|
|
} catch (err) {
|
|
|
|
error = err;
|
|
|
|
} finally {
|
2020-07-02 20:46:54 -04:00
|
|
|
expect(fetchMock.calls(mockTimeoutUrl)).toHaveLength(1);
|
|
|
|
expect(error).toEqual({
|
|
|
|
error: 'Request timed out',
|
|
|
|
statusText: 'timeout',
|
2020-07-29 17:33:46 -04:00
|
|
|
timeout: 1,
|
2020-07-02 20:46:54 -04:00
|
|
|
});
|
|
|
|
}
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
|
2020-07-02 20:46:54 -04:00
|
|
|
it('resolves if the request does not exceed the timeout', async () => {
|
2018-09-07 21:36:47 -04:00
|
|
|
expect.assertions(1);
|
2020-07-02 20:46:54 -04:00
|
|
|
const { json } = await callApiAndParseWithTimeout({
|
|
|
|
url: mockGetUrl,
|
|
|
|
method: 'GET',
|
|
|
|
timeout: 100,
|
|
|
|
});
|
|
|
|
expect(json).toEqual(mockGetPayload);
|
2018-09-07 21:36:47 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|