UNPKG

react-http-fetch

Version:

An http library for React JS built on top of native JS fetch

207 lines (206 loc) 12.2 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = require("@testing-library/react"); var jest_mock_extended_1 = require("jest-mock-extended"); var __1 = require(".."); var http_in_memory_cache_store_1 = require("../cache/http-in-memory-cache-store"); var http_client_config_provider_fixture_1 = require("./fixtures/http-client-config-provider.fixture"); jest.mock('../cache/http-in-memory-cache-store'); var MockedHttpInMemoryCacheStore = http_in_memory_cache_store_1.HttpInMemoryCacheStore; var inMemoryCacheMockInstance = MockedHttpInMemoryCacheStore.mock.instances[0]; var fetch = global.fetch; describe('http-client-config-provider', function () { var fetchResponse = { name: 'Phelony', role: 'Admin', }; beforeEach(function () { jest.useFakeTimers(); jest.spyOn(global, 'setTimeout'); inMemoryCacheMockInstance.get.mockClear(); inMemoryCacheMockInstance.put.mockClear(); }); afterEach(function () { (0, react_1.cleanup)(); fetch.resetMocks(); jest.runAllTimers(); }); test('should override global req options', function () { return __awaiter(void 0, void 0, void 0, function () { var customReqOpts, result, get, res1, _a, fetchUrl, fetchParams, _b, fetchMethod, fetchHeaders, fetchCredentials, fetchBody, fetchSignal; return __generator(this, function (_c) { switch (_c.label) { case 0: fetch.mockResponseOnce(JSON.stringify(fetchResponse)); customReqOpts = { headers: { 'Content-Type': 'text/html; charset=UTF-8', }, }; result = (0, react_1.renderHook)(function () { return (0, __1.useHttpClient)(); }, { wrapper: http_client_config_provider_fixture_1.HttpClientProviderConfigFixture.create({ reqOptions: customReqOpts, }), }).result; get = result.current.get; return [4 /*yield*/, get({ relativeUrl: 'posts/1', })]; case 1: res1 = _c.sent(); expect(res1).toEqual(fetchResponse); _a = fetch.mock.calls[0], fetchUrl = _a[0], fetchParams = _a[1]; _b = fetchParams || {}, fetchMethod = _b.method, fetchHeaders = _b.headers, fetchCredentials = _b.credentials, fetchBody = _b.body, fetchSignal = _b.signal; expect(fetchUrl).toBe('/posts/1'); expect(fetchMethod).toBe(__1.HttpMethod.Get); expect(fetchHeaders).toEqual(customReqOpts.headers); expect(fetchCredentials).toBeUndefined(); expect(fetchBody).toBeNull(); expect(fetchSignal).toBeUndefined(); return [2 /*return*/]; } }); }); }); test('should override global base url', function () { return __awaiter(void 0, void 0, void 0, function () { var customBaseUrl, result, get, res1, _a, fetchUrl, fetchParams, _b, fetchMethod, fetchHeaders, fetchCredentials, fetchBody, fetchSignal; return __generator(this, function (_c) { switch (_c.label) { case 0: fetch.mockResponseOnce(JSON.stringify(fetchResponse)); customBaseUrl = 'http://phelony.com'; result = (0, react_1.renderHook)(function () { return (0, __1.useHttpClient)(); }, { wrapper: http_client_config_provider_fixture_1.HttpClientProviderConfigFixture.create({ baseUrl: 'http://phelony.com', }), }).result; get = result.current.get; return [4 /*yield*/, get({ relativeUrl: 'posts/1', })]; case 1: res1 = _c.sent(); expect(res1).toEqual(fetchResponse); _a = fetch.mock.calls[0], fetchUrl = _a[0], fetchParams = _a[1]; _b = fetchParams || {}, fetchMethod = _b.method, fetchHeaders = _b.headers, fetchCredentials = _b.credentials, fetchBody = _b.body, fetchSignal = _b.signal; expect(fetchUrl).toBe("".concat(customBaseUrl, "/posts/1")); expect(fetchMethod).toBe(__1.HttpMethod.Get); expect(fetchHeaders).toEqual(__1.defaultHttpReqConfig.reqOptions.headers); expect(fetchCredentials).toBeUndefined(); expect(fetchBody).toBeNull(); expect(fetchSignal).toBeUndefined(); return [2 /*return*/]; } }); }); }); test('should override cache store', function () { return __awaiter(void 0, void 0, void 0, function () { var customBaseUrl, httpCacheStoreMock, result, get, res1, _a, fetchUrl, fetchParams, _b, fetchMethod, fetchHeaders, fetchCredentials, fetchBody, fetchSignal; return __generator(this, function (_c) { switch (_c.label) { case 0: fetch.mockResponseOnce(JSON.stringify(fetchResponse)); customBaseUrl = 'http://phelony.com'; httpCacheStoreMock = (0, jest_mock_extended_1.mock)(); result = (0, react_1.renderHook)(function () { return (0, __1.useHttpClient)(); }, { wrapper: http_client_config_provider_fixture_1.HttpClientProviderConfigFixture.create({ baseUrl: 'http://phelony.com', cacheStore: httpCacheStoreMock, }), }).result; get = result.current.get; return [4 /*yield*/, get({ relativeUrl: 'posts/1', requestOptions: { queryParams: { orderBy: 'title' }, maxAge: 1000, }, })]; case 1: res1 = _c.sent(); expect(res1).toEqual(fetchResponse); _a = fetch.mock.calls[0], fetchUrl = _a[0], fetchParams = _a[1]; _b = fetchParams || {}, fetchMethod = _b.method, fetchHeaders = _b.headers, fetchCredentials = _b.credentials, fetchBody = _b.body, fetchSignal = _b.signal; expect(fetchUrl).toBe("".concat(customBaseUrl, "/posts/1?orderBy=title")); expect(fetchMethod).toBe(__1.HttpMethod.Get); expect(fetchHeaders).toEqual(__1.defaultHttpReqConfig.reqOptions.headers); expect(fetchCredentials).toBeUndefined(); expect(fetchBody).toBeNull(); expect(fetchSignal).toBeUndefined(); expect(httpCacheStoreMock.get).toHaveBeenCalledTimes(1); expect(httpCacheStoreMock.get).toHaveBeenCalledWith('rhf__http://phelony.com/posts/1?orderBy=title'); return [2 /*return*/]; } }); }); }); test('should override cache store prefix and separator', function () { return __awaiter(void 0, void 0, void 0, function () { var customBaseUrl, result, get, res1, _a, fetchUrl, fetchParams, _b, fetchMethod, fetchHeaders, fetchCredentials, fetchBody, fetchSignal; return __generator(this, function (_c) { switch (_c.label) { case 0: fetch.mockResponseOnce(JSON.stringify(fetchResponse)); customBaseUrl = 'http://phelony.com'; result = (0, react_1.renderHook)(function () { return (0, __1.useHttpClient)(); }, { wrapper: http_client_config_provider_fixture_1.HttpClientProviderConfigFixture.create({ baseUrl: 'http://phelony.com', cacheStorePrefix: 'phy', cacheStoreSeparator: '--', }), }).result; get = result.current.get; return [4 /*yield*/, get({ relativeUrl: 'posts/1', requestOptions: { maxAge: 1000 }, })]; case 1: res1 = _c.sent(); expect(res1).toEqual(fetchResponse); _a = fetch.mock.calls[0], fetchUrl = _a[0], fetchParams = _a[1]; _b = fetchParams || {}, fetchMethod = _b.method, fetchHeaders = _b.headers, fetchCredentials = _b.credentials, fetchBody = _b.body, fetchSignal = _b.signal; expect(fetchUrl).toBe("".concat(customBaseUrl, "/posts/1")); expect(fetchMethod).toBe(__1.HttpMethod.Get); expect(fetchHeaders).toEqual(__1.defaultHttpReqConfig.reqOptions.headers); expect(fetchCredentials).toBeUndefined(); expect(fetchBody).toBeNull(); expect(fetchSignal).toBeUndefined(); expect(inMemoryCacheMockInstance.get).toHaveBeenCalledTimes(1); expect(inMemoryCacheMockInstance.get).toHaveBeenCalledWith('phy--http://phelony.com/posts/1'); expect(inMemoryCacheMockInstance.put).toHaveBeenCalledTimes(1); return [2 /*return*/]; } }); }); }); });