Skip to content

Commit

Permalink
add test to export ids for group blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilReinking committed Aug 20, 2024
1 parent 37229af commit 6cf95d6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
9 changes: 7 additions & 2 deletions app/Models/FormBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,18 @@ public function updateInteractionSequence(array $sequence)

public function toTemplate()
{
$blocks = $this->only(self::TEMPLATE_ATTRIBUTES);
$attributes = $this->only(self::TEMPLATE_ATTRIBUTES);

// if the block is a group, we need to add an id attribute
if ($this->type === FormBlockType::group) {
$attributes['id'] = $this->uuid;
}

$interactions = $this->formBlockInteractions->map(function ($interactions) {
return $interactions->toTemplate();
})->toArray();

return array_merge($blocks, [
return array_merge($attributes, [
'formBlockInteractions' => $interactions,
]);
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Feature/FormTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,35 @@
$this->assertNotNull($action);
});

test('group blocks have an Id attribute and children have a parent Id', function () {
$form = Form::factory()
->create([
'name' => 'Test Form',
]);

$groupBlock = FormBlock::factory()->create([
'form_id' => $form->id,
'type' => FormBlockType::group,
]);

$childBlock = FormBlock::factory()->create([
'form_id' => $form->id,
'type' => FormBlockType::none,
'parent_block' => $groupBlock->uuid,
]);

$response = $this->actingAs($form->user)
->json('GET', route('api.forms.template-export', [
'form' => $form->uuid,
]))->assertStatus(200);

// assert that the group block has an id
$this->assertArrayHasKey('id', $response->json('blocks.0'));

// assert that the child block has a parent id
$this->assertArrayHasKey('parent_block', $response->json('blocks.1'));
});

test('can import a string template for an existing form', function () {
/** @var User $user */
$user = User::factory()->withTeam()->create();
Expand Down

0 comments on commit 6cf95d6

Please sign in to comment.