Refactor to signel use route resolving method

This commit is contained in:
Fayaz Ahmed
2024-08-28 01:10:05 +05:30
parent deeb580fee
commit 4b5880eb30
10 changed files with 84 additions and 97 deletions
@@ -0,0 +1,51 @@
import { describe, it, expect, vi } from 'vitest';
import { useReplaceRoute } from '../useReplaceRoute';
vi.mock('dashboard/composables/route', () => ({
useRouter: vi.fn(),
useRoute: vi.fn(),
}));
import { useRouter, useRoute } from 'dashboard/composables/route';
describe('useReplaceRoute', () => {
const mockReplace = vi.fn();
const mockRouter = { replace: mockReplace };
beforeEach(() => {
vi.clearAllMocks();
useRouter.mockReturnValue(mockRouter);
});
it('should replace route when current route is different', () => {
useRoute.mockReturnValue({ name: 'currentRoute' });
const replaceRoute = useReplaceRoute();
replaceRoute('newRoute', { id: 1 });
expect(mockReplace).toHaveBeenCalledWith({
name: 'newRoute',
params: { id: 1 },
});
});
it('should not replace route when current route is the same', () => {
useRoute.mockReturnValue({ name: 'sameRoute' });
const replaceRoute = useReplaceRoute();
const result = replaceRoute('sameRoute');
expect(mockReplace).not.toHaveBeenCalled();
expect(result).toBeUndefined();
});
it('should return the result of router.replace when replacing route', () => {
useRoute.mockReturnValue({ name: 'currentRoute' });
mockReplace.mockReturnValue('replacementResult');
const replaceRoute = useReplaceRoute();
const result = replaceRoute('newRoute');
expect(result).toBe('replacementResult');
});
});
@@ -1,59 +0,0 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { useRouterHelper } from '../useRouterHelper';
const mockRouter = {
currentRoute: { name: 'initialRoute' },
replace: vi.fn(),
};
vi.mock('../../composables/useRouterHelper', () => ({
useRouterHelper: () => ({
replaceRoute: async (name, params = {}) => {
if (mockRouter.currentRoute.name !== name) {
return mockRouter.replace({ name, params });
}
return undefined;
},
}),
}));
describe('useRouterHelper', () => {
let routerHelper;
beforeEach(() => {
vi.resetAllMocks();
mockRouter.currentRoute = { name: 'initialRoute' };
routerHelper = useRouterHelper();
});
describe('replaceRoute', () => {
it('should replace route when current route is different', async () => {
mockRouter.replace.mockResolvedValue(undefined);
await routerHelper.replaceRoute('newRoute', { id: 1 });
expect(mockRouter.replace).toHaveBeenCalledWith({
name: 'newRoute',
params: { id: 1 },
});
});
it('should not replace route when current route is the same', async () => {
mockRouter.currentRoute.name = 'sameRoute';
const result = await routerHelper.replaceRoute('sameRoute');
expect(mockRouter.replace).not.toHaveBeenCalled();
expect(result).toBeUndefined();
});
it('should handle router replace rejection', async () => {
const error = new Error('Navigation aborted');
mockRouter.replace.mockRejectedValue(error);
await expect(routerHelper.replaceRoute('newRoute')).rejects.toThrow(
'Navigation aborted'
);
});
});
});
@@ -0,0 +1,21 @@
import { useRouter, useRoute } from 'dashboard/composables/route';
/**
* Composable for replacing the current route with a new one if it's different.
* @param {string} name - The name of the route to replace with.
* @param {Object} params - The params to pass to the new route.
* @returns {Function} A function that replaces the route when called.
*/
export const useReplaceRoute = () => {
const router = useRouter();
const route = useRoute();
return (name, params = {}) => {
if (route.name !== name) {
return router.replace({ name, params });
}
return undefined;
};
};
export default useReplaceRoute;
@@ -1,26 +0,0 @@
import { useRouter, useRoute } from 'dashboard/composables/route';
/**
* Composable for handling router-related operations.
* @returns {Object} An object containing methods for router manipulation.
*/
export function useRouterHelper() {
const router = useRouter();
const route = useRoute();
/**
* Replaces the current route with a new one if it's different.
* @param {string} name - The name of the route to replace with.
* @param {Object} params - The params to pass to the new route.
* @returns {Promise|undefined} A promise that resolves when the navigation is complete, or undefined if no navigation occurs.
*/
const replaceRoute = async (name, params = {}) => {
if (route.name !== name) {
return router.replace({ name, params });
}
return undefined;
};
return {
replaceRoute,
};
}