2022-04-10 20:29:46 -05:00
|
|
|
import { expect } from 'chai'
|
2022-03-07 16:36:22 -05:00
|
|
|
import { polyfill } from '../mod.js'
|
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
describe('Fetch', () => {
|
|
|
|
const target = {}
|
2022-03-07 16:37:50 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
before(() => polyfill(target))
|
2022-03-07 16:37:50 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
it('Fetch functionality', () => {
|
|
|
|
expect(target).to.have.property('fetch').that.is.a('function')
|
|
|
|
})
|
2022-03-07 16:37:50 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
it('Fetch with https', async () => {
|
|
|
|
const { fetch } = target
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
const response = await fetch('https://api.openbrewerydb.org/breweries')
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
expect(response.constructor).to.equal(target.Response)
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
const json = await response.json()
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
expect(json).to.be.an('array')
|
|
|
|
})
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
it('Fetch with data', async () => {
|
|
|
|
const { fetch } = target
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
const jsonURI = `data:application/json,${encodeURIComponent(
|
|
|
|
JSON.stringify({
|
|
|
|
name: '@astrojs/webapi',
|
|
|
|
})
|
|
|
|
)}`
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
const response = await fetch(jsonURI)
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
const json = await response.json()
|
2022-03-07 16:36:22 -05:00
|
|
|
|
2022-04-10 20:29:46 -05:00
|
|
|
expect(json.name).to.equal('@astrojs/webapi')
|
|
|
|
})
|
2022-03-07 16:36:22 -05:00
|
|
|
})
|