Pārlūkot izejas kodu

redefine coverage, add some tests for sseSource

Olivier Massot 3 gadi atpakaļ
vecāks
revīzija
5a0d5bf7a8
3 mainītis faili ar 71 papildinājumiem un 12 dzēšanām
  1. 0 2
      jest.config.js
  2. 14 10
      services/sse/sseSource.ts
  3. 57 0
      tests/unit/services/sse/sseSource.spec.ts

+ 0 - 2
jest.config.js

@@ -20,11 +20,9 @@ module.exports = {
   },
   },
   collectCoverage: true,
   collectCoverage: true,
   collectCoverageFrom: [
   collectCoverageFrom: [
-    '<rootDir>/components/**/*.vue',
     '<rootDir>/middleware/**/*.ts',
     '<rootDir>/middleware/**/*.ts',
     '<rootDir>/services/**/*.ts',
     '<rootDir>/services/**/*.ts',
     '<rootDir>/composables/**/*.ts',
     '<rootDir>/composables/**/*.ts',
-    '<rootDir>/pages/**/*.vue'
   ],
   ],
   setupFiles: ['<rootDir>/tests/unit/index.ts']
   setupFiles: ['<rootDir>/tests/unit/index.ts']
 }
 }

+ 14 - 10
services/sse/sseSource.ts

@@ -6,7 +6,7 @@ class SseSource {
   private readonly onMessage: ((eventData: Array<any>) => void)
   private readonly onMessage: ((eventData: Array<any>) => void)
   private readonly onClose: (() => void)
   private readonly onClose: (() => void)
   private readonly withCredentials: boolean
   private readonly withCredentials: boolean
-  private eventSource: EventSource | null = null
+  protected eventSource: EventSource | null = null
 
 
   constructor(
   constructor(
     mercureHubBaseUrl: string,
     mercureHubBaseUrl: string,
@@ -24,8 +24,18 @@ class SseSource {
     this.withCredentials = withCredentials
     this.withCredentials = withCredentials
   }
   }
 
 
-  isConnected () {
-    return this.eventSource !== null && this.eventSource.readyState === EventSource.OPEN
+  protected createEventSource(url: string, withCredentials: boolean): EventSourcePolyfill {
+    return new EventSourcePolyfill(
+      url,
+      {
+        withCredentials: withCredentials,
+        heartbeatTimeout: 45 * 1000 // in ms, timeout can not be disabled yet, so I set it very large instead
+      }
+    );
+  }
+
+  public isConnected () {
+    return this.eventSource !== null && this.eventSource.readyState === EventSourcePolyfill.OPEN
   }
   }
 
 
   public subscribe () {
   public subscribe () {
@@ -36,13 +46,7 @@ class SseSource {
       throw new Error('SSE - Cannot subscribe on server side')
       throw new Error('SSE - Cannot subscribe on server side')
     }
     }
 
 
-    this.eventSource = new EventSourcePolyfill(
-      this.url.toString(),
-      {
-        withCredentials: this.withCredentials,
-        heartbeatTimeout: 45 * 1000 // in ms, timeout can not be disabled yet, so I set it very large instead
-      }
-    );
+    this.eventSource = this.createEventSource(this.url.toString(), this.withCredentials)
 
 
     this.eventSource.onerror = (event) => {
     this.eventSource.onerror = (event) => {
       console.error('SSE - An error happened')
       console.error('SSE - An error happened')

+ 57 - 0
tests/unit/services/sse/sseSource.spec.ts

@@ -0,0 +1,57 @@
+import SseSource from "~/services/sse/sseSource";
+import {EventSourcePolyfill} from "event-source-polyfill";
+
+class TestableSseSource extends SseSource {
+  public eventSource: EventSourcePolyfill | null = null
+  public createEventSource(url: string, withCredentials: boolean): EventSourcePolyfill {
+    return super.createEventSource(url, withCredentials)
+  }
+}
+
+describe('SseSource', () => {
+  describe('createEventSource', () => {
+    it('returnValidEventSourcePolyfill', () => {
+      const sseSource = new TestableSseSource(
+        'http://mercure',
+        '',
+        () => {},
+        (eventData) => {},
+        () => {}
+      )
+
+      const eventSource = sseSource.createEventSource('http://mercure', true)
+
+      expect(eventSource.url).toEqual('http://mercure')
+      expect(eventSource.withCredentials).toEqual(true)
+    })
+  })
+
+  describe('isConnected', () => {
+    it('is true when readyState is open', () =>
+    {
+      const eventSource = new EventSourcePolyfill('http://mercure')
+      Object.defineProperty(eventSource, 'readyState', {value: EventSourcePolyfill.OPEN})
+
+      const sseSource = new TestableSseSource(
+        'http://mercure',
+        '',
+        () => {},
+        (eventData) => {},
+        () => {}
+      )
+
+      sseSource.eventSource = eventSource
+      expect(sseSource.isConnected()).toBeTruthy()
+    })
+    it('is false else', () => {
+      const sseSource = new TestableSseSource(
+        'http://mercure',
+        '',
+        () => {},
+        (eventData) => {},
+        () => {}
+      )
+      expect(sseSource.isConnected()).toBeFalsy()
+    })
+  })
+})