Skip to content

Commit 34aede4

Browse files
committed
fix tests, remove profile DNE err
1 parent 5e105e0 commit 34aede4

File tree

2 files changed

+17
-34
lines changed

2 files changed

+17
-34
lines changed

src/features/profile/profileSlice.test.tsx

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { render, waitFor } from '@testing-library/react';
22
import fetchMock from 'jest-fetch-mock';
33
import { FetchMock } from 'jest-fetch-mock/types';
4-
import { ErrorBoundary } from 'react-error-boundary';
54
import { Provider } from 'react-redux';
65
import { createTestStore } from '../../app/store';
76
import { makeKBaseServices } from '../../test/kbaseServiceMock';
@@ -39,30 +38,4 @@ describe('useLoggedInProfileUser', () => {
3938
)
4039
);
4140
});
42-
43-
test('throws error when called with invalid username', async () => {
44-
const onErr = jest.fn();
45-
const consoleError = jest.spyOn(console, 'error');
46-
// eslint-disable-next-line @typescript-eslint/no-empty-function
47-
consoleError.mockImplementation(() => {});
48-
const Component = () => {
49-
useLoggedInProfileUser('not_a_user');
50-
return <></>;
51-
};
52-
render(
53-
<ErrorBoundary fallback={<></>} onError={onErr}>
54-
<Provider store={testStore}>
55-
<Component />
56-
</Provider>
57-
</ErrorBoundary>
58-
);
59-
await waitFor(() => {
60-
expect(onErr).toHaveBeenCalled();
61-
expect(consoleError).toHaveBeenCalled();
62-
});
63-
expect(testStore.getState().profile.loggedInProfile?.user.username).toBe(
64-
undefined
65-
);
66-
consoleError.mockRestore();
67-
});
6841
});

src/features/profile/profileSlice.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,28 @@ export const useLoggedInProfileUser = (username?: string) => {
4747
);
4848

4949
useEffect(() => {
50-
if (query.isSuccess) {
50+
if (query.isSuccess && query.data) {
5151
// Set the logged in profile once it loads
5252
// If profile DNE, (eg auth service local user), set profile to undefined
53-
if (!query.data[0][0]) {
54-
dispatch(setLoggedInProfile(undefined));
55-
} else if (query.data[0][0].user.username !== profileUser) {
53+
const queryUsername = query.data?.[0]?.[0]?.user?.username;
54+
if (query.data[0][0] && queryUsername === username) {
5655
dispatch(setLoggedInProfile(query.data[0][0]));
57-
} else {
58-
throw new Error('loading profile failed');
56+
} else if (!query.data[0][0]) {
57+
// likely a local user, no profile!
58+
setLoggedInProfile(undefined);
5959
}
6060
}
61-
}, [dispatch, profileUser, query.data, query.error, query.isSuccess]);
61+
if (query.error) {
62+
throw query.error;
63+
}
64+
}, [
65+
dispatch,
66+
profileUser,
67+
query.data,
68+
query.error,
69+
query.isSuccess,
70+
username,
71+
]);
6272

6373
return query;
6474
};

0 commit comments

Comments
 (0)