Skip to content

Commit

Permalink
Test adding paths
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Jan 23, 2025
1 parent 4046fb0 commit dea8d75
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/Hotreload.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

class Hotreload
{
private static $htmlPaths = [];
private static $stimulusPaths = [];
private static $cssPaths = [];
protected static $htmlPaths = [];
protected static $stimulusPaths = [];
protected static $cssPaths = [];

public static function withInotifyWatcher(): void
{
Expand Down Expand Up @@ -45,24 +45,17 @@ public static function addCssPath(string $path): void

public static function htmlPaths(): array
{
return array_values(array_filter(array_merge([
resource_path('views/'),
], static::$htmlPaths), 'is_dir'));
return array_values(array_merge(static::defaultPaths('html'), static::$htmlPaths));
}

public static function stimulusPaths(): array
{
return array_values(array_filter(array_merge([
resource_path('js/controllers/'),
], static::$stimulusPaths), 'is_dir'));
return array_values(array_merge(static::defaultPaths('stimulus'), static::$stimulusPaths));
}

public static function cssPaths(): array
{
return array_values(array_filter(array_merge([
resource_path('css/'),
public_path('dist/css/'),
], static::$cssPaths), 'is_dir'));
return array_values(array_merge(static::defaultPaths('css'), static::$cssPaths));
}

public static function watchers(): FileWatchers
Expand All @@ -83,6 +76,29 @@ public static function watchers(): FileWatchers
]);
}

public static function resetPaths(): void
{
static::$cssPaths = static::defaultPaths('css');
static::$stimulusPaths = static::defaultPaths('stimulus');
static::$htmlPaths = static::defaultPaths('html');
}

protected static function defaultPaths(string $type): array
{
return [
'html' => array_values(array_filter([
resource_path('views/'),
], 'is_dir')),
'css' => array_values(array_filter([
resource_path('css/'),
public_path('dist/css/'),
], 'is_dir')),
'stimulus' => array_values(array_filter([
resource_path('js/controllers/'),
], 'is_dir')),
][$type];
}

protected static function watcherFor(string $path, Closure $onChange): FileWatcher
{
if (config('hotwire-hotreload.watcher') === 'inotify' && ! extension_loaded('inotify')) {
Expand Down
3 changes: 3 additions & 0 deletions tests/BrowserTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use Exception;
use HotwiredLaravel\Hotreload\Hotreload;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Sleep;
Expand Down Expand Up @@ -39,6 +40,8 @@ public static function setUpBeforeClass(): void
#[Override]
protected function setUp(): void
{
Hotreload::resetPaths();

$this->afterApplicationCreated(function () {
$this->clearViews();
});
Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/HotreloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,26 @@ public function can_configure_the_watcher(): void

$this->assertInstanceOf(SimpleFileWatcher::class, collect(Hotreload::watchers()->all())->first());
}

#[Test]
public function can_add_paths(): void
{
$this->assertNotContains(__DIR__, Hotreload::htmlPaths());

Hotreload::addHtmlPath(__DIR__);

$this->assertContains(__DIR__, Hotreload::htmlPaths());

$this->assertNotContains(__DIR__, Hotreload::stimulusPaths());

Hotreload::addStimulusPath(__DIR__);

$this->assertContains(__DIR__, Hotreload::stimulusPaths());

$this->assertNotContains(__DIR__, Hotreload::cssPaths());

Hotreload::addCssPath(__DIR__);

$this->assertContains(__DIR__, Hotreload::cssPaths());
}
}
10 changes: 10 additions & 0 deletions tests/UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

namespace Tests;

use HotwiredLaravel\Hotreload\Hotreload;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase as Orchestra;
use Override;

abstract class UnitTestCase extends Orchestra
{
use RefreshDatabase;
use WithWorkbench;

#[Override]
protected function setUp(): void
{
parent::setUp();

Hotreload::resetPaths();
}
}

0 comments on commit dea8d75

Please sign in to comment.