test: verify worker properties value
[poolifier.git] / tests / worker / thread-worker.test.mjs
index 26da45e12bbe1ac63e3bfa169564faf7d59abcff..6d694ec6464c412d1d3860d246438689abb44e47 100644 (file)
@@ -1,20 +1,22 @@
 import { expect } from 'expect'
 import { restore, stub } from 'sinon'
-import { ThreadWorker } from '../../lib/index.js'
-import { DEFAULT_TASK_NAME } from '../../lib/utils.js'
 
-describe('Thread worker test suite', () => {
-  class SpyWorker extends ThreadWorker {
-    constructor (fn) {
-      super(fn)
-      this.port = { postMessage: stub().returns() }
-    }
-  }
+import { ThreadWorker } from '../../lib/index.cjs'
+import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs'
 
+describe('Thread worker test suite', () => {
   afterEach(() => {
     restore()
   })
 
+  it('Verify worker properties value after initialization', () => {
+    const worker = new ThreadWorker(() => {})
+    expect(worker.isMain).toBe(true)
+    expect(worker.mainWorker).toBe(null)
+    expect(worker.taskFunctions).toBeInstanceOf(Map)
+    expect(worker.taskFunctions.size).toBe(2)
+  })
+
   it('Verify that sync kill handler is called when worker is killed', () => {
     const worker = new ThreadWorker(() => {}, {
       killHandler: stub().returns()
@@ -40,6 +42,9 @@ describe('Thread worker test suite', () => {
       return 2
     }
     const worker = new ThreadWorker({ fn1, fn2 })
+    worker.port = {
+      postMessage: stub().returns()
+    }
     expect(worker.removeTaskFunction(0, fn1)).toStrictEqual({
       status: false,
       error: new TypeError('name parameter is not a string')
@@ -48,9 +53,6 @@ describe('Thread worker test suite', () => {
       status: false,
       error: new TypeError('name parameter is an empty string')
     })
-    worker.port = {
-      postMessage: stub().returns()
-    }
     expect(worker.taskFunctions.get(DEFAULT_TASK_NAME)).toBeInstanceOf(Function)
     expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
     expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
@@ -87,8 +89,9 @@ describe('Thread worker test suite', () => {
     expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage)
   })
 
-  it('Verify worker invokes the postMessage() method on port property', () => {
-    const worker = new SpyWorker(() => {})
+  it('Verify that sendToMainWorker() method invokes the port property postMessage() method', () => {
+    const worker = new ThreadWorker(() => {})
+    worker.port = { postMessage: stub().returns() }
     worker.sendToMainWorker({ ok: 1 })
     expect(worker.port.postMessage.calledOnce).toBe(true)
   })