|
| 1 | +<?php |
| 2 | + |
| 3 | +use Code16\Sharp\EntityList\Commands\EntityCommand; |
| 4 | +use Code16\Sharp\EntityList\Commands\InstanceCommand; |
| 5 | +use Code16\Sharp\Form\Layout\FormLayout; |
| 6 | +use Code16\Sharp\Form\Layout\FormLayoutColumn; |
| 7 | +use Code16\Sharp\Form\SharpForm; |
| 8 | +use Code16\Sharp\Tests\Fixtures\Entities\PersonEntity; |
| 9 | +use Code16\Sharp\Tests\Fixtures\Sharp\PersonList; |
| 10 | +use Code16\Sharp\Tests\Fixtures\Sharp\PersonShow; |
| 11 | +use Code16\Sharp\Tests\Http\Api\Fixtures\ApiFormRefreshControllerEmbed; |
| 12 | +use Code16\Sharp\Tests\Http\Api\Fixtures\RefreshFormFields; |
| 13 | +use Illuminate\Testing\Fluent\AssertableJson; |
| 14 | + |
| 15 | +beforeEach(function () { |
| 16 | + sharp()->config()->declareEntity(PersonEntity::class); |
| 17 | + login(); |
| 18 | +}); |
| 19 | + |
| 20 | +it('gets updated HTML fields with live refresh of a form', function () { |
| 21 | + fakeFormFor('person', new class() extends SharpForm |
| 22 | + { |
| 23 | + use RefreshFormFields; |
| 24 | + |
| 25 | + public function buildFormLayout(FormLayout $formLayout): void |
| 26 | + { |
| 27 | + $formLayout->addColumn(12, function (FormLayoutColumn $column) { |
| 28 | + $column->withField('html_string') |
| 29 | + ->withField('html_non_refreshable') |
| 30 | + ->withField('html_view') |
| 31 | + ->withField('html_closure') |
| 32 | + ->withListField('list', function (FormLayoutColumn $column) { |
| 33 | + $column->withField('list_name') |
| 34 | + ->withField('list_html'); |
| 35 | + }); |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + public function find($id): array |
| 40 | + { |
| 41 | + return $this->transform([]); |
| 42 | + } |
| 43 | + |
| 44 | + public function update(mixed $id, array $data) {} |
| 45 | + }); |
| 46 | + |
| 47 | + $this->post( |
| 48 | + route('code16.sharp.api.form.refresh.update', [ |
| 49 | + 'entityKey' => 'person', |
| 50 | + 'instance_id' => 1, |
| 51 | + ]), |
| 52 | + [ |
| 53 | + 'name' => 'Albert Einstein', |
| 54 | + 'html_non_refreshable' => ['text' => 'example'], |
| 55 | + 'html_string' => ['text' => 'example'], |
| 56 | + 'html_view' => ['text' => 'example'], |
| 57 | + 'html_closure' => ['text' => 'example'], |
| 58 | + 'list' => [ |
| 59 | + ['id' => 1, 'list_name' => 'Marie Curie', 'list_html' => ['text' => 'example']], |
| 60 | + ], |
| 61 | + ] |
| 62 | + ) |
| 63 | + ->assertOk() |
| 64 | + ->assertJson(fn (AssertableJson $json) => $json |
| 65 | + ->missing('form.data.html_non_refreshable') |
| 66 | + ->where('form.data.html_string', '<h1>Albert Einstein</h1><p>example</p>') |
| 67 | + ->where('form.data.html_view', "<h1>Albert Einstein</h1><p>example</p>\n") |
| 68 | + ->where('form.data.html_closure', '<h1>Albert Einstein</h1><p>example</p>') |
| 69 | + ->where('form.data.list.0.list_html', '<h1>Marie Curie</h1><p>example</p>') |
| 70 | + ); |
| 71 | +}); |
| 72 | + |
| 73 | +it('gets updated HTML fields with live refresh of an entity list entity command form', function () { |
| 74 | + fakeListFor('person', new class() extends PersonList |
| 75 | + { |
| 76 | + protected function getEntityCommands(): ?array |
| 77 | + { |
| 78 | + return [ |
| 79 | + 'cmd' => new class() extends EntityCommand |
| 80 | + { |
| 81 | + use RefreshFormFields; |
| 82 | + |
| 83 | + public function label(): ?string |
| 84 | + { |
| 85 | + return 'entity'; |
| 86 | + } |
| 87 | + |
| 88 | + public function execute(array $data = []): array |
| 89 | + { |
| 90 | + return []; |
| 91 | + } |
| 92 | + }, |
| 93 | + ]; |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + $this->post( |
| 98 | + route('code16.sharp.api.form.refresh.update', [ |
| 99 | + 'entityKey' => 'person', |
| 100 | + 'entity_list_command_key' => 'cmd', |
| 101 | + ]), |
| 102 | + [ |
| 103 | + 'name' => 'Albert Einstein', |
| 104 | + 'html_non_refreshable' => ['text' => 'example'], |
| 105 | + 'html_string' => ['text' => 'example'], |
| 106 | + 'html_view' => ['text' => 'example'], |
| 107 | + 'html_closure' => ['text' => 'example'], |
| 108 | + 'list' => [ |
| 109 | + ['id' => 1, 'list_name' => 'Marie Curie', 'list_html' => ['text' => 'example']], |
| 110 | + ], |
| 111 | + ] |
| 112 | + ) |
| 113 | + ->assertOk() |
| 114 | + ->assertJson(fn (AssertableJson $json) => $json |
| 115 | + ->missing('form.data.html_non_refreshable') |
| 116 | + ->where('form.data.html_string', '<h1>Albert Einstein</h1><p>example</p>') |
| 117 | + ->where('form.data.html_view', "<h1>Albert Einstein</h1><p>example</p>\n") |
| 118 | + ->where('form.data.html_closure', '<h1>Albert Einstein</h1><p>example</p>') |
| 119 | + ->where('form.data.list.0.list_html', '<h1>Marie Curie</h1><p>example</p>') |
| 120 | + ); |
| 121 | +}); |
| 122 | + |
| 123 | +it('gets updated HTML fields with live refresh of an entity list instance command form', function () { |
| 124 | + fakeListFor('person', new class() extends PersonList |
| 125 | + { |
| 126 | + protected function getInstanceCommands(): ?array |
| 127 | + { |
| 128 | + return [ |
| 129 | + 'cmd' => new class() extends InstanceCommand |
| 130 | + { |
| 131 | + use RefreshFormFields; |
| 132 | + |
| 133 | + public function label(): ?string |
| 134 | + { |
| 135 | + return 'entity'; |
| 136 | + } |
| 137 | + |
| 138 | + public function execute(mixed $instanceId, array $data = []): array |
| 139 | + { |
| 140 | + return []; |
| 141 | + } |
| 142 | + }, |
| 143 | + ]; |
| 144 | + } |
| 145 | + }); |
| 146 | + |
| 147 | + $this->post( |
| 148 | + route('code16.sharp.api.form.refresh.update', [ |
| 149 | + 'entityKey' => 'person', |
| 150 | + 'entity_list_command_key' => 'cmd', |
| 151 | + 'instance_id' => 1, |
| 152 | + ]), |
| 153 | + [ |
| 154 | + 'name' => 'Albert Einstein', |
| 155 | + 'html_non_refreshable' => ['text' => 'example'], |
| 156 | + 'html_string' => ['text' => 'example'], |
| 157 | + 'html_view' => ['text' => 'example'], |
| 158 | + 'html_closure' => ['text' => 'example'], |
| 159 | + 'list' => [ |
| 160 | + ['id' => 1, 'list_name' => 'Marie Curie', 'list_html' => ['text' => 'example']], |
| 161 | + ], |
| 162 | + ] |
| 163 | + ) |
| 164 | + ->assertOk() |
| 165 | + ->assertJson(fn (AssertableJson $json) => $json |
| 166 | + ->missing('form.data.html_non_refreshable') |
| 167 | + ->where('form.data.html_string', '<h1>Albert Einstein</h1><p>example</p>') |
| 168 | + ->where('form.data.html_view', "<h1>Albert Einstein</h1><p>example</p>\n") |
| 169 | + ->where('form.data.html_closure', '<h1>Albert Einstein</h1><p>example</p>') |
| 170 | + ->where('form.data.list.0.list_html', '<h1>Marie Curie</h1><p>example</p>') |
| 171 | + ); |
| 172 | +}); |
| 173 | + |
| 174 | +it('gets updated HTML fields with live refresh of a show instance command form', function () { |
| 175 | + fakeShowFor('person', new class() extends PersonShow |
| 176 | + { |
| 177 | + public function getInstanceCommands(): ?array |
| 178 | + { |
| 179 | + return [ |
| 180 | + 'cmd' => new class() extends InstanceCommand |
| 181 | + { |
| 182 | + use RefreshFormFields; |
| 183 | + |
| 184 | + public function label(): ?string |
| 185 | + { |
| 186 | + return 'entity'; |
| 187 | + } |
| 188 | + |
| 189 | + public function execute(mixed $instanceId, array $data = []): array |
| 190 | + { |
| 191 | + return []; |
| 192 | + } |
| 193 | + }, |
| 194 | + ]; |
| 195 | + } |
| 196 | + }); |
| 197 | + |
| 198 | + $this->post( |
| 199 | + route('code16.sharp.api.form.refresh.update', [ |
| 200 | + 'entityKey' => 'person', |
| 201 | + 'show_command_key' => 'cmd', |
| 202 | + 'instance_id' => 1, |
| 203 | + ]), |
| 204 | + [ |
| 205 | + 'name' => 'Albert Einstein', |
| 206 | + 'html_non_refreshable' => ['text' => 'example'], |
| 207 | + 'html_string' => ['text' => 'example'], |
| 208 | + 'html_view' => ['text' => 'example'], |
| 209 | + 'html_closure' => ['text' => 'example'], |
| 210 | + 'list' => [ |
| 211 | + ['id' => 1, 'list_name' => 'Marie Curie', 'list_html' => ['text' => 'example']], |
| 212 | + ], |
| 213 | + ] |
| 214 | + ) |
| 215 | + ->assertOk() |
| 216 | + ->assertJson(fn (AssertableJson $json) => $json |
| 217 | + ->missing('form.data.html_non_refreshable') |
| 218 | + ->where('form.data.html_string', '<h1>Albert Einstein</h1><p>example</p>') |
| 219 | + ->where('form.data.html_view', "<h1>Albert Einstein</h1><p>example</p>\n") |
| 220 | + ->where('form.data.html_closure', '<h1>Albert Einstein</h1><p>example</p>') |
| 221 | + ->where('form.data.list.0.list_html', '<h1>Marie Curie</h1><p>example</p>') |
| 222 | + ); |
| 223 | +}); |
| 224 | + |
| 225 | +it('gets updated HTML fields with live refresh of an embed form', function () { |
| 226 | + $this |
| 227 | + ->postJson( |
| 228 | + route('code16.sharp.api.form.refresh.update', [ |
| 229 | + 'entityKey' => 'person', |
| 230 | + 'embed_key' => (new ApiFormRefreshControllerEmbed())->key(), |
| 231 | + ]), |
| 232 | + [ |
| 233 | + 'name' => 'Albert Einstein', |
| 234 | + 'html_non_refreshable' => ['text' => 'example'], |
| 235 | + 'html_string' => ['text' => 'example'], |
| 236 | + 'html_view' => ['text' => 'example'], |
| 237 | + 'html_closure' => ['text' => 'example'], |
| 238 | + 'list' => [ |
| 239 | + ['id' => 1, 'list_name' => 'Marie Curie', 'list_html' => ['text' => 'example']], |
| 240 | + ], |
| 241 | + ] |
| 242 | + ) |
| 243 | + ->assertOk() |
| 244 | + ->assertJson(fn (AssertableJson $json) => $json |
| 245 | + ->missing('form.data.html_non_refreshable') |
| 246 | + ->where('form.data.html_string', '<h1>Albert Einstein</h1><p>example</p>') |
| 247 | + ->where('form.data.html_view', "<h1>Albert Einstein</h1><p>example</p>\n") |
| 248 | + ->where('form.data.html_closure', '<h1>Albert Einstein</h1><p>example</p>') |
| 249 | + ->where('form.data.list.0.list_html', '<h1>Marie Curie</h1><p>example</p>') |
| 250 | + ); |
| 251 | +}); |
0 commit comments