mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
test: added tests for the koaConsoleRedirectProxy function (#4182)
* test: added tests for the koaConsoleRedirectProxy function * test: added tests for the koaConsoleRedirectProxy function with clearer descriptions
This commit is contained in:
parent
591f78f743
commit
7cda535629
2 changed files with 55 additions and 0 deletions
|
@ -0,0 +1,54 @@
|
|||
import { ossConsolePath } from '@logto/schemas';
|
||||
import type { Next } from 'koa';
|
||||
|
||||
import { MockQueries } from '#src/test-utils/tenant.js';
|
||||
import { createContextWithRouteParameters } from '#src/utils/test-utils.js';
|
||||
|
||||
import koaConsoleRedirectProxy from './koa-console-redirect-proxy.js';
|
||||
|
||||
const { jest } = import.meta;
|
||||
|
||||
const hasActiveUsers = jest.fn();
|
||||
const queries = new MockQueries({
|
||||
users: { hasActiveUsers },
|
||||
});
|
||||
const next: Next = jest.fn(); // Mock the behaviour of next
|
||||
|
||||
// tests for the koaConsoleRedirectProxy function.
|
||||
describe('koaConsoleRedirectProxy()', () => {
|
||||
afterEach(() => {
|
||||
// Performed after each test
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should redirect with 'ossConsolePath/welcome if ctx.path is '/' or '/welcome' AND hasUser is false'", async () => {
|
||||
const ctx = createContextWithRouteParameters({
|
||||
url: '/',
|
||||
});
|
||||
hasActiveUsers.mockResolvedValue(false); // Set a return mocked value for hasActiveUsers
|
||||
|
||||
await koaConsoleRedirectProxy(queries)(ctx, next);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
expect(ctx.redirect).toHaveBeenCalledWith(`${ossConsolePath}/welcome`);
|
||||
});
|
||||
it("should redirect with 'ossConsolePath/welcome if ctx.path is '/' or '/welcome' AND hasUser is true", async () => {
|
||||
const ctx = createContextWithRouteParameters({
|
||||
url: '/',
|
||||
});
|
||||
hasActiveUsers.mockResolvedValue(true); // Set a return mocked value for hasActiveUsers
|
||||
|
||||
await koaConsoleRedirectProxy(queries)(ctx, next);
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
expect(ctx.redirect).toHaveBeenCalledWith(`${ossConsolePath}`);
|
||||
});
|
||||
it("should not redirect if ctx.path is '/some_path' AND hasUser is false", async () => {
|
||||
const ctx = createContextWithRouteParameters({
|
||||
url: '/some_path',
|
||||
});
|
||||
hasActiveUsers.mockResolvedValue(false); // Set a return mocked value for hasActiveUsers
|
||||
|
||||
await koaConsoleRedirectProxy(queries)(ctx, next);
|
||||
expect(next).toHaveBeenCalled();
|
||||
expect(ctx.redirect).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
|
@ -94,6 +94,7 @@ export const createContextWithRouteParameters = (
|
|||
|
||||
return {
|
||||
...ctx,
|
||||
path: ctx.path,
|
||||
URL: ctx.URL,
|
||||
params: {},
|
||||
router: new Router(),
|
||||
|
|
Loading…
Reference in a new issue