Sha256: 284231e8bf6149776066660021bb68168331d4f8c12ea2500945681a4c28ac65

Contents?: true

Size: 1.85 KB

Versions: 26

Compression:

Stored size: 1.85 KB

Contents

import { it, expect } from 'vitest'
import { kResponsePromise, RequestController } from './RequestController'

it('creates a pending response promise on construction', () => {
  const controller = new RequestController(new Request('http://localhost'))
  expect(controller[kResponsePromise]).toBeInstanceOf(Promise)
  expect(controller[kResponsePromise].state).toBe('pending')
})

it('resolves the response promise with the response provided to "respondWith"', async () => {
  const controller = new RequestController(new Request('http://localhost'))
  controller.respondWith(new Response('hello world'))

  const response = (await controller[kResponsePromise]) as Response

  expect(response).toBeInstanceOf(Response)
  expect(response.status).toBe(200)
  expect(await response.text()).toBe('hello world')
})

it('resolves the response promise with the error provided to "errorWith"', async () => {
  const controller = new RequestController(new Request('http://localhost'))
  const error = new Error('Oops!')
  controller.errorWith(error)

  await expect(controller[kResponsePromise]).resolves.toEqual(error)
})

it('throws when calling "respondWith" multiple times', () => {
  const controller = new RequestController(new Request('http://localhost'))
  controller.respondWith(new Response('hello world'))

  expect(() => {
    controller.respondWith(new Response('second response'))
  }).toThrow(
    'Failed to respond to the "GET http://localhost/" request: the "request" event has already been handled.'
  )
})

it('throws when calling "errorWith" multiple times', () => {
  const controller = new RequestController(new Request('http://localhost'))
  controller.errorWith(new Error('Oops!'))

  expect(() => {
    controller.errorWith(new Error('second error'))
  }).toThrow(
    'Failed to error the "GET http://localhost/" request: the "request" event has already been handled.'
  )
})

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.25 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.24 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.23 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.22 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.21 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.20 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.19 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.18 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.17 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.16 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.15 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.14 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.13 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.12 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.11 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.10 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.9 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.8 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts
clapton-0.0.7 lib/clapton/javascripts/node_modules/@mswjs/interceptors/src/RequestController.test.ts