Skip to content

Commit a194f0c

Browse files
committed
After merge cleanup and tests fixing
1 parent 18741bd commit a194f0c

File tree

6 files changed

+20
-26
lines changed

6 files changed

+20
-26
lines changed

src/LiveComponent/assets/test/controller/model.test.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ describe('LiveController data-model Tests', () => {
201201
});
202202

203203
it('sends correct data for checkbox fields', async () => {
204-
const checkboxTemplate = (data) => `
204+
const checkboxTemplate = (data: any) => `
205205
<div
206206
${initLiveComponent('/_components/my_component', data)}
207207
data-action="change->live#update"
@@ -213,29 +213,24 @@ describe('LiveController data-model Tests', () => {
213213
<label>
214214
Checkbox 2: <input type="checkbox" name="form[check2]" value="1" ${data.form.check2 ? 'checked' : ''} />
215215
</label>
216+
217+
Checkbox 2 is ${data.form.check2 ? 'checked' : 'unchecked' }
216218
</div>
217219
`;
218-
const data = { form: { check1: null, check2: null} };
220+
const data = { form: { check1: false, check2: false} };
219221
const { element, controller } = await startStimulus(checkboxTemplate(data));
220222

221223
const check1Element = getByLabelText(element, 'Checkbox 1:');
222224
const check2Element = getByLabelText(element, 'Checkbox 2:');
223225

226+
// no mockRerender needed... not sure why. This first Ajax call is likely
227+
// interrupted by the next immediately starting
224228
await userEvent.click(check1Element);
225-
await waitFor(() => expect(check1Element).toBeChecked());
226229

227-
mockRerender({ form: {check1: '1'}}, checkboxTemplate, (data) => {
228-
data.form.check1 = '1';
229-
data.form.check2 = null;
230-
});
230+
mockRerender({ form: {check1: '1', check2: '1'}}, checkboxTemplate);
231231

232232
await userEvent.click(check2Element);
233-
await waitFor(() => expect(check2Element).toBeChecked());
234-
235-
mockRerender({ form: {check1: '1', check2: '1'}}, checkboxTemplate, (data) => {
236-
data.form.check1 = '1';
237-
data.form.check2 = '1';
238-
});
233+
await waitFor(() => expect(element).toHaveTextContent('Checkbox 2 is checked'));
239234

240235
expect(controller.dataValue).toEqual({form: {check1: '1', check2: '1'}});
241236

src/LiveComponent/tests/Fixtures/Component/FormComponent1.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\UX\LiveComponent\Tests\Fixture\Component;
12+
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Component;
1313

1414
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1515
use Symfony\Component\Form\FormFactoryInterface;
1616
use Symfony\Component\Form\FormInterface;
1717
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
1818
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
1919
use Symfony\UX\LiveComponent\DefaultActionTrait;
20-
use Symfony\UX\LiveComponent\Tests\Fixture\Form\FormType1;
20+
use Symfony\UX\LiveComponent\Tests\Fixtures\Form\FormType1;
2121

2222
/**
2323
* @author Jakub Caban <[email protected]>

src/LiveComponent/tests/Fixture/Form/FormType1.php renamed to src/LiveComponent/tests/Fixtures/Form/FormType1.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace Symfony\UX\LiveComponent\Tests\Fixture\Form;
14+
namespace Symfony\UX\LiveComponent\Tests\Fixtures\Form;
1515

1616
use Symfony\Component\Form\AbstractType;
1717
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div
2-
{{ init_live_component(this) }}
2+
{{ attributes }}
33
>
44
{{ form(this.form) }}
55
</div>

src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\DomCrawler\Crawler;
1818
use Symfony\Component\Form\FormFactoryInterface;
1919
use Symfony\UX\LiveComponent\LiveComponentHydrator;
20-
use Symfony\UX\LiveComponent\Tests\Fixture\Component\FormComponent1;
20+
use Symfony\UX\LiveComponent\Tests\Fixtures\Component\FormComponent1;
2121
use Symfony\UX\LiveComponent\Tests\Fixtures\Component\FormWithCollectionTypeComponent;
2222
use Symfony\UX\LiveComponent\Tests\Fixtures\Form\BlogPostFormType;
2323
use Symfony\UX\TwigComponent\ComponentFactory;
@@ -144,6 +144,7 @@ public function testFormRemembersValidationFromInitialForm(): void
144144
->assertContains('The title field should not be blank')
145145
;
146146
}
147+
147148
public function testHandleCheckboxChanges(): void
148149
{
149150
/** @var LiveComponentHydrator $hydrator */
@@ -176,7 +177,7 @@ public function testHandleCheckboxChanges(): void
176177

177178
$this->browser()
178179
->throwExceptions()
179-
->get('/_components/form_component1?'.http_build_query($dehydrated))
180+
->get('/_components/form_component1?data='.urlencode(json_encode($dehydrated)))
180181
->assertSuccessful()
181182
->assertContains('<input type="checkbox" id="form_choice_multiple_1" name="form[choice_multiple][]" value="2" checked="checked" />')
182183
->assertContains('<input type="checkbox" id="form_choice_multiple_0" name="form[choice_multiple][]" value="1" />')
@@ -194,7 +195,7 @@ public function testHandleCheckboxChanges(): void
194195

195196
$dehydrated['form'] = $bareForm;
196197
})
197-
->get('/_components/form_component1?'.http_build_query($dehydrated))
198+
->get('/_components/form_component1?data='.urlencode(json_encode($dehydrated)))
198199
->assertContains('<input type="checkbox" id="form_choice_multiple_1" name="form[choice_multiple][]" value="2" checked="checked" />')
199200
->assertContains('<input type="checkbox" id="form_choice_multiple_0" name="form[choice_multiple][]" value="1" checked="checked" />')
200201
->use(function (HtmlResponse $response) use (&$fullBareData, &$dehydrated, &$bareForm) {
@@ -214,7 +215,7 @@ public function testHandleCheckboxChanges(): void
214215

215216
$dehydrated['form'] = $bareForm;
216217
})
217-
->get('/_components/form_component1?'.http_build_query($dehydrated))
218+
->get('/_components/form_component1?data='.urlencode(json_encode($dehydrated)))
218219
->assertContains('<input type="checkbox" id="form_choice_multiple_1" name="form[choice_multiple][]" value="2" />')
219220
->assertContains('<input type="checkbox" id="form_choice_multiple_0" name="form[choice_multiple][]" value="1" />')
220221
->assertContains('<input type="checkbox" id="form_checkbox" name="form[checkbox]" required="required" value="1" checked="checked" />')

src/LiveComponent/tests/Unit/Form/ComponentWithFormTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,18 @@
1414
namespace Symfony\UX\LiveComponent\Tests\Unit\Form;
1515

1616
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
17-
use Symfony\UX\LiveComponent\Tests\ContainerBC;
18-
use Symfony\UX\LiveComponent\Tests\Fixture\Component\FormComponent1;
17+
use Symfony\UX\LiveComponent\Tests\Fixtures\Component\FormComponent1;
1918

2019
/**
2120
* @author Jakub Caban <[email protected]>
2221
*/
2322
class ComponentWithFormTest extends KernelTestCase
2423
{
25-
use ContainerBC;
26-
2724
public function testFormValues(): void
2825
{
2926
$formFactory = self::getContainer()->get('form.factory');
3027
$component = new FormComponent1($formFactory);
28+
$component->postMount([]);
3129

3230
$this->assertSame(
3331
[
@@ -42,7 +40,7 @@ public function testFormValues(): void
4240
'file' => '',
4341
'hidden' => '',
4442
],
45-
$component->getFormValues()
43+
$component->formValues
4644
);
4745
}
4846
}

0 commit comments

Comments
 (0)