|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | use CodebarAg\FilamentJsonField\Infolists\Components\JsonEntry; |
| 4 | +use Filament\Infolists\Infolist; |
| 5 | +use Filament\Infolists\Concerns\InteractsWithInfolists; |
| 6 | +use Livewire\Component; |
| 7 | +use Livewire\Livewire; |
4 | 8 |
|
5 | | -it('can have folding code ', function () { |
6 | | - $field = JsonEntry::make('json'); |
| 9 | +// Test component for infolist integration |
| 10 | +class TestJsonEntryComponent extends Component |
| 11 | +{ |
| 12 | + use InteractsWithInfolists; |
7 | 13 |
|
8 | | - expect($field->getHasFoldingCode())->toBe(true); |
| 14 | + public array $data = [ |
| 15 | + 'json_field' => ['key' => 'value', 'nested' => ['test' => 'data']] |
| 16 | + ]; |
9 | 17 |
|
10 | | - $field->foldingCode(false); |
11 | | - expect($field->getHasFoldingCode())->toBe(false); |
| 18 | + public function mount(): void |
| 19 | + { |
| 20 | + $this->infolist->fill($this->data); |
| 21 | + } |
12 | 22 |
|
13 | | - $field->foldingCode(true); |
14 | | - expect($field->getHasFoldingCode())->toBe(true); |
15 | | -}); |
| 23 | + public function infolist(Infolist $infolist): Infolist |
| 24 | + { |
| 25 | + return $infolist |
| 26 | + ->schema([ |
| 27 | + JsonEntry::make('json_field') |
| 28 | + ->label('JSON Field'), |
| 29 | + ]) |
| 30 | + ->state($this->data); |
| 31 | + } |
16 | 32 |
|
17 | | -it('can have folded code ', function () { |
18 | | - $field = JsonEntry::make('json'); |
| 33 | + public function render() |
| 34 | + { |
| 35 | + return view('livewire.test-infolist-component'); |
| 36 | + } |
| 37 | +} |
19 | 38 |
|
20 | | - expect($field->getHasFoldedCode())->toBe(false); |
| 39 | +// Create a simple test view for infolists |
| 40 | +if (!file_exists(__DIR__ . '/../resources/views/livewire/')) { |
| 41 | + mkdir(__DIR__ . '/../resources/views/livewire/', 0755, true); |
| 42 | +} |
21 | 43 |
|
22 | | - $field->foldedCode(false); |
23 | | - expect($field->getHasFoldedCode())->toBe(false); |
| 44 | +file_put_contents(__DIR__ . '/../resources/views/livewire/test-infolist-component.blade.php', ' |
| 45 | +<div> |
| 46 | + {{ $this->infolist }} |
| 47 | +</div> |
| 48 | +'); |
24 | 49 |
|
25 | | - $field->foldedCode(true); |
26 | | - expect($field->getHasFoldedCode())->toBe(true); |
27 | | -}); |
| 50 | +describe('JsonEntry Component', function () { |
| 51 | + it('can be instantiated', function () { |
| 52 | + $entry = JsonEntry::make('json_field'); |
| 53 | + |
| 54 | + expect($entry)->toBeInstanceOf(JsonEntry::class); |
| 55 | + expect($entry->getName())->toBe('json_field'); |
| 56 | + }); |
28 | 57 |
|
29 | | -it('can have auto closing brackets code ', function () { |
30 | | - $field = JsonEntry::make('json'); |
| 58 | + it('has correct default values', function () { |
| 59 | + $entry = JsonEntry::make('json_field'); |
| 60 | + |
| 61 | + expect($entry->getHasLineNumbers())->toBe(true); |
| 62 | + expect($entry->getHasLineWrapping())->toBe(true); |
| 63 | + expect($entry->getHasAutoCloseBrackets())->toBe(true); |
| 64 | + expect($entry->getHasFoldingCode())->toBe(true); |
| 65 | + expect($entry->getHasDarkTheme())->toBe(false); |
| 66 | + expect($entry->getHasFoldedCode())->toBe(false); |
| 67 | + }); |
31 | 68 |
|
32 | | - expect($field->getHasAutoCloseBrackets())->toBe(true); |
| 69 | + it('can configure folding code', function () { |
| 70 | + $entry = JsonEntry::make('json'); |
33 | 71 |
|
34 | | - $field->autoCloseBrackets(false); |
35 | | - expect($field->getHasAutoCloseBrackets())->toBe(false); |
| 72 | + expect($entry->getHasFoldingCode())->toBe(true); |
36 | 73 |
|
37 | | - $field->autoCloseBrackets(true); |
38 | | - expect($field->getHasAutoCloseBrackets())->toBe(true); |
39 | | -}); |
| 74 | + $entry->foldingCode(false); |
| 75 | + expect($entry->getHasFoldingCode())->toBe(false); |
40 | 76 |
|
41 | | -it('can have line numbers code ', function () { |
42 | | - $field = JsonEntry::make('json'); |
| 77 | + $entry->foldingCode(true); |
| 78 | + expect($entry->getHasFoldingCode())->toBe(true); |
| 79 | + }); |
43 | 80 |
|
44 | | - expect($field->getHasLineNumbers())->toBe(true); |
| 81 | + it('can configure folded code', function () { |
| 82 | + $entry = JsonEntry::make('json'); |
45 | 83 |
|
46 | | - $field->lineNumbers(false); |
47 | | - expect($field->getHasLineNumbers())->toBe(false); |
| 84 | + expect($entry->getHasFoldedCode())->toBe(false); |
48 | 85 |
|
49 | | - $field->lineNumbers(true); |
50 | | - expect($field->getHasLineNumbers())->toBe(true); |
51 | | -}); |
| 86 | + $entry->foldedCode(false); |
| 87 | + expect($entry->getHasFoldedCode())->toBe(false); |
52 | 88 |
|
53 | | -it('can have line wrapping code ', function () { |
54 | | - $field = JsonEntry::make('json'); |
| 89 | + $entry->foldedCode(true); |
| 90 | + expect($entry->getHasFoldedCode())->toBe(true); |
| 91 | + }); |
55 | 92 |
|
56 | | - expect($field->getHasLineWrapping())->toBe(true); |
| 93 | + it('can configure auto closing brackets', function () { |
| 94 | + $entry = JsonEntry::make('json'); |
57 | 95 |
|
58 | | - $field->lineWrapping(false); |
59 | | - expect($field->getHasLineWrapping())->toBe(false); |
| 96 | + expect($entry->getHasAutoCloseBrackets())->toBe(true); |
60 | 97 |
|
61 | | - $field->lineWrapping(true); |
62 | | - expect($field->getHasLineWrapping())->toBe(true); |
63 | | -}); |
| 98 | + $entry->autoCloseBrackets(false); |
| 99 | + expect($entry->getHasAutoCloseBrackets())->toBe(false); |
| 100 | + |
| 101 | + $entry->autoCloseBrackets(true); |
| 102 | + expect($entry->getHasAutoCloseBrackets())->toBe(true); |
| 103 | + }); |
| 104 | + |
| 105 | + it('can configure line numbers', function () { |
| 106 | + $entry = JsonEntry::make('json'); |
| 107 | + |
| 108 | + expect($entry->getHasLineNumbers())->toBe(true); |
| 109 | + |
| 110 | + $entry->lineNumbers(false); |
| 111 | + expect($entry->getHasLineNumbers())->toBe(false); |
| 112 | + |
| 113 | + $entry->lineNumbers(true); |
| 114 | + expect($entry->getHasLineNumbers())->toBe(true); |
| 115 | + }); |
| 116 | + |
| 117 | + it('can configure line wrapping', function () { |
| 118 | + $entry = JsonEntry::make('json'); |
| 119 | + |
| 120 | + expect($entry->getHasLineWrapping())->toBe(true); |
| 121 | + |
| 122 | + $entry->lineWrapping(false); |
| 123 | + expect($entry->getHasLineWrapping())->toBe(false); |
| 124 | + |
| 125 | + $entry->lineWrapping(true); |
| 126 | + expect($entry->getHasLineWrapping())->toBe(true); |
| 127 | + }); |
| 128 | + |
| 129 | + it('can configure dark theme', function () { |
| 130 | + $entry = JsonEntry::make('json'); |
| 131 | + |
| 132 | + expect($entry->getHasDarkTheme())->toBe(false); |
| 133 | + |
| 134 | + $entry->darkTheme(false); |
| 135 | + expect($entry->getHasDarkTheme())->toBe(false); |
| 136 | + |
| 137 | + $entry->darkTheme(true); |
| 138 | + expect($entry->getHasDarkTheme())->toBe(true); |
| 139 | + }); |
| 140 | + |
| 141 | + it('can be configured with all features', function () { |
| 142 | + $entry = JsonEntry::make('json_field') |
| 143 | + ->darkTheme(true) |
| 144 | + ->lineNumbers(true) |
| 145 | + ->lineWrapping(true) |
| 146 | + ->autoCloseBrackets(true) |
| 147 | + ->foldingCode(true) |
| 148 | + ->foldedCode(true); |
| 149 | + |
| 150 | + expect($entry->getHasDarkTheme())->toBe(true); |
| 151 | + expect($entry->getHasLineNumbers())->toBe(true); |
| 152 | + expect($entry->getHasLineWrapping())->toBe(true); |
| 153 | + expect($entry->getHasAutoCloseBrackets())->toBe(true); |
| 154 | + expect($entry->getHasFoldingCode())->toBe(true); |
| 155 | + expect($entry->getHasFoldedCode())->toBe(true); |
| 156 | + }); |
| 157 | + |
| 158 | + it('can have a label', function () { |
| 159 | + $entry = JsonEntry::make('json_field')->label('JSON Data'); |
| 160 | + |
| 161 | + expect($entry->getLabel())->toBe('JSON Data'); |
| 162 | + }); |
| 163 | + |
| 164 | + it('can have a name', function () { |
| 165 | + $entry = JsonEntry::make('json_field')->name('custom_name'); |
| 166 | + |
| 167 | + expect($entry->getName())->toBe('custom_name'); |
| 168 | + }); |
| 169 | + |
| 170 | + it('can have an ID', function () { |
| 171 | + $entry = JsonEntry::make('json_field')->id('custom_id'); |
| 172 | + |
| 173 | + expect($entry->getId())->toBe('custom_id'); |
| 174 | + }); |
| 175 | + |
| 176 | + it('can be hidden', function () { |
| 177 | + $entry = JsonEntry::make('json_field')->hidden(); |
| 178 | + |
| 179 | + expect($entry->isHidden())->toBe(true); |
| 180 | + }); |
| 181 | + |
| 182 | + it('can be visible', function () { |
| 183 | + $entry = JsonEntry::make('json_field')->visible(); |
| 184 | + |
| 185 | + expect($entry->isHidden())->toBe(false); |
| 186 | + }); |
| 187 | + |
| 188 | + it('can have a hint', function () { |
| 189 | + $entry = JsonEntry::make('json_field')->hint('JSON data display'); |
| 190 | + |
| 191 | + expect($entry->getHint())->toBe('JSON data display'); |
| 192 | + }); |
| 193 | + |
| 194 | + it('can be configured with fluent interface', function () { |
| 195 | + $entry = JsonEntry::make('json_field') |
| 196 | + ->label('JSON Data') |
| 197 | + ->hint('JSON data display') |
| 198 | + ->darkTheme(true) |
| 199 | + ->lineNumbers(true) |
| 200 | + ->lineWrapping(true) |
| 201 | + ->autoCloseBrackets(true) |
| 202 | + ->foldingCode(true) |
| 203 | + ->foldedCode(true); |
| 204 | + |
| 205 | + expect($entry->getLabel())->toBe('JSON Data'); |
| 206 | + expect($entry->getHint())->toBe('JSON data display'); |
| 207 | + expect($entry->getHasDarkTheme())->toBe(true); |
| 208 | + expect($entry->getHasLineNumbers())->toBe(true); |
| 209 | + expect($entry->getHasLineWrapping())->toBe(true); |
| 210 | + expect($entry->getHasAutoCloseBrackets())->toBe(true); |
| 211 | + expect($entry->getHasFoldingCode())->toBe(true); |
| 212 | + expect($entry->getHasFoldedCode())->toBe(true); |
| 213 | + }); |
| 214 | + |
| 215 | + it('can format JSON data correctly', function () { |
| 216 | + $entry = JsonEntry::make('json_field'); |
| 217 | + |
| 218 | + $testData = ['key' => 'value', 'nested' => ['test' => 'data']]; |
| 219 | + $entry->state($testData); |
| 220 | + |
| 221 | + expect($entry->getState())->toBe($testData); |
| 222 | + }); |
64 | 223 |
|
65 | | -it('can have code ', function () { |
66 | | - $field = JsonEntry::make('json'); |
| 224 | + it('can handle null values', function () { |
| 225 | + $entry = JsonEntry::make('json_field'); |
| 226 | + |
| 227 | + $entry->state(null); |
| 228 | + |
| 229 | + expect($entry->getState())->toBeNull(); |
| 230 | + }); |
67 | 231 |
|
68 | | - expect($field->getHasDarkTheme())->toBe(false); |
| 232 | + it('can handle complex nested JSON', function () { |
| 233 | + $entry = JsonEntry::make('json_field'); |
| 234 | + |
| 235 | + $complexData = [ |
| 236 | + 'users' => [ |
| 237 | + [ |
| 238 | + 'id' => 1, |
| 239 | + 'name' => 'John Doe', |
| 240 | + |
| 241 | + 'settings' => [ |
| 242 | + 'theme' => 'dark', |
| 243 | + 'notifications' => true, |
| 244 | + 'preferences' => [ |
| 245 | + 'language' => 'en', |
| 246 | + 'timezone' => 'UTC' |
| 247 | + ] |
| 248 | + ] |
| 249 | + ], |
| 250 | + [ |
| 251 | + 'id' => 2, |
| 252 | + 'name' => 'Jane Smith', |
| 253 | + |
| 254 | + 'settings' => [ |
| 255 | + 'theme' => 'light', |
| 256 | + 'notifications' => false |
| 257 | + ] |
| 258 | + ] |
| 259 | + ], |
| 260 | + 'metadata' => [ |
| 261 | + 'total_users' => 2, |
| 262 | + 'created_at' => '2024-01-01T00:00:00Z', |
| 263 | + 'tags' => ['important', 'urgent', 'feature'] |
| 264 | + ] |
| 265 | + ]; |
| 266 | + |
| 267 | + $entry->state($complexData); |
| 268 | + |
| 269 | + expect($entry->getState())->toBe($complexData); |
| 270 | + }); |
69 | 271 |
|
70 | | - $field->darkTheme(false); |
71 | | - expect($field->getHasDarkTheme())->toBe(false); |
| 272 | + it('has correct view path', function () { |
| 273 | + $entry = JsonEntry::make('json_field'); |
| 274 | + |
| 275 | + expect($entry->getView())->toBe('filament-json-field::infolists.components.json-entry'); |
| 276 | + }); |
72 | 277 |
|
73 | | - $field->darkTheme(true); |
74 | | - expect($field->getHasDarkTheme())->toBe(true); |
| 278 | + it('can have extra attributes', function () { |
| 279 | + $entry = JsonEntry::make('json_field')->extraAttributes(['data-test' => 'value']); |
| 280 | + |
| 281 | + expect($entry->getExtraAttributes())->toBe(['data-test' => 'value']); |
| 282 | + }); |
75 | 283 | }); |
0 commit comments