Skip to content

Commit

Permalink
test: added more test for SchemaCreate livewire component
Browse files Browse the repository at this point in the history
  • Loading branch information
armezit committed Jan 2, 2024
1 parent 67f11b8 commit d530f8f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ public function getFieldTypesProperty()
return CodePoolFieldType::labels();
}

public function addField()
public function addField(?string $name = null, ?string $type = null): void
{
$this->fields[] = new CodePoolSchemaField(
name: null,
type: CodePoolFieldType::Raw,
name: $name,
type: $type !== null ? CodePoolFieldType::from($type) : CodePoolFieldType::Raw,
order: count($this->fields) + 1
);
}

public function updatingFields(&$value, $name): void
{
if (preg_match('/(\d+)\.(.+)/', $name, $matches) !== false) {
if ($matches[2] === 'type') {
if (isset($matches[2]) && $matches[2] === 'type') {
$value = CodePoolFieldType::from($value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,98 @@ class SchemaCreateTest extends TestCase
use RefreshDatabase;
use WithFaker;

/** @test */
public function component_mounts_correctly()
public function setUp(): void
{
parent::setUp();

$staff = Staff::factory()->create([
'admin' => true,
]);
$this->actingAs($staff, 'staff');
}

private function getCodePoolSchemaTable(): string
{
return (new CodePoolSchema)->getTable();
}

LiveWire::actingAs($staff, 'staff')
->test(SchemaCreate::class)
/** @test */
public function component_mounts_correctly()
{
LiveWire::test(SchemaCreate::class)
->assertViewIs('lunarphp-virtual-product::livewire.components.code-pool.schemas.create');
}

/** @test */
public function can_add_field_correctly()
public function schema_name_is_required()
{
$staff = Staff::factory()->create([
'admin' => true,
]);
Livewire::test(SchemaCreate::class)
->set('schema.name', '')
->call('create')
->assertHasErrors(['schema.name' => 'required']);
}

/** @test */
public function schema_fields_validation_is_correct()
{
Livewire::test(SchemaCreate::class)
->set('fields', new CodePoolSchemaFieldsList(
fields: CodePoolSchemaField::collection([])
))
->call('create')
->assertHasErrors(['fields' => 'required']);

Livewire::test(SchemaCreate::class)
->set('fields', new CodePoolSchemaFieldsList(
fields: CodePoolSchemaField::collection([
[
'name' => '',
'type' => CodePoolFieldType::Raw->value,
'order' => 1,
]
])
))
->call('create')
->assertHasErrors(['fields.0.name' => 'required']);
}

/** @test */
public function can_add_fields_correctly()
{
$fields = [
['name' => 'int-field', 'type' => CodePoolFieldType::Integer->value],
['name' => 'raw-field', 'type' => CodePoolFieldType::Raw->value],
['name' => 'int-field', 'type' => CodePoolFieldType::Integer->value, 'order' => 1],
['name' => 'raw-field', 'type' => CodePoolFieldType::Raw->value, 'order' => 2],
];

LiveWire::actingAs($staff, 'staff')
->test(SchemaCreate::class)
->call('addField', $fields[0])
->call('addField', $fields[1])
->assertSet('fields', new CodePoolSchemaFieldsList(
fields: CodePoolSchemaField::collection(array_merge($fields, [['order' => 1], ['order' => 2]]))
));
LiveWire::test(SchemaCreate::class)
->set('fields', new CodePoolSchemaFieldsList(
fields: CodePoolSchemaField::collection([])
))
->call('addField', ...$fields[0])
->call('addField', ...$fields[1])
->assertCount('fields', 2);
}

/** @test */
public function can_create_schema()
{
$staff = Staff::factory()->create([
'admin' => true,
]);
$fields = [
['name' => 'int-field', 'type' => CodePoolFieldType::Integer->value, 'order' => 1],
['name' => 'raw-field', 'type' => CodePoolFieldType::Raw->value, 'order' => 2],
];

LiveWire::test(SchemaCreate::class)
->set('schema.name', 'Test Schema')
->set('fields', new CodePoolSchemaFieldsList(
fields: CodePoolSchemaField::collection([])
))
->call('addField', ...$fields[0])
->call('addField', ...$fields[1])
->call('create');

LiveWire::actingAs($staff, 'staff')
->test(SchemaCreate::class)
->call('addField', [
'name' => 'int-field',
'type' => CodePoolFieldType::Integer,
])
->call('addField', [
'name' => 'raw-field',
'type' => CodePoolFieldType::Raw,
])
->assertSet('fields', 7);
$this->assertDatabaseHas($this->getCodePoolSchemaTable(), [
'name' => 'Test Schema',
'fields' => $this->castAsJson($fields),
]);
}
}

0 comments on commit d530f8f

Please sign in to comment.