Skip to content

Commit

Permalink
test: create tests
Browse files Browse the repository at this point in the history
  • Loading branch information
epessine committed Jun 1, 2024
1 parent 4331675 commit 6fc8ebb
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 5 deletions.
5 changes: 0 additions & 5 deletions tests/Feature.php

This file was deleted.

3 changes: 3 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

uses(Tests\TestCase::class)->in('*');
30 changes: 30 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Tests;

use Illuminate\Contracts\Config\Repository;
use Illuminate\Encryption\Encrypter;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Vessel\VesselManager;

abstract class TestCase extends BaseTestCase
{
use WithWorkbench;

protected $enablesPackageDiscoveries = true;

protected function defineRoutes($router): void
{
$router->get('/context', fn (): string => VesselManager::getContextId());
}

protected function defineEnvironment($app)
{
tap($app['config'], function (Repository $config) {
$config->set('app.key', 'base64:'.base64_encode(
Encrypter::generateKey($config['app.cipher'])
));
});
}
}
79 changes: 79 additions & 0 deletions tests/Unit/Attributes/VesselTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

use Illuminate\View\ViewException;
use Livewire\Component;
use Vessel\Attributes\Vessel;
use Vessel\BaseVessel;
use Vessel\Exceptions\CannotCallVesselDirectlyException;

use function Pest\Livewire\livewire;

class TestVessel extends BaseVessel
{
public string $filter = 'one';
}

class One extends Component
{
#[Vessel]
public function state(): string
{
return TestVessel::class;
}

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

class Two extends Component
{
#[Vessel]
public function state(): string
{
return TestVessel::class;
}

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

test('should get and set vessel properties', function (): void {
$one = livewire(One::class);
$two = livewire(Two::class);

expect($one->get('state.filter'))->toBe($two->get('state.filter'));

$one->state->filter = 'two';

expect($one->state->filter)->toBe('two');
expect($one->get('state.filter'))->toBe($two->get('state.filter'));

$two->state->filter = 'one';

expect($one->state->filter)->toBe('one');
expect($one->get('state.filter'))->toBe($two->get('state.filter'));
});

test('should throw exception when calling vessel method directly', function (): void {
livewire(One::class)->call('state');
})->throws(CannotCallVesselDirectlyException::class);

test('should throw exception when vessel method does not return vessel', function (): void {
livewire((new class() extends Component
{
#[Vessel]
public function state(): string
{
return 'invalid';
}

public function render(): string
{
return '<div></div>';
}
})::class)->get('state');
})->throws(ViewException::class);
45 changes: 45 additions & 0 deletions tests/Unit/BaseVesselTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Livewire\Component;
use Vessel\BaseVessel;

use function Livewire\invade;

beforeEach(function (): void {
$component = new class() extends Component
{
public function render(): string
{
return '<div></div>';
}
};

$this->vessel = new class(app('livewire')->new($component::class)) extends BaseVessel
{
public string $property;

public function init(): void
{
$this->property = 'value';
}
};
});

test('should get public properties', function (): void {
$props = $this->vessel->getPublicProperties();

expect(count($props))->toBe(1);
expect($props[0]->getName())->toBe('property');
});

test('should transform to array', function (): void {
$props = invade($this->vessel)->toArray();

expect($props)->toBe(['property' => 'value']);
});

test('should get lifetime from session by default', function (): void {
$lifetime = invade($this->vessel)->lifetime;

expect($lifetime)->toBe(120);
});
9 changes: 9 additions & 0 deletions tests/Unit/VesselManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Vessel\VesselManager;

use function Pest\Laravel\get;

test('should set context id from header', function (): void {
get('/context', [VesselManager::CONTEXT_HEADER => 'test'])->assertContent('test');
});

0 comments on commit 6fc8ebb

Please sign in to comment.