diff --git a/src/screens/LoginPage/LoginPage.tsx b/src/screens/LoginPage/LoginPage.tsx
index a7f4029b92..481f8d09c2 100644
--- a/src/screens/LoginPage/LoginPage.tsx
+++ b/src/screens/LoginPage/LoginPage.tsx
@@ -191,6 +191,7 @@ function loginPage(): JSX.Element {
loginData.login.user.adminApproved === true)
) {
localStorage.setItem('token', loginData.login.accessToken);
+ localStorage.setItem('refreshToken', loginData.login.refreshToken);
localStorage.setItem('id', loginData.login.user._id);
localStorage.setItem('IsLoggedIn', 'TRUE');
localStorage.setItem('UserType', loginData.login.user.userType);
diff --git a/src/screens/MemberDetail/MemberDetail.tsx b/src/screens/MemberDetail/MemberDetail.tsx
index 22510a62e5..d190b47246 100644
--- a/src/screens/MemberDetail/MemberDetail.tsx
+++ b/src/screens/MemberDetail/MemberDetail.tsx
@@ -36,10 +36,18 @@ const MemberDetail: React.FC = ({ id }): JSX.Element => {
data: userData,
loading: loading,
error: error,
+ refetch: refetch,
} = useQuery(USER_DETAILS, {
variables: { id: currentUrl }, // For testing we are sending the id as a prop
});
+ /* istanbul ignore next */
+ const toggleStateValue = (): void => {
+ if (state === 1) setState(2);
+ else setState(1);
+ refetch();
+ };
+
if (loading) {
return ;
}
@@ -286,7 +294,7 @@ const MemberDetail: React.FC = ({ id }): JSX.Element => {
) : (
-
+
)}
diff --git a/src/utils/getRefreshToken.test.ts b/src/utils/getRefreshToken.test.ts
new file mode 100644
index 0000000000..12d8448b76
--- /dev/null
+++ b/src/utils/getRefreshToken.test.ts
@@ -0,0 +1,52 @@
+import { refreshToken } from './getRefreshToken';
+
+jest.mock('@apollo/client', () => {
+ const originalModule = jest.requireActual('@apollo/client');
+
+ return {
+ __esModule: true,
+ ...originalModule,
+ ApolloClient: jest.fn(() => ({
+ mutate: jest.fn(() =>
+ Promise.resolve({
+ data: {
+ refreshToken: {
+ accessToken: 'newAccessToken',
+ refreshToken: 'newRefreshToken',
+ },
+ },
+ })
+ ),
+ })),
+ };
+});
+
+describe('refreshToken', () => {
+ // Mock window.location.reload()
+ const { location } = window;
+ delete (global.window as any).location;
+ global.window.location = { ...location, reload: jest.fn() };
+
+ // Mock localStorage.setItem() and localStorage.clear()
+ Storage.prototype.setItem = jest.fn();
+ Storage.prototype.clear = jest.fn();
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ it('returns true when the token is refreshed successfully', async () => {
+ const result = await refreshToken();
+
+ expect(localStorage.setItem).toHaveBeenCalledWith(
+ 'token',
+ 'newAccessToken'
+ );
+ expect(localStorage.setItem).toHaveBeenCalledWith(
+ 'refreshToken',
+ 'newRefreshToken'
+ );
+ expect(result).toBe(true);
+ expect(window.location.reload).toHaveBeenCalled();
+ });
+});
diff --git a/src/utils/getRefreshToken.ts b/src/utils/getRefreshToken.ts
new file mode 100644
index 0000000000..f3145c4b48
--- /dev/null
+++ b/src/utils/getRefreshToken.ts
@@ -0,0 +1,31 @@
+import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client';
+import { BACKEND_URL } from 'Constant/constant';
+import { REFRESH_TOKEN_MUTATION } from 'GraphQl/Mutations/mutations';
+
+export async function refreshToken(): Promise
{
+ const client = new ApolloClient({
+ link: new HttpLink({
+ uri: BACKEND_URL,
+ }),
+ cache: new InMemoryCache(),
+ });
+
+ const refreshToken = localStorage.getItem('refreshToken');
+ /* istanbul ignore next */
+ try {
+ const { data } = await client.mutate({
+ mutation: REFRESH_TOKEN_MUTATION,
+ variables: {
+ refreshToken: refreshToken,
+ },
+ });
+
+ localStorage.setItem('token', data.refreshToken.accessToken);
+ localStorage.setItem('refreshToken', data.refreshToken.refreshToken);
+ window.location.reload();
+ return true;
+ } catch (error) {
+ console.error('Failed to refresh token', error);
+ return false;
+ }
+}