Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Jan 11, 2024
1 parent eb79118 commit b120619
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 31 deletions.
3 changes: 2 additions & 1 deletion src/BundleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public function bundleContents($fileName): Response
// ->header('Last-Modified', 'TODO');
}

private function hash($input, $length = 12) {
public function hash($input, $length = 12): string
{
$hash = hash('sha256', $input);
return substr($hash, 0, $length);
}
Expand Down
9 changes: 6 additions & 3 deletions src/Contracts/BundleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public function __construct(Bundler $bundler);
/** Bundles a given script */
public function bundle(string $script): SplFileInfo;

/** Get the bundle config */
public function config(): RepositoryContract;

/** Get an instance of the temporary disk the bundler reads from */
public function tempDisk(): Filesystem;

Expand All @@ -25,9 +28,9 @@ public function buildDisk(): Filesystem;
/** Get the contents of a given bundle */
public function bundleContents($fileName): Response;

/** Get the bundle config */
public function config(): RepositoryContract;

/** Get the contents of a given chunk */
// public function chunkContents($fileName): Response;

/** Hashes a given string */
public function hash($input, $length = 12): string;
}
6 changes: 5 additions & 1 deletion tests/Feature/Commands/BuildCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

use Leuverink\Bundle\BundleManager;

it('imports from relative path alias', function() {
it('scans glob patterns', function() {

})->todo();

it('generates a bundle', function() {
$manager = BundleManager::new();

// Scan the fixtures dir as build path
Expand Down
97 changes: 71 additions & 26 deletions tests/Feature/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use Leuverink\Bundle\BundleManager;
use Leuverink\Bundle\Exceptions\BundlingFailedException;

it('transpiles JavaScript')->bundle(
<<< JS
const foo = 'bar'
Expand Down Expand Up @@ -39,27 +42,29 @@
);


it('supports inline synchronous imports')
->todo()
->bundle("import filter from 'lodash/filter'")
->toContain('node_modules/lodash/filter.js');
// These two should be browser tests? or can we get the file's hash some other wayy?
test('generated bundles are reachable over http', function() {
$js = <<< JS
const filter = await import('~/alert')
JS;

$this->artisan('bundle:clear');
$manager = BundleManager::new();
$file = $manager->hash($js) . '.min.js';

it('supports code splitting for dynamic imports')
->todo()
->bundle(
<<< JS
const filter = await import('lodash/filter')
JS
)->transpilesTo(
<<< JS
var filter = await import("./x-script/chunks/filter-GWHK62RL.js");
JS
);
$this->get(
route('x-bundle', $file)
)->assertNotFound();

// These two should be browser tests? or can we get the file's hash some other wayy?
test('generated bundles are reachable over http')->todo();
test('generated chunks are reachable over http')->skip('Code splitting not implemented');
$manager->bundle($js);

$this->get(
route('x-bundle', $file)
)->assertOk();
});

test('generated chunks are reachable over http')
->skip('Code splitting not implemented');


it('generates sourcemaps when enabled')
Expand All @@ -68,7 +73,7 @@
)
->bundle(
<<< JS
const filter = await import('lodash/filter')
const filter = await import('~/alert')
JS
)
->content()
Expand All @@ -77,18 +82,58 @@
it('doesnt generate sourcemaps by default')
->bundle(
<<< JS
const filter = await import('lodash/filter')
const filter = await import('~/alert')
JS
)
->content()
->not->toContain('//# debugId');


it('imports from node_modules are chunked')->todo();
it('imports from outside node_modules are inlined (due to issue with Bun)')->todo();
// 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();


// So the user can import their own js scripts from the resources/js dir
it('supports custom node paths')->todo();
it('creates a single bundle when no imports are used')->todo();
it('generates a single chunk when two sourcefiles use the same dependency')->todo();
it('is unable to resolve local scripts by their relative path', function() {
expect(function() {
bundle(
<<< JS
const filter = await import('./resources/js/alert')
JS
);
})->toThrow(BundlingFailedException::class);
});

it('is able to resolve local scripts when aliased in jsconfig.json', function() {
expect(function() {
// ~/ is aliased in jsconfig.json
bundle(
<<< JS
const filter = await import('~/alert')
JS
);
})->not->toThrow(BundlingFailedException::class);
});


it('creates a single bundle when no imports are used')->skip('Code splitting not implemented');
it('generates a single chunk when two sourcefiles use the same dependency')->skip('Code splitting not implemented');


// it('supports inline synchronous imports')
// ->todo()
// ->bundle("import filter from 'lodash/filter'")
// ->toContain('node_modules/lodash/filter.js');

// Probably not possible. TODO: Create issue in Bun repo
// it('supports code splitting for dynamic imports')
// ->todo()
// ->bundle(
// <<< JS
// const filter = await import('lodash/filter')
// JS
// )->transpilesTo(
// <<< JS
// var filter = await import("./x-script/chunks/filter-GWHK62RL.js");
// JS
// );

0 comments on commit b120619

Please sign in to comment.