Skip to content

Commit

Permalink
Merge pull request #2 from lrljoe/AddClassAssertions
Browse files Browse the repository at this point in the history
Add Class Assertions for Multiple Classes
  • Loading branch information
joshhanley authored May 18, 2024
2 parents 657b1a4 + 9056aa5 commit 039e915
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 75 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ Check them out in the Livewire source in ['livewire/src/Features/SupportTesting/
In addition, the package provides the following:

- `assertSeeInOrder('@container', ['a', 'b', 'c'])`
- `assertIsVisibleInContainer(@container, @item)`
- `assertIsNotVisibleInContainer(@container, @item)`
- `assertIsVisibleInContainer('@container', '@item')`
- `assertIsNotVisibleInContainer('@container', '@item')`
- `assertHasClasses('@selector', ['class1', 'class2', 'class3'])`
- `assertHasOnlyClasses('@selector', ['class1', 'class2', 'class3'])`
- `assertMissingClasses('@selector', ['class1', 'class2', 'class3'])`

## Demo Package

Expand Down
51 changes: 51 additions & 0 deletions src/DuskBrowserMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,57 @@ public function assertIsNotVisibleInContainer()
};
}

public function assertHasClasses()
{
return function (string $selector, array $contents = []) {
/** @var \Laravel\Dusk\Browser $this */
$fullSelector = $this->resolver->format($selector);

$invalidClasses = array_diff($contents, explode(' ', $this->attribute($selector, 'class')));

PHPUnit::assertEmpty(
$invalidClasses,
"Element [{$fullSelector}] is missing required classes [".implode(' ', $invalidClasses).'].'
);

return $this;
};
}

public function assertHasOnlyClasses()
{
return function (string $selector, array $contents = []) {
/** @var \Laravel\Dusk\Browser $this */
$fullSelector = $this->resolver->format($selector);

$invalidClasses = array_diff(explode(' ', $this->attribute($selector, 'class')), $contents);

PHPUnit::assertEmpty(
$invalidClasses,
"Element [{$fullSelector}] has classes that must not be present [".implode(' ', $invalidClasses).'].'
);

return $this;
};
}

public function assertMissingClasses()
{
return function (string $selector, array $contents = []) {
/** @var \Laravel\Dusk\Browser $this */
$fullSelector = $this->resolver->format($selector);

$invalidClasses = array_intersect($contents, explode(' ', $this->attribute($selector, 'class')));

PHPUnit::assertEmpty(
$invalidClasses,
"Element [{$fullSelector}] has classes that must be missing [".implode(' ', $invalidClasses).'].'
);

return $this;
};
}

protected function isVisibleScript()
{
return '
Expand Down
251 changes: 251 additions & 0 deletions tests/Browser/DuskBrowserMixinTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
<?php

namespace LivewireDuskTestbench\Tests\Browser;

use Livewire\Component;
use Livewire\Livewire;
use PHPUnit\Framework\AssertionFailedError;

class DuskBrowserMixinTest extends TestCase
{
/** @test */
public function assert_see_in_order_macro_passes()
{
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_see_in_order_macro_fails()
{
$this->expectException(AssertionFailedError::class);

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', ['john', 'bob', 'bill']);
}

/** @test */
public function assert_is_visibile_in_container_passes()
{
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_visibile_in_container_fails()
{
$this->expectException(AssertionFailedError::class);

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', '@john');
}

/** @test */
public function assert_is_not_visibile_in_container_passes()
{
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');
}

/** @test */
public function assert_is_not_visibile_in_container_fails()
{
$this->expectException(AssertionFailedError::class);

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', '@bob');
}

/** @test */
public function assert_has_classes_passes()
{
Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div>
<p class="test other sample" dusk="item"></p>
</div>
HTML;
}
})
->assertHasClasses('@item', ['test', 'other']);
}

/** @test */
public function assert_has_classes_fails()
{
$this->expectException(AssertionFailedError::class);

Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div>
<p class="test sample" dusk="item"></p>
</div>
HTML;
}
})
->assertHasClasses('@item', ['test', 'other']);
}

/** @test */
public function assert_has_only_classes_passes()
{
Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div>
<p class="test other" dusk="item"></p>
</div>
HTML;
}
})
->assertHasOnlyClasses('@item', ['test', 'other']);
}

/** @test */
public function assert_has_only_classes_fails()
{
$this->expectException(AssertionFailedError::class);

Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div>
<p class="test other sample" dusk="item"></p>
</div>
HTML;
}
})
->assertHasOnlyClasses('@item', ['test', 'other']);
}

/** @test */
public function assert_missing_classes_passes()
{
Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div>
<p class="sample" dusk="item"></p>
</div>
HTML;
}
})
->assertMissingClasses('@item', ['test', 'other']);
}

/** @test */
public function assert_missing_classes_fails()
{
$this->expectException(AssertionFailedError::class);

Livewire::visit(new class extends Component
{
public function render()
{
return <<< 'HTML'
<div>
<p class="sample" dusk="item"></p>
</div>
HTML;
}
})
->assertMissingClasses('@item', ['test', 'sample']);
}
}
73 changes: 0 additions & 73 deletions tests/Browser/DuskBrowserMixinsTest.php

This file was deleted.

0 comments on commit 039e915

Please sign in to comment.