Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,15 @@ $this->get('/some-route')
```


For simple and quick checks, you can use `assertContainsElement`.
This method allows you to verify that a specific element exists on the page you can optionally include an array of expected attributes.
For simple and quick checks, you can use `assertContainsElement` or `assertDoesntExist`
These methods allow you to verify that a specific element exists on the page.

`assertContainsElement` optionally allows an array of expected attributes
```
$this->get('/some-route')
->assertContainsElement('#content')
->assertContainsElement('div.banner', ['text' => 'Successfully deleted', 'data-status' => 'success']);
->assertContainsElement('div.banner', ['text' => 'Successfully deleted', 'data-status' => 'success'])
->assertDoesntExist('div.not-here');
```

### Testing forms
Expand Down
18 changes: 18 additions & 0 deletions ide-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public function assertContainsElement($selector, array $attributes = [])
/** @var \Illuminate\Testing\TestResponse $instance */
return $instance;
}

public function assertDoesntExist($selector)
{
/** @var \Illuminate\Testing\TestResponse $instance */
return $instance;
}
}

class TestView
Expand Down Expand Up @@ -89,6 +95,12 @@ public function assertContainsElement($selector, array $attributes = [])
/** @var \Illuminate\Testing\TestView $instance */
return $instance;
}

public function assertDoesntExist($selector)
{
/** @var \Illuminate\Testing\TestView $instance */
return $instance;
}
}

class TestComponent
Expand Down Expand Up @@ -128,5 +140,11 @@ public function assertContainsElement($selector, array $attributes = [])
/** @var \Illuminate\Testing\TestComponent $instance */
return $instance;
}

public function assertDoesntExist($selector)
{
/** @var \Illuminate\Testing\TestComponent $instance */
return $instance;
}
}
}
28 changes: 28 additions & 0 deletions src/TestComponentMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,34 @@ public function assertContainsElement(): Closure
};
}

public function assertDoesntExist(): Closure
{
return function (string $selector): TestComponent {
/** @var TestComponent $this */
Assert::assertNotEmpty(
(string) $this,
'The component is empty!'
);

try {
if (! app()->has('dom-assertions.parser')) {
app()->instance('dom-assertions.parser', DomParser::new((string) $this));
}
} catch (DOMException $exception) {
Assert::fail($exception->getMessage());
}

$element = app()->make('dom-assertions.parser')->query($selector);

Assert::assertNull(
$element,
sprintf('Expected no element with selector: %s, but one was found.', $selector)
);

return $this;
};
}

public function assertForm(): Closure
{
return $this->assertFormExists();
Expand Down
28 changes: 28 additions & 0 deletions src/TestResponseMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,34 @@ public function assertContainsElement(): Closure
};
}

public function assertDoesntExist(): Closure
{
return function (string $selector): TestResponse {
/** @var TestResponse $this */
Assert::assertNotEmpty(
(string) $this->getContent(),
'The view is empty!'
);

try {
if (! app()->has('dom-assertions.parser')) {
app()->instance('dom-assertions.parser', DomParser::new($this->getContent()));
}
} catch (DOMException $exception) {
Assert::fail($exception->getMessage());
}

$element = app()->make('dom-assertions.parser')->query($selector);

Assert::assertNull(
$element,
sprintf('Expected no element with selector: %s, but one was found.', $selector)
);

return $this;
};
}

public function assertForm(): Closure
{
return $this->assertFormExists();
Expand Down
28 changes: 28 additions & 0 deletions src/TestViewMacros.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,34 @@ public function assertContainsElement(): Closure
};
}

public function assertDoesntExist(): Closure
{
return function (string $selector): TestView {
/** @var TestView $this */
Assert::assertNotEmpty(
(string) $this,
'The view is empty!'
);

try {
if (! app()->has('dom-assertions.parser')) {
app()->instance('dom-assertions.parser', DomParser::new((string) $this));
}
} catch (DOMException $exception) {
Assert::fail($exception->getMessage());
}

$element = app()->make('dom-assertions.parser')->query($selector);

Assert::assertNull(
$element,
sprintf('Expected no element with selector: %s, but one was found.', $selector)
);

return $this;
};
}

public function assertForm(): Closure
{
return $this->assertFormExists();
Expand Down
8 changes: 7 additions & 1 deletion tests/ComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
});
});

it('assertContainsElement works as expects', function () {
it('assertDoesntExist works as expected', function () {
$this->component(NestedComponent::class)
->assertDoesntExist('span.fake')
->assertDoesntExist('nav.fake');
});

it('assertContainsElement works as expected', function () {
$this->component(NestedComponent::class)
->assertContainsElement('span.foo', ['text' => 'Foo', 'class' => 'bar foo'])
->assertContainsElement('nav');
Expand Down
8 changes: 7 additions & 1 deletion tests/DomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
});
});

it('assertContainsElement works as expects', function () {
it('assertDoesntExist works as expected', function () {
$this->get('nesting')
->assertDoesntExist('span.fake')
->assertDoesntExist('nav.fake');
});

it('assertContainsElement works as expected', function () {
$this->get('nesting')
->assertContainsElement('span.foo', ['text' => 'Foo', 'class' => 'bar foo'])
->assertContainsElement('nav');
Expand Down
8 changes: 7 additions & 1 deletion tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
use PHPUnit\Framework\AssertionFailedError;
use Sinnbeck\DomAssertions\Asserts\AssertElement;

it('assertContainsElement works as expects', function () {
it('assertDoesntExist works as expected', function () {
$this->get('nesting')
->assertDoesntExist('span.fake')
->assertDoesntExist('nav.fake');
});

it('assertContainsElement works as expected', function () {
$this->view('nesting')
->assertContainsElement('span.foo', ['text' => 'Foo'])
->assertContainsElement('nav');
Expand Down