diff --git a/tests/DishesTable.php b/tests/DishesTable.php index a94f470a..96c5f742 100644 --- a/tests/DishesTable.php +++ b/tests/DishesTable.php @@ -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'; @@ -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') @@ -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() ]; } diff --git a/tests/Feature/SortTest.php b/tests/Feature/SortTest.php new file mode 100644 index 00000000..96b2bf21 --- /dev/null +++ b/tests/Feature/SortTest.php @@ -0,0 +1,245 @@ +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" + ], + ]; +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 22d8aa65..c51d0d0e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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'], @@ -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, @@ -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)