|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Support\Collection; |
| 4 | +use Laravel\Prompts\Prompt; |
| 5 | +use Laravel\Prompts\Themes\Default\Concerns\DrawsTabs; |
| 6 | +use Laravel\Prompts\Themes\Default\Renderer; |
| 7 | + |
| 8 | +class TestPrompt extends Prompt |
| 9 | +{ |
| 10 | + public function __construct( |
| 11 | + public Collection $tabs, |
| 12 | + public int $selected = 0, |
| 13 | + public int $width = 60, |
| 14 | + ) { |
| 15 | + static::$themes['default'][static::class] = TestRenderer::class; |
| 16 | + } |
| 17 | + |
| 18 | + public function value(): mixed |
| 19 | + { |
| 20 | + return null; |
| 21 | + } |
| 22 | + |
| 23 | + public function display(): void |
| 24 | + { |
| 25 | + static::output()->write($this->renderTheme()); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class TestRenderer extends Renderer |
| 30 | +{ |
| 31 | + use DrawsTabs; |
| 32 | + |
| 33 | + public function __invoke(TestPrompt $prompt) |
| 34 | + { |
| 35 | + return $this->tabs($prompt->tabs, $prompt->selected, $prompt->width); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Note: Trailing whitespace is intentional in order to match the output. |
| 41 | + * Removing it will cause the test to fail (correctly) while allowing |
| 42 | + * the output to appear indistinguishable from the expected output. |
| 43 | + */ |
| 44 | + |
| 45 | +it('renders tabs', function () { |
| 46 | + Prompt::fake(); |
| 47 | + |
| 48 | + $tabs = collect(['One', 'Two', 'Three', 'Four', 'Five', 'Six']); |
| 49 | + |
| 50 | + (new TestPrompt($tabs))->display(); |
| 51 | + |
| 52 | + Prompt::assertStrippedOutputContains(<<<'OUTPUT' |
| 53 | + ╭─────╮ |
| 54 | + │ One │ Two Three Four Five Six |
| 55 | + ┴─────┴───────────────────────────────────────────────────── |
| 56 | + OUTPUT); |
| 57 | +}); |
| 58 | + |
| 59 | +it('highlights tabs', function () { |
| 60 | + Prompt::fake(); |
| 61 | + |
| 62 | + $tabs = collect(['One', 'Two', 'Three', 'Four', 'Five', 'Six']); |
| 63 | + |
| 64 | + (new TestPrompt($tabs, 2))->display(); |
| 65 | + |
| 66 | + Prompt::assertStrippedOutputContains(<<<'OUTPUT' |
| 67 | + ╭───────╮ |
| 68 | + One Two │ Three │ Four Five Six |
| 69 | + ──────────────┴───────┴───────────────────────────────────── |
| 70 | + OUTPUT); |
| 71 | +}); |
| 72 | + |
| 73 | +it('truncates tabs', function () { |
| 74 | + Prompt::fake(); |
| 75 | + |
| 76 | + $tabs = collect(['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']); |
| 77 | + |
| 78 | + (new TestPrompt($tabs))->display(); |
| 79 | + |
| 80 | + Prompt::assertStrippedOutputContains(<<<'OUTPUT' |
| 81 | + ╭─────╮ |
| 82 | + │ One │ Two Three Four Five Six Seven Eig |
| 83 | + ┴─────┴───────────────────────────────────────────────────── |
| 84 | + OUTPUT); |
| 85 | +}); |
| 86 | + |
| 87 | +it('scrolls tabs', function () { |
| 88 | + Prompt::fake(); |
| 89 | + |
| 90 | + $tabs = collect(['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight']); |
| 91 | + |
| 92 | + (new TestPrompt($tabs, 7))->display(); |
| 93 | + |
| 94 | + Prompt::assertStrippedOutputContains(<<<'OUTPUT' |
| 95 | + ╭───────╮ |
| 96 | + e Two Three Four Five Six Seven │ Eight │ |
| 97 | + ───────────────────────────────────────────────────┴───────┴ |
| 98 | + OUTPUT); |
| 99 | +}); |
0 commit comments