sseSource.spec.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import SseSource from "~/services/sse/sseSource";
  2. import {EventSourcePolyfill} from "event-source-polyfill";
  3. class TestableSseSource extends SseSource {
  4. public eventSource: EventSourcePolyfill | null = null
  5. public createEventSource(url: string, withCredentials: boolean): EventSourcePolyfill {
  6. return super.createEventSource(url, withCredentials)
  7. }
  8. }
  9. describe('SseSource', () => {
  10. describe('createEventSource', () => {
  11. it('returnValidEventSourcePolyfill', () => {
  12. const sseSource = new TestableSseSource(
  13. 'http://mercure',
  14. '',
  15. () => {},
  16. (eventData) => {},
  17. () => {}
  18. )
  19. const eventSource = sseSource.createEventSource('http://mercure', true)
  20. expect(eventSource.url).toEqual('http://mercure')
  21. expect(eventSource.withCredentials).toEqual(true)
  22. })
  23. })
  24. describe('isConnected', () => {
  25. it('is true when readyState is open', () =>
  26. {
  27. const eventSource = new EventSourcePolyfill('http://mercure')
  28. Object.defineProperty(eventSource, 'readyState', {value: EventSourcePolyfill.OPEN})
  29. const sseSource = new TestableSseSource(
  30. 'http://mercure',
  31. '',
  32. () => {},
  33. (eventData) => {},
  34. () => {}
  35. )
  36. sseSource.eventSource = eventSource
  37. expect(sseSource.isConnected()).toBeTruthy()
  38. })
  39. it('is false else', () => {
  40. const sseSource = new TestableSseSource(
  41. 'http://mercure',
  42. '',
  43. () => {},
  44. (eventData) => {},
  45. () => {}
  46. )
  47. expect(sseSource.isConnected()).toBeFalsy()
  48. })
  49. })
  50. })