@@ -635,4 +635,82 @@ public function test_once_prop_is_included_in_once_props_by_default(): void
635635 ],
636636 ]);
637637 }
638+
639+ public function test_flash_data_is_flashed_to_session_on_redirect (): void
640+ {
641+ Route::middleware ([StartSession::class, ExampleMiddleware::class])->post ('/flash-test ' , function () {
642+ return Inertia::flash (['message ' => 'Success! ' ])->back ();
643+ });
644+
645+ $ response = $ this ->post ('/flash-test ' , [], [
646+ 'X-Inertia ' => 'true ' ,
647+ ]);
648+
649+ $ response ->assertRedirect ();
650+ $ this ->assertEquals (['message ' => 'Success! ' ], session ('inertia.flash_data ' ));
651+ }
652+
653+ public function test_render_with_flash_includes_flash_in_page (): void
654+ {
655+ Route::middleware ([StartSession::class, ExampleMiddleware::class])->post ('/flash-test ' , function () {
656+ return Inertia::flash ('type ' , 'success ' )
657+ ->render ('User/Edit ' , ['user ' => 'Jonathan ' ])
658+ ->flash (['message ' => 'User updated! ' ]);
659+ });
660+
661+ $ response = $ this ->post ('/flash-test ' , [], [
662+ 'X-Inertia ' => 'true ' ,
663+ ]);
664+
665+ $ response ->assertSuccessful ();
666+ $ response ->assertJson ([
667+ 'component ' => 'User/Edit ' ,
668+ 'props ' => [
669+ 'user ' => 'Jonathan ' ,
670+ ],
671+ 'flash ' => [
672+ 'message ' => 'User updated! ' ,
673+ 'type ' => 'success ' ,
674+ ],
675+ ]);
676+
677+ // Flash data should not persist in session after being included in response
678+ $ this ->assertNull (session ('inertia.flash_data ' ));
679+ }
680+
681+ public function test_render_without_flash_does_not_include_flash_key (): void
682+ {
683+ Route::middleware ([StartSession::class, ExampleMiddleware::class])->get ('/no-flash ' , function () {
684+ return Inertia::render ('User/Edit ' , ['user ' => 'Jonathan ' ]);
685+ });
686+
687+ $ response = $ this ->get ('/no-flash ' , [
688+ 'X-Inertia ' => 'true ' ,
689+ ]);
690+
691+ $ response ->assertSuccessful ();
692+ $ response ->assertJson ([
693+ 'component ' => 'User/Edit ' ,
694+ ]);
695+ $ response ->assertJsonMissing (['flash ' ]);
696+ }
697+
698+ public function test_multiple_flash_calls_are_merged (): void
699+ {
700+ Route::middleware ([StartSession::class, ExampleMiddleware::class])->post ('/create ' , function () {
701+ Inertia::flash ('foo ' , 'value1 ' );
702+ Inertia::flash ('bar ' , 'value2 ' );
703+
704+ return Inertia::render ('User/Show ' );
705+ });
706+
707+ $ response = $ this ->post ('/create ' , [], ['X-Inertia ' => 'true ' ]);
708+
709+ $ response ->assertJson ([
710+ 'flash ' => [
711+ 'foo ' => 'value1 ' ,
712+ 'bar ' => 'value2 ' ,
713+ ],
714+ ]);
715+ }
638716}
0 commit comments