Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support narrowing spy type #86

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions packages/core/lib/queues/HandlerSpy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ export class HandlerSpy<MessagePayloadSchemas extends object> {
)
}

waitForMessageWithId(id: string, processingResult?: MessageProcessingResult) {
return this.waitForMessage(
waitForMessageWithId<T extends MessagePayloadSchemas>(
id: string,
processingResult?: MessageProcessingResult,
) {
return this.waitForMessage<T>(
// @ts-ignore
{
[this.messageIdField]: id,
Expand All @@ -92,24 +95,22 @@ export class HandlerSpy<MessagePayloadSchemas extends object> {
)
}

waitForMessage(
fields: DeepPartial<MessagePayloadSchemas>,
waitForMessage<T extends MessagePayloadSchemas>(
fields: DeepPartial<T>,
processingResult?: MessageProcessingResult,
): Promise<SpyResult<MessagePayloadSchemas>> {
): Promise<SpyResult<T>> {
const processedMessageEntry = Object.values(this.messageBuffer.items).find(
// @ts-ignore
(spyResult: SpyResultCacheEntry<MessagePayloadSchemas>) => {
(spyResult: SpyResultCacheEntry<T>) => {
return this.messageMatchesFilter(spyResult.value, fields, processingResult)
},
)
if (processedMessageEntry) {
return Promise.resolve(processedMessageEntry.value)
}

let resolve: (
value: SpyResult<MessagePayloadSchemas> | PromiseLike<SpyResult<MessagePayloadSchemas>>,
) => void
const spyPromise = new Promise<SpyResult<MessagePayloadSchemas>>((_resolve) => {
let resolve: (value: SpyResult<T> | PromiseLike<SpyResult<T>>) => void
const spyPromise = new Promise<SpyResult<T>>((_resolve) => {
resolve = _resolve
})

Expand Down
56 changes: 56 additions & 0 deletions packages/core/test/queues/HandlerSpy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ type Message = {
payload?: Record<string, unknown>
}

type MessageB = {
id2: string
status2: string
payload?: Record<string, unknown>
}

type DeepMessage = {
id: string
status: string
Expand All @@ -24,11 +30,21 @@ const TEST_MESSAGE: Message = {
status: 'done',
}

const TEST_MESSAGEB: MessageB = {
id2: 'abc',
status2: 'done',
}

const TEST_MESSAGE_2: Message = {
id: 'abcd',
status: 'inprogress',
}

const TEST_MESSAGE_2B: MessageB = {
id2: 'abcd',
status2: 'inprogress',
}

const TEST_MESSAGE_3: Message = {
id: 'abcd',
status: 'inprogress',
Expand Down Expand Up @@ -104,6 +120,26 @@ describe('HandlerSpy', () => {
expect(message.message).toEqual(TEST_MESSAGE)
})

it('Finds previously consumed event with type narrowing', async () => {
const spy = new HandlerSpy<Message | MessageB>()

spy.addProcessedMessage({
processingResult: 'consumed',
message: TEST_MESSAGE_2B,
})

spy.addProcessedMessage({
processingResult: 'consumed',
message: TEST_MESSAGEB,
})

const message = await spy.waitForMessage<MessageB>({
status2: 'done',
})

expect(message.message).toEqual(TEST_MESSAGEB)
})

it('Finds previously consumed event with a deep match', async () => {
const spy = new HandlerSpy<Message>()

Expand Down Expand Up @@ -272,6 +308,26 @@ describe('HandlerSpy', () => {
expect(message.message).toEqual(TEST_MESSAGE)
})

it('Finds previously consumed event with type narrowing', async () => {
const spy = new HandlerSpy<Message | MessageB>({
messageIdField: 'id2',
})

spy.addProcessedMessage({
processingResult: 'consumed',
message: TEST_MESSAGE_2B,
})

spy.addProcessedMessage({
processingResult: 'consumed',
message: TEST_MESSAGEB,
})

const message = await spy.waitForMessageWithId<MessageB>(TEST_MESSAGEB.id2)

expect(message.message).toEqual(TEST_MESSAGEB)
})

it('Waits for an invalid message to be rejected by id', async () => {
const spy = new HandlerSpy<Message>()

Expand Down
Loading