Skip to content
This repository was archived by the owner on Mar 18, 2025. It is now read-only.

Commit 6086c55

Browse files
committed
Cleanup tests
1 parent 4755f04 commit 6086c55

19 files changed

+174
-437
lines changed

tests/Components/Choice/CheckboxGroupTest.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,17 @@ class CheckboxGroupTest extends ComponentTestCase
1212
/** @test */
1313
public function can_be_rendered(): void
1414
{
15-
$template = <<<HTML
16-
<x-checkbox-group>
17-
Checkboxes...
18-
</x-checkbox-group>
19-
HTML;
20-
21-
$this->assertMatchesSnapshot($this->renderComponent($template));
15+
$this->assertMatchesSnapshot(
16+
(string) $this->blade('<x-checkbox-group>Checkboxes...</x-checkbox-group>')
17+
);
2218
}
2319

2420
/** @test */
2521
public function can_be_not_stacked(): void
2622
{
27-
$template = <<<HTML
28-
<x-checkbox-group :stacked="false">
29-
Checkboxes...
30-
</x-checkbox-group>
31-
HTML;
32-
33-
$this->assertMatchesSnapshot($this->renderComponent($template));
23+
$this->assertMatchesSnapshot(
24+
(string) $this->blade('<x-checkbox-group :stacked="$stacked">Checkboxes...</x-checkbox-group>', ['stacked' => false])
25+
);
3426
}
3527

3628
/** @test */
@@ -43,6 +35,6 @@ public function can_have_a_custom_amount_of_grid_columns(): void
4335
</x-checkbox-group>
4436
HTML;
4537

46-
$this->assertMatchesSnapshot($this->renderComponent($template));
38+
$this->assertMatchesSnapshot((string) $this->blade($template));
4739
}
4840
}

tests/Components/Choice/CheckboxTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ class CheckboxTest extends ComponentTestCase
1313
public function can_be_rendered(): void
1414
{
1515
$this->assertMatchesSnapshot(
16-
$this->renderComponent('<x-checkbox name="remember_me" />')
16+
(string) $this->blade('<x-checkbox name="remember_me" />')
1717
);
1818
}
1919

2020
/** @test */
2121
public function specific_attributes_can_be_used(): void
2222
{
2323
$this->assertMatchesSnapshot(
24-
$this->renderComponent('<x-checkbox name="remember_me" id="rememberMe" class="p-4" value="remember" label="Remember me" />')
24+
(string) $this->blade('<x-checkbox name="remember_me" id="rememberMe" class="p-4" value="remember" label="Remember me" />')
2525
);
2626
}
2727

2828
/** @test */
2929
public function label_can_be_slotted(): void
3030
{
3131
$this->assertMatchesSnapshot(
32-
$this->renderComponent('<x-checkbox name="remember_me">Remember me</x-checkbox>')
32+
(string) $this->blade('<x-checkbox name="remember_me">Remember me</x-checkbox>')
3333
);
3434
}
3535

@@ -39,15 +39,15 @@ public function can_have_old_values(): void
3939
$this->flashOld(['remember_me' => true]);
4040

4141
$this->assertMatchesSnapshot(
42-
$this->renderComponent('<x-checkbox name="remember_me" label="Remember me" />')
42+
(string) $this->blade('<x-checkbox name="remember_me" label="Remember me" />')
4343
);
4444
}
4545

4646
/** @test */
4747
public function can_have_a_description(): void
4848
{
4949
$this->assertMatchesSnapshot(
50-
$this->renderComponent('<x-checkbox name="remember_me" label="Remember me" description="My description" />')
50+
(string) $this->blade('<x-checkbox name="remember_me" label="Remember me" description="My description" />')
5151
);
5252
}
5353

@@ -62,14 +62,14 @@ public function description_can_be_slotted(): void
6262
</x-checkbox>
6363
HTML;
6464

65-
$this->assertMatchesSnapshot($this->renderComponent($template));
65+
$this->assertMatchesSnapshot((string) $this->blade($template));
6666
}
6767

6868
/** @test */
6969
public function checked_is_not_rendered_if_wire_model_is_present(): void
7070
{
7171
$this->assertMatchesSnapshot(
72-
$this->renderComponent('<x-checkbox name="remember_me" label="Remember me" wire:model="remember" checked />')
72+
(string) $this->blade('<x-checkbox name="remember_me" label="Remember me" wire:model="remember" checked />')
7373
);
7474
}
7575
}

tests/Components/Choice/RadioTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ class RadioTest extends ComponentTestCase
1313
public function can_be_rendered(): void
1414
{
1515
$this->assertMatchesSnapshot(
16-
$this->renderComponent('<x-radio name="remember_me" />')
16+
(string) $this->blade('<x-radio name="remember_me" />')
1717
);
1818
}
1919

2020
/** @test */
2121
public function specific_attributes_can_be_used(): void
2222
{
2323
$this->assertMatchesSnapshot(
24-
$this->renderComponent('<x-radio name="remember_me" id="rememberMe" class="p-4" value="remember" label="Remember me" />')
24+
(string) $this->blade('<x-radio name="remember_me" id="rememberMe" class="p-4" value="remember" label="Remember me" />')
2525
);
2626
}
2727

2828
/** @test */
2929
public function label_can_be_slotted(): void
3030
{
3131
$this->assertMatchesSnapshot(
32-
$this->renderComponent('<x-radio name="remember_me">Remember me</x-radio>')
32+
(string) $this->blade('<x-radio name="remember_me">Remember me</x-radio>')
3333
);
3434
}
3535

@@ -39,15 +39,15 @@ public function can_have_old_values(): void
3939
$this->flashOld(['remember_me' => true]);
4040

4141
$this->assertMatchesSnapshot(
42-
$this->renderComponent('<x-radio name="remember_me" label="Remember me" />')
42+
(string) $this->blade('<x-radio name="remember_me" label="Remember me" />')
4343
);
4444
}
4545

4646
/** @test */
4747
public function can_have_a_description(): void
4848
{
4949
$this->assertMatchesSnapshot(
50-
$this->renderComponent('<x-radio name="remember_me" label="Remember me" description="My description" />')
50+
(string) $this->blade('<x-radio name="remember_me" label="Remember me" description="My description" />')
5151
);
5252
}
5353

@@ -62,14 +62,14 @@ public function description_can_be_slotted(): void
6262
</x-radio>
6363
HTML;
6464

65-
$this->assertMatchesSnapshot($this->renderComponent($template));
65+
$this->assertMatchesSnapshot((string) $this->blade($template));
6666
}
6767

6868
/** @test */
6969
public function checked_is_not_rendered_if_wire_model_is_present(): void
7070
{
7171
$this->assertMatchesSnapshot(
72-
$this->renderComponent('<x-radio name="remember_me" label="Remember me" wire:model="remember" checked />')
72+
(string) $this->blade('<x-radio name="remember_me" label="Remember me" wire:model="remember" checked />')
7373
);
7474
}
7575
}

tests/Components/Choice/SwitchToggleTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,55 @@ class SwitchToggleTest extends ComponentTestCase
1515
public function can_be_rendered(): void
1616
{
1717
$this->assertMatchesSnapshot(
18-
$this->renderComponent('<x-switch-toggle id="foo" />')
18+
(string) $this->blade('<x-switch-toggle id="foo" />')
1919
);
2020
}
2121

2222
/** @test */
2323
public function accepts_a_container_class(): void
2424
{
2525
$this->assertMatchesSnapshot(
26-
$this->renderComponent('<x-switch-toggle id="foo" container-class="foo" />')
26+
(string) $this->blade('<x-switch-toggle id="foo" container-class="foo" />')
2727
);
2828
}
2929

3030
/** @test */
3131
public function custom_attributes_are_applied_to_the_button(): void
3232
{
3333
$this->assertMatchesSnapshot(
34-
$this->renderComponent('<x-switch-toggle id="foo" class="foo-class" data-foo="bar" />')
34+
(string) $this->blade('<x-switch-toggle id="foo" class="foo-class" data-foo="bar" />')
3535
);
3636
}
3737

3838
/** @test */
3939
public function can_have_a_wire_model_instead_of_value(): void
4040
{
4141
$this->assertMatchesSnapshot(
42-
$this->renderComponent('<x-switch-toggle id="foo" wire:model="foo" />')
42+
(string) $this->blade('<x-switch-toggle id="foo" wire:model="foo" />')
4343
);
4444
}
4545

4646
/** @test */
4747
public function creates_a_hidden_input_when_a_name_is_used(): void
4848
{
4949
$this->assertMatchesSnapshot(
50-
$this->renderComponent('<x-switch-toggle name="foo" />')
50+
(string) $this->blade('<x-switch-toggle name="foo" />')
5151
);
5252
}
5353

5454
/** @test */
5555
public function can_have_a_label(): void
5656
{
5757
$this->assertMatchesSnapshot(
58-
$this->renderComponent('<x-switch-toggle id="foo" label="My label" />')
58+
(string) $this->blade('<x-switch-toggle id="foo" label="My label" />')
5959
);
6060
}
6161

6262
/** @test */
6363
public function can_have_a_label_on_the_left(): void
6464
{
6565
$this->assertMatchesSnapshot(
66-
$this->renderComponent('<x-switch-toggle id="foo" label="My label" label-position="left" />')
66+
(string) $this->blade('<x-switch-toggle id="foo" label="My label" label-position="left" />')
6767
);
6868
}
6969

@@ -77,14 +77,14 @@ public function can_have_on_and_off_state_icons(): void
7777
</x-switch-toggle>
7878
HTML;
7979

80-
$this->assertMatchesSnapshot($this->renderComponent($template));
80+
$this->assertMatchesSnapshot((string) $this->blade($template));
8181
}
8282

8383
/** @test */
8484
public function can_be_different_sizes(): void
8585
{
8686
$this->assertMatchesSnapshot(
87-
$this->renderComponent('<x-switch-toggle id="foo" size="lg" />')
87+
(string) $this->blade('<x-switch-toggle id="foo" size="lg" />')
8888
);
8989
}
9090
}

tests/Components/ComponentPrefixTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function getEnvironmentSetUp($app): void
2121
public function a_custom_prefix_can_be_used(): void
2222
{
2323
$this->assertMatchesSnapshot(
24-
$this->renderComponent('<x-tw-form action="http://example.com" />')
24+
(string) $this->blade('<x-tw-form action="http://example.com" />')
2525
);
2626
}
2727
}

tests/Components/ComponentTestCase.php

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,32 @@
66

77
use BladeUI\Heroicons\BladeHeroiconsServiceProvider;
88
use BladeUI\Icons\BladeIconsServiceProvider;
9-
use Gajus\Dindent\Indenter;
9+
use Illuminate\Foundation\Testing\Concerns\InteractsWithViews;
1010
use Orchestra\Testbench\TestCase;
1111
use Rawilk\FormComponents\FormComponentsServiceProvider;
12-
use Rawilk\FormComponents\Tests\Concerns\InteractsWithViews;
12+
use ReflectionProperty;
1313

1414
abstract class ComponentTestCase extends TestCase
1515
{
1616
use InteractsWithViews;
1717

18-
// protected function setUp(): void
19-
// {
20-
// parent::setUp();
21-
//
22-
// // TODO: get this line to work in github tests workflow for php8 -- prefer lowest test
23-
// // $this->artisan('view:clear');
24-
// }
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->initSession();
23+
24+
// TODO: get this line to work in github tests workflow for php8 -- prefer lowest test
25+
// $this->artisan('view:clear');
26+
}
27+
28+
protected function initSession(): void
29+
{
30+
// This is to avoid "Session store not set on request" errors for some components.
31+
$session = new ReflectionProperty(app('request'), 'session');
32+
$session->setAccessible(true);
33+
$session->setValue(app('request'), app('session')->driver('array'));
34+
}
2535

2636
protected function flashOld(array $input): void
2737
{
@@ -38,33 +48,4 @@ protected function getPackageProviders($app): array
3848
FormComponentsServiceProvider::class,
3949
];
4050
}
41-
42-
public function assertComponentRenders(string $expected, string $template, array $data = []): void
43-
{
44-
$indenter = new Indenter;
45-
46-
$blade = (string) $this->blade($template, $data);
47-
$indented = $indenter->indent($blade);
48-
$cleaned = str_replace(
49-
[' >', "\n/>", ' </div>', '> ', "\n>"],
50-
['>', ' />', "\n</div>", ">\n ", ">"],
51-
$indented
52-
);
53-
$cleaned = $this->trimExcessWhitespace($cleaned);
54-
$expected = $this->trimExcessWhitespace($expected);
55-
56-
self::assertSame($expected, $cleaned);
57-
}
58-
59-
protected function renderComponent(string $template, array $data = []): string
60-
{
61-
return (string) $this->blade($template, $data);
62-
}
63-
64-
protected function trimExcessWhitespace(string $content): string
65-
{
66-
return trim(
67-
preg_replace('/\s+/', ' ', $content)
68-
);
69-
}
7051
}

0 commit comments

Comments
 (0)