Skip to content

Commit

Permalink
✅ test: Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Dec 23, 2024
1 parent 0fef7f1 commit 78f21f5
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 66 deletions.
39 changes: 0 additions & 39 deletions src/app/(main)/(mobile)/me/(home)/__tests__/useCategory.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,6 @@ describe('useCategory', () => {

const { result } = renderHook(() => useCategory());

act(() => {
const items = result.current;
expect(items.some((item) => item.key === 'profile')).toBe(false);
expect(items.some((item) => item.key === 'setting')).toBe(true);
expect(items.some((item) => item.key === 'data')).toBe(true);
expect(items.some((item) => item.key === 'docs')).toBe(true);
expect(items.some((item) => item.key === 'feedback')).toBe(true);
expect(items.some((item) => item.key === 'discord')).toBe(true);
});
});

it('should return correct items when the user is logged in with Clerk', () => {
act(() => {
useUserStore.setState({ isSignedIn: true });
});
enableClerk = true;

const { result } = renderHook(() => useCategory());

act(() => {
const items = result.current;
expect(items.some((item) => item.key === 'profile')).toBe(true);
Expand All @@ -80,26 +61,6 @@ describe('useCategory', () => {
});
});

it('should return correct items when the user is logged in with NextAuth', () => {
act(() => {
useUserStore.setState({ isSignedIn: true, enableAuth: () => true, enabledNextAuth: true });
});

const { result } = renderHook(() => useCategory());

act(() => {
const items = result.current;
// Should not render profile for NextAuth, it's Clerk only
expect(items.some((item) => item.key === 'profile')).toBe(false);
expect(items.some((item) => item.key === 'setting')).toBe(true);
expect(items.some((item) => item.key === 'data')).toBe(true);
expect(items.some((item) => item.key === 'docs')).toBe(true);
expect(items.some((item) => item.key === 'feedback')).toBe(true);
expect(items.some((item) => item.key === 'discord')).toBe(true);
expect(items.some((item) => item.key === 'nextauthSignout')).toBe(true);
});
});

it('should return correct items when the user is not logged in', () => {
act(() => {
useUserStore.setState({ isSignedIn: false, enableAuth: () => true });
Expand Down
2 changes: 1 addition & 1 deletion src/app/(main)/(mobile)/me/(home)/features/useCategory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ export const useCategory = () => {
{
type: 'divider',
},
...profile,
...(enableAuth ? (isLoginWithAuth ? settings : []) : settingsWithoutAuth),
...(!enableAuth || (enableAuth && isLoginWithAuth) ? profile : []),
/* ↓ cloud slot ↓ */

/* ↑ cloud slot ↑ */
Expand Down
6 changes: 3 additions & 3 deletions src/features/User/UserPanel/useMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ export const useMenu = () => {
const openSettings = useOpenSettings();
const { t } = useTranslation(['common', 'setting', 'auth']);
const { showCloudPromotion, hideDocs } = useServerConfigStore(featureFlagsSelectors);
const [isLogin, isLoginWithAuth, isLoginWithClerk] = useUserStore((s) => [
const [enableAuth, isLogin, isLoginWithAuth] = useUserStore((s) => [
authSelectors.enabledAuth(s),
authSelectors.isLogin(s),
authSelectors.isLoginWithAuth(s),
authSelectors.isLoginWithClerk(s),
]);

const profile: MenuProps['items'] = [
Expand Down Expand Up @@ -237,7 +237,7 @@ export const useMenu = () => {
type: 'divider',
},
...(isLogin ? settings : []),
...(isLoginWithClerk ? profile : []),
...(!enableAuth || (enableAuth && isLoginWithAuth) ? profile : []),
/* ↓ cloud slot ↓ */

/* ↑ cloud slot ↑ */
Expand Down
27 changes: 4 additions & 23 deletions src/features/User/__tests__/useMenu.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { act, renderHook } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

import { useUserStore } from '@/store/user';
import { ServerConfigStoreProvider } from '@/store/serverConfig';
import { useUserStore } from '@/store/user';

import { useMenu } from '../UserPanel/useMenu';

const wrapper: React.JSXElementConstructor<{ children: React.ReactNode }> = ({ children }) =>
const wrapper: React.JSXElementConstructor<{ children: React.ReactNode }> = ({ children }) => (
<ServerConfigStoreProvider>{children}</ServerConfigStoreProvider>
);

// Mock dependencies
vi.mock('next/link', () => ({
Expand Down Expand Up @@ -75,26 +76,6 @@ describe('useMenu', () => {

const { result } = renderHook(() => useMenu(), { wrapper });

act(() => {
const { mainItems, logoutItems } = result.current;
expect(mainItems?.some((item) => item?.key === 'profile')).toBe(false);
expect(mainItems?.some((item) => item?.key === 'setting')).toBe(true);
expect(mainItems?.some((item) => item?.key === 'import')).toBe(true);
expect(mainItems?.some((item) => item?.key === 'export')).toBe(true);
expect(mainItems?.some((item) => item?.key === 'discord')).toBe(true);
expect(logoutItems.some((item) => item?.key === 'logout')).toBe(true);
});
});

it('should provide correct menu items when user is logged in with Clerk', () => {
act(() => {
useUserStore.setState({ isSignedIn: true });
});
enableAuth = true;
enableClerk = true;

const { result } = renderHook(() => useMenu(), { wrapper });

act(() => {
const { mainItems, logoutItems } = result.current;
expect(mainItems?.some((item) => item?.key === 'profile')).toBe(true);
Expand All @@ -116,7 +97,7 @@ describe('useMenu', () => {

act(() => {
const { mainItems, logoutItems } = result.current;
expect(mainItems?.some((item) => item?.key === 'profile')).toBe(false);
expect(mainItems?.some((item) => item?.key === 'profile')).toBe(true);
expect(mainItems?.some((item) => item?.key === 'setting')).toBe(true);
expect(mainItems?.some((item) => item?.key === 'import')).toBe(true);
expect(mainItems?.some((item) => item?.key === 'export')).toBe(true);
Expand Down

0 comments on commit 78f21f5

Please sign in to comment.