diff --git a/test/lib/adapter.test.ts b/test/lib/adapter.test.ts index 5d46f3c98..531b07b0d 100644 --- a/test/lib/adapter.test.ts +++ b/test/lib/adapter.test.ts @@ -1,9 +1,9 @@ import 'jest-extended' -import {AsyncAPIDocumentV2 as AsyncAPIDocument, ServerInterface as Server} from '@asyncapi/parser' +import { AsyncAPIDocumentV2 as AsyncAPIDocument, ServerInterface as Server } from '@asyncapi/parser' import GleeConnection from '../../src/lib/connection.js' import Glee from '../../src/lib/glee.js' import GleeMessage from '../../src/lib/message.js' -import GleeAdapter from '../../src/lib/adapter.js' +import GleeAdapter, { GleeAdapterOptions } from '../../src/lib/adapter.js' import { jest } from '@jest/globals' const TEST_SERVER_NAME = 'test' @@ -11,7 +11,7 @@ const ANOTHER_TEST_SERVER_NAME = 'another' const TEST_CHANNEL = 'test/channel' const TEST_ASYNCAPI_DOCUMENT = new AsyncAPIDocument({ asyncapi: '2.2.0', - info: {title: '', version: ''}, + info: { title: '', version: '' }, servers: { test: { url: 'mqtt://fake-url', @@ -54,7 +54,7 @@ class TEST_ADAPTER extends GleeAdapter { }) } } -class ANOTHER_TEST_ADAPTER extends GleeAdapter {} +class ANOTHER_TEST_ADAPTER extends GleeAdapter { } const fakeConnection = new GleeConnection({ connection: 'fake-connection', @@ -66,12 +66,13 @@ const fakeConnection = new GleeConnection({ const initializeAdapter = () => { const app = new Glee() - const adapter = new GleeAdapter( - app, - TEST_SERVER_NAME, - TEST_SERVER, - TEST_ASYNCAPI_DOCUMENT - ) + const adapterOptions: GleeAdapterOptions = { + glee: app, + serverName: TEST_SERVER_NAME, + server: TEST_SERVER!!, + parsedAsyncAPI: TEST_ASYNCAPI_DOCUMENT + } + const adapter = new GleeAdapter(adapterOptions) app.on('adapter:server:connection:open', (ev) => { expect(ev.serverName).toStrictEqual(TEST_SERVER_NAME) expect(ev.server).toStrictEqual(TEST_SERVER) @@ -87,15 +88,17 @@ const initializeAdapter = () => { } describe('adapter', () => { + + const adapterOptions = { + serverName: TEST_SERVER_NAME, + server: TEST_SERVER!!, + parsedAsyncAPI: TEST_ASYNCAPI_DOCUMENT + } + describe('glee', () => { it('returns the glee app passed on constructor', async () => { const app = new Glee() - const adapter = new GleeAdapter( - app, - TEST_SERVER_NAME, - TEST_SERVER, - TEST_ASYNCAPI_DOCUMENT - ) + const adapter = new GleeAdapter({ ...adapterOptions, glee: app }) expect(adapter.glee).toStrictEqual(app) }) }) @@ -103,12 +106,7 @@ describe('adapter', () => { describe('serverName', () => { it('returns the server name passed on constructor', async () => { const app = new Glee() - const adapter = new GleeAdapter( - app, - TEST_SERVER_NAME, - TEST_SERVER, - TEST_ASYNCAPI_DOCUMENT - ) + const adapter = new GleeAdapter({ ...adapterOptions, glee: app }) expect(adapter.serverName).toStrictEqual(TEST_SERVER_NAME) }) }) @@ -116,12 +114,7 @@ describe('adapter', () => { describe('AsyncAPIServer', () => { it('returns the AsyncAPI server object passed on constructor', async () => { const app = new Glee() - const adapter = new GleeAdapter( - app, - TEST_SERVER_NAME, - TEST_SERVER, - TEST_ASYNCAPI_DOCUMENT - ) + const adapter = new GleeAdapter({ ...adapterOptions, glee: app }) expect(adapter.AsyncAPIServer).toStrictEqual(TEST_SERVER) }) }) @@ -129,12 +122,7 @@ describe('adapter', () => { describe('parsedAsyncAPI', () => { it('returns the AsyncAPI document object passed on constructor', async () => { const app = new Glee() - const adapter = new GleeAdapter( - app, - TEST_SERVER_NAME, - TEST_SERVER, - TEST_ASYNCAPI_DOCUMENT - ) + const adapter = new GleeAdapter({ ...adapterOptions, glee: app }) expect(adapter.parsedAsyncAPI).toStrictEqual(TEST_ASYNCAPI_DOCUMENT) }) }) @@ -142,14 +130,9 @@ describe('adapter', () => { describe('channelNames', () => { it('returns the list of associated channel names', async () => { const app = new Glee() - const adapter = new GleeAdapter( - app, - TEST_SERVER_NAME, - TEST_SERVER, - TEST_ASYNCAPI_DOCUMENT - ) + const adapter = new GleeAdapter({ ...adapterOptions, glee: app }) expect(adapter.channelNames).toStrictEqual( - TEST_ASYNCAPI_DOCUMENT.channels().all().map(e =>e.address()) + TEST_ASYNCAPI_DOCUMENT.channels().all().map(e => e.address()) ) }) }) @@ -157,12 +140,7 @@ describe('adapter', () => { describe('connections', () => { it('returns an empty array when the adapter is just initialized', async () => { const app = new Glee() - const adapter = new GleeAdapter( - app, - TEST_SERVER_NAME, - TEST_SERVER, - TEST_ASYNCAPI_DOCUMENT - ) + const adapter = new GleeAdapter({ ...adapterOptions, glee: app }) expect(adapter.connections).toStrictEqual([]) }) @@ -201,12 +179,14 @@ describe('adapter', () => { it('returns the server URL with variables expanded', async () => { const app = new Glee() - const adapter = new GleeAdapter( - app, - ANOTHER_TEST_SERVER_NAME, - ANOTHER_TEST_SERVER, - TEST_ASYNCAPI_DOCUMENT - ) + const anotherAdapterOptions: GleeAdapterOptions = { + glee: app, + serverName: ANOTHER_TEST_SERVER_NAME, + server: ANOTHER_TEST_SERVER!!, + parsedAsyncAPI: TEST_ASYNCAPI_DOCUMENT + + } + const adapter = new GleeAdapter(anotherAdapterOptions) expect(adapter.serverUrlExpanded).toStrictEqual( 'ws://fake-url-with-vars:8000' ) @@ -276,12 +256,7 @@ describe('adapter', () => { describe('getSubscribedChannels()', () => { it('returns the list of channels to which the adapter is subscribed', async () => { const app = new Glee() - const adapter = new GleeAdapter( - app, - TEST_SERVER_NAME, - TEST_SERVER, - TEST_ASYNCAPI_DOCUMENT - ) + const adapter = new GleeAdapter({ ...adapterOptions, glee: app }) expect(adapter.getSubscribedChannels()).toStrictEqual(['test/channel']) }) }) @@ -289,12 +264,7 @@ describe('adapter', () => { describe('connect()', () => { it('throws', async () => { const app = new Glee() - const adapter = new GleeAdapter( - app, - TEST_SERVER_NAME, - TEST_SERVER, - TEST_ASYNCAPI_DOCUMENT - ) + const adapter = new GleeAdapter({ ...adapterOptions, glee: app }) await expect(adapter.connect()).rejects.toThrowError( new Error('Method `connect` is not implemented.') ) @@ -307,12 +277,7 @@ describe('adapter', () => { payload: 'test', }) const app = new Glee() - const adapter = new GleeAdapter( - app, - TEST_SERVER_NAME, - TEST_SERVER, - TEST_ASYNCAPI_DOCUMENT - ) + const adapter = new GleeAdapter({ ...adapterOptions, glee: app }) await expect(adapter.send(msg)).rejects.toThrowError( new Error('Method `send` is not implemented.') ) diff --git a/test/lib/glee.test.ts b/test/lib/glee.test.ts index ad1915529..6ca7c3ec1 100644 --- a/test/lib/glee.test.ts +++ b/test/lib/glee.test.ts @@ -1,6 +1,6 @@ import 'jest-extended' -import {AsyncAPIDocumentV2 as AsyncAPIDocument, ServerInterface as Server} from '@asyncapi/parser' -import {jest} from '@jest/globals' +import { AsyncAPIDocumentV2 as AsyncAPIDocument, ServerInterface as Server } from '@asyncapi/parser' +import { jest } from '@jest/globals' import GleeConnection from '../../src/lib/connection.js' import Glee from '../../src/lib/glee.js' import GleeMessage from '../../src/lib/message.js' @@ -11,7 +11,7 @@ const ANOTHER_TEST_SERVER_NAME = 'another' const TEST_CHANNEL = 'test/channel' const TEST_ASYNCAPI_DOCUMENT = new AsyncAPIDocument({ asyncapi: '2.2.0', - info: {title: '', version: ''}, + info: { title: '', version: '' }, servers: { test: { url: 'mqtt://fake-url', @@ -36,8 +36,8 @@ const TEST_ASYNCAPI_DOCUMENT = new AsyncAPIDocument({ }) const TEST_SERVER: Server | undefined = TEST_ASYNCAPI_DOCUMENT.servers().get(TEST_SERVER_NAME) const ANOTHER_TEST_SERVER: Server | undefined = TEST_ASYNCAPI_DOCUMENT.servers().get(ANOTHER_TEST_SERVER_NAME) -class TEST_ADAPTER extends GleeAdapter {} -class ANOTHER_TEST_ADAPTER extends GleeAdapter {} +class TEST_ADAPTER extends GleeAdapter { } +class ANOTHER_TEST_ADAPTER extends GleeAdapter { } const fakeConnection = new GleeConnection({ connection: 'anything', @@ -50,7 +50,7 @@ const fakeConnection = new GleeConnection({ describe('glee', () => { describe('options', () => { it('returns options passed on constructor', async () => { - const fakeOptions = { ws: { server: {port: 7000} } } + const fakeOptions = { ws: { server: {} } } const app = new Glee(fakeOptions) expect(app.options).toStrictEqual(fakeOptions) }) @@ -62,7 +62,7 @@ describe('glee', () => { payload: 'test' }) const middlewareFn = jest.fn() - const middlewareFn2:any = jest.fn() + const middlewareFn2: any = jest.fn() const outboundMiddlewareFn = jest.fn() const app = new Glee() app.use(middlewareFn) @@ -75,11 +75,11 @@ describe('glee', () => { expect(outboundMiddlewareFn).not.toHaveBeenCalledOnce() expect(middlewareFn).toHaveBeenCalledBefore(middlewareFn2) }) - + it('registers inbound error middlewares in order', async () => { const middlewareFn = jest.fn() const errorMiddlewareFn = jest.fn(async (err, message, next) => next) - const errorMiddlewareFn2:any = jest.fn(async (err, message, next) => next) + const errorMiddlewareFn2: any = jest.fn(async (err, message, next) => next) const outboundMiddlewareFn = jest.fn(async (err, message, next) => next) const app = new Glee() app.use(middlewareFn) @@ -102,7 +102,7 @@ describe('glee', () => { payload: 'test' }) const middlewareFn = jest.fn() - const middlewareFn2:any = jest.fn() + const middlewareFn2: any = jest.fn() const inboundMiddlewareFn = jest.fn() const app = new Glee() app.use(inboundMiddlewareFn) @@ -120,10 +120,10 @@ describe('glee', () => { const msg = new GleeMessage({ payload: 'test' }) - const middlewareFn = jest.fn((message, next:any) => next(new Error('fake-error'))) - const errorMiddlewareFn = jest.fn(async (err, message, next:any) => next(err)) - const errorMiddlewareFn2: any = jest.fn(async (err, message, next:any) => next(err)) - const inboundMiddlewareFn = jest.fn(async (err, message, next:any) => next(err)) + const middlewareFn = jest.fn((message, next: any) => next(new Error('fake-error'))) + const errorMiddlewareFn = jest.fn(async (err, message, next: any) => next(err)) + const errorMiddlewareFn2: any = jest.fn(async (err, message, next: any) => next(err)) + const inboundMiddlewareFn = jest.fn(async (err, message, next: any) => next(err)) const app = new Glee() app.useOutbound(middlewareFn) app.useOutbound(errorMiddlewareFn) @@ -138,7 +138,7 @@ describe('glee', () => { expect(errorMiddlewareFn).toHaveBeenCalledBefore(errorMiddlewareFn2) }) }) - + describe('connect()', () => { it('tells all adapters to connect', async () => { TEST_ADAPTER.prototype.connect = jest.fn() @@ -152,7 +152,7 @@ describe('glee', () => { expect(TEST_ADAPTER.prototype.connect).toHaveBeenCalledOnce() }) }) - + describe('send()', () => { it('sends a message to the appropriate server', async () => { const msg = new GleeMessage({ @@ -160,10 +160,10 @@ describe('glee', () => { channel: TEST_CHANNEL, serverName: TEST_SERVER_NAME, }) - TEST_ADAPTER.prototype.connect = jest.fn(async () => {}) - TEST_ADAPTER.prototype.send = jest.fn(async () => {}) - ANOTHER_TEST_ADAPTER.prototype.connect = jest.fn(async () => {}) - ANOTHER_TEST_ADAPTER.prototype.send = jest.fn(async () => {}) + TEST_ADAPTER.prototype.connect = jest.fn(async () => { }) + TEST_ADAPTER.prototype.send = jest.fn(async () => { }) + ANOTHER_TEST_ADAPTER.prototype.connect = jest.fn(async () => { }) + ANOTHER_TEST_ADAPTER.prototype.send = jest.fn(async () => { }) const app = new Glee() app.addAdapter(TEST_ADAPTER, { serverName: TEST_SERVER_NAME,