Skip to content

Commit

Permalink
fix: hot
Browse files Browse the repository at this point in the history
refactor Components collection
  • Loading branch information
lee-to committed Dec 13, 2023
1 parent d3f8e4c commit b5df409
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 2 deletions.
3 changes: 3 additions & 0 deletions resources/views/components/components.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
@props([
'components' => []
])
@foreach($components as $component)
@continue(!isSeeWhenExists($component))

Expand Down
6 changes: 4 additions & 2 deletions src/Collections/MoonShineRenderElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ abstract class MoonShineRenderElements extends Collection
protected function extractOnly($elements, string $type, array &$data): void
{
foreach ($elements as $element) {
if ($element instanceof HasFields) {
$this->extractOnly($element->getFields(), $type, $data);
}

if ($element instanceof $type) {
$data[] = $element;
} elseif ($element instanceof HasFields) {
$this->extractOnly($element->getFields(), $type, $data);
}
}
}
Expand Down
90 changes: 90 additions & 0 deletions tests/Unit/PageComponentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

use MoonShine\Components\FormBuilder;
use MoonShine\Components\TableBuilder;
use MoonShine\Decorations\Block;
use MoonShine\Decorations\LineBreak;
use MoonShine\Decorations\Tab;
use MoonShine\Decorations\Tabs;
use MoonShine\Fields\Field;
use MoonShine\Fields\Switcher;
use MoonShine\Pages\PageComponents;

uses()->group('core');
uses()->group('new');

beforeEach(function (): void {
$this->data = Block::make([
FormBuilder::make()->fields([
Tabs::make([
Tab::make([
LineBreak::make()->name('line-break'),
FormBuilder::make()->name('inner-form')->fields([
Switcher::make('Switcher')
]),

]),
Tab::make([
TableBuilder::make()
->name('first-table'),

TableBuilder::make()
->name('second-table')
]),
])
])->name('parent-form'),

TableBuilder::make()
->name('parent-table')
])->name('block');

$this->collection = PageComponents::make([
$this->data
]);
});

it('find table', function () {
expect($this->collection->findTable('second-table'))
->toBeInstanceOf(TableBuilder::class)
->getName()
->toBe('second-table');
});

it('only forms', function () {
expect($this->collection->onlyForms())
->toHaveCount(2)
->each->toBeInstanceOf(FormBuilder::class);
});

it('only tables', function () {
expect($this->collection->onlyTables())
->toHaveCount(3)
->each->toBeInstanceOf(TableBuilder::class);
});

it('only fields', function () {
expect($this->collection->onlyFields())
->toHaveCount(1)
->each->toBeInstanceOf(Field::class);
});

it('find form', function () {
expect($this->collection->findForm('inner-form'))
->toBeInstanceOf(FormBuilder::class)
->getName()
->toBe('inner-form');
});

it('find field', function () {
expect($this->collection->onlyFields()->findByColumn('switcher'))
->toBeInstanceOf(Switcher::class)
->name()
->toBe('switcher');
});

it('find by name', function () {
expect($this->collection->findByName('line-break'))
->toBeInstanceOf(LineBreak::class)
->getName()
->toBe('line-break');
});

0 comments on commit b5df409

Please sign in to comment.