Skip to content

Commit

Permalink
test(listen): add test for last-event-id
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge committed Jan 3, 2025
1 parent 6d83f02 commit 06bf696
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion test/listen.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {AddressInfo} from 'node:net'

import {type ClientConfig, createClient} from '@sanity/client'
import {catchError, firstValueFrom, of} from 'rxjs'
import {catchError, firstValueFrom, lastValueFrom, of, take, toArray} from 'rxjs'
import {describe, expect, test, vitest} from 'vitest'

import {createSseServer, type OnRequest} from './helpers/sseServer'
Expand Down Expand Up @@ -116,6 +116,37 @@ describe.skipIf(typeof EdgeRuntime === 'string' || typeof document !== 'undefine
}
})

test('sends last-event-id header when reconnecting', async () => {
expect.assertions(2)

let attempt = 0
const {server, client} = await testSse(({channel, request}) => {
attempt++
channel!.send({event: 'welcome'})
channel!.send({event: 'mutation', id: '123', data: {foo: 'bar'}})
if (attempt === 2) {
expect(request.headers['last-event-id'], 'should send last-event-id').toBe('123')
}
channel!.close()
process.nextTick(() => channel!.close())
})

const events = await lastValueFrom(
client.listen('*', {}, {events: ['reconnect', 'mutation']}).pipe(
take(3),
catchError((err) => of(err)),
toArray(),
),
)
expect(events).toEqual([
{type: 'mutation', foo: 'bar'},
{type: 'reconnect'},
{type: 'mutation', foo: 'bar'},
])

server.close()
})

test('emits channel errors', async () => {
expect.assertions(1)

Expand Down

0 comments on commit 06bf696

Please sign in to comment.