Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/request / __tests__ / OneRequest.6.test.ts
Size: Mime:
import 'jest'
import '../src/deps/polyfill'
import { Request as ExpressRequest } from 'express'
import { toBoolean, EMPTY_OBJ } from 'exotic'
import { RESTDataSource } from 'apollo-datasource-rest'
import { apolloRestDataSource } from '../src/adapters/apollo'
// import { server } from '../src/deps/server'
import { OneRequest } from '../src/TwoRequest'
import { OneRequestStore } from '../src/typings'
import { adaptRequest as adaptRequestToMock } from '../src/adapters/mock'
import { config } from '../src/config'

describe('[1Request] - 5.0.0-6.0.0 updated/added functionality', () => {
  // can replace with the __mocks__ like in datasources
  // here we are testing first the values
  // beforeAll(async () => {
  //   await server.start()
  // })
  // afterAll(() => server.stop())

  beforeEach(() => {
    config.set('constantParams', {
      isDefaultConstantParams: true,
    })
    config.set('constantHeaders', {
      isDefaultConstantHeaders: true,
    })
  })

  describe('config:headers', () => {
    it('should use headers from config - AND PASS REQUEST IN IF IT IS A FUNCTION', async () => {
      // you set the constant headers in 1 place
      const constantHeaders = {
        'x-sk-session-id': (store: OneRequestStore) => {
          return store.get('headers').get('Cookie')
        },
      }
      config.set('constantHeaders', constantHeaders)

      // all the requests as usual
      const request = new OneRequest()
        .url('http://localhost:3333/serialize')
        .get()

      // this setting of the cookie is done in gql in forwardRequest
      // for every request
      request.header('Cookie', '111')

      // this is all for the tests
      const asRequest = request.toRequest()
      const headers = asRequest.headers
      const sanityConstantHeaders = config.get('constantHeaders') as any
      expect(typeof sanityConstantHeaders['x-sk-session-id']).toEqual(
        'function'
      )
      expect(sanityConstantHeaders['x-sk-session-id'](request.store)).toEqual(
        '111'
      )
      expect(headers.get('x-sk-session-id')).toEqual('111')
      expect(request.toJSON()).toMatchSnapshot()
    })

    it('should use headers from config', async () => {
      const constantHeaders = {
        isHeader: true,
      }
      config.set('constantHeaders', constantHeaders)

      const request = new OneRequest()
        .url('http://localhost:3333/serialize')
        .get()

      const asRequest = request.toRequest()
      const headers = asRequest.headers

      const sanityConstantHeaders = config.get('constantHeaders') as any
      expect(sanityConstantHeaders.isHeader).toEqual(true)

      // or could check with the serialize?
      const isHeader = toBoolean(headers.get('isHeader'))
      const falseTest = toBoolean(headers.get('falseTest'))
      expect(isHeader).toEqual(true)
      expect(falseTest).toEqual(false)

      expect(request.toJSON()).toMatchSnapshot()
    })
  })

  describe('config:params', () => {
    // can restore, but each test should be stateless
    // const previousConstantParams = config.get('constantParams')
    // config.set('constantParams', previousConstantParams)
    it('should use params from config', async () => {
      const constantParams = {
        isParam: true,
      }
      config.set('constantParams', constantParams)

      const request = new OneRequest()
        .url('http://localhost:3333/serialize')
        .get()

      const asSerialized = request.toJSON()

      // @todo optimize
      // @todo ensure this flow is correct
      const params = asSerialized.params
      const isParam = toBoolean(params.isParam)
      expect(isParam).toEqual(true)

      const falseTest = toBoolean(params.falseTest)
      expect(falseTest).toEqual(false)

      expect(request.toJSON()).toMatchSnapshot()
    })
    it('should merge params from config - including url', () => {
      const constantParams = {
        isParam: true,
      }
      config.set('constantParams', constantParams)

      const request = new OneRequest()
        .url('http://localhost:3333/serialize')
        .get()

      // const asRequest = request.toRequest()
      const asRequest = request.toJSON()

      expect(asRequest.href.includes('isParam=true')).toEqual(true)
    })

    it('should merge params from config', async () => {
      const constantParams = {
        isParam: true,
      }
      config.set('constantParams', constantParams)

      const request = new OneRequest()
        .url('http://localhost:3333/serialize')
        .get()
        .params({ presetParam: 'preset' })

      // const params = request.store.get('params') as any
      const { params } = request.toJSON()
      const isParam = toBoolean(params.isParam)
      expect(isParam).toEqual(true)

      const presetParam = params.presetParam
      expect(presetParam).toEqual('preset')

      expect(request.toJSON()).toMatchSnapshot()
    })
  })
})