From b2c2b01b5bff3d09afeef043f949724c9cb1fc41 Mon Sep 17 00:00:00 2001 From: Quentin Renard Date: Sun, 5 Jul 2020 23:51:31 -0400 Subject: [PATCH] Add nested module test --- tests/integration/ModulesCategoriesTest.php | 73 +++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/integration/ModulesCategoriesTest.php b/tests/integration/ModulesCategoriesTest.php index f2fab35ff..3acf0fb1b 100644 --- a/tests/integration/ModulesCategoriesTest.php +++ b/tests/integration/ModulesCategoriesTest.php @@ -2,6 +2,8 @@ namespace A17\Twill\Tests\Integration; +use App\Models\Category; + class ModulesCategoriesTest extends ModulesTestBase { public function testCanDisplayModuleInNavigation() @@ -37,4 +39,75 @@ public function testCanShowCategoriesIndex() count(json_decode($this->content(), true)['tableData']) ); } + + public function testCanReorderCategories() + { + $this->createCategory(2); + + $category1 = Category::ordered() + ->get() + ->first(); + + $category2 = Category::orderBy('position', 'desc') + ->first(); + + $this->assertEquals(1, $category1->position); + $this->assertEquals(2, $category2->position); + + $this->request('/twill/categories/reorder', 'POST', [ + 'ids' => [ + [ + 'id' => $category2->id, + 'children' => [], + ], + [ + 'id' => $category1->id, + 'children' => [], + ], + ], + ])->assertStatus(200); + + $this->assertNothingWrongHappened(); + + $category1->refresh(); + $category2->refresh(); + + $this->assertEquals(1, $category1->position); + $this->assertEquals(0, $category2->position); + } + + public function testCanNestCategories() + { + $this->createCategory(2); + + $category1 = Category::ordered() + ->get() + ->first(); + + $category2 = Category::orderBy('position', 'desc') + ->first(); + + $this->request('/twill/categories/reorder', 'POST', [ + 'ids' => [ + [ + 'id' => $category2->id, + 'children' => [ + [ + 'id' => $category1->id, + 'children' => [], + ], + ], + ], + ], + ])->assertStatus(200); + + $this->assertNothingWrongHappened(); + + $category1->refresh(); + $category2->refresh(); + + $this->assertTrue($category2->isAncestorOf($category1)); + $this->assertEquals(0, $category1->position); + $this->assertEquals($category2->title, $category1->parent->title); + } }