Skip to content

Commit

Permalink
Refactored how event was triggered
Browse files Browse the repository at this point in the history
  • Loading branch information
asantibanez committed Jul 25, 2023
1 parent 1f26a9e commit 46d82cf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion resources/js/treeMapChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const treeMapChart = () => {

const block = data[series[seriesIndex].name][dataPointIndex]

component.emit(onBlockClickEventName, block)
component.call('onBlockClick', block)
},
}
},
Expand Down
11 changes: 11 additions & 0 deletions src/Charts/LivewireTreeMapChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ public function mount(TreeMapChartModel $treeMapChartModel)
$this->treeMapChartModel = $treeMapChartModel->toArray();
}

public function onBlockClick($block)
{
$onBlockClickEventName = data_get($this->treeMapChartModel, 'onBlockClickEventName');

if ($onBlockClickEventName === null) {
return;
}

$this->dispatch($onBlockClickEventName, $block);
}

public function render()
{
return view('livewire-charts::livewire-tree-map-chart');
Expand Down
19 changes: 19 additions & 0 deletions tests/LivewireTreeMapChartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,23 @@ public function can_build_component()
//Assert
$this->assertNotNull($component);
}

/** @test */
public function should_emit_event_if_present()
{
//Arrange
$component = $this->buildComponent();

$treeMapChartModel = $component->treeMapChartModel;

data_set($treeMapChartModel, 'onBlockClickEventName', 'custom-event');

$component->set('treeMapChartModel', $treeMapChartModel);

//Act
$component->runAction('onBlockClick', []);

//Assert
$component->assertDispatched('custom-event');
}
}

0 comments on commit 46d82cf

Please sign in to comment.