Skip to content

Commit

Permalink
Merge pull request #69 from lifeomic/update-use-async-storage
Browse files Browse the repository at this point in the history
PHC-4151 Include setItem in async storage hook
  • Loading branch information
bruce-glazier committed Mar 7, 2023
2 parents 5bba1fc + fe51b62 commit d1a925e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
31 changes: 16 additions & 15 deletions src/hooks/useActiveAccount.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ import {
} from './useActiveAccount';
import AsyncStorage from '@react-native-async-storage/async-storage';
import AsyncStorageMock from '@react-native-async-storage/async-storage/jest/async-storage-mock';
import {
QueryClient,
QueryClientProvider,
QueryObserverLoadingResult,
UseQueryResult,
} from 'react-query';
import { QueryClient, QueryClientProvider, UseQueryResult } from 'react-query';
import { mockDeep } from 'jest-mock-extended';
import * as GetAsyncStorage from './useGetAsyncStorage';
import * as useAsyncStorage from './useAsyncStorage';

jest.mock('./useAccounts', () => ({
useAccounts: jest.fn(),
Expand All @@ -23,7 +18,7 @@ jest.mock('./useAccounts', () => ({
const useAccountsMock = useAccounts as jest.Mock;

const refetchMock = jest.fn();
let useGetAsyncStorageSpy = jest.spyOn(GetAsyncStorage, 'useGetAsyncStorage');
let useAsyncStorageSpy = jest.spyOn(useAsyncStorage, 'useAsyncStorage');

const accountId1 = 'acct1';
const accountId2 = 'acct2';
Expand Down Expand Up @@ -56,8 +51,11 @@ beforeEach(() => {
data: mockAccounts,
refetch: refetchMock,
});
useGetAsyncStorageSpy.mockReturnValue({
...mockDeep<UseQueryResult<string | null>>(),
useAsyncStorageSpy.mockReturnValue({
useGetItem: (_: string) => ({
...mockDeep<UseQueryResult<string | null>>(),
}),
setItem: AsyncStorage.setItem,
});
});

Expand Down Expand Up @@ -85,9 +83,12 @@ test('exposes some props from useAccounts', async () => {
isFetched: true,
error,
});
useGetAsyncStorageSpy.mockReturnValueOnce({
...mockDeep<QueryObserverLoadingResult<string | null>>(),
isFetched: true,
useAsyncStorageSpy.mockReturnValueOnce({
useGetItem: (_: string) => ({
...mockDeep<UseQueryResult<string | null>>(),
isFetched: true,
}),
setItem: AsyncStorage.setItem,
});

const { result } = await renderHookInContext();
Expand Down Expand Up @@ -164,7 +165,7 @@ test('provider allows for account override by id', async () => {
});

test('uses account from async storage', async () => {
useGetAsyncStorageSpy.mockRestore();
useAsyncStorageSpy.mockRestore();

AsyncStorageMock.getItem = jest.fn().mockResolvedValueOnce(accountId1);
const { result, rerender } = await renderHookInContext();
Expand All @@ -188,7 +189,7 @@ test('uses account from async storage', async () => {
accountsWithProduct: mockAccounts,
});

useGetAsyncStorageSpy = jest.spyOn(GetAsyncStorage, 'useGetAsyncStorage');
useAsyncStorageSpy = jest.spyOn(useAsyncStorage, 'useAsyncStorage');
});

test('initial render writes selected account to async storage', async () => {
Expand Down
28 changes: 10 additions & 18 deletions src/hooks/useActiveAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import React, {
useContext,
useCallback,
} from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import { Account, useAccounts } from './useAccounts';
import { QueryObserverResult } from 'react-query';
import { useGetAsyncStorage } from './useGetAsyncStorage';
import { useAsyncStorage } from './useAsyncStorage';

export type ActiveAccountProps = {
account?: Account;
Expand Down Expand Up @@ -60,11 +59,8 @@ export const ActiveAccountContextProvider = ({
const accountsResult = useAccounts();
const accountsWithProduct = filterNonLRAccounts(accountsResult.data);
const [activeAccount, setActiveAccount] = useState<ActiveAccountProps>({});
const storedAccountResult = useGetAsyncStorage(selectedAccountIdKey);

const storeAccountId = useCallback(async (accountId: string) => {
await AsyncStorage.setItem(selectedAccountIdKey, accountId);
}, []);
const { useGetItem, setItem } = useAsyncStorage();
const storedAccountResult = useGetItem(selectedAccountIdKey);

/**
* Initial setting of activeAccount
Expand Down Expand Up @@ -92,12 +88,12 @@ export const ActiveAccountContextProvider = ({
},
trialExpired: getTrialExpired(selectedAccount),
});
storeAccountId(selectedAccount.id);
setItem(selectedAccountIdKey, selectedAccount.id);
}, [
accountsWithProduct,
activeAccount?.account?.id,
accountIdToSelect,
storeAccountId,
setItem,
storedAccountResult.data,
storedAccountResult.isLoading,
]);
Expand All @@ -118,12 +114,12 @@ export const ActiveAccountContextProvider = ({
},
trialExpired: getTrialExpired(selectedAccount),
});
storeAccountId(selectedAccount.id);
setItem(selectedAccountIdKey, selectedAccount.id);
} catch (error) {
console.warn('Unable to set active account', error);
}
},
[accountsWithProduct, storeAccountId],
[accountsWithProduct, setItem],
);

const refetch = useCallback(async () => {
Expand All @@ -137,13 +133,9 @@ export const ActiveAccountContextProvider = ({
accountsWithProduct,
refetch,
setActiveAccountId,
isLoading: !!(
accountsResult.isLoading || storedAccountResult.isLoading
),
isFetched: !!(
accountsResult.isFetched && storedAccountResult.isFetched
),
error: accountsResult.error || storedAccountResult.error,
isLoading: accountsResult.isLoading,
isFetched: accountsResult.isFetched,
error: accountsResult.error,
}}
>
{children}
Expand Down
17 changes: 17 additions & 0 deletions src/hooks/useAsyncStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import { useQuery } from 'react-query';

export function useAsyncStorage() {
const useGetItem = (key: string) =>
useQuery<string | null>('async-storage-get', () =>
AsyncStorage.getItem(key),
);

const setItem = (key: string, value: string) =>
AsyncStorage.setItem(key, value);

return {
useGetItem: useGetItem,
setItem: setItem,
};
}
8 changes: 0 additions & 8 deletions src/hooks/useGetAsyncStorage.ts

This file was deleted.

0 comments on commit d1a925e

Please sign in to comment.