Skip to content

Commit

Permalink
add testing fake feature
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Jan 16, 2024
1 parent 9280606 commit f7db3b4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
16 changes: 15 additions & 1 deletion src/BundleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Leuverink\Bundle;

use Mockery;
use Throwable;
use SplFileInfo;
use Mockery\MockInterface;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
use Leuverink\Bundle\Traits\Constructable;
Expand Down Expand Up @@ -49,7 +51,7 @@ public function bundle(string $script): SplFileInfo
);
} catch (Throwable $e) {
$this->cleanup($file);
throw $e; // TODO: Consider raising a browser console error or whoops in a dialog instead
throw $e;
} finally {
$this->cleanup($file);
}
Expand Down Expand Up @@ -121,4 +123,16 @@ private function cleanup($file)
rmdir($this->tempDisk()->path(''));
}
}

public static function fake(): MockInterface
{
$mock = Mockery::mock(BundleManagerContract::class, fn ($mock) => $mock
->makePartial()
->shouldReceive('bundle')
->andReturn(new SplFileInfo('mock.js'))
->atLeast()->once()
);

return app()->instance(BundleManagerContract::class, $mock);
}
}
3 changes: 1 addition & 2 deletions src/Components/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ class Import extends Component
public function __construct(
public string $module,
public ?string $as = null,
public bool $inline = false // TODO: Implement this
public bool $inline = false
) {
}

public function render()
{
// Bundle it up
try {
return $this->bundle();
} catch (BundlingFailedException $e) {
Expand Down
4 changes: 4 additions & 0 deletions src/Contracts/BundleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Leuverink\Bundle\Contracts;

use SplFileInfo;
use Mockery\MockInterface;
use Illuminate\Http\Response;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Contracts\Config\Repository as RepositoryContract;
Expand Down Expand Up @@ -31,4 +32,7 @@ public function bundleContents($fileName): Response;

/** Hashes a given string */
public function hash($input, $length = 12): string;

/** Mock for testing */
public static function fake(): MockInterface;
}
2 changes: 1 addition & 1 deletion src/Traits/Constructable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

trait Constructable
{
public static function new(): self
public static function new(): mixed
{
$interfaces = class_implements(static::class);

Expand Down
7 changes: 7 additions & 0 deletions tests/Feature/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Leuverink\Bundle\BundleManager;
use Illuminate\Support\Facades\Blade;
use Leuverink\Bundle\Components\Import;
use Leuverink\Bundle\Exceptions\BundlingFailedException;

Expand Down Expand Up @@ -185,6 +186,12 @@
it('serves chunks over http')
->skip('Code splitting not implemented');

it('can be faked', function () {
BundleManager::fake();

Blade::renderComponent(new Import('~/foo', 'bar'));
});

// Probably not possible. TODO: Create issue in Bun repo
// it('imports from node_modules are chunked')->todo();
// it('imports from outside node_modules are inlined (due to issue with Bun)')->todo();
Expand Down

0 comments on commit f7db3b4

Please sign in to comment.