Skip to content

Commit

Permalink
Merge pull request #89 from dansysanalyst/add-sort-test
Browse files Browse the repository at this point in the history
Add Column Sort test
  • Loading branch information
luanfreitasdev authored Nov 2, 2021
2 parents 4535329 + a52202f commit 108b054
Show file tree
Hide file tree
Showing 3 changed files with 312 additions and 33 deletions.
12 changes: 12 additions & 0 deletions tests/DishesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function addColumns(): ?PowerGridEloquent
return PowerGrid::eloquent()
->addColumn('id')
->addColumn('name')
->addColumn('storage_room')
->addColumn('calories')
->addColumn('calories', function (Dish $dish) {
return $dish->calories . ' kcal';
Expand Down Expand Up @@ -101,6 +102,11 @@ public function columns(): array
->searchable()
->sortable(),

Column::add()
->title(__('Stored at'))
->field('storage_room')
->sortable(),

Column::add()
->title(__('Prato'))
->field('name')
Expand Down Expand Up @@ -143,6 +149,12 @@ public function columns(): array
->title(__('Data de produção'))
->field('produced_at_formatted')
->makeInputDatePicker('produced_at'),

Column::add()
->title(__('Data'))
->field('produced_at')
->makeInputDatePicker('produced_at')
->sortable()
];
}

Expand Down
245 changes: 245 additions & 0 deletions tests/Feature/SortTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
<?php

use Illuminate\Support\Facades\DB;
use PowerComponents\LivewirePowerGrid\Tests\DishesTable;

beforeEach(
function () {
DB::table('dishes')->truncate();
$this->seeders(dishesForSorting());
}
);

it('properly sorts ASC/DESC with: date')
->livewire(DishesTable::class)
->set('perPage', '10')
->call('sortBy', 'produced_at')
->set('sortDirection', 'desc')
->assertSeeHtml('Dish J')
->assertSeeHtml('Dish I')
->assertSeeHtml('Dish H')
->assertSeeHtml('Dish G')
->assertSeeHtml('Dish F')
->assertSeeHtml('Dish E')
->assertSeeHtml('Dish D')
->assertDontSeeHtml('Dish K')
->assertDontSeeHtml('Dish L')
->call('sortBy', 'produced_at')
->set('sortDirection', 'asc')
->assertSeeHtml('Dish K')
->assertSeeHtml('Dish L')
->assertSeeHtml('Dish A')
->assertSeeHtml('Dish B')
->assertSeeHtml('Dish C')
->assertSeeHtml('Dish D')
->assertSeeHtml('Dish E')
->assertSeeHtml('Dish F')
->assertSeeHtml('Dish G')
->assertSeeHtml('Dish H')
->assertDontSeeHtml('Dish I')
->assertDontSeeHtml('Dish J');

it('properly sorts ASC/DESC with: int')
->livewire(DishesTable::class)
->set('perPage', '10')
->call('sortBy', 'id')
->set('sortDirection', 'desc')
->assertSeeHtml('Dish L')
->assertSeeHtml('Dish K')
->assertDontSeeHtml('Dish A')
->assertDontSeeHtml('Dish B')
->call('sortBy', 'id')
->set('sortDirection', 'asc')
->assertSeeHtml('Dish A')
->assertSeeHtml('Dish B')
->assertSeeHtml('Dish C');

it('properly sorts ASC/DESC with: string')
->livewire(DishesTable::class)
->set('perPage', '10')
->call('sortBy', 'name')
->set('sortDirection', 'desc')
->assertSeeHtml('Zebra Dish H')
->assertSeeHtml('Dish K')
->assertDontSeeHtml('Dish A')
->assertDontSeeHtml('Dish B')
->call('sortBy', 'name')
->set('sortDirection', 'asc')
->assertSeeHtml('Dish A')
->assertSeeHtml('Dish B')
->assertDontSeeHtml('Zebra Dish H');


it('properly sorts ASC/DESC with: float')
->livewire(DishesTable::class)
->set('perPage', '10')
->call('sortBy', 'price')
->set('sortDirection', 'desc')
->assertSeeHtml('Zebra Dish H')
->assertSeeHtml('Dish K')
->assertDontSeeHtml('Dish A')
->assertDontSeeHtml('Dish B')
->call('sortBy', 'price')
->set('sortDirection', 'asc')
->assertSeeHtml('Dish A')
->assertSeeHtml('Dish B')
->assertDontSeeHtml('Zebra Dish H');

it('properly sorts ASC/DESC with: boolean')
->livewire(DishesTable::class)
->set('perPage', '10')
->call('sortBy', 'in_stock')
->set('sortDirection', 'asc')
->assertSeeHtml('Dish L')
->assertSeeHtml('Dish K')
->set('sortDirection', 'desc')
->assertDontSeeHtml('Dish L')
->assertDontSeeHtml('Dish K');

it('properly sorts ASC/DESC with: string-number')
->livewire(DishesTable::class)
->set('perPage', '10')
->call('sortBy', 'stored_at')
->set('sortDirection', 'asc')
->assertSeeHtml('Dish K')
->assertSeeHtml('Dish L')
->assertSeeHtml('Dish A')
->assertSeeHtml('Dish B')
->assertSeeHtml('Dish C')
->assertSeeHtml('Dish D')
->assertSeeHtml('Dish E')
->assertSeeHtml('Dish F')
->assertSeeHtml('Dish G')
->assertSeeHtml('Dish H')
->assertDontSeeHtml('Dish I')
->set('sortDirection', 'desc')
->assertSeeHtml('Dish J')
->assertSeeHtml('Dish I')
->assertSeeHtml('Dish H')
->assertSeeHtml('Dish G')
->assertSeeHtml('Dish F')
->assertSeeHtml('Dish E')
->assertSeeHtml('Dish D')
->assertSeeHtml('Dish C')
->assertSeeHtml('Dish B')
->assertSeeHtml('Dish K')
->assertDontSeeHtml('Dish A');

/**
* Small Dish dataset for sorting test
*
* @return array
*/
function dishesForSorting(): array
{
return [
[
"name" => "Dish A",
"category_id" => 7,
"price" => 100.00,
"stored_at" => "1",
"calories" => 224,
"in_stock" => true,
"produced_at" => "2021-10-01"
],
[
"name" => "Dish B",
"category_id" => 7,
"price" => 200.10,
"stored_at" => "2",
"calories" => 224,
"in_stock" => true,
"produced_at" => "2021-10-02"
],
[
"name" => "Dish C",
"category_id" => 7,
"price" => 300.50,
"stored_at" => "3",
"calories" => 224,
"in_stock" => true,
"produced_at" => "2021-10-03"
],
[
"name" => "Dish D",
"category_id" => 7,
"price" => 400.00,
"stored_at" => "4",
"calories" => 224,
"in_stock" => true,
"produced_at" => "2021-10-04"
],
[
"name" => "Dish E",
"category_id" => 7,
"price" => 500.00,
"stored_at" => "5",
"calories" => 224,
"in_stock" => true,
"produced_at" => "2021-10-05"
],
[
"name" => "Dish F",
"category_id" => 7,
"price" => 600.00,
"stored_at" => "6",
"calories" => 224,
"in_stock" => true,
"produced_at" => "2021-10-06"
],
[
"name" => "Dish G",
"category_id" => 7,
"price" => 700.00,
"stored_at" => "7",
"calories" => 224,
"in_stock" => true,
"produced_at" => "2021-10-07"
],
[
"name" => "Zebra Dish H",
"category_id" => 7,
"price" => 7500.00,
"stored_at" => "8",
"calories" => 224,
"in_stock" => true,
"produced_at" => "2021-10-08"
],
[
"name" => "Dish I",
"category_id" => 7,
"price" => 800.00,
"stored_at" => "9",
"calories" => 224,
"in_stock" => true,
"produced_at" => "2021-10-09"
],
[
"name" => "Dish J",
"category_id" => 7,
"price" => 900.00,
"stored_at" => "10",
"calories" => 224,
"in_stock" => true,
"produced_at" => "2021-10-10"
],
[
"name" => "Dish K",
"category_id" => 7,
"price" => 1000.00,
"stored_at" => "1b",
"calories" => 224,
"in_stock" => false,
"produced_at" => "2021-02-01"
],
[
"name" => "Dish L",
"category_id" => 7,
"price" => 2000.00,
"stored_at" => "1a",
"calories" => 224,
"in_stock" => false,
"produced_at" => "2021-01-01"
],
];
}
88 changes: 55 additions & 33 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ protected function migrations(): void
$table->double('price');
$table->integer('calories');
$table->boolean('in_stock')->default(false);
$table->string('stored_at');
$table->boolean('active')->default(true);
$table->date('produced_at');
$table->timestamps();
});
}

protected function seeders()
protected function seeders(array $dishes = []): void
{
DB::table('categories')->insert([
['name' => 'Carnes'],
Expand All @@ -54,7 +55,34 @@ protected function seeders()
['name' => 'Sopas'],
]);

$dishes = [
if (empty($dishes)) {
$dishes = $this->getDishes();
}

DB::table('dishes')->insert($dishes);
}

/**
* Define environment setup.
*
* @param Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'testbench');
$app['config']->set('app.key', 'base64:RygUQvaR926QuH4d5G6ZDf9ToJEEeO2p8qDSCq6emPk=');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}


protected function getDishes()
{
$dishes = collect([
[
'name' => 'Pastel de Nata',
'category_id' => 6,
Expand Down Expand Up @@ -217,43 +245,37 @@ protected function seeders()
['name' => 'Strudel de Maçã', 'category_id' => 6],
['name' => 'Sopa de Tomates Assados', 'category_id' => 7],
['name' => 'Sopa Creme de Ervilha', 'category_id' => 7],
];
]);

$faker = faker();

return $dishes->map(function ($dish) use ($faker) {
if (!isset($dish['price'])) {
$dish['price'] = $faker->randomFloat(2, 50, 200);
};

if (!isset($dish['stored_at'])) {
$dish['stored_at'] = rand(1, 3) . $faker->randomElement(['', 'a', 'b']);
};

foreach ($dishes as $dish) {
$price = (empty($dish['price']) ? $faker->randomFloat(2, 50, 200) : $dish['price']);
$in_stock = (!isset($dish['in_stock']) ? $faker->boolean() : $dish['in_stock']);
$produced_at = (empty($dish['produced_at']) ? $faker->dateTimeBetween($startDate = '-1 months', $endDate = 'now')->format('Y-m-d') : $dish['produced_at']);
if (!isset($dish['calories'])) {
$dish['calories'] = $faker->biasedNumberBetween($min = 40, $max = 890, $function = 'sqrt');
}

$dish = [
'name' => $dish['name'],
'category_id' => $dish['category_id'],
'price' => $price,
'calories' => $faker->biasedNumberBetween($min = 40, $max = 890, $function = 'sqrt'),
'in_stock' => $in_stock,
'produced_at' => $produced_at,
];
if (!isset($dish['in_stock'])) {
$dish['in_stock'] = $dish['in_stock'] = $faker->boolean();
}

DB::table('dishes')->insert($dish);
}
}
if (!isset($dish['produced_at'])) {
$dish['produced_at'] = $dish['produced_at'] = $faker->dateTimeBetween($startDate = '-1 months', $endDate = 'now')->format("Y-m-d");
}

/**
* Define environment setup.
*
* @param Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'testbench');
$app['config']->set('app.key', 'base64:RygUQvaR926QuH4d5G6ZDf9ToJEEeO2p8qDSCq6emPk=');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
if (!isset($dish['stored_at'])) {
$dish['price'] = $faker->randomFloat(2, 50, 200);
};

return $dish;
})->toArray();
}

protected function getPackageProviders($app)
Expand Down

0 comments on commit 108b054

Please sign in to comment.