rsnext/test/integration/polyfills/pages/fetch.js
Janicklas Ralph 332852bf01 Integration test case for fetch alias (#9391)
* Polyfilling fetch and object-assign

* Polyfilling corejs object-assign

* Adding object-assign in polyfills.js. IE11 does not support Object.assign

* Fixing failing test

* Updating object.assign polyfill to fix aliasing

* Updating test case value to match new build stats

* Increasing the size of default build to 225kb

* Fixing defer-script test case to not include polyfill.js

* Revert README.md

* Re-design the polyfill approach based on PR feedback

* Adding comment and fixing test case

* Rename polyfills chunk

* Extract aliases into helper

* Remove extra new line

* Fix TypeScript typings

* Adding _internal_fetch alias

* Adjust build manifest plugin

* Build manifest plugin changes - adding a separate entry for polyfills

* Rename polyfills entry in build-manifest.json

* Remove old comment

* Fix TS

* Set key

* Polyfills already added

* Filtring polyfill.module.js

* Fix test

* Add __internal_fetch to alias rule

* Adjust name

* bump size

* ignore polyfills

* sigh

* Aliasing Object.assign

* Mergin latest changes

* Integration test for polyfilling fetch

* Remove comment

* Fix prettier error

* Fix review comments

* Fix review comments

* Update fetch.js

* Fix tests
2019-11-14 22:47:08 -08:00

47 lines
1.2 KiB
JavaScript

import { useState, useEffect } from 'react'
import unfetchImp from 'unfetch'
import isomorphicUnfetchImp from 'isomorphic-unfetch'
const testWhatwgFetchMethods = whatWgFetch => {
return (
whatWgFetch.Headers.name === 'Headers' &&
whatWgFetch.Request.name === 'Request' &&
whatWgFetch.Response.name === 'Response'
)
}
const testFetchImports = async () => {
const whatwgFetchImp = await import('whatwg-fetch')
const whatwgFetchReq = require('whatwg-fetch')
const unfetchReq = require('unfetch')
const isomorphicUnfetchReq = require('isomorphic-unfetch')
let areImportsMatching =
[whatwgFetchImp.fetch, whatwgFetchReq.fetch].every(
lib => lib.name === 'fetch'
) &&
[unfetchImp, unfetchReq, isomorphicUnfetchImp, isomorphicUnfetchReq].every(
lib => lib.name === 'bound fetch'
)
return areImportsMatching &&
testWhatwgFetchMethods(whatwgFetchReq) &&
testWhatwgFetchMethods(whatwgFetchImp)
? 'pass'
: 'fail'
}
const Page = () => {
const [testStatus, setTestStatus] = useState('computing')
useEffect(() => {
testFetchImports().then(status => {
console.log(status)
setTestStatus(status)
})
}, [])
return <div id="test-status">{testStatus}</div>
}
export default Page