Skip to content

Commit da63411

Browse files
committed
test
1 parent 852de44 commit da63411

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

tests/SelectCascadingTest.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
<?php
2+
3+
use Backstage\Fields\Fields\Select;
4+
use Backstage\Fields\Models\Field;
5+
use Filament\Forms\Components\Select as Input;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
it('creates a select field with cascading configuration', function () {
9+
$field = new Field([
10+
'name' => 'Test Cascading Select',
11+
'field_type' => 'select',
12+
'config' => [
13+
'parentField' => 'category_id',
14+
'parentRelationship' => 'categories',
15+
'childRelationship' => 'products',
16+
'parentKey' => 'id',
17+
'childKey' => 'id',
18+
'parentValue' => 'name',
19+
'childValue' => 'name',
20+
],
21+
]);
22+
23+
$input = Select::make('test_field', $field);
24+
25+
expect($input)->toBeInstanceOf(Input::class);
26+
expect($input->getName())->toBe('test_field');
27+
expect($input->getLabel())->toBe('Test Cascading Select');
28+
});
29+
30+
it('creates a select field with live reactive options when cascading is configured', function () {
31+
$field = new Field([
32+
'name' => 'Test Cascading Select',
33+
'field_type' => 'select',
34+
'config' => [
35+
'parentField' => 'category_id',
36+
'parentRelationship' => 'categories',
37+
'childRelationship' => 'products',
38+
'parentKey' => 'id',
39+
'childKey' => 'id',
40+
'parentValue' => 'name',
41+
'childValue' => 'name',
42+
],
43+
]);
44+
45+
$input = Select::make('test_field', $field);
46+
47+
// Check if the field has live() method applied
48+
$reflection = new ReflectionClass($input);
49+
$liveProperty = $reflection->getProperty('isLive');
50+
$liveProperty->setAccessible(true);
51+
52+
expect($liveProperty->getValue($input))->toBeTrue();
53+
});
54+
55+
it('creates a regular select field when no cascading is configured', function () {
56+
$field = new Field([
57+
'name' => 'Test Regular Select',
58+
'field_type' => 'select',
59+
'config' => [
60+
'options' => ['option1' => 'Option 1', 'option2' => 'Option 2'],
61+
],
62+
]);
63+
64+
$input = Select::make('test_field', $field);
65+
66+
// Check if the field has live() method applied
67+
$reflection = new ReflectionClass($input);
68+
$liveProperty = $reflection->getProperty('isLive');
69+
$liveProperty->setAccessible(true);
70+
71+
$isLive = $liveProperty->getValue($input);
72+
expect($isLive)->toBeNull(); // Regular select fields don't have isLive set
73+
});
74+
75+
it('normalizes select values correctly for single selection', function () {
76+
$field = new Field([
77+
'ulid' => 'test_field',
78+
'config' => ['multiple' => false],
79+
]);
80+
81+
$record = new class extends Model {
82+
public $valueColumn = 'values';
83+
public $values = ['test_field' => 'single_value'];
84+
};
85+
86+
$data = ['values' => []];
87+
$data = Select::mutateFormDataCallback($record, $field, $data);
88+
89+
expect($data['values']['test_field'])->toBe('single_value');
90+
});
91+
92+
it('normalizes select values correctly for multiple selection', function () {
93+
$field = new Field([
94+
'ulid' => 'test_field',
95+
'config' => ['multiple' => true],
96+
]);
97+
98+
$record = new class extends Model {
99+
public $valueColumn = 'values';
100+
public $values = ['test_field' => '["value1", "value2"]'];
101+
};
102+
103+
$data = ['values' => []];
104+
$data = Select::mutateFormDataCallback($record, $field, $data);
105+
106+
expect($data['values']['test_field'])->toBe(['value1', 'value2']);
107+
});
108+
109+
it('handles null values correctly', function () {
110+
$field = new Field([
111+
'ulid' => 'test_field',
112+
'config' => ['multiple' => false],
113+
]);
114+
115+
$record = new class extends Model {
116+
public $valueColumn = 'values';
117+
public $values = ['test_field' => null];
118+
};
119+
120+
$data = ['values' => []];
121+
$data = Select::mutateFormDataCallback($record, $field, $data);
122+
123+
expect($data['values'])->toHaveKey('test_field');
124+
expect($data['values']['test_field'])->toBeNull();
125+
});
126+
127+
it('handles empty arrays for multiple selection', function () {
128+
$field = new Field([
129+
'ulid' => 'test_field',
130+
'config' => ['multiple' => true],
131+
]);
132+
133+
$record = new class extends Model {
134+
public $valueColumn = 'values';
135+
public $values = ['test_field' => null];
136+
};
137+
138+
$data = ['values' => []];
139+
$data = Select::mutateFormDataCallback($record, $field, $data);
140+
141+
expect($data['values'])->toHaveKey('test_field');
142+
expect($data['values']['test_field'])->toBe([]);
143+
});

0 commit comments

Comments
 (0)