Skip to content

Commit f483df5

Browse files
committed
chore(supabase): update expo tests
1 parent 2aab9e6 commit f483df5

File tree

2 files changed

+108
-33
lines changed

2 files changed

+108
-33
lines changed

packages/core/supabase-js/test/integration/expo/__tests__/Index.test.tsx

Lines changed: 73 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,77 @@ describe('Index', () => {
66
cleanup()
77
})
88

9-
it('should display SUBSCRIBED status when realtime connection is established', async () => {
10-
const { getByTestId, unmount } = render(<Index />)
11-
12-
// Initially, the text should be empty
13-
expect(getByTestId('realtime_status')).toHaveTextContent('')
14-
15-
// Wait for the subscription status to be updated
16-
await waitFor(
17-
() => {
18-
const status = getByTestId('realtime_status').props.children
19-
expect(status).toBe('SUBSCRIBED')
20-
},
21-
{
22-
timeout: 30000, // 30 seconds timeout for waitFor
23-
interval: 1000, // Check every second
24-
onTimeout: (error) => {
25-
const currentStatus = getByTestId('realtime_status').props.children
26-
throw new Error(
27-
`Timeout waiting for SUBSCRIBED status. Current status: ${currentStatus}. ${error.message}`
28-
)
29-
},
30-
}
31-
)
32-
33-
// Unmount the component to trigger cleanup.
34-
unmount()
35-
}, 35000) // 35 seconds timeout for the entire test
9+
const versions = [{ vsn: '1.0.0' }, { vsn: '2.0.0' }]
10+
11+
versions.forEach(({ vsn }) => {
12+
describe(`Realtime with vsn: ${vsn}`, () => {
13+
it('should display SUBSCRIBED status when realtime connection is established', async () => {
14+
const { getByTestId, unmount } = render(<Index vsn={vsn} />)
15+
16+
// Verify correct version is being used
17+
expect(getByTestId('vsn')).toHaveTextContent(vsn)
18+
19+
// Initially, the status should be empty
20+
expect(getByTestId('realtime_status')).toHaveTextContent('')
21+
22+
// Wait for the subscription status to be updated
23+
await waitFor(
24+
() => {
25+
const status = getByTestId('realtime_status').props.children
26+
expect(status).toBe('SUBSCRIBED')
27+
},
28+
{
29+
timeout: 30000, // 30 seconds timeout for waitFor
30+
interval: 1000, // Check every second
31+
onTimeout: (error) => {
32+
const currentStatus = getByTestId('realtime_status').props.children
33+
throw new Error(
34+
`Timeout waiting for SUBSCRIBED status with vsn ${vsn}. Current status: ${currentStatus}. ${error.message}`
35+
)
36+
},
37+
}
38+
)
39+
40+
// Unmount the component to trigger cleanup
41+
unmount()
42+
}, 35000) // 35 seconds timeout for the entire test
43+
44+
it('can broadcast and receive messages', async () => {
45+
const { getByTestId, unmount } = render(<Index vsn={vsn} />)
46+
47+
// Wait for subscription
48+
await waitFor(
49+
() => {
50+
const status = getByTestId('realtime_status').props.children
51+
expect(status).toBe('SUBSCRIBED')
52+
},
53+
{
54+
timeout: 30000,
55+
interval: 1000,
56+
}
57+
)
58+
59+
// Wait for broadcast message to be received
60+
await waitFor(
61+
() => {
62+
const message = getByTestId('received_message').props.children
63+
expect(message).toBe('Hello from Expo!')
64+
},
65+
{
66+
timeout: 30000,
67+
interval: 1000,
68+
onTimeout: (error) => {
69+
const currentMessage = getByTestId('received_message').props.children
70+
throw new Error(
71+
`Timeout waiting for broadcast message with vsn ${vsn}. Current message: ${currentMessage}. ${error.message}`
72+
)
73+
},
74+
}
75+
)
76+
77+
// Unmount the component to trigger cleanup
78+
unmount()
79+
}, 35000) // 35 seconds timeout for the entire test
80+
})
81+
})
3682
})

packages/core/supabase-js/test/integration/expo/app/index.tsx

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,51 @@ const SUPABASE_URL = 'http://127.0.0.1:54321'
66
const TEST_ANON_KEY =
77
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0'
88

9-
const supabase = createClient(SUPABASE_URL, TEST_ANON_KEY)
9+
interface IndexProps {
10+
vsn?: string
11+
}
1012

11-
export default function Index() {
13+
export default function Index({ vsn = '1.0.0' }: IndexProps) {
1214
const [realtimeStatus, setRealtimeStatus] = useState<string | null>(null)
13-
const channel = supabase.channel('realtime:public:todos')
15+
const [receivedMessage, setReceivedMessage] = useState<string | null>(null)
1416

1517
useEffect(() => {
18+
const supabase = createClient(SUPABASE_URL, TEST_ANON_KEY, {
19+
realtime: {
20+
heartbeatIntervalMs: 500,
21+
vsn: vsn,
22+
}
23+
})
24+
25+
const channel = supabase.channel(`realtime:public:todos-${vsn}`, {
26+
config: { broadcast: { ack: true, self: true } }
27+
})
28+
29+
// Listen for broadcast messages
30+
channel.on('broadcast', { event: 'test-event' }, (payload) => {
31+
setReceivedMessage(payload.payload.message)
32+
})
33+
34+
// Subscribe to the channel
1635
if (channel.state === 'closed') {
17-
channel.subscribe((status) => {
18-
if (status === 'SUBSCRIBED') setRealtimeStatus(status)
36+
channel.subscribe(async (status) => {
37+
if (status === 'SUBSCRIBED') {
38+
setRealtimeStatus(status)
39+
40+
await channel.send({
41+
type: 'broadcast',
42+
event: 'test-event',
43+
payload: { message: 'Hello from Expo!' }
44+
})
45+
}
1946
})
2047
}
2148

2249
return () => {
2350
channel.unsubscribe()
2451
supabase.realtime.disconnect()
2552
}
26-
}, [])
53+
}, [vsn])
2754

2855
return (
2956
<View
@@ -34,7 +61,9 @@ export default function Index() {
3461
backgroundColor: '#fff',
3562
}}
3663
>
64+
<Text testID="vsn">{vsn}</Text>
3765
<Text testID="realtime_status">{realtimeStatus || ''}</Text>
66+
<Text testID="received_message">{receivedMessage || ''}</Text>
3867
</View>
3968
)
4069
}

0 commit comments

Comments
 (0)