mirror of
https://github.com/inaturalist/iNaturalistReactNative.git
synced 2026-05-05 06:05:12 -04:00
Restore language chooser with system locale (signed out users), web locale (signed in users), or app locale (as selected by user)
76 lines
2.8 KiB
JavaScript
76 lines
2.8 KiB
JavaScript
// In this case we *do* want manual cleanup because we want to make sure the
|
|
// component tree has been taken down before we delete all the content in
|
|
// Realm. Otherwise we get after effects from listeners on Realm, Realm
|
|
// queries, and Realm objects that are hard/impossible to wait for before
|
|
// tests finish
|
|
// eslint-disable-next-line testing-library/no-manual-cleanup
|
|
import { cleanup } from "@testing-library/react-native";
|
|
import { API_HOST } from "components/LoginSignUp/AuthenticationService.ts";
|
|
import { getInatLocaleFromSystemLocale } from "i18n/initI18next";
|
|
import i18next from "i18next";
|
|
import inatjs from "inaturalistjs";
|
|
import nock from "nock";
|
|
import RNSInfo from "react-native-sensitive-info";
|
|
import changeLanguage from "sharedHelpers/changeLanguage.ts";
|
|
import safeRealmWrite from "sharedHelpers/safeRealmWrite";
|
|
import { makeResponse } from "tests/factory";
|
|
|
|
const TEST_JWT = "test-json-web-token";
|
|
const TEST_ACCESS_TOKEN = "test-access-token";
|
|
|
|
async function signOut( options = {} ) {
|
|
const realm = options.realm || global.realm;
|
|
i18next.language = undefined;
|
|
// As mentioned above, clean up the component tree so we don't have after
|
|
// effects related to deleting all realm objects.
|
|
// Note: if we ever write tests explicitly testing UI sign out, we might
|
|
// want to write an optional exception to this step
|
|
cleanup( );
|
|
// This is the nuclear option, maybe revisit if it's a source of bugs
|
|
safeRealmWrite( realm, ( ) => {
|
|
realm.deleteAll( );
|
|
}, "deleting entire realm in signOut function, user.js" );
|
|
const systemLocale = getInatLocaleFromSystemLocale( );
|
|
changeLanguage( systemLocale );
|
|
await RNSInfo.deleteItem( "username" );
|
|
await RNSInfo.deleteItem( "jwtToken" );
|
|
await RNSInfo.deleteItem( "jwtGeneratedAt" );
|
|
await RNSInfo.deleteItem( "accessToken" );
|
|
inatjs.users.me.mockClear( );
|
|
}
|
|
|
|
async function signIn( user, options = {} ) {
|
|
const realm = options.realm || global.realm;
|
|
await RNSInfo.setItem( "username", user.login );
|
|
await RNSInfo.setItem( "jwtToken", TEST_JWT );
|
|
await RNSInfo.setItem( "jwtGeneratedAt", Date.now( ).toString( ), {} );
|
|
await RNSInfo.setItem( "accessToken", TEST_ACCESS_TOKEN );
|
|
inatjs.users.me.mockResolvedValue( makeResponse( [user] ) );
|
|
user.signedIn = true;
|
|
safeRealmWrite( realm, ( ) => {
|
|
realm.create( "User", user, "modified" );
|
|
}, "signing user in, user.js" );
|
|
if ( user?.locale ) {
|
|
changeLanguage( user.locale );
|
|
}
|
|
nock( API_HOST )
|
|
.post( "/oauth/token" )
|
|
.reply( 200, { access_token: TEST_ACCESS_TOKEN } )
|
|
.get( "/users/edit.json" )
|
|
.reply( 200, { login: user.login, id: user.id } );
|
|
nock( API_HOST, {
|
|
reqheaders: {
|
|
authorization: `Bearer ${TEST_ACCESS_TOKEN}`
|
|
}
|
|
} )
|
|
.get( "/users/api_token.json" )
|
|
.reply( 200, { api_token: "some-jwt" } );
|
|
}
|
|
|
|
export {
|
|
signIn,
|
|
signOut,
|
|
TEST_ACCESS_TOKEN,
|
|
TEST_JWT
|
|
};
|