I have a project started with Laravel Jetstream, and when running a test on a Livewire component that uses $request->session()
, I get an error message:
Illuminate\View\ViewException : Session store not set on request. (View: /var/www/html/vendor/livewire/livewire/src/views/mount-component.blade.php)
It's happening on two Livewire components that use the session, but for reference, here's the smaller component. Front end:
<div class="m-6">
<x-range-slider :options="$options" wire:model="range.min,range.max" wire:click="onChange" />
</div>
And here's the back end:
class TierSlider extends Component
{
public array $options = [
"start" => [1, 4],
"step" => 1,
"range" => [
"min" => [1],
"max" => [4],
],
"connect" => true,
"behaviour" => "tap-drag",
"pips" => [
"mode" => "values",
"stepped" => true,
"density" => 100,
"values" => [1, 2, 3, 4],
],
];
public array $range;
public int $armorId;
public function render()
{
return view("livewire.tier-slider");
}
public function mount(Request $request)
{
$armorAndTiers = $request
->session()
->get("armors.{$this->armorId}", ["minTier" => 1, "maxTier" => 4]);
$this->options["start"] = array_values($armorAndTiers);
$this->range = [
"min" => strval($armorAndTiers["minTier"]),
"max" => strval($armorAndTiers["maxTier"]),
];
}
public function onChange()
{
$this->emit("updateShoppingList", [
$this->armorId => [
"minTier" => intval($this->range["min"]),
"maxTier" => intval($this->range["max"]),
],
]);
}
}
I could probably make this work by using the global helper session()
rather than $request->session()
, but it kinda seems like the Laravel docs don't encourage that. Also, the app works fine as is; it's only during testing that I get this error. Speaking of which, here's the beginning of my test class:
class TierSliderTest extends TestCase
{
/** @test */
public function the_component_can_render()
{
$component = Livewire::test(TierSlider::class, ["armorId" => 4]);
$component->assertStatus(200);
}
}
So what do I need to do to avoid this error during testing?