added more support for supabase-js
This commit is contained in:
@@ -4,35 +4,48 @@ import { createAnonClient } from './setup.ts';
|
||||
const client = createAnonClient();
|
||||
|
||||
describe('Realtime', () => {
|
||||
it('should receive insert events', async () => {
|
||||
it('should resume subscription from last_event_id', async () => {
|
||||
// 1. Create a message while no one is listening
|
||||
const { data: inserted, error } = await client
|
||||
.from('todos')
|
||||
.insert({ title: 'Missed Event', completed: false })
|
||||
.select()
|
||||
.single();
|
||||
expect(error).toBeNull();
|
||||
|
||||
// We need to know the ID of this event in realtime history.
|
||||
// Ideally we query `madbase_realtime.messages` but client can't.
|
||||
// So we just assume ID > 0.
|
||||
// Wait, we need to pass `last_event_id` < actual_id.
|
||||
// Let's assume we want everything after ID=0.
|
||||
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
// 2. Connect with last_event_id = 0 (should fetch all history)
|
||||
const channel = client
|
||||
.channel('public:todos')
|
||||
.channel('public:todos', { config: { last_event_id: 0 } as any })
|
||||
.on(
|
||||
'postgres_changes',
|
||||
{ event: 'INSERT', schema: 'public', table: 'todos' },
|
||||
(payload) => {
|
||||
console.log('Received INSERT event:', payload);
|
||||
expect(payload.new).toBeDefined();
|
||||
expect(payload.new.title).toBe('Realtime Test');
|
||||
client.removeChannel(channel).then(() => resolve());
|
||||
console.log('Received missed event:', payload);
|
||||
if (payload.new && payload.new.title === 'Missed Event') {
|
||||
expect(payload.new.id).toBe(inserted.id);
|
||||
client.removeChannel(channel).then(() => resolve());
|
||||
}
|
||||
}
|
||||
)
|
||||
.subscribe(async (status) => {
|
||||
if (status === 'SUBSCRIBED') {
|
||||
// Trigger an insert
|
||||
const { error } = await client
|
||||
.from('todos')
|
||||
.insert({ title: 'Realtime Test', completed: false });
|
||||
|
||||
if (error) reject(error);
|
||||
}
|
||||
.subscribe((status, err) => {
|
||||
if (status === 'SUBSCRIBED') {
|
||||
console.log('Subscribed with resume');
|
||||
}
|
||||
if (status === 'CHANNEL_ERROR') {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
|
||||
// Timeout if no event received
|
||||
setTimeout(() => {
|
||||
reject(new Error('Timeout waiting for Realtime event'));
|
||||
}, 10000);
|
||||
reject(new Error('Timeout waiting for missed event'));
|
||||
}, 5000);
|
||||
});
|
||||
}, 10000);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user