Skip to content

Commit

Permalink
test: create new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
epessine committed Nov 18, 2023
1 parent 1546306 commit f58fdb9
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
52 changes: 52 additions & 0 deletions tests/Feature/ChartTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use Axis\Attributes\Axis;
use Axis\Chart;
use Axis\Charts\ChartJs;
use Livewire\Component;

use function Pest\Livewire\livewire;

test('it should return chartjs instance with proper props', function () {
$config = [
'type' => 'bar',
'data' => [
'labels' => ['a', 'b', 'c'],
'datasets' => [
['label' => 'dataset a', 'data' => [1, 2, 3], 'borderColor' => 'red'],
['label' => 'dataset b', 'data' => [2, 3, 5], 'borderColor' => 'green'],
['label' => 'dataset c', 'data' => [6, 4, 9], 'borderColor' => 'yellow'],
],
'options' => [
'responsive' => true,
],
],
];

$chart = Chart::chartjs($config);

expect(invade($chart, 'config'))->toBe($config);
});

test('it should properly set axis attribute when called on livewire context for chartjs', function () {
$component = new class extends Component
{
#[Axis]
public function chartjs(): ChartJs
{
return Chart::chartjs([]);
}

public function render(): string
{
return '<div></div>';
}
};

$component = livewire($component::class);

$chartjs = $component->get('chartjs');

expect(invade($chartjs, 'attribute'))->toBeInstanceOf(Axis::class);
expect(invade($chartjs, 'component'))->toBeInstanceOf(Component::class);
});
5 changes: 0 additions & 5 deletions tests/Unit/ExampleTest.php

This file was deleted.

48 changes: 48 additions & 0 deletions tests/Unit/Traits/ParsesJsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use Axis\Traits\ParsesJs;

beforeEach(function () {
$this->class = new class
{
use ParsesJs {
js as traitJs;
minify as traitMinify;
}

public function js(mixed $data): string
{
return $this->traitJs($data);
}

public function minify(string $data): string
{
return $this->traitMinify($data);
}
};
});

test('it should minify string', function () {
$string = <<<'JS'
const foo = 'bar';
const bar = 'foo';
if (foo !== bar) {
console.log('foo is not bar');
}
JS;

expect($this->class->minify($string))
->toBe("const foo = 'bar';const bar = 'foo';if (foo !== bar) {console.log('foo is not bar');}");
});

test('it should parse data into minified js', function () {
$data = [
'foo' => 'bar',
'bar' => 'foo',
'foobar' => 'barfoo',
];

expect($this->class->js($data))
->toBe("JSON.parse('{\u0022foo\u0022:\u0022bar\u0022,\u0022bar\u0022:\u0022foo\u0022,\u0022foobar\u0022:\u0022barfoo\u0022}')");
});

0 comments on commit f58fdb9

Please sign in to comment.