Skip to content

Commit

Permalink
Use deep comparison for field matching (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad authored Dec 7, 2023
1 parent e9dc98a commit 68a29e3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/core/lib/queues/HandlerSpy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { randomUUID } from 'node:crypto'

import { isObject } from '@lokalise/node-core'
import { deepEqual } from 'fast-equals'
import { Fifo } from 'toad-cache'

import type { MessageProcessingResult } from '../types/MessageQueueTypes'
Expand Down Expand Up @@ -64,7 +65,7 @@ export class HandlerSpy<MessagePayloadSchemas extends object> {
return (
Object.entries(fields).every(([key, value]) => {
// @ts-ignore
return spyResult.message[key] === value
return deepEqual(spyResult.message[key], value)
}) &&
(!processingResult || spyResult.processingResult === processingResult)
)
Expand Down
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"dependencies": {
"@lokalise/node-core": "^8.3.0",
"fast-equals": "^5.0.1",
"toad-cache": "^3.4.1"
},
"devDependencies": {
Expand Down
26 changes: 26 additions & 0 deletions packages/core/test/queues/HandlerSpy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HandlerSpy, isHandlerSpy } from '../../lib/queues/HandlerSpy'
type Message = {
id: string
status: string
payload?: Record<string, unknown>
}

const TEST_MESSAGE: Message = {
Expand All @@ -17,6 +18,14 @@ const TEST_MESSAGE_2: Message = {
status: 'inprogress',
}

const TEST_MESSAGE_3: Message = {
id: 'abcd',
status: 'inprogress',
payload: {
projectId: 1,
},
}

describe('HandlerSpy', () => {
describe('clear', () => {
it('Remove stored events', () => {
Expand Down Expand Up @@ -55,6 +64,23 @@ describe('HandlerSpy', () => {
expect(message.message).toEqual(TEST_MESSAGE)
})

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

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

const message = await spy.waitForMessage({
payload: {
projectId: 1,
},
})

expect(message.message).toEqual(TEST_MESSAGE_3)
})

it('Finds multiple previously consumed events', async () => {
const spy = new HandlerSpy<Message>()

Expand Down

0 comments on commit 68a29e3

Please sign in to comment.