Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: state field is cleared after returning from country list #58080

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 29 additions & 26 deletions src/pages/AddressPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,36 @@ function AddressPage({title, address, updateAddress, isLoadingApp = true, backTo
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [address?.state, address?.country, address?.city, address?.zip]);

const handleAddressChange = useCallback((value: unknown, key: unknown) => {
const addressPart = value as string;
const addressPartKey = key as keyof Address;
const handleAddressChange = useCallback(
(value: unknown, key: unknown) => {
const addressPart = value as string;
const addressPartKey = key as keyof Address;

if (addressPartKey !== INPUT_IDS.COUNTRY && addressPartKey !== INPUT_IDS.STATE && addressPartKey !== INPUT_IDS.CITY && addressPartKey !== INPUT_IDS.ZIP_POST_CODE) {
return;
}
if (addressPartKey === INPUT_IDS.COUNTRY) {
setCurrentCountry(addressPart as Country | '');
setState('');
setCity('');
setZipcode('');
return;
}
if (addressPartKey === INPUT_IDS.STATE) {
setState(addressPart);
setCity('');
setZipcode('');
return;
}
if (addressPartKey === INPUT_IDS.CITY) {
setCity(addressPart);
setZipcode('');
return;
}
setZipcode(addressPart);
}, []);
if (addressPartKey !== INPUT_IDS.COUNTRY && addressPartKey !== INPUT_IDS.STATE && addressPartKey !== INPUT_IDS.CITY && addressPartKey !== INPUT_IDS.ZIP_POST_CODE) {
return;
}
if (addressPartKey === INPUT_IDS.COUNTRY && addressPart !== currentCountry) {
setCurrentCountry(addressPart as Country | '');
setState('');
setCity('');
setZipcode('');
return;
}
if (addressPartKey === INPUT_IDS.STATE) {
setState(addressPart);
setCity('');
setZipcode('');
return;
}
if (addressPartKey === INPUT_IDS.CITY) {
setCity(addressPart);
setZipcode('');
return;
}
setZipcode(addressPart);
},
[currentCountry],
);

return (
<ScreenWrapper
Expand Down
81 changes: 81 additions & 0 deletions tests/ui/AddressPageTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {PortalProvider} from '@gorhom/portal';
import {NavigationContainer} from '@react-navigation/native';
import {act, render, screen} from '@testing-library/react-native';
import Onyx from 'react-native-onyx';
import ComposeProviders from '@components/ComposeProviders';
import {LocaleContextProvider} from '@components/LocaleContextProvider';
import OnyxProvider from '@components/OnyxProvider';
import {CurrentReportIDContextProvider} from '@hooks/useCurrentReportID';
import Navigation, {navigationRef} from '@libs/Navigation/Navigation';
import createPlatformStackNavigator from '@libs/Navigation/PlatformStackNavigation/createPlatformStackNavigator';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import PersonalAddressPage from '@pages/settings/Profile/PersonalDetails/PersonalAddressPage';
import ONYXKEYS from '@src/ONYXKEYS';
import SCREENS from '@src/SCREENS';
import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';

jest.mock('@rnmapbox/maps', () => {
return {
default: jest.fn(),
MarkerView: jest.fn(),
setAccessToken: jest.fn(),
};
});

jest.mock('@react-native-community/geolocation', () => ({
setRNConfiguration: jest.fn(),
}));

const Stack = createPlatformStackNavigator<SettingsNavigatorParamList>();

const renderPage = (initialRouteName: typeof SCREENS.SETTINGS.PROFILE.ADDRESS) => {
return render(
<ComposeProviders components={[OnyxProvider, LocaleContextProvider, CurrentReportIDContextProvider]}>
<PortalProvider>
<NavigationContainer ref={navigationRef}>
<Stack.Navigator initialRouteName={initialRouteName}>
<Stack.Screen
name={SCREENS.SETTINGS.PROFILE.ADDRESS}
component={PersonalAddressPage}
/>
</Stack.Navigator>
</NavigationContainer>
</PortalProvider>
</ComposeProviders>,
);
};

describe('AddressPageTest', () => {
beforeAll(() => {
Onyx.init({
keys: ONYXKEYS,
});
});
it('should not reset state', async () => {
await TestHelper.signInWithTestUser();
await act(async () => {
await Onyx.merge(`${ONYXKEYS.PRIVATE_PERSONAL_DETAILS}`, {
addresses: [
{
state: 'Test',
country: 'VN',
street: 'Test',
},
],
});
await Onyx.merge(`${ONYXKEYS.IS_LOADING_APP}`, false);
});

renderPage(SCREENS.SETTINGS.PROFILE.ADDRESS);

await waitForBatchedUpdatesWithAct();
const state = screen.getAllByLabelText('State / Province');
expect(state.at(1)?.props.value).toEqual('Test');
Navigation.setParams({
country: 'VN',
});
await waitForBatchedUpdatesWithAct();
expect(state?.at(1)?.props.value).toEqual('Test');
});
});