11import AsyncStorage from '@react-native-async-storage/async-storage' ;
22import { renderHook } from '@testing-library/react-hooks' ;
3- import { waitFor } from '@testing-library/react-native' ;
43
54import { PREFERENCES_KEY } from 'data/asyncStorageKeys' ;
65import { PreferencesProvider , resetPreferencesForTests , usePreferences } from 'Preferences' ;
76
87// Mock out AsyncStorage for tests
9- // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-require-imports
8+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
109jest . mock ( '@react-native-async-storage/async-storage' , ( ) => require ( '@react-native-async-storage/async-storage/jest/async-storage-mock' ) ) ;
1110
1211describe ( 'Preferences' , ( ) => {
@@ -22,12 +21,11 @@ describe('Preferences', () => {
2221
2322 it ( 'updates after preferences are loaded' , async ( ) => {
2423 await AsyncStorage . setItem ( PREFERENCES_KEY , JSON . stringify ( { center : 'BAC' , hasSeenCenterPicker : true } ) ) ;
25- const { result} = renderHook ( ( ) => usePreferences ( ) , { wrapper : PreferencesProvider } ) ;
24+ const { result, waitForNextUpdate } = renderHook ( ( ) => usePreferences ( ) , { wrapper : PreferencesProvider } ) ;
2625 expect ( result . current . preferences . center ) . toEqual ( 'NWAC' ) ;
2726
28- await waitFor ( ( ) => {
29- expect ( result . current . preferences . center ) . toEqual ( 'BAC' ) ;
30- } ) ;
27+ await waitForNextUpdate ( ) ;
28+ expect ( result . current . preferences . center ) . toEqual ( 'BAC' ) ;
3129 } ) ;
3230
3331 it ( 'falls back to defaults if preferences cannot be parsed' , async ( ) => {
@@ -50,68 +48,61 @@ describe('Preferences', () => {
5048
5149 it ( 'updates to a default value after loading' , async ( ) => {
5250 await AsyncStorage . setItem ( PREFERENCES_KEY , JSON . stringify ( { center : 'BAC' , hasSeenCenterPicker : true } ) ) ;
53- const { result} = renderHook ( ( ) => usePreferences ( ) , { wrapper : PreferencesProvider } ) ;
51+ const { result, waitForNextUpdate } = renderHook ( ( ) => usePreferences ( ) , { wrapper : PreferencesProvider } ) ;
5452 expect ( result . current . preferences . mixpanelUserId ) . toBeUndefined ( ) ;
5553
56- await waitFor ( ( ) => {
57- expect ( result . current . preferences . mixpanelUserId ) . toMatch ( / ^ [ 0 - 9 a - f ] { 8 } - ( [ 0 - 9 a - f ] { 4 } - ) { 3 } [ 0 - 9 a - f ] { 12 } $ / ) ;
58- } ) ;
54+ await waitForNextUpdate ( ) ;
55+ expect ( result . current . preferences . mixpanelUserId ) . toMatch ( / ^ [ 0 - 9 a - f ] { 8 } - ( [ 0 - 9 a - f ] { 4 } - ) { 3 } [ 0 - 9 a - f ] { 12 } $ / ) ;
5956 } ) ;
6057
6158 it ( 'does not overwrite a previously saved id' , async ( ) => {
6259 await AsyncStorage . setItem ( PREFERENCES_KEY , JSON . stringify ( { center : 'BAC' , hasSeenCenterPicker : true , mixpanelUserId : '00000000-0000-0000-0000-000000000000' } ) ) ;
63- const { result} = renderHook ( ( ) => usePreferences ( ) , { wrapper : PreferencesProvider } ) ;
60+ const { result, waitForNextUpdate } = renderHook ( ( ) => usePreferences ( ) , { wrapper : PreferencesProvider } ) ;
6461 expect ( result . current . preferences . mixpanelUserId ) . toBeUndefined ( ) ;
6562
66- await waitFor ( ( ) => {
67- expect ( result . current . preferences . mixpanelUserId ) . toEqual ( '00000000-0000-0000-0000-000000000000' ) ;
68- } ) ;
63+ await waitForNextUpdate ( ) ;
64+ expect ( result . current . preferences . mixpanelUserId ) . toEqual ( '00000000-0000-0000-0000-000000000000' ) ;
6965 } ) ;
7066
7167 it ( 'is preserved when clearPreferences is called' , async ( ) => {
7268 const userId = 'CE998943-7231-42C4-A22F-24845B2CF567' ;
7369 await AsyncStorage . setItem ( PREFERENCES_KEY , JSON . stringify ( { center : 'BAC' , hasSeenCenterPicker : true , mixpanelUserId : userId } ) ) ;
7470
7571 // Render the hook to load the preferences. First we'll see the default value, then the saved value
76- const { result} = renderHook ( ( ) => usePreferences ( ) , { wrapper : PreferencesProvider } ) ;
72+ const { result, waitForNextUpdate } = renderHook ( ( ) => usePreferences ( ) , { wrapper : PreferencesProvider } ) ;
7773 expect ( result . current . preferences . mixpanelUserId ) . toBeUndefined ( ) ;
78- await waitFor ( ( ) => {
79- expect ( result . current . preferences . center ) . toEqual ( 'BAC' ) ;
80- expect ( result . current . preferences . mixpanelUserId ) . toEqual ( userId ) ;
81- } ) ;
74+ await waitForNextUpdate ( ) ;
75+ expect ( result . current . preferences . center ) . toEqual ( 'BAC' ) ;
76+ expect ( result . current . preferences . mixpanelUserId ) . toEqual ( userId ) ;
8277
8378 // Now clear the preferences. This is not async - we clear them in memory immediately, and lazily persist the change.
8479 result . current . clearPreferences ( ) ;
8580
86- await waitFor ( ( ) => {
87- // After clearing, the center is reset to the default, but the userId is preserved
88- expect ( result . current . preferences . center ) . toEqual ( 'NWAC' ) ;
89- expect ( result . current . preferences . mixpanelUserId ) . toEqual ( userId ) ;
90- } ) ;
81+ // After clearing, the center is reset to the default, but the userId is preserved
82+ expect ( result . current . preferences . center ) . toEqual ( 'NWAC' ) ;
83+ expect ( result . current . preferences . mixpanelUserId ) . toEqual ( userId ) ;
9184 } ) ;
9285
9386 it ( 'is lost when preferences are damaged' , async ( ) => {
9487 const userId = 'CE998943-7231-42C4-A22F-24845B2CF567' ;
9588 await AsyncStorage . setItem ( PREFERENCES_KEY , JSON . stringify ( { mixpanelUserId : userId , center : 'this is not a valid center' } ) ) ;
9689
9790 // Render the hook to load the preferences. First we'll see the default value, then the saved value
98- const { result} = renderHook ( ( ) => usePreferences ( ) , { wrapper : PreferencesProvider } ) ;
91+ const { result, waitForNextUpdate } = renderHook ( ( ) => usePreferences ( ) , { wrapper : PreferencesProvider } ) ;
9992 expect ( result . current . preferences . mixpanelUserId ) . toBeUndefined ( ) ;
100- await waitFor ( ( ) => {
101- // The center is invalid, so preferences parsing will fail. The previous user id is lost, and we get a different UUID in its place.
102- expect ( result . current . preferences . mixpanelUserId ) . toMatch ( / ^ [ 0 - 9 a - f ] { 8 } - ( [ 0 - 9 a - f ] { 4 } - ) { 3 } [ 0 - 9 a - f ] { 12 } $ / ) ;
103- expect ( result . current . preferences . mixpanelUserId ) . not . toEqual ( userId ) ;
104- } ) ;
93+ await waitForNextUpdate ( ) ;
94+ // The center is invalid, so preferences parsing will fail. The previous user id is lost, and we get a different UUID in its place.
95+ expect ( result . current . preferences . mixpanelUserId ) . toMatch ( / ^ [ 0 - 9 a - f ] { 8 } - ( [ 0 - 9 a - f ] { 4 } - ) { 3 } [ 0 - 9 a - f ] { 12 } $ / ) ;
96+ expect ( result . current . preferences . mixpanelUserId ) . not . toEqual ( userId ) ;
10597 } ) ;
10698
10799 it ( 'does overwrite an invalid id' , async ( ) => {
108100 await AsyncStorage . setItem ( PREFERENCES_KEY , JSON . stringify ( { center : 'BAC' , hasSeenCenterPicker : true , mixpanelUserId : 'not a uuid' } ) ) ;
109- const { result} = renderHook ( ( ) => usePreferences ( ) , { wrapper : PreferencesProvider } ) ;
101+ const { result, waitForNextUpdate } = renderHook ( ( ) => usePreferences ( ) , { wrapper : PreferencesProvider } ) ;
110102 expect ( result . current . preferences . mixpanelUserId ) . toBeUndefined ( ) ;
111103
112- await waitFor ( ( ) => {
113- expect ( result . current . preferences . mixpanelUserId ) . toMatch ( / ^ [ 0 - 9 a - f ] { 8 } - ( [ 0 - 9 a - f ] { 4 } - ) { 3 } [ 0 - 9 a - f ] { 12 } $ / ) ;
114- } ) ;
104+ await waitForNextUpdate ( ) ;
105+ expect ( result . current . preferences . mixpanelUserId ) . toMatch ( / ^ [ 0 - 9 a - f ] { 8 } - ( [ 0 - 9 a - f ] { 4 } - ) { 3 } [ 0 - 9 a - f ] { 12 } $ / ) ;
115106 } ) ;
116107 } ) ;
117108
0 commit comments