-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efe513d
commit 657b1a4
Showing
4 changed files
with
161 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace LivewireDuskTestbench; | ||
|
||
use Illuminate\Testing\Constraints\SeeInOrder; | ||
use PHPUnit\Framework\Assert as PHPUnit; | ||
|
||
class DuskBrowserMixin | ||
{ | ||
public function assertSeeInOrder() | ||
{ | ||
return function ($selector, $contents) { | ||
/** @var \Laravel\Dusk\Browser $this */ | ||
$fullSelector = $this->resolver->format($selector); | ||
|
||
$element = $this->resolver->findOrFail($selector); | ||
|
||
$contentsString = implode(', ', $contents); | ||
|
||
PHPUnit::assertThat( | ||
array_map('e', $contents), | ||
new SeeInOrder($element->getText()), | ||
"Did not see expected contents [{$contentsString}] within element [{$fullSelector}]." | ||
); | ||
|
||
return $this; | ||
}; | ||
} | ||
|
||
public function assertIsVisibleInContainer() | ||
{ | ||
$script = $this->isVisibleScript(); | ||
|
||
return function ($container, $selector) use ($script) { | ||
/** @var \Laravel\Dusk\Browser $this */ | ||
$fullSelector = $this->resolver->format($selector); | ||
$fullContainer = $this->resolver->format($container); | ||
|
||
$this->resolver->findOrFail($selector); | ||
$this->resolver->findOrFail($container); | ||
|
||
PHPUnit::assertTrue( | ||
$this->driver->executeScript(sprintf($script, $fullSelector, $fullContainer)), | ||
"Element [{$fullSelector}] is not visible in [{$fullContainer}]" | ||
); | ||
|
||
return $this; | ||
}; | ||
} | ||
|
||
public function assertIsNotVisibleInContainer() | ||
{ | ||
$script = $this->isVisibleScript(); | ||
|
||
return function ($container, $selector) use ($script) { | ||
/** @var \Laravel\Dusk\Browser $this */ | ||
$fullSelector = $this->resolver->format($selector); | ||
$fullContainer = $this->resolver->format($container); | ||
|
||
$this->resolver->findOrFail($selector); | ||
$this->resolver->findOrFail($container); | ||
|
||
PHPUnit::assertFalse( | ||
$this->driver->executeScript(sprintf($script, $fullSelector, $fullContainer)), | ||
"Element [{$fullSelector}] is visible in [{$fullContainer}]" | ||
); | ||
|
||
return $this; | ||
}; | ||
} | ||
|
||
protected function isVisibleScript() | ||
{ | ||
return ' | ||
let elRect = document.querySelector(`%1$s`).getBoundingClientRect() | ||
let containerRect = document.querySelector(`%2$s`).getBoundingClientRect() | ||
return containerRect.top < elRect.bottom && containerRect.bottom > elRect.top | ||
'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace LivewireDuskTestbench\Tests\Browser; | ||
|
||
use Livewire\Component; | ||
use Livewire\Livewire; | ||
|
||
class DuskBrowserMixinsTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function assert_see_in_order_macro_works() | ||
{ | ||
Livewire::visit(new class extends Component | ||
{ | ||
public function render() | ||
{ | ||
return <<< 'HTML' | ||
<div> | ||
<ul dusk="list"> | ||
<li>bob</li> | ||
<li>john</li> | ||
<li>bill</li> | ||
</ul> | ||
</div> | ||
HTML; | ||
} | ||
}) | ||
->assertSeeInOrder('@list', ['bob', 'john', 'bill']); | ||
} | ||
|
||
/** @test */ | ||
public function assert_is_visibile_in_container_works() | ||
{ | ||
Livewire::visit(new class extends Component | ||
{ | ||
public function render() | ||
{ | ||
return <<< 'HTML' | ||
<div> | ||
<ul style="height:10px" dusk="list"> | ||
<li style="height:10px" dusk="bob">bob</li> | ||
<li style="height:10px" dusk="john">john</li> | ||
<li style="height:10px" dusk="bill">bill</li> | ||
</ul> | ||
</div> | ||
HTML; | ||
} | ||
}) | ||
->assertIsVisibleInContainer('@list', '@bob'); | ||
} | ||
|
||
/** @test */ | ||
public function assert_is_not_visibile_in_container_works() | ||
{ | ||
Livewire::visit(new class extends Component | ||
{ | ||
public function render() | ||
{ | ||
return <<< 'HTML' | ||
<div> | ||
<ul style="height:10px" dusk="list"> | ||
<li style="height:10px" dusk="bob">bob</li> | ||
<li style="height:10px" dusk="john">john</li> | ||
<li style="height:10px" dusk="bill">bill</li> | ||
</ul> | ||
</div> | ||
HTML; | ||
} | ||
}) | ||
->assertIsNotVisibleInContainer('@list', '@john') | ||
->assertIsNotVisibleInContainer('@list', '@bill'); | ||
} | ||
} |