refactor: refs #8534 simplify stateQueryGuard usage and improve test structure
gitea/salix-front/pipeline/pr-dev This commit looks good
Details
gitea/salix-front/pipeline/pr-dev This commit looks good
Details
This commit is contained in:
parent
5c2c761eb0
commit
7648fc6743
|
@ -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();
|
||||||
|
|
|
@ -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 = [
|
||||||
|
|
Loading…
Reference in New Issue