From f1f2f943fdc96c27453048ae6afbf48f582cfee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20P=C3=B6hls?= Date: Thu, 16 Nov 2023 05:48:14 +0100 Subject: [PATCH] add test for route service provider --- packages/core/test/fixtures/routes/web.js | 2 + packages/core/test/route-service-provider.js | 56 ++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 packages/core/test/fixtures/routes/web.js create mode 100644 packages/core/test/route-service-provider.js diff --git a/packages/core/test/fixtures/routes/web.js b/packages/core/test/fixtures/routes/web.js new file mode 100644 index 00000000..93065a43 --- /dev/null +++ b/packages/core/test/fixtures/routes/web.js @@ -0,0 +1,2 @@ + +globalThis.valueSetByRouteServiceProvider = 'Supercharge' diff --git a/packages/core/test/route-service-provider.js b/packages/core/test/route-service-provider.js new file mode 100644 index 00000000..89b6bbab --- /dev/null +++ b/packages/core/test/route-service-provider.js @@ -0,0 +1,56 @@ + +import { test } from 'uvu' +import Path from 'node:path' +import { expect } from 'expect' +import { fileURLToPath } from 'node:url' +import { HttpKernel, Application, ErrorHandler, RouteServiceProvider as BaseRouteServiceProvider } from '../dist/index.js' + +const __dirname = Path.dirname(fileURLToPath(import.meta.url)) +const appRootPath = Path.resolve(__dirname, './fixtures') + +class RouteServiceProvider extends BaseRouteServiceProvider { + /** + * Boot the service provider. + */ + async boot () { + this.loadRoutesFrom( + this.app().resolveGlobFromBasePath('routes/web.**') + ) + } +} + +let app = createApp() + +function createApp () { + const app = Application + .createWithAppRoot(appRootPath) + .withErrorHandler(ErrorHandler) + .bind('view', () => { + // empty view mock + }) + + app.register( + new RouteServiceProvider(app) + ) + + app.config().set('app.key', 1234) + app.config().set('http', { + host: 'localhost', + port: 1234, + cookie: {} + }) + + return app +} + +test.before.each(() => { + app = createApp() +}) + +test('boot route service provider', async () => { + const kernel = new HttpKernel(app) + await kernel.prepare() + expect(globalThis.valueSetByRouteServiceProvider).toEqual('Supercharge') +}) + +test.run()