Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
"illuminate/filesystem": "^12.0|^13.0",
"illuminate/routing": "^12.0|^13.0",
"illuminate/support": "^12.0|^13.0",
"laravel/ranger": "^0.2.4",
"laravel/surveyor": "^0.2.1",
"laravel/ranger": "dev-main as 0.2.x-dev",
"laravel/surveyor": "dev-template-bindings-via-static-method-dispatch-on-model-subclasses as 0.2.x-dev",
"phpstan/phpdoc-parser": "^2.3"
},
"require-dev": {
"laravel/framework": "12.x-dev|^13.9.0",
"inertiajs/inertia-laravel": "^2.0",
"laravel/pint": "^1.21",
"orchestra/testbench": "^10.1|^11.0"
Expand Down Expand Up @@ -74,5 +75,11 @@
"Laravel\\Wayfinder\\WayfinderServiceProvider"
]
}
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/levikl/surveyor"
}
]
}
24 changes: 10 additions & 14 deletions tests/EloquentProductController.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { readFileSync } from "fs";
import { join } from "path";
import { describe, expect, test } from "vitest";
import { describe, expectTypeOf, test } from "vitest";

describe("EloquentProductController", () => {
const typesPath = join(
__dirname,
"../workbench/resources/js/wayfinder/types.d.ts",
);
import type { App, Inertia } from "../workbench/resources/js/wayfinder/types";

const types = () => readFileSync(typesPath, "utf-8");
type NotAny<T> = 0 extends (1 & T) ? never : T;

describe("EloquentProductController", () => {
test("Index narrows products to App.Models.Product[]", () => {
expect(types()).toContain("{ products: App.Models.Product[] }");
expect(types()).not.toContain(
"products: Illuminate.Database.Eloquent.Collection",
);
type Subject = NotAny<Inertia.Pages.EloquentProducts.Index["products"]>;

expectTypeOf<Subject>().toEqualTypeOf<App.Models.Product[]>();
});

test("Show narrows product to App.Models.Product", () => {
expect(types()).toContain("{ product: App.Models.Product }");
type Subject = NotAny<Inertia.Pages.EloquentProducts.Show["product"]>;

expectTypeOf<Subject>().toEqualTypeOf<App.Models.Product>();
});
});
43 changes: 43 additions & 0 deletions tests/PaginatedEloquentProductController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { describe, expectTypeOf, test } from "vitest";

import type { App, Inertia } from "../workbench/resources/js/wayfinder/types";

type NotAny<T> = 0 extends (1 & T) ? never : T;

describe("PaginatedEloquentProductController", () => {
test("Product::paginate()) emits expected types", () => {
type Subject = Inertia.Pages.PaginatedEloquentProducts.Paginate["products"];

expectTypeOf<NotAny<Subject["data"]>>().toEqualTypeOf< App.Models.Product[] >();

// TODO: add generic return type to laravel/framework LengthAwarePaginator's linkCollection() docblock and this passes
// expectTypeOf<NotAny<Subject["links"]>>().toEqualTypeOf<
// { url: string | null; label: string; active: boolean; page?: number | null }[]
// >();

expectTypeOf<NotAny<Subject["path"]>>().toEqualTypeOf<string | null>();
expectTypeOf<NotAny<Subject["from"]>>().toEqualTypeOf<number | null>();
expectTypeOf<NotAny<Subject["to"]>>().toEqualTypeOf<number | null>();
expectTypeOf<NotAny<Subject["total"]>>().toEqualTypeOf<number>();
expectTypeOf<NotAny<Subject["current_page"]>>().toEqualTypeOf<number>();
expectTypeOf<NotAny<Subject["last_page"]>>().toEqualTypeOf<number>();
expectTypeOf<NotAny<Subject["first_page_url"]>>().toEqualTypeOf<string>();
expectTypeOf<NotAny<Subject["prev_page_url"]>>().toEqualTypeOf<string | null>();
expectTypeOf<NotAny<Subject["next_page_url"]>>().toEqualTypeOf<string | null>();
expectTypeOf<NotAny<Subject["last_page_url"]>>().toEqualTypeOf<string>();
expectTypeOf<NotAny<Subject["total"]>>().toEqualTypeOf<number>();
expectTypeOf<NotAny<Subject["per_page"]>>().toEqualTypeOf<number>();
});

test("simplePaginate narrows products.data to App.Models.Product[]", () => {
type Subject = Inertia.Pages.PaginatedEloquentProducts.SimplePaginate["products"];

expectTypeOf<NotAny<Subject["data"]>>().toEqualTypeOf<App.Models.Product[]>();
});

test("cursorPaginate narrows products.data to App.Models.Product[]", () => {
type Subject = Inertia.Pages.PaginatedEloquentProducts.CursorPaginate["products"];

expectTypeOf<NotAny<Subject["data"]>>().toEqualTypeOf<App.Models.Product[]>();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Controllers;

use App\Models\Product;
use Inertia\Inertia;
use Inertia\Response;

class PaginatedEloquentProductController
{
public function paginate(): Response
{
return Inertia::render('PaginatedEloquentProducts/Paginate', [
'products' => Product::paginate(5),
]);
}

public function simplePaginate(): Response
{
return Inertia::render('PaginatedEloquentProducts/SimplePaginate', [
'products' => Product::simplePaginate(5),
]);
}

public function cursorPaginate(): Response
{
return Inertia::render('PaginatedEloquentProducts/CursorPaginate', [
'products' => Product::cursorPaginate(5),
]);
}
}
5 changes: 5 additions & 0 deletions workbench/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Http\Controllers\NavigationItemController;
use App\Http\Controllers\Nested\NestedController;
use App\Http\Controllers\OptionalController;
use App\Http\Controllers\PaginatedEloquentProductController;
use App\Http\Controllers\ParameterNameController;
use App\Http\Controllers\PostController;
use App\Http\Controllers\Prism\Prism\PrismController as NestedPrismController;
Expand Down Expand Up @@ -50,6 +51,10 @@
Route::get('/eloquent-products', [EloquentProductController::class, 'index'])->name('eloquent-products.index');
Route::get('/eloquent-products/{product}', [EloquentProductController::class, 'show'])->name('eloquent-products.show');

Route::get('/paginated-eloquent-products/paginate', [PaginatedEloquentProductController::class, 'paginate'])->name('paginated-eloquent-products.paginate');
Route::get('/paginated-eloquent-products/simple-paginate', [PaginatedEloquentProductController::class, 'simplePaginate'])->name('paginated-eloquent-products.simple-paginate');
Route::get('/paginated-eloquent-products/cursor-paginate', [PaginatedEloquentProductController::class, 'cursorPaginate'])->name('paginated-eloquent-products.cursor-paginate');

Route::get('/date-time', [DateTimeController::class, 'show'])->name('date-time.show');

Route::get('/dashboard', function () {
Expand Down
Loading