|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Hyde\Framework\Testing\Unit\Views; |
| 4 | + |
| 5 | +use Hyde\Framework\Hyde; |
| 6 | +use Hyde\Framework\Services\AssetService; |
| 7 | +use Hyde\Testing\TestCase; |
| 8 | +use Illuminate\Support\Facades\Blade; |
| 9 | + |
| 10 | +/** |
| 11 | + * @see resources/views/layouts/scripts.blade.php |
| 12 | + */ |
| 13 | +class ScriptsComponentViewTest extends TestCase |
| 14 | +{ |
| 15 | + protected ?string $mockCurrentPage = null; |
| 16 | + |
| 17 | + protected function renderTestView(): string |
| 18 | + { |
| 19 | + view()->share('currentPage', $this->mockCurrentPage ?? ''); |
| 20 | + |
| 21 | + return Blade::render(file_get_contents( |
| 22 | + Hyde::vendorPath('resources/views/layouts/scripts.blade.php') |
| 23 | + )); |
| 24 | + } |
| 25 | + |
| 26 | + public function test_component_can_be_rendered() |
| 27 | + { |
| 28 | + $this->assertStringContainsString('<script defer', $this->renderTestView()); |
| 29 | + } |
| 30 | + |
| 31 | + public function test_component_has_link_to_app_js_file_when_it_exists() |
| 32 | + { |
| 33 | + touch(Hyde::path('_media/app.js')); |
| 34 | + $this->assertStringContainsString('<script defer src="media/app.js"', $this->renderTestView()); |
| 35 | + unlink(Hyde::path('_media/app.js')); |
| 36 | + } |
| 37 | + |
| 38 | + public function test_component_does_not_render_link_to_app_js_when_it_does_not_exist() |
| 39 | + { |
| 40 | + $this->assertStringNotContainsString('<script defer src="media/app.js"', $this->renderTestView()); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_component_uses_relative_path_to_app_js_file_for_nested_pages() |
| 44 | + { |
| 45 | + touch(Hyde::path('_media/app.js')); |
| 46 | + $this->mockCurrentPage = 'foo'; |
| 47 | + $this->assertStringContainsString('<script defer src="media/app.js"', $this->renderTestView()); |
| 48 | + $this->mockCurrentPage = 'foo/bar'; |
| 49 | + $this->assertStringContainsString('<script defer src="../media/app.js"', $this->renderTestView()); |
| 50 | + $this->mockCurrentPage = 'foo/bar/cat.html'; |
| 51 | + $this->assertStringContainsString('<script defer src="../../media/app.js"', $this->renderTestView()); |
| 52 | + $this->mockCurrentPage = null; |
| 53 | + unlink(Hyde::path('_media/app.js')); |
| 54 | + } |
| 55 | + |
| 56 | + public function test_scripts_can_be_pushed_to_the_component_scripts_stack() |
| 57 | + { |
| 58 | + view()->share('currentPage', ''); |
| 59 | + |
| 60 | + $this->assertStringContainsString('foo bar', |
| 61 | + Blade::render(' |
| 62 | + @push("scripts") |
| 63 | + foo bar |
| 64 | + @endpush |
| 65 | + |
| 66 | + @include("hyde::layouts.scripts")' |
| 67 | + ) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + public function test_component_renders_link_to_hyde_js_when_it_exists() |
| 72 | + { |
| 73 | + touch(Hyde::path('_media/hyde.js')); |
| 74 | + $this->assertStringContainsString('<script defer src="media/hyde.js"', $this->renderTestView()); |
| 75 | + unlink(Hyde::path('_media/hyde.js')); |
| 76 | + } |
| 77 | + |
| 78 | + public function test_component_does_not_render_link_to_hyde_js_when_it_does_not_exist() |
| 79 | + { |
| 80 | + $this->assertStringNotContainsString('<script defer src="media/hyde.js"', $this->renderTestView()); |
| 81 | + } |
| 82 | + |
| 83 | + public function test_component_renders_cdn_link_when_no_local_file_exists() |
| 84 | + { |
| 85 | + $this->assertStringContainsString('https://cdn.jsdelivr.net/npm/hydefront', $this->renderTestView()); |
| 86 | + } |
| 87 | + |
| 88 | + public function test_component_does_not_render_cdn_link_when_a_local_file_exists() |
| 89 | + { |
| 90 | + touch(Hyde::path('_media/hyde.js')); |
| 91 | + $this->assertStringNotContainsString('https://cdn.jsdelivr.net/npm/hydefront', $this->renderTestView()); |
| 92 | + unlink(Hyde::path('_media/hyde.js')); |
| 93 | + } |
| 94 | + |
| 95 | + public function test_cdn_link_uses_the_correct_version_defined_in_the_asset_manager() |
| 96 | + { |
| 97 | + $expectedVersion = (new AssetService)->version(); |
| 98 | + $this->assertStringContainsString( |
| 99 | + 'https://cdn.jsdelivr.net/npm/hydefront@'.$expectedVersion.'/dist/hyde.js', |
| 100 | + $this->renderTestView() |
| 101 | + ); |
| 102 | + } |
| 103 | +} |
0 commit comments