Repository URL to install this package:
|
Version:
7.1.2 ▾
|
/**
* @todo running in parallel this `beforeAll` messes up
* @michael ^
*/
import 'jest'
import '../src/deps/polyfill'
import { RESTDataSource } from 'apollo-datasource-rest'
import { apolloRestDataSource } from '../src/adapters/apollo'
import { server } from '../src/deps/server'
import { OneRequest } from '../src/TwoRequest'
import { adaptRequest as adaptRequestToMock } from '../src/adapters/mock'
import { fromRequestToSerialized } from '../src/deps/fromRequestToSerialized'
import { fromResponseToSerialized } from '../src/deps/fromResponseToSerialized'
const productListParams = {
// locale: 'en_US',
// storeId: 77,
// appid: 'reactqa',
// offset: 0,
// limit: 5,
// sort: '',
selectedFacets: [{ key: 'Brand', value: ['ford creek'] }],
}
const patchCallBodyParams = {
title: 'patch call test',
}
export const registerGuestParams = {
locale: 'en_US',
storeId: 26,
appid: 'reactqa',
username: 'createorgetuser',
userinfo: {
// customParams: JSON.stringify({
// createIfNecessary: ['true'],
// useFP: ['false'],
// }),
customParams: {
createIfNecessary: ['true'],
useFP: ['false'],
},
},
// https://uxui.skavaone.com/skavastream/xact/v5/reactdemoqaskavauser/register
// ?appid=skavastore
// &locale=en_US
// &storeId=26
// https://uxui.skavaone.com/skavastream/xact/v5/reactdemoqaskavauser/register?appid=skavastore&locale=en_US&storeId=26&
// &userinfo=%7B%22customParams%22:%7B%22createIfNecessary%22:[%22true%22],%22useFP%22:[%22false%22]%7D%7D
// userinfo=%257B%2522customParams%2522%3A%257B%2522createIfNecessary%2522%3A%5B%2522true%2522%5D%2C%2522useFP%2522%3A%5B%2522false%2522%5D%257D%257D&username=createorgetuser
// &userinfo=%257B%2522customParams%2522%3A%257B%2522createIfNecessary%2522%3A%5B%2522true%2522%5D%2C%2522useFP%2522%3A%5B%2522false%2522%5D%257D%257D&username=createorgetuser
// userinfo:
// '%7B%22customParams%22:%7B%22createIfNecessary%22:[%22true%22],%22useFP%22:[%22false%22]%7D%7D',
}
const urls = {
bag: {
add: `api/addtobag`,
delete: `api/deletefrombag`,
update: 'api/updatefrombag',
view: 'api/viewbag',
},
}
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// this is where we use the generated types
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const params = {
addToBag(input: Object) {
return {
isAddToBag: true,
}
},
deleteFromBag(input: Object) {
return input
},
updateBag(input: Object) {
return input
},
viewBag(input: Object) {
return input
},
}
const transform = {
addToBag(response: Object) {
return response
},
deleteFromBag(response: Object) {
return response
},
updateBag(response: Object) {
return response
},
viewBag(response: Object) {
return response
},
}
const addToBag = OneRequest.init()
.tapRequestParams(params.addToBag)
.tapResponseData(transform.addToBag)
.url('http://localhost:3333/' + urls.bag.add)
.post()
const deleteFromBag = OneRequest.init()
.tapRequestParams(params.deleteFromBag)
.tapResponseData(transform.deleteFromBag)
.url('http://localhost:3333/' + urls.bag.delete)
.post()
const updateBag = OneRequest.init()
.tapRequestParams(params.updateBag)
.tapResponseData(transform.updateBag)
.url('http://localhost:3333/' + urls.bag.update)
.post()
const viewBag = OneRequest.init()
.tapRequestParams(params.viewBag)
.tapResponseData(transform.viewBag)
.url('http://localhost:3333/' + urls.bag.view)
.get()
const context = {
add: addToBag,
update: updateBag,
delete: deleteFromBag,
list: viewBag,
}
describe('One Requested', () => {
beforeAll(async () => {
await server.start()
})
afterAll(() => server.stop())
describe('[1Request] ', () => {
it('basic', async () => {
const request = context.update
const response = await request.doRequest()
expect(response.data.hi).toEqual('eh')
})
it('should not keep cookies without a jar', async () => {
const request = context.update.clone().header('Cookie', 69)
const response = await request.doRequest()
expect(request.store.get('headers').has('Cookie')).toEqual(false)
})
it('serialize params correctly', async () => {
const url = 'http://localhost:3333/serialize'
const request = context.update
.clone()
.url(url)
.params({ isStringied: { nested: { isDeep: true } }, two: 2 })
.header('Cookie', 69)
const expectedEncoded =
'http://localhost:3333/serialize?isStringied=%7B%22nested%22%3A%7B%22isDeep%22%3Atrue%7D%7D&two=2'
const expectedPretty = 'isStringied={"nested":{"isDeep":true}}&two=2'
const expectedDecoded = decodeURIComponent(expectedEncoded)
const actualUrl = request.toRequest().url
const actualDecoded = decodeURIComponent(actualUrl)
// expect(actualUrl).toEqual(expectedEncoded)
// expect(actualDecoded).toEqual(expectedDecoded)
expect(expectedDecoded).toContain(expectedPretty)
})
it('serializes selectedFacets correctly', async () => {
const url = 'http://localhost:3333/serialize'
const request = context.list
.clone()
.url(url)
.params(productListParams)
const actualUrl = request.toRequest().url
const expectedEncoded =
'http://localhost:3333/serialize?selectedFacets=%5B%7B%22key%22%3A%22Brand%22%2C%22value%22%3A%5B%22ford%20creek%22%5D%7D%5D'
const expectedPretty =
'selectedFacets=[{"key":"Brand","value":["ford creek"]}]'
const expectedDecoded = decodeURIComponent(expectedEncoded)
// expect(actualUrl).toEqual(expectedEncoded)
// expect(actualDecoded).toEqual(expectedDecoded)
expect(expectedDecoded).toContain(expectedPretty)
})
it('should deleteHeader', async () => {
const request = new OneRequest()
.url('http://localhost:3333/serialize')
.get()
request.header('eh', 'canada')
request.header('moose', 'igloo')
expect(request.store.get('headers').get('eh')).toEqual('canada')
expect(request.store.get('headers').get('moose')).toEqual('igloo')
expect(request.store.get('headers').has('eh')).toEqual(true)
request.deleteHeader('eh')
expect(request.store.get('headers').has('eh')).toEqual(false)
})
it('should work with headers', () => {
expect(
context.update.header('eh', 69).store.get('headers')
).toBeInstanceOf(Headers)
expect(
context.update
.headers({ eh: 'canada' })
.store.get('headers')
.get('eh')
).toEqual('canada')
expect(
context.update
.headers(new Headers({ eh: 'canada' }))
.store.get('headers')
.get('eh')
).toEqual('canada')
})
it('should work adapting `as` apollo data source', () => {
const adapter = context.update.as(apolloRestDataSource)
expect(adapter).toBeInstanceOf(RESTDataSource)
})
it('should work adapting `as` with any adapter', () => {
const adapter = context.update.as(store => -42 as any)
expect(adapter).toEqual(-42)
})
it('should work adapting `as` with mock', () => {
const adapter = context.update.toMock()
expect(adapter).toEqual(adaptRequestToMock(context.update.store))
})
it.skip('should work with Wretch', async () => {
// @todo test like this
// toWretch() {
// return adaptWretchRequest(this.store)
// }
const request = OneRequest.init()
.url('http://localhost:3333')
.toWretch()
.get()
const response = await request.json()
expect(typeof response).toBe('object')
})
it('should be able to clone', () => {
const thing1 = context.update.header('one', 1).clone()
const thing2 = context.update.header('two', 2).clone()
expect(thing1).not.toEqual(thing2)
})
it('should be able to forward requests', async () => {
const forwarded = context.update.forwardRequest({
httpRequest: new Request('https://eh', {
headers: new Headers({ cookie: 'eh' }),
}),
})
forwarded.doRequest()
expect(forwarded.store.get('headers').get('cookie')).toEqual('eh')
})
it('[update, delete, create, list] ', async () => {
const response = await context.update
.clone()
.params({ isStringied: { nested: { isDeep: true } }, two: 2 })
.header('Cookie', 69)
// .toMock()
.doRequest()
expect(response.data.hi).toEqual('eh')
})
it.skip('should clone and be 100% side-effect-free', () => {
// @todo
})
it.skip('should serialize params to url based on config', () => {
// used for stream
})
it.skip('should match request snapshot', () => {
// @todo
})
it.skip('should work for jwt', () => {
// @todo
})
})
})
describe('example requests', () => {
it('should handle a get request', async () => {
const productListRequest = new OneRequest()
.url(
'https://uxuidev.skavaone.com/skavastream/core/v5/wrskavastore/productlist/'
)
.params(productListParams)
.get()
const response = await productListRequest.path('fashionmen').doRequest()
console.log(response.req)
console.log(response.data)
expect(typeof response).toBe('object')
})
it('should handle a post request', async () => {
jest.setTimeout(20000)
/**
* @todo do not use our own api for tests, probably too slow
*/
const registerGuestRequest = new OneRequest()
// https://uxui.skavaone.com/skavastream/xact/v5/reactdemoqaskavauser/register?locale=en_US&appid=reactqa&username=createorgetuser&userinfo=%7B%22customParams%22:%7B%22createIfNecessary%22:[%22true%22],%22useFP%22:[%22false%22]%7D%7D&storeId=26
.url(
'https://uxui.skavaone.com/skavastream/xact/v5/reactdemoqaskavauser/register'
)
.post()
// .body(registerGuestParams)
.params(registerGuestParams)
// console.log('requesting...', registerGuestRequest)
const response = await registerGuestRequest.doRequest()
console.log('@@@@___', response.toDebug, fromResponseToSerialized(response))
expect(typeof response.data).toBe('object')
})
it('should handle a patch request', async () => {
const updateCartRequest = new OneRequest()
.url('https://jsonplaceholder.typicode.com/posts/1')
.patch()
.body(patchCallBodyParams)
const response = await updateCartRequest.doRequest()
console.log('@@@@___', response.toDebug, fromResponseToSerialized(response))
// console.log(JSON.stringify(response.data))
expect(typeof response.data).toBe('object')
})
})