Skip to content

Commit

Permalink
Merge pull request bagisto#8798 from devansh-webkul/testcases
Browse files Browse the repository at this point in the history
test: login buttons and navigation tests added
  • Loading branch information
devansh-webkul authored Oct 26, 2023
2 parents aaa9512 + a11b56d commit 8fde247
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 9 deletions.
23 changes: 14 additions & 9 deletions packages/Webkul/Shop/tests/Feature/API/ProductTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Pest\Expectation;
use Webkul\Faker\Helpers\Product;
use Webkul\Faker\Helpers\Product as ProductFaker;
use Webkul\Product\Models\Product as ProductModel;

use function Pest\Laravel\getJson;
Expand All @@ -14,7 +14,7 @@
});

it('returns a new products listing', function () {
// Prepare
// Arrange
$newProductOptions = [
'attributes' => [
5 => 'new',
Expand All @@ -27,10 +27,12 @@
],
];

(new Product($newProductOptions))->create(1, 'simple');
(new ProductFaker($newProductOptions))->create(1, 'simple');

// Act
$response = getJson(route('shop.api.products.index', ['new' => 1]))->collect();
$response = getJson(route('shop.api.products.index', ['new' => 1]))
->assertOk()
->collect();

// Assert
expect($response['data'])->each(function (Expectation $product) {
Expand All @@ -39,7 +41,7 @@
});

it('returns a featured products listing', function () {
// Prepare
// Arrange
$featuredProductOptions = [
'attributes' => [
6 => 'featured',
Expand All @@ -52,10 +54,12 @@
],
];

(new Product($featuredProductOptions))->create(1, 'simple');
(new ProductFaker($featuredProductOptions))->create(1, 'simple');

// Act
$response = getJson(route('shop.api.products.index', ['featured' => 1]))->collect();
$response = getJson(route('shop.api.products.index', ['featured' => 1]))
->assertOk()
->collect();

// Assert
expect($response['data'])->each(function (Expectation $product) {
Expand All @@ -64,11 +68,12 @@
});

it('returns all products listing', function () {
// Prepare
$product = (new Product())->create(1, 'simple')->first();
// Arrange
$product = (new ProductFaker())->create(1, 'simple')->first();

// Act & Assert
getJson(route('shop.api.products.index'))
->assertOk()
->assertJsonIsArray('data')
->assertJsonFragment([
'id' => $product->id,
Expand Down
32 changes: 32 additions & 0 deletions packages/Webkul/Shop/tests/Feature/HomePageTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
<?php

use Webkul\Customer\Models\Customer as CustomerModel;

use function Pest\Laravel\get;

afterEach(function () {
/**
* Cleaning up rows which are created.
*/
CustomerModel::query()->delete();
});

it('returns a successful response', function () {
// Act & Assert
get(route('shop.home.index'))
->assertOk();
});

it('displays the "Sign In" and "Sign Up" buttons when the customer is not logged in', function () {
// Act & Assert
get(route('shop.home.index'))
->assertOk()
->assertSeeText(trans('shop::app.components.layouts.header.sign-in'))
->assertSeeText(trans('shop::app.components.layouts.header.sign-up'));
});

it('displays navigation buttons when the customer is logged in', function () {
// Act
$this->loginAsCustomer();

// Assert
get(route('shop.home.index'))
->assertOk()
->assertSee([
trans('shop::app.components.layouts.header.profile'),
trans('shop::app.components.layouts.header.orders'),
trans('shop::app.components.layouts.header.wishlist'),
trans('shop::app.components.layouts.header.logout'),
]);
});
13 changes: 13 additions & 0 deletions packages/Webkul/Shop/tests/ShopTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,20 @@
namespace Webkul\Shop\Tests;

use Tests\TestCase;
use Webkul\Customer\Contracts\Customer as CustomerContract;
use Webkul\Faker\Helpers\Customer as CustomerFaker;

class ShopTestCase extends TestCase
{
/**
* Login as customer.
*/
public function loginAsCustomer(CustomerContract $customer = null): CustomerContract
{
$customer = $customer ?? (new CustomerFaker())->create(1)->first();

$this->actingAs($customer);

return $customer;
}
}

0 comments on commit 8fde247

Please sign in to comment.