Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KhudaDad414 committed Dec 1, 2023
1 parent fc2abf8 commit 89fdc7b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 91 deletions.
107 changes: 36 additions & 71 deletions test/lib/adapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
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'
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',
Expand Down Expand Up @@ -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',
Expand All @@ -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)
Expand All @@ -87,82 +88,59 @@ 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)
})
})

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)
})
})

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)
})
})

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)
})
})

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())
)
})
})

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([])
})

Expand Down Expand Up @@ -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'
)
Expand Down Expand Up @@ -276,25 +256,15 @@ 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'])
})
})

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.')
)
Expand All @@ -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.')
)
Expand Down
40 changes: 20 additions & 20 deletions test/lib/glee.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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)
})
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -152,18 +152,18 @@ describe('glee', () => {
expect(TEST_ADAPTER.prototype.connect).toHaveBeenCalledOnce()
})
})

describe('send()', () => {
it('sends a message to the appropriate server', async () => {
const msg = new GleeMessage({
payload: 'test',
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,
Expand Down

0 comments on commit 89fdc7b

Please sign in to comment.