|
| 1 | +<?php |
| 2 | + |
| 3 | +use Livewire\Blaze\BladeService; |
| 4 | + |
| 5 | +describe('componentNameToPath', function () { |
| 6 | + beforeEach(function () { |
| 7 | + $basePath = __DIR__ . '/fixtures/components'; |
| 8 | + app('blade.compiler')->anonymousComponentPath($basePath); |
| 9 | + app('blade.compiler')->anonymousComponentPath($basePath, 'fixtures'); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('namespaced', function () { |
| 13 | + it('gets the correct path for namespaced direct component files', function () { |
| 14 | + $input = 'fixtures::button'; |
| 15 | + $expected = __DIR__ . '/fixtures/components/button.blade.php'; |
| 16 | + |
| 17 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 18 | + }); |
| 19 | + |
| 20 | + it('prefers namespaced direct component file over index.blade.php for root components', function () { |
| 21 | + $input = 'fixtures::card'; |
| 22 | + $expected = __DIR__ . '/fixtures/components/card.blade.php'; |
| 23 | + |
| 24 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 25 | + }); |
| 26 | + |
| 27 | + it('gets the correct path for namespaced nested components', function () { |
| 28 | + $input = 'fixtures::form.input'; |
| 29 | + $expected = __DIR__ . '/fixtures/components/form/input.blade.php'; |
| 30 | + |
| 31 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 32 | + }); |
| 33 | + |
| 34 | + it('gets the correct path for namespaced deeply nested components', function () { |
| 35 | + $input = 'fixtures::form.fields.text'; |
| 36 | + $expected = __DIR__ . '/fixtures/components/form/fields/text.blade.php'; |
| 37 | + |
| 38 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 39 | + }); |
| 40 | + |
| 41 | + it('gets the correct path for namespaced root components using index.blade.php', function () { |
| 42 | + $input = 'fixtures::form'; |
| 43 | + $expected = __DIR__ . '/fixtures/components/form/index.blade.php'; |
| 44 | + |
| 45 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 46 | + }); |
| 47 | + |
| 48 | + it('gets the correct path for namespaced root components using same-name.blade.php', function () { |
| 49 | + $input = 'fixtures::panel'; |
| 50 | + $expected = __DIR__ . '/fixtures/components/panel/panel.blade.php'; |
| 51 | + |
| 52 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 53 | + }); |
| 54 | + |
| 55 | + it('gets the correct path for namespaced nested root components using same-name.blade.php', function () { |
| 56 | + $input = 'fixtures::kanban.comments'; |
| 57 | + $expected = __DIR__ . '/fixtures/components/kanban/comments/comments.blade.php'; |
| 58 | + |
| 59 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 60 | + }); |
| 61 | + |
| 62 | + it('handles non-existent namespaced components gracefully', function () { |
| 63 | + $input = 'fixtures::nonexistent.component'; |
| 64 | + |
| 65 | + expect((new BladeService)->componentNameToPath($input))->toBe(''); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('non-namespaced', function () { |
| 70 | + it('gets the correct path for direct component files', function () { |
| 71 | + $input = 'button'; |
| 72 | + $expected = __DIR__ . '/fixtures/components/button.blade.php'; |
| 73 | + |
| 74 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 75 | + }); |
| 76 | + |
| 77 | + it('prefers direct component file over index.blade.php for root components', function () { |
| 78 | + $input = 'card'; |
| 79 | + $expected = __DIR__ . '/fixtures/components/card.blade.php'; |
| 80 | + |
| 81 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 82 | + }); |
| 83 | + |
| 84 | + it('gets the correct path for nested components', function () { |
| 85 | + $input = 'form.input'; |
| 86 | + $expected = __DIR__ . '/fixtures/components/form/input.blade.php'; |
| 87 | + |
| 88 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 89 | + }); |
| 90 | + |
| 91 | + it('gets the correct path for deeply nested components', function () { |
| 92 | + $input = 'form.fields.text'; |
| 93 | + $expected = __DIR__ . '/fixtures/components/form/fields/text.blade.php'; |
| 94 | + |
| 95 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 96 | + }); |
| 97 | + |
| 98 | + it('gets the correct path for root components using index.blade.php', function () { |
| 99 | + $input = 'form'; |
| 100 | + $expected = __DIR__ . '/fixtures/components/form/index.blade.php'; |
| 101 | + |
| 102 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 103 | + }); |
| 104 | + |
| 105 | + it('gets the correct path for root components using same-name.blade.php', function () { |
| 106 | + $input = 'panel'; |
| 107 | + $expected = __DIR__ . '/fixtures/components/panel/panel.blade.php'; |
| 108 | + |
| 109 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 110 | + }); |
| 111 | + |
| 112 | + it('gets the correct path for nested root components using same-name.blade.php', function () { |
| 113 | + $input = 'kanban.comments'; |
| 114 | + $expected = __DIR__ . '/fixtures/components/kanban/comments/comments.blade.php'; |
| 115 | + |
| 116 | + expect((new BladeService)->componentNameToPath($input))->toBe($expected); |
| 117 | + }); |
| 118 | + |
| 119 | + it('handles non-existent components gracefully', function () { |
| 120 | + $input = 'nonexistent.component'; |
| 121 | + |
| 122 | + expect((new BladeService)->componentNameToPath($input))->toBe(''); |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments