Skip to content

Commit

Permalink
✅ test: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed May 3, 2024
1 parent 362b7c3 commit cd3a587
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 2 deletions.
55 changes: 55 additions & 0 deletions src/app/(main)/@nav/_layout/Desktop/Avatar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { act, fireEvent, render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

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

import Avatar from './Avatar';

// Mock UserAvatar and UserPanel components
vi.mock('@/features/User/UserAvatar', () => ({
default: vi.fn(() => <div>Mocked UserAvatar</div>),
}));

vi.mock('@/features/User/UserPanel', () => ({
default: vi.fn(({ children }) => <div>Mocked UserPanel {children}</div>),
}));

beforeEach(() => {
vi.clearAllMocks();
});

describe('Avatar', () => {
it('should render UserAvatar and UserPanel when hideSettingsMoveGuide is true', () => {
render(<Avatar />);

expect(screen.getByText('Mocked UserPanel')).toBeInTheDocument();
expect(screen.getByText('Mocked UserAvatar')).toBeInTheDocument();
expect(screen.queryByText('userPanel.moveGuide')).not.toBeInTheDocument();
});

it('should render Tooltip with guide content when hideSettingsMoveGuide is false', () => {
act(() => {
useUserStore.getState().updateGuideState({ moveSettingsToAvatar: false });
});

render(<Avatar />);

expect(screen.getByText('userPanel.moveGuide')).toBeInTheDocument();
expect(screen.getByText('Mocked UserPanel')).toBeInTheDocument();
expect(screen.getByText('Mocked UserAvatar')).toBeInTheDocument();
});

it('should call updateGuideState when close icon is clicked in Tooltip', () => {
const updateGuideStateMock = vi.fn();
act(() => {
useUserStore.getState().updateGuideState({ moveSettingsToAvatar: false });
useUserStore.setState({ updateGuideState: updateGuideStateMock });
});

render(<Avatar />);

fireEvent.click(screen.getByRole('close-guide'));

expect(updateGuideStateMock).toHaveBeenCalledWith({ moveSettingsToAvatar: true });
});
});
42 changes: 40 additions & 2 deletions src/store/user/slices/preference/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { act, renderHook, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { withSWR } from '~test-utils';

import { globalService } from '@/services/global';
import { useUserStore } from '@/store/user';

import { type Guide } from './initialState';
import { DEFAULT_PREFERENCE, type Guide, UserPreference } from './initialState';

beforeEach(() => {
vi.clearAllMocks();
Expand Down Expand Up @@ -58,5 +57,44 @@ describe('createPreferenceSlice', () => {
expect(result.current.isPreferenceInit).toBeTruthy();
});
});
it('should return default preference when local storage is empty', async () => {
const { result } = renderHook(() => useUserStore());

vi.spyOn(result.current.preferenceStorage, 'getFromLocalStorage').mockResolvedValueOnce(
{} as any,
);

renderHook(() => result.current.useInitPreference(), {
wrapper: withSWR,
});

await waitFor(() => {
expect(result.current.preference).toEqual(DEFAULT_PREFERENCE);
expect(result.current.isPreferenceInit).toBeTruthy();
});
});

it('should return saved preference when local storage has data', async () => {
const { result } = renderHook(() => useUserStore());
const savedPreference: UserPreference = {
...DEFAULT_PREFERENCE,
hideSyncAlert: true,
guide: { topic: false, moveSettingsToAvatar: true },
};

vi.spyOn(result.current.preferenceStorage, 'getFromLocalStorage').mockResolvedValueOnce(
savedPreference,
);

const { result: prefernce } = renderHook(() => result.current.useInitPreference(), {
wrapper: withSWR,
});

await waitFor(() => {
expect(prefernce.current.data).toEqual(savedPreference);
expect(result.current.isPreferenceInit).toBeTruthy();
expect(result.current.preference).toEqual(savedPreference);
});
});
});
});
82 changes: 82 additions & 0 deletions src/store/user/slices/preference/selectors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { describe, expect, it } from 'vitest';

import { UserStore } from '@/store/user';

import { initialPreferenceState } from './initialState';
import { preferenceSelectors } from './selectors';

describe('preferenceSelectors', () => {
let store: UserStore;

beforeEach(() => {
store = {
...initialPreferenceState,
} as unknown as UserStore;
});

describe('useCmdEnterToSend', () => {
it('should return the value of useCmdEnterToSend preference', () => {
store.preference.useCmdEnterToSend = true;
expect(preferenceSelectors.useCmdEnterToSend(store)).toBe(true);

store.preference.useCmdEnterToSend = false;
expect(preferenceSelectors.useCmdEnterToSend(store)).toBe(false);
});

it('should return false if useCmdEnterToSend preference is undefined', () => {
store.preference.useCmdEnterToSend = undefined;
expect(preferenceSelectors.useCmdEnterToSend(store)).toBe(false);
});
});

describe('userAllowTrace', () => {
it('should return the value of telemetry preference', () => {
store.preference.telemetry = true;
expect(preferenceSelectors.userAllowTrace(store)).toBe(true);

store.preference.telemetry = false;
expect(preferenceSelectors.userAllowTrace(store)).toBe(false);

store.preference.telemetry = null;
expect(preferenceSelectors.userAllowTrace(store)).toBe(null);
});
});

describe('hideSyncAlert', () => {
it('should return the value of hideSyncAlert preference', () => {
store.preference.hideSyncAlert = true;
expect(preferenceSelectors.hideSyncAlert(store)).toBe(true);

store.preference.hideSyncAlert = false;
expect(preferenceSelectors.hideSyncAlert(store)).toBe(false);

store.preference.hideSyncAlert = undefined;
expect(preferenceSelectors.hideSyncAlert(store)).toBeUndefined();
});
});

describe('hideSettingsMoveGuide', () => {
it('should return the value of moveSettingsToAvatar guide preference', () => {
store.preference.guide = { moveSettingsToAvatar: true };
expect(preferenceSelectors.hideSettingsMoveGuide(store)).toBe(true);

store.preference.guide = { moveSettingsToAvatar: false };
expect(preferenceSelectors.hideSettingsMoveGuide(store)).toBe(false);
});

it('should return undefined if guide preference is undefined', () => {
store.preference.guide = undefined;
expect(preferenceSelectors.hideSettingsMoveGuide(store)).toBeUndefined();
});
});

describe('isPreferenceInit', () => {
it('should return the value of isPreferenceInit state', () => {
store.isPreferenceInit = true;
expect(preferenceSelectors.isPreferenceInit(store)).toBe(true);

store.isPreferenceInit = false;
expect(preferenceSelectors.isPreferenceInit(store)).toBe(false);
});
});
});

0 comments on commit cd3a587

Please sign in to comment.