diff --git a/README.md b/README.md index d2d8734..143d3b9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/DuskBrowserMixin.php b/src/DuskBrowserMixin.php index fe07050..4e4b487 100644 --- a/src/DuskBrowserMixin.php +++ b/src/DuskBrowserMixin.php @@ -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 ' diff --git a/tests/Browser/DuskBrowserMixinTest.php b/tests/Browser/DuskBrowserMixinTest.php new file mode 100644 index 0000000..7549225 --- /dev/null +++ b/tests/Browser/DuskBrowserMixinTest.php @@ -0,0 +1,251 @@ + + + + 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' +
+ +
+ 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' +
+ +
+ 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' +
+ +
+ 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' +
+ +
+ 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' +
+ +
+ HTML; + } + }) + ->assertIsNotVisibleInContainer('@list', '@bob'); + } + + /** @test */ + public function assert_has_classes_passes() + { + Livewire::visit(new class extends Component + { + public function render() + { + return <<< 'HTML' +
+

+
+ 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' +
+

+
+ HTML; + } + }) + ->assertHasClasses('@item', ['test', 'other']); + } + + /** @test */ + public function assert_has_only_classes_passes() + { + Livewire::visit(new class extends Component + { + public function render() + { + return <<< 'HTML' +
+

+
+ 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' +
+

+
+ HTML; + } + }) + ->assertHasOnlyClasses('@item', ['test', 'other']); + } + + /** @test */ + public function assert_missing_classes_passes() + { + Livewire::visit(new class extends Component + { + public function render() + { + return <<< 'HTML' +
+

+
+ 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' +
+

+
+ HTML; + } + }) + ->assertMissingClasses('@item', ['test', 'sample']); + } +} diff --git a/tests/Browser/DuskBrowserMixinsTest.php b/tests/Browser/DuskBrowserMixinsTest.php deleted file mode 100644 index f4b53c3..0000000 --- a/tests/Browser/DuskBrowserMixinsTest.php +++ /dev/null @@ -1,73 +0,0 @@ - - - - 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' -
- -
- 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' -
- -
- HTML; - } - }) - ->assertIsNotVisibleInContainer('@list', '@john') - ->assertIsNotVisibleInContainer('@list', '@bill'); - } -}