Skip to content

Commit

Permalink
Add withoutLazyLoading() testing helper (#8326)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio committed Apr 18, 2024
1 parent 470ecbc commit 01a4ee8
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
30 changes: 30 additions & 0 deletions docs/lazy.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,33 @@ If you want to set a default placeholder view for all your components you can do
```

Now, when a component is lazy-loaded and no `placeholder()` is defined, Livewire will use the configured Blade view (`livewire.placeholder` in this case.)

## Disabling lazy loading for tests

When unit testing a lazy component, or a page with nested lazy components, you may want to disable the "lazy" behavior so that you can assert the final rendered behavior. Otherwise, those components would be rendered as their placeholders during your tests.

You can easily disable lazy loading using the `Livewire::withoutLazyLoading()` testing helper like so:

```php
<?php

namespace Tests\Feature\Livewire;

use App\Livewire\Dashboard;
use Livewire\Livewire;
use Tests\TestCase;

class DashboardTest extends TestCase
{
/** @test */
public function renders_successfully()
{
Livewire::withoutLazyLoading() // [tl! highlight]
->test(Dashboard::class)
->assertSee(...);
}
}
```

Now, when the dashboard component is rendered for this test, it will skip rendering the `placeholder()` and instead render the full component as if lazy loading wasn't applied at all.

1 change: 1 addition & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ Livewire provides many more testing utilities. Below is a comprehensive list of
| `Livewire::withCookie('color', 'blue')` | Set the test's `color` cookie to the provided value (`blue`). |
| `Livewire::withCookies(['color' => 'blue', 'name' => 'Taylor])` | Set the test's `color` and `name` cookies to the provided values (`blue`, `Taylor`). |
| `Livewire::withHeaders(['X-COLOR' => 'blue', 'X-NAME' => 'Taylor])` | Set the test's `X-COLOR` and `X-NAME` headers to the provided values (`blue`, `Taylor`). |
| `Livewire::withoutLazyLoading()` | Disable lazy loading in this and all child components under test. |


### Interacting with components
Expand Down
15 changes: 14 additions & 1 deletion src/Features/SupportLazyLoading/SupportLazyLoading.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@

use Livewire\Features\SupportLifecycleHooks\SupportLifecycleHooks;
use Livewire\Mechanisms\HandleComponents\ViewContext;
use function Livewire\{ store, trigger, wrap };
use function Livewire\{ on, store, trigger, wrap };
use Illuminate\Routing\Route;
use Livewire\ComponentHook;
use Livewire\Drawer\Utils;
use Livewire\Component;

class SupportLazyLoading extends ComponentHook
{
static $disableWhileTesting = false;

static function disableWhileTesting()
{
static::$disableWhileTesting = true;
}

static function provide()
{
static::registerRouteMacro();

on('flush-state', function () {
static::$disableWhileTesting = false;
});
}

static function registerRouteMacro()
Expand All @@ -35,6 +46,8 @@ public function mount($params)
$reflectionClass = new \ReflectionClass($this->component);
$lazyAttribute = $reflectionClass->getAttributes(\Livewire\Attributes\Lazy::class)[0] ?? null;

// If Livewire::withoutLazyLoading()...
if (static::$disableWhileTesting) return;
// If `:lazy="false"` disable lazy loading...
if ($hasLazyParam && ! $lazyProperty) return;
// If no lazy loading is included at all...
Expand Down
31 changes: 31 additions & 0 deletions src/Features/SupportLazyLoading/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ public function can_lazy_load_component_with_custom_layout()
$this->get('/two')->assertSee('This is a custom layout');
$this->get('/three')->assertSee('This is a custom layout');
}

/** @test */
public function can_disable_lazy_loading_during_unit_tests()
{
Livewire::component('lazy-component', BasicLazyComponent::class);

Livewire::withoutLazyLoading()->test(new class extends Component {
public function render()
{
return <<<'HTML'
<div>
<livewire:lazy-component />
</div>
HTML;
}
})
->assertDontSee('Loading...')
->assertSee('Hello world!');
}
}

#[Lazy]
class BasicLazyComponent extends Component {
public function placeholder() {
return '<div>Loading...</div>';
}

public function render()
{
return '<div>Hello world!</div>';
}
}

#[Layout('components.layouts.custom'), Lazy]
Expand Down
10 changes: 9 additions & 1 deletion src/LivewireManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Livewire\Features\SupportTesting\Testable;
use Livewire\Features\SupportTesting\DuskTestable;
use Livewire\Features\SupportAutoInjectedAssets\SupportAutoInjectedAssets;
use Livewire\Features\SupportLazyLoading\SupportLazyLoading;

class LivewireManager
{
Expand Down Expand Up @@ -179,14 +180,21 @@ function withHeaders($headers)
return $this;
}

function withoutLazyLoading()
{
SupportLazyLoading::disableWhileTesting();

return $this;
}

function test($name, $params = [])
{
return Testable::create(
$name,
$params,
$this->queryParamsForTesting,
$this->cookiesForTesting,
$this->headersForTesting
$this->headersForTesting,
);
}

Expand Down

0 comments on commit 01a4ee8

Please sign in to comment.