Skip to content

Commit

Permalink
Slot method on view class is not working (#2341)
Browse files Browse the repository at this point in the history
* Added tests for all view macro functions and added new setSlot method to set a default of a component slot

* Removed dubble underscore

* Added withoutExceptionHandling to see why the test is failing

Signed-off-by: Gertjan Roke <gertjan@2solar.nl>

* Added tests to test all view functions

Signed-off-by: Gertjan Roke <g.a.roke90@gmail.com>

* Removed setSlot method because it should not be in this branch

Signed-off-by: Gertjan Roke <g.a.roke90@gmail.com>

* Fixed the failing test for the slot method

Signed-off-by: Gertjan Roke <g.a.roke90@gmail.com>

Co-authored-by: Gertjan Roke <gertjan@2solar.nl>
  • Loading branch information
GertjanRoke and Gertjan Roke committed Jan 20, 2021
1 parent 8663232 commit b430180
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __invoke(Container $container, Route $route)
'view' => $this->initialLayoutConfiguration['view'] ?? config('livewire.layout', 'layouts.app'),
'params' => $this->initialLayoutConfiguration['params'] ?? [],
'slotOrSection' => $this->initialLayoutConfiguration['slotOrSection'] ?? [
'extends' => 'content', 'component' => 'default',
'extends' => 'content', 'component' => 'slot',
][$layoutType],
'manager' => $manager,
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Macros/ViewMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function layout()
return function ($view, $params = []) {
$this->livewireLayout = [
'type' => 'component',
'slotOrSection' => 'default',
'slotOrSection' => 'slot',
'view' => $view,
'params' => $params,
];
Expand Down
4 changes: 3 additions & 1 deletion src/Macros/livewire-view-component.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@component($view, $params)
{!! $manager->initialDehydrate()->toInitialResponse()->effects['html']; !!}
@slot($slotOrSection)
{!! $manager->initialDehydrate()->toInitialResponse()->effects['html']; !!}
@endslot
@endcomponent
90 changes: 90 additions & 0 deletions tests/Unit/ComponentLayoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Tests\Unit;

use Illuminate\Support\Facades\Route;
use Livewire\Component;
use Livewire\Livewire;

class ComponentLayoutTest extends TestCase
{
/** @test */
public function can_extend_a_blade_layout()
{
Livewire::component(ComponentWithExtendsLayout::class);

Route::get('/foo', ComponentWithExtendsLayout::class);

$this->get('/foo')->assertSee('baz');
}

/** @test */
public function can_set_custom_section()
{
Livewire::component(ComponentWithCustomSection::class);

Route::get('/foo', ComponentWithCustomSection::class);

$this->get('/foo')->assertSee('baz');
}

/** @test */
public function can_set_custom_layout()
{
Livewire::component(ComponentWithCustomLayout::class);

Route::get('/foo', ComponentWithCustomLayout::class);

$this->get('/foo')->assertSee('baz');
}

/** @test */
public function can_set_custom_slot_for_a_layout()
{
Livewire::component(ComponentWithCustomSlotForLayout::class);

Route::get('/foo', ComponentWithCustomSlotForLayout::class);

$this->withoutExceptionHandling()->get('/foo')->assertSee('baz');
}
}

class ComponentWithExtendsLayout extends Component
{
public function render()
{
return view('null-view')->extends('layouts.app-extends', [
'bar' => 'baz'
]);
}
}

class ComponentWithCustomSection extends Component
{
public $name = 'baz';

public function render()
{
return view('show-name')->extends('layouts.app-custom-section')->section('body');
}
}

class ComponentWithCustomLayout extends Component
{
public function render()
{
return view('null-view')->layout('layouts.app-with-bar', [
'bar' => 'baz'
]);
}
}

class ComponentWithCustomSlotForLayout extends Component
{
public $name = 'baz';

public function render()
{
return view('show-name')->layout('layouts.app-custom-slot')->slot('main');
}
}
1 change: 1 addition & 0 deletions tests/Unit/views/layouts/app-custom-section.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@yield('body')
1 change: 1 addition & 0 deletions tests/Unit/views/layouts/app-custom-slot.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ $main }}
3 changes: 3 additions & 0 deletions tests/Unit/views/layouts/app-extends.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@yield('content')

{{ $bar }}

0 comments on commit b430180

Please sign in to comment.