mirror of
https://github.com/Cameri/nostream.git
synced 2025-04-08 11:58:03 +02:00
test: add parameterized replaceable evts unit test
This commit is contained in:
parent
8cf3069a8b
commit
7d5188c366
@ -0,0 +1,101 @@
|
||||
import chai from 'chai'
|
||||
import chaiAsPromised from 'chai-as-promised'
|
||||
import Sinon from 'sinon'
|
||||
|
||||
chai.use(chaiAsPromised)
|
||||
|
||||
import { EventDeduplicationMetadataKey, EventTags } from '../../../../src/constants/base'
|
||||
import { DatabaseClient } from '../../../../src/@types/base'
|
||||
import { Event } from '../../../../src/@types/event'
|
||||
import { EventRepository } from '../../../../src/repositories/event-repository'
|
||||
import { IEventRepository } from '../../../../src/@types/repositories'
|
||||
import { IEventStrategy } from '../../../../src/@types/message-handlers'
|
||||
import { IWebSocketAdapter } from '../../../../src/@types/adapters'
|
||||
import { ParameterizedReplaceableEventStrategy } from '../../../../src/handlers/event-strategies/parameterized-replaceable-event-strategy'
|
||||
import { WebSocketAdapterEvent } from '../../../../src/constants/adapter'
|
||||
|
||||
const { expect } = chai
|
||||
|
||||
describe('ParameterizedReplaceableEventStrategy', () => {
|
||||
const event: Event = {
|
||||
tags: [
|
||||
[EventTags.Deduplication, 'dedup'],
|
||||
],
|
||||
} as any
|
||||
let webSocket: IWebSocketAdapter
|
||||
let eventRepository: IEventRepository
|
||||
|
||||
let webSocketEmitStub: Sinon.SinonStub
|
||||
let eventRepositoryUpsertStub: Sinon.SinonStub
|
||||
|
||||
let strategy: IEventStrategy<Event, Promise<void>>
|
||||
|
||||
let sandbox: Sinon.SinonSandbox
|
||||
|
||||
beforeEach(() => {
|
||||
sandbox = Sinon.createSandbox()
|
||||
|
||||
eventRepositoryUpsertStub = sandbox.stub(EventRepository.prototype, 'upsert')
|
||||
|
||||
webSocketEmitStub = sandbox.stub()
|
||||
webSocket = {
|
||||
emit: webSocketEmitStub,
|
||||
} as any
|
||||
const client: DatabaseClient = {} as any
|
||||
eventRepository = new EventRepository(client)
|
||||
|
||||
strategy = new ParameterizedReplaceableEventStrategy(webSocket, eventRepository)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
sandbox.restore()
|
||||
})
|
||||
|
||||
describe('execute', () => {
|
||||
it('upserts event without d tag', async () => {
|
||||
event.tags = []
|
||||
await strategy.execute(event)
|
||||
|
||||
expect(eventRepositoryUpsertStub).to.have.been.calledOnceWithExactly(event)
|
||||
expect(eventRepositoryUpsertStub.firstCall.firstArg).to.have.property(EventDeduplicationMetadataKey).and.deep.equal([''])
|
||||
})
|
||||
|
||||
it('upserts event with d tag and one string', async () => {
|
||||
event.tags = [[EventTags.Deduplication, 'one']]
|
||||
await strategy.execute(event)
|
||||
|
||||
expect(eventRepositoryUpsertStub).to.have.been.calledOnceWithExactly(event)
|
||||
expect(eventRepositoryUpsertStub.firstCall.firstArg).to.have.property(EventDeduplicationMetadataKey).and.deep.equal(['one'])
|
||||
})
|
||||
|
||||
it('upserts event with d tag and two strings', async () => {
|
||||
event.tags = [[EventTags.Deduplication, 'one', 'two']]
|
||||
await strategy.execute(event)
|
||||
|
||||
expect(eventRepositoryUpsertStub).to.have.been.calledOnceWithExactly(event)
|
||||
expect(eventRepositoryUpsertStub.firstCall.firstArg).to.have.property(EventDeduplicationMetadataKey).and.deep.equal(['one', 'two'])
|
||||
})
|
||||
|
||||
it('broadcast event if event is created', async () => {
|
||||
eventRepositoryUpsertStub.resolves(1)
|
||||
|
||||
await strategy.execute(event)
|
||||
|
||||
expect(eventRepositoryUpsertStub).to.have.been.calledOnceWithExactly(event)
|
||||
expect(webSocketEmitStub).to.have.been.calledOnceWithExactly(
|
||||
WebSocketAdapterEvent.Broadcast,
|
||||
event
|
||||
)
|
||||
})
|
||||
|
||||
it('rejects if unable to upsert event', async () => {
|
||||
const error = new Error()
|
||||
eventRepositoryUpsertStub.rejects(error)
|
||||
|
||||
await expect(strategy.execute(event)).to.eventually.be.rejectedWith(error)
|
||||
|
||||
expect(eventRepositoryUpsertStub).to.have.been.calledOnceWithExactly(event)
|
||||
expect(webSocketEmitStub).not.to.have.been.called
|
||||
})
|
||||
})
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user