-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from mbland/replace-postform-with-postformdata
Replace postForm() with postFormData()
- Loading branch information
Showing
13 changed files
with
129 additions
and
70 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
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
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,25 @@ | ||
/* eslint-env browser, node, jest, vitest */ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import { default as calculators, DEFAULT_ENDPOINT } from './calculators' | ||
import { afterEach, describe, expect, test, vi } from 'vitest' | ||
import setupFetchStub from '../test/fetch-stub' | ||
import { postOptions } from './request' | ||
|
||
describe('calculators', () => { | ||
afterEach(() => { vi.unstubAllGlobals() }) | ||
|
||
test('defaultPost requests expected backend', async () => { | ||
const data = new FormData() | ||
const fetchStub = setupFetchStub(JSON.stringify({ ok: true })) | ||
data.append('want', 'status') | ||
|
||
await expect(calculators.api.impl(data)).resolves.toEqual({ ok: true }) | ||
expect(fetchStub).toHaveBeenCalledWith( | ||
DEFAULT_ENDPOINT, postOptions({ want: 'status' })) | ||
}) | ||
}) |
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
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,30 @@ | ||
/* eslint-env browser, node */ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import {vi} from 'vitest' | ||
|
||
/** | ||
* Stubs the global fetch() with a vi.fn() configured with a Response | ||
* | ||
* Use `afterEach(() => { vi.unstubAllGlobals() })` to clean up this stub | ||
* after every test. | ||
* @see https://developer.mozilla.org/docs/Web/API/Fetch_API/Using_Fetch | ||
* @see https://developer.mozilla.org/docs/Web/API/Response/Response | ||
* @param {object} body - an object defining a body for the response | ||
* @param {object} options - optional values to include in the response | ||
* @param {object} options.status - HTTP status code of the response | ||
* @param {object} options.statusText - text accompanying the status response | ||
* @param {object} options.headers - HTTP Headers to include with the response | ||
* @returns {Function} - a vi.fn() stub configured to return a Response once | ||
*/ | ||
export default function setupFetchStub(body, options) { | ||
const fetchStub = vi.fn() | ||
|
||
fetchStub.mockReturnValueOnce(Promise.resolve(new Response(body, options))) | ||
vi.stubGlobal('fetch', fetchStub) | ||
return fetchStub | ||
} |
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