2022-06-09 11:42:52 +08:00
|
|
|
import { MountedApps } from '@/env-set';
|
|
|
|
import { hasActiveUsers } from '@/queries/user';
|
|
|
|
import { createContextWithRouteParameters } from '@/utils/test-utils';
|
|
|
|
|
|
|
|
import koaWelcomeProxy from './koa-welcome-proxy';
|
|
|
|
|
|
|
|
jest.mock('@/queries/user', () => ({
|
|
|
|
hasActiveUsers: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('koaWelcomeProxy', () => {
|
|
|
|
const next = jest.fn();
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should redirect to admin console if has AdminUsers', async () => {
|
|
|
|
(hasActiveUsers as jest.Mock).mockResolvedValue(true);
|
|
|
|
const ctx = createContextWithRouteParameters({
|
|
|
|
url: `/${MountedApps.Welcome}`,
|
|
|
|
});
|
|
|
|
|
|
|
|
await koaWelcomeProxy()(ctx, next);
|
|
|
|
|
|
|
|
expect(ctx.redirect).toBeCalledWith(`/${MountedApps.Console}`);
|
|
|
|
expect(next).not.toBeCalled();
|
|
|
|
});
|
|
|
|
|
2022-06-20 10:58:27 +08:00
|
|
|
it('should redirect to welcome page if has no Users', async () => {
|
2022-06-09 11:42:52 +08:00
|
|
|
(hasActiveUsers as jest.Mock).mockResolvedValue(false);
|
|
|
|
const ctx = createContextWithRouteParameters({
|
|
|
|
url: `/${MountedApps.Welcome}`,
|
|
|
|
});
|
|
|
|
|
|
|
|
await koaWelcomeProxy()(ctx, next);
|
2022-06-20 10:58:27 +08:00
|
|
|
expect(ctx.redirect).toBeCalledWith(`/${MountedApps.Console}/welcome`);
|
2022-06-09 11:42:52 +08:00
|
|
|
expect(next).not.toBeCalled();
|
|
|
|
});
|
|
|
|
});
|