Skip to content

Commit 6b293fa

Browse files
committed
added tests
1 parent b33e579 commit 6b293fa

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

src/GenerateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private function writeControllerFile(Collection $routes, string $namespace): voi
154154

155155
if ($invokable->isEmpty()) {
156156
$exportedMethods = $methods->map(fn (Route $route) => $route->jsMethod());
157-
$reservedMethods = $methods->filter(fn (Route $route) => $route->originalJsMethod() !== $route->jsMethod())->map(fn (Route $route) => $route->originalJsMethod().': '.$route->jsMethod());
157+
$reservedMethods = $methods->filter(fn (Route $route) => $route->originalJsMethod() !== $route->jsMethod())->map(fn (Route $route) => TypeScript::quoteIfNeeded($route->originalJsMethod()).': '.$route->jsMethod());
158158
$exportedMethods = $exportedMethods->merge($reservedMethods);
159159

160160
$methodProps = "const {$defaultExport} = { ";

src/TypeScript.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ public static function safeMethod(string $method, string $suffix): string
7676
return $method;
7777
}
7878

79+
public static function quoteIfNeeded(string $name): string
80+
{
81+
if (is_numeric($name)) {
82+
return $name;
83+
}
84+
85+
if (is_numeric($name[0])) {
86+
return '"'.$name.'"';
87+
}
88+
89+
return $name;
90+
}
91+
7992
public static function cleanUp(string $view): string
8093
{
8194
$replacements = [

tests/DisallowedMethodNames.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import DisallowedMethodNameController, {
33
deleteMethod,
44
method404,
55
} from "../workbench/resources/js/actions/App/Http/Controllers/DisallowedMethodNameController";
6+
import method2fa from "../workbench/resources/js/routes/2fa";
7+
import defaultMethod from "../workbench/resources/js/routes/default";
68
import disallowed from "../workbench/resources/js/routes/disallowed";
79

810
test("will append `method` to invalid methods", () => {
@@ -17,3 +19,15 @@ test("will append `method` to invalid methods", () => {
1719
test("will append `method` to invalid methods", () => {
1820
expect(disallowed[404].url()).toBe("/disallowed/404");
1921
});
22+
23+
test("will properly handle leading numbers", () => {
24+
expect(method2fa.disallowed.url()).toBe("/disallowed/2fa");
25+
expect(DisallowedMethodNameController["2fa"].url()).toBe("/disallowed/2fa");
26+
});
27+
28+
test("will properly handle reserved JS words", () => {
29+
expect(defaultMethod.login.url()).toBe("/disallowed/default");
30+
expect(DisallowedMethodNameController["default"].url()).toBe(
31+
"/disallowed/default",
32+
);
33+
});

workbench/routes/web.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@
6767

6868
Route::get('/disallowed/delete', [DisallowedMethodNameController::class, 'delete']);
6969
Route::get('/disallowed/404', [DisallowedMethodNameController::class, '404'])->name('disallowed.404');
70+
Route::get('/disallowed/2fa', [DisallowedMethodNameController::class, '2fa'])->name('2fa.disallowed');
71+
Route::get('/disallowed/default', [DisallowedMethodNameController::class, 'default'])->name('default.login');
7072

7173
Route::get('/anonymous-middleware', [AnonymousMiddlewareController::class, 'show']);
7274

0 commit comments

Comments
 (0)