/nova-assertions

Laravel Nova assertions for your tests

Primary LanguagePHPMIT LicenseMIT

Nova Assertions

Latest Version on Github Total Downloads Twitter Follow

Nova requests & assertions for Laravel tests - View examples

testing tdd laravel nova

Assert: Policies | Cards | Actions | Filters | Lenses | Resources | Fields | Relations


Installation

composer require dillingham/nova-assertions --dev

Enable by adding the NovaAssertions to a test

use NovaTesting\NovaAssertions;

class UserTest extends TestCase
{
    use NovaAssertions;
}

Authentication

Log in a user that has access to Nova

$this->be(factory(User::class)->create());

Nova Requests

Request using a resource's uriKey to perform assertions:

$response = $this->novaIndex('users');

$response = $this->novaDetail('users', $user->id);

$response = $this->novaCreate('users');

$response = $this->novaEdit('users', $user->id);

$response = $this->novaLens('users', Lens::class);

Request Filters

You may also pass filters & their values to indexes & lenses

$response = $this->novaIndex('users', [
    StatusFilter::class => 'active'
]);
$response = $this->novaLens('users', Lens::class, [
    StatusFilter::class => 'active'
]);

Assert Http

You can call http response methods as usual:

$response->assertOk();

Assert Resources

$response->assertResourceCount(3);
$response->assertResources(function($resources) {
    return $resources->count() > 0;
});

Assert Cards

$response->assertCardCount(5);
$response->assertCardsInclude(Card::class);
$response->assertCardsExclude(Card::class);
$response->assertCards(function($cards) {
    return $cards->count() > 0;
});

Assert Actions

$response->assertActionCount(5);
$response->assertActionsInclude(Action::class);
$response->assertActionsExclude(Action::class);
$response->assertActions(function($actions) {
    return $actions->count() > 0;
});

Assert Filters

$response->assertFilterCount(5);
$response->assertFiltersInclude(Filter::class);
$response->assertFiltersExclude(Filter::class);
$response->assertFilters(function($filters) {
    return $filters->count() > 0;
});

Assert Lenses

$response->assertLensCount(5);
$response->assertLensesInclude(Lens::class);
$response->assertLensesExclude(Lens::class);
$response->assertLenses(function($lenses) {
    return $lenses->count() > 0;
});

Assert Fields

$response->assertFieldCount(5);

Assert a specific field exists

$response->assertFieldsInclude('id');

Assert a specific field contains a value

$response->assertFieldsInclude('id', $user->id);

Assert multiple fields exist

$response->assertFieldsInclude(['id', 'email']);

Assert multiple fields with specific values exist

$response->assertFieldsInclude(['id' => 1, 'email' => 'example']);

Assert multiple values for one field exist

$response->assertFieldsInclude('id', $users->pluck('id'));

Make assertions against a collection of fields

$response->assertFields(function($fields) {
    return $fields->count() > 0;
});

Also exclude works in all of these scenarios

$response->assertFieldsExclude(['id' => 1, 'email' => 'example']);

Assert Relations

// App\Nova\Post
// BelongsTo::make('Category'),
$response = $this->novaCreate('posts');

$response->assertRelation('categories', function($categories) {
    //
});
// App\Nova\Category
// HasMany::make('Posts'),
$response = $this->novaDetail('categories');

$response->assertRelation('posts', function($posts) {
    //
});

Assert Policies

Assert Nova's use of policies & the authed user:

$response->assertCanView();

$response->assertCanCreate();

$response->assertCanUpdate();

$response->assertCanDelete();

$response->assertCanForceDelete();

$response->assertCanRestore();

Also can assert cannot for each:

$response->assertCannotView();

Author

Hi 👋, Im Brian Dillingham, creator of this Nova package and others

Hope you find it useful. Feel free to reach out with feedback.

Follow me on twitter: @im_brian_d