37 lines
1.4 KiB
TypeScript
37 lines
1.4 KiB
TypeScript
|
|
import { describe, expect, it, vi } from 'vitest';
|
||
|
|
import { HttpException } from '@nestjs/common';
|
||
|
|
|
||
|
|
import { RoutesController } from '../../../src/nest/routes/routes.controller';
|
||
|
|
import type { RoutesService } from '../../../src/nest/routes/routes.service';
|
||
|
|
|
||
|
|
async function thrown(fn: () => Promise<unknown>): Promise<{ status: number; body: unknown }> {
|
||
|
|
try {
|
||
|
|
await fn();
|
||
|
|
} catch (err) {
|
||
|
|
expect(err).toBeInstanceOf(HttpException);
|
||
|
|
const e = err as HttpException;
|
||
|
|
return { status: e.getStatus(), body: e.getResponse() };
|
||
|
|
}
|
||
|
|
throw new Error('expected the handler to throw');
|
||
|
|
}
|
||
|
|
|
||
|
|
describe('RoutesController', () => {
|
||
|
|
it('validates the route plan request body', async () => {
|
||
|
|
const controller = new RoutesController({ plan: vi.fn() } as unknown as RoutesService);
|
||
|
|
|
||
|
|
expect(await thrown(() => controller.plan({ mode: 'driving', points: [{ lat: 1, lng: 2 }] }))).toEqual({
|
||
|
|
status: 400,
|
||
|
|
body: { error: 'Invalid route plan request' },
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
it('returns the service route plan result', async () => {
|
||
|
|
const plan = vi.fn().mockResolvedValue({ supported: false, reason: 'AMAP_UNSUPPORTED_AREA' });
|
||
|
|
|
||
|
|
await expect(new RoutesController({ plan } as unknown as RoutesService).plan({
|
||
|
|
mode: 'walking',
|
||
|
|
points: [{ lat: 39.9, lng: 116.3 }, { lat: 40, lng: 116.4 }],
|
||
|
|
})).resolves.toEqual({ supported: false, reason: 'AMAP_UNSUPPORTED_AREA' });
|
||
|
|
});
|
||
|
|
});
|