Skip to content

Commit

Permalink
test: Options
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-to committed Mar 9, 2025
1 parent fb0c999 commit c618024
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
11 changes: 8 additions & 3 deletions src/Support/src/DTOs/Select/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public function getProperties(string $value): OptionProperty
return $properties;
}

$properties = $this->normalizeProperties($properties);

return new OptionProperty(...$properties);
return new OptionProperty(
...$this->normalizeProperties($properties)
);
}

/**
Expand Down Expand Up @@ -148,6 +148,11 @@ public function toRaw(): array
];
}

/**
* @param array{image: OptionImage} $properties
*
* @return array
*/
private function normalizeProperties(array $properties): array
{
if (! isset($properties['image']) || $properties['image'] instanceof OptionImage) {
Expand Down
2 changes: 0 additions & 2 deletions src/UI/src/Components/MoonShineComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ public function __construct(
}

$this->booted();

$this->class('moonshine-component');
}

protected function booted(): void
Expand Down
44 changes: 37 additions & 7 deletions tests/Unit/Fields/SelectFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use MoonShine\Support\DTOs\Select\Option;
use MoonShine\Support\DTOs\Select\OptionGroup;
use MoonShine\Support\DTOs\Select\OptionImage;
use MoonShine\Support\DTOs\Select\OptionProperty;
use MoonShine\Support\DTOs\Select\Options;
Expand Down Expand Up @@ -185,8 +186,8 @@
});
});

describe('select field with images (passed via arrays)', function () {
it('renders images passed as strings', function () {
describe('select field with images', function () {
it('by array', function () {
$field = Select::make('Select field with images')
->options([
1 => 'Option 1',
Expand All @@ -212,7 +213,7 @@
]);
});

it('renders images passed via OptionImage object', function () {
it('array with OptionImage object', function () {
$field = Select::make('Select field with images')
->options([
1 => 'Option 1',
Expand All @@ -237,10 +238,8 @@
'objectFit' => ObjectFit::COVER->value,
]);
});
});

describe('select field with images (passed via Options object)', function () {
it('renders images passed as strings', function () {
it('objects with image string', function () {
$options = new Options([
new Option(
'Option 1',
Expand All @@ -253,6 +252,14 @@
true,
properties: new OptionProperty('image2.png')
),
new OptionGroup('Group', new Options([
new Option(
'Option 3',
'3',
true,
properties: new OptionProperty('image3.png')
),
]))
]);

$field = Select::make('Select field with images')
Expand All @@ -270,10 +277,15 @@
'width' => 10,
'height' => 10,
'objectFit' => ObjectFit::COVER->value,
])->and($result['values']['Group']['values'][0]['properties']['image'])->toBe([
'src' => 'image3.png',
'width' => 10,
'height' => 10,
'objectFit' => ObjectFit::COVER->value,
]);
});

it('renders images passed via OptionImage', function () {
it('only objects', function () {
$options = new Options([
new Option(
'Option 1',
Expand All @@ -298,6 +310,19 @@
)
)
),
new OptionGroup('Group', new Options([
new Option(
'Option 3',
'3',
properties: new OptionProperty(
new OptionImage(
src: 'image3.png',
width: 8,
objectFit: ObjectFit::CONTAIN
)
)
),
]))
]);

$field = Select::make('Select field with images')
Expand All @@ -317,6 +342,11 @@
'width' => 8,
'height' => 10,
'objectFit' => ObjectFit::CONTAIN->value,
])->and($result['values']['Group']['values'][0]['properties']['image'])->toBe([
'src' => 'image3.png',
'width' => 8,
'height' => 10,
'objectFit' => ObjectFit::CONTAIN->value,
]);
});

Expand Down

0 comments on commit c618024

Please sign in to comment.