Skip to content

Commit

Permalink
add alpine integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Jan 18, 2024
1 parent 09a38ed commit ae6f279
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 2 deletions.
22 changes: 22 additions & 0 deletions docs/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ If a module supports per method exports, like `lodash` does, it is recomended to
<x-import module="lodash" as="lodash" /> <!-- 78kb -->
```

## Global helper functions

```javascript
export function foo() {
//
}

export function bar() {
//
}
```

```html
<x-import module="~/named-functions" as="helpers" />

<script type="module">
const foo = await _import("helpers", "foo");
foo();
</script>
```

## Sourcemaps

Sourcemaps are disabled by default. You may enable this by setting `BUNDLE_SOURCEMAPS_ENABLED` to true in your env file or by publishing and updating the bundle config.
Expand Down
118 changes: 118 additions & 0 deletions tests/Browser/AlpineIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Leuverink\Bundle\Tests\Browser;

use Leuverink\Bundle\Tests\DuskTestCase;

// Pest & Workbench Dusk don't play nicely together
// We need to fall back to PHPUnit syntax.

class AlpineIntegrationTest extends DuskTestCase
{
/** @test */
public function it_can_bootstrap_alpine_via_import()
{
$browser = $this->blade(<<< 'HTML'
<x-import module="~/bootstrap/alpine" />
<div
id="component"
x-text="message"
x-data="{
message: 'Hello World!'
}"
></div>
HTML);

// Doesn't raise console errors
$this->assertEmpty($browser->driver->manage()->getLog('browser'));

$browser->assertSeeIn('#component', 'Hello World!');
}

/** @test */
public function it_can_bootstrap_plugins_via_import()
{
$browser = $this->blade(<<< 'HTML'
<x-import module="~/bootstrap/alpine" />
<div
id="component"
x-text="message"
x-data="{
message: 'Hello World!'
}"
></div>
HTML);

// Doesn't raise console errors
$this->assertEmpty($browser->driver->manage()->getLog('browser'));

$browser->assertSeeIn('#component', 'Hello World!');
}

/** @test */
public function it_can_use_other_imports_inside_x_init_directive()
{
$browser = $this->blade(<<< 'HTML'
<x-import module="~/bootstrap/alpine" />
<x-import module="lodash/filter" as="filter" />
<div
id="component"
x-init="
const filter = await _import('lodash', 'filter');
let data = [
{ 'name': 'Foo', 'active': false },
{ 'name': 'Hello World!', 'active': true }
];
// Filter only active
let filtered = filter(data, o => o.active)
$el.innerHTML = filtered[0].name
"
></div>
HTML);

// Doesn't raise console errors
$this->assertEmpty($browser->driver->manage()->getLog('browser'));

$browser->assertSeeIn('#component', 'Hello World!');

}

/** @test */
public function it_can_use_other_imports_inside_x_data_directive()
{
$browser = $this->blade(<<< 'HTML'
<x-import module="~/bootstrap/alpine" />
<x-import module="lodash/filter" as="filter" />
<div
id="component"
x-data="
async init() {
const filter = await _import('lodash', 'filter');
let data = [
{ 'name': 'Foo', 'active': false },
{ 'name': 'Hello World!', 'active': true }
];
// Filter only active
let filtered = filter(data, o => o.active)
$el.innerHTML = filtered[0].name
}
"
></div>
HTML);

// Doesn't raise console errors
$this->assertEmpty($browser->driver->manage()->getLog('browser'));

$browser->assertSeeIn('#component', 'Hello World!');
}
}
30 changes: 30 additions & 0 deletions workbench/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion workbench/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"devDependencies": {
"bun": "^1.0.21",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"alpinejs": "^3.13.3",
"@alpinejs/persist": "^3.13.3"
}
}
2 changes: 1 addition & 1 deletion workbench/resources/js/alert.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default function(message) {
export default function alert(message) {
alert(message)
}
10 changes: 10 additions & 0 deletions workbench/resources/js/bootstrap/alpine-with-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Alpine from "alpinejs";
import persist from '@alpinejs/persist'

Alpine.plugin(persist)

export default (() => {
window.Alpine = Alpine;

Alpine.start();
})();
7 changes: 7 additions & 0 deletions workbench/resources/js/bootstrap/alpine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Alpine from "alpinejs";

export default (() => {
window.Alpine = Alpine;

Alpine.start();
})();

0 comments on commit ae6f279

Please sign in to comment.