|
2 | 2 |
|
3 | 3 | namespace Inertia\Tests;
|
4 | 4 |
|
| 5 | +use Closure; |
| 6 | +use Inertia\Inertia; |
5 | 7 | use Inertia\Middleware;
|
6 | 8 | use Illuminate\Http\Request;
|
| 9 | +use Illuminate\Support\MessageBag; |
7 | 10 | use Illuminate\Support\Facades\App;
|
| 11 | +use Illuminate\Support\ViewErrorBag; |
8 | 12 | use Illuminate\Contracts\Http\Kernel;
|
9 | 13 | use Illuminate\Support\Facades\Blade;
|
10 | 14 | use Illuminate\Support\Facades\Route;
|
| 15 | +use Illuminate\Support\Facades\Session; |
11 | 16 |
|
12 | 17 | class ServiceProviderTest extends TestCase
|
13 | 18 | {
|
@@ -49,4 +54,52 @@ public function test_middleware_is_registered()
|
49 | 54 |
|
50 | 55 | $this->assertTrue($kernel->hasMiddleware(Middleware::class));
|
51 | 56 | }
|
| 57 | + |
| 58 | + public function test_validation_errors_are_registered() |
| 59 | + { |
| 60 | + $this->assertInstanceOf(Closure::class, Inertia::getShared('errors')); |
| 61 | + } |
| 62 | + |
| 63 | + public function test_validation_errors_can_be_empty() |
| 64 | + { |
| 65 | + $errors = Inertia::getShared('errors')(); |
| 66 | + |
| 67 | + $this->assertIsObject($errors); |
| 68 | + $this->assertEmpty(get_object_vars($errors)); |
| 69 | + } |
| 70 | + |
| 71 | + public function test_validation_errors_are_not_registered_when_already_registered() |
| 72 | + { |
| 73 | + Inertia::share('errors', 'This is a validation error'); |
| 74 | + |
| 75 | + $this->assertSame('This is a validation error', Inertia::getShared('errors')); |
| 76 | + } |
| 77 | + |
| 78 | + public function test_validation_errors_are_returned_in_the_correct_format() |
| 79 | + { |
| 80 | + Session::put('errors', (new ViewErrorBag())->put('default', new MessageBag([ |
| 81 | + 'name' => 'The name field is required.', |
| 82 | + 'email' => 'Not a valid email address.', |
| 83 | + ]))); |
| 84 | + |
| 85 | + $errors = Inertia::getShared('errors')(); |
| 86 | + |
| 87 | + $this->assertIsObject($errors); |
| 88 | + $this->assertSame('The name field is required.', $errors->name); |
| 89 | + $this->assertSame('Not a valid email address.', $errors->email); |
| 90 | + } |
| 91 | + |
| 92 | + public function test_validation_errors_with_named_error_bags_are_scoped() |
| 93 | + { |
| 94 | + Session::put('errors', (new ViewErrorBag())->put('example', new MessageBag([ |
| 95 | + 'name' => 'The name field is required.', |
| 96 | + 'email' => 'Not a valid email address.', |
| 97 | + ]))); |
| 98 | + |
| 99 | + $errors = Inertia::getShared('errors')(); |
| 100 | + |
| 101 | + $this->assertIsObject($errors); |
| 102 | + $this->assertSame('The name field is required.', $errors->example->name); |
| 103 | + $this->assertSame('Not a valid email address.', $errors->example->email); |
| 104 | + } |
52 | 105 | }
|
0 commit comments