refactor: refs #8534 simplify stateQueryGuard usage and improve test structure
gitea/salix-front/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Alex Moreno 2025-03-28 08:06:51 +01:00
parent 5c2c761eb0
commit 7648fc6743
2 changed files with 7 additions and 10 deletions

View File

@ -1,17 +1,15 @@
import { describe, it, expect, vi } from 'vitest'; import { describe, it, expect, vi } from 'vitest';
import { ref, nextTick } from 'vue'; import { ref, nextTick } from 'vue';
import { stateQueryGuard } from 'src/router/hooks'; import { stateQueryGuard } from 'src/router/hooks';
import { __test as testStateQuery } from 'src/stores/useStateQueryStore'; import { useStateQueryStore } from 'src/stores/useStateQueryStore';
vi.mock('src/stores/useStateQueryStore', () => { vi.mock('src/stores/useStateQueryStore', () => {
const isLoading = ref(true); const isLoading = ref(true);
return { return {
useStateQueryStore: () => ({ useStateQueryStore: () => ({
isLoading: () => isLoading, isLoading: () => isLoading,
setLoading: isLoading,
}), }),
__test: {
isLoading,
},
}; };
}); });
@ -21,16 +19,15 @@ describe('hooks', () => {
it('should wait until the state query is not loading and then call next()', async () => { it('should wait until the state query is not loading and then call next()', async () => {
const next = vi.fn(); const next = vi.fn();
const guardPromise = stateQueryGuard(foo, { name: 'bar' }, next); stateQueryGuard(foo, { name: 'bar' }, next);
expect(next).not.toHaveBeenCalled(); expect(next).not.toHaveBeenCalled();
testStateQuery.isLoading.value = false; useStateQueryStore().setLoading.value = false;
await nextTick(); await nextTick();
await guardPromise;
expect(next).toHaveBeenCalled(); expect(next).toHaveBeenCalled();
}); });
it('should ignore if both routes are the same', async () => { it('should ignore if both routes are the same', () => {
const next = vi.fn(); const next = vi.fn();
stateQueryGuard(foo, foo, next); stateQueryGuard(foo, foo, next);
expect(next).toHaveBeenCalled(); expect(next).toHaveBeenCalled();

View File

@ -23,8 +23,8 @@ export { Router };
export default defineRouter(() => { export default defineRouter(() => {
const state = useState(); const state = useState();
Router.beforeEach((to, from, next) => navigationGuard(to, from, next, Router, state)); Router.beforeEach((to, from, next) => navigationGuard(to, from, next, Router, state));
Router.beforeEach((to, from, next) => stateQueryGuard(to, from, next)); Router.beforeEach(stateQueryGuard);
Router.afterEach((to) => setPageTitle(to)); Router.afterEach(setPageTitle);
Router.onError(({ message }) => { Router.onError(({ message }) => {
const errorMessages = [ const errorMessages = [