salix-front/src/router/__tests__/hooks.spec.js

37 lines
1.1 KiB
JavaScript

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