This repository has been archived by the owner on Jun 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* change version (#19) * added check license (#21) * remove only of it * fix merge and eslint * remove yarn-error * create a BeagleHttpClient * fix import * fix review * fix type error * fix review * fix identation Co-authored-by: Rafael de Melo Silva <[email protected]>
- Loading branch information
1 parent
c79ac2d
commit a07ad1c
Showing
7 changed files
with
109 additions
and
7 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,51 @@ | ||
/* | ||
* Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA | ||
* | ||
* Licensed 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. | ||
*/ | ||
import nock from 'nock' | ||
import beagleHttpClient from "../src/BeagleHttpClient" | ||
|
||
describe.only('BeagleHttpClient', () => { | ||
const url = 'http://test.com' | ||
|
||
beforeEach(() => { | ||
nock.cleanAll() | ||
}) | ||
|
||
it('should use window.fetch as default fetch function', async () => { | ||
const path = '/example' | ||
nock(url).get(path).reply(200, { status: 'OK' }) | ||
const response = await beagleHttpClient.fetch(url + path, {}) | ||
expect(await response.json()).toEqual({ status: 'OK' }) | ||
expect(nock.isDone()).toBe(true) | ||
}) | ||
|
||
it('should use options when fetching content from server', async () => { | ||
const path = '/example'; | ||
nock(url, { reqheaders: { test: 'test' } }) | ||
.post(path, (body) => body.test).reply(200, { status: 'OK' }) | ||
const body = new URLSearchParams() | ||
body.set('test', 'test') | ||
const parametersOptions = { body, headers: { test: 'test' }, method: 'post' } | ||
await beagleHttpClient.fetch(url + path, parametersOptions) | ||
expect(nock.isDone()).toBe(true) | ||
}) | ||
|
||
it('should use custom fetch function', async () => { | ||
const fetchFunc = jest.fn() | ||
beagleHttpClient.setFetchFunction(fetchFunc) | ||
await beagleHttpClient.fetch(url, {}) | ||
expect(fetchFunc).toHaveBeenCalledWith(url, {}) | ||
}) | ||
}) |
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
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
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,29 @@ | ||
/* | ||
* Copyright 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA | ||
* | ||
* Licensed 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. | ||
*/ | ||
import { BeagleHttpClient } from './types' | ||
|
||
function createBeagleHttpClient(): BeagleHttpClient { | ||
let fetchFn: typeof fetch = fetch | ||
|
||
return { | ||
fetch: (...args) => fetchFn(...args), | ||
setFetchFunction: (newFetchFn: typeof fetch) => fetchFn = newFetchFn, | ||
} | ||
} | ||
|
||
const beagleHttpClient = createBeagleHttpClient() | ||
|
||
export default beagleHttpClient |
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
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
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