|
| 1 | +// :snippet-start: two-realm-contexts |
| 2 | +import React from 'react'; |
| 3 | +import {AppProvider, UserProvider} from '@realm/react'; |
| 4 | +import {RealmContext} from '../RealmConfig'; |
| 5 | +import {SecondRealmContext} from '../RealmConfig'; |
| 6 | +// :remove-start: |
| 7 | +import {render} from '@testing-library/react-native'; |
| 8 | +import {useApp} from '@realm/react'; |
| 9 | +import {View, Text} from 'react-native'; |
| 10 | + |
| 11 | +const APP_ID = 'example-testers-kvjdy'; |
| 12 | + |
| 13 | +function AppSectionOne() { |
| 14 | + const app = useApp(); |
| 15 | + |
| 16 | + if (app.id !== APP_ID) { |
| 17 | + throw new Error('Did not instantiate app client'); |
| 18 | + } |
| 19 | + |
| 20 | + return ( |
| 21 | + <View> |
| 22 | + <Text>Foo</Text> |
| 23 | + </View> |
| 24 | + ); |
| 25 | +} |
| 26 | + |
| 27 | +function AppSectionTwo() { |
| 28 | + const app = useApp(); |
| 29 | + |
| 30 | + if (app.id !== APP_ID) { |
| 31 | + throw new Error('Did not instantiate app client'); |
| 32 | + } |
| 33 | + |
| 34 | + return ( |
| 35 | + <View> |
| 36 | + <Text>Bar</Text> |
| 37 | + </View> |
| 38 | + ); |
| 39 | +} |
| 40 | +// :remove-end: |
| 41 | + |
| 42 | +function TwoRealmsWrapper() { |
| 43 | + const {RealmProvider: RealmProvider} = RealmContext; |
| 44 | + const {RealmProvider: SecondRealmProvider} = SecondRealmContext; |
| 45 | + |
| 46 | + return ( |
| 47 | + <AppProvider id={APP_ID}> |
| 48 | + <UserProvider> |
| 49 | + {/* This realm uses Flexible Sync. */} |
| 50 | + <RealmProvider |
| 51 | + sync={{flexible: true, onError: error => console.error(error)}} |
| 52 | + > |
| 53 | + <AppSectionOne /> |
| 54 | + </RealmProvider> |
| 55 | + {/* This is a separate local-only realm. */} |
| 56 | + <SecondRealmProvider> |
| 57 | + <AppSectionTwo /> |
| 58 | + </SecondRealmProvider> |
| 59 | + </UserProvider> |
| 60 | + </AppProvider> |
| 61 | + ); |
| 62 | +} |
| 63 | +// :snippet-end: |
| 64 | + |
| 65 | +test('Instantiate SecondRealmProvider correctly', () => { |
| 66 | + render(<TwoRealmsWrapper />); |
| 67 | +}); |
0 commit comments