diff --git a/app/Helpers/general.php b/app/Helpers/general.php index 6d797c60..dde4fb4b 100644 --- a/app/Helpers/general.php +++ b/app/Helpers/general.php @@ -769,7 +769,7 @@ function convertMenu($menu, $parentId = null, &$idCounter = 1) 'parent_id' => $parentId, 'text' => $menu['text'] ?? 'text', 'href' => $menu['url'] ?? null, - 'icon' => $menu['icon'], + 'icon' => $menu['icon'] ?? null, 'permission' => $menu['permission'] ?? null, ]; diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 573f4b3b..fbfee5ed 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -47,7 +47,7 @@ public function actingAsAdmin($admin) { $defaultGuard = config('auth.defaults.guard'); $this->actingAs($admin, 'web'); - \Auth::shouldUse($defaultGuard); + \Illuminate\Support\Facades\Auth::shouldUse($defaultGuard); return $this; } diff --git a/tests/Feature/GroupMenuDisplayTest.php b/tests/Feature/GroupMenuDisplayTest.php new file mode 100644 index 00000000..902533af --- /dev/null +++ b/tests/Feature/GroupMenuDisplayTest.php @@ -0,0 +1,189 @@ +user = User::first(); + + // Get any existing team or create one + $this->team = Team::first(); + } + + /** + * Test that menu list API endpoint is called correctly + */ + public function test_menu_list_api_endpoint_is_called(): void + { + $response = $this->get("/pengaturan/groups/edit/{$this->team->id}"); + + // Assert that the page contains the API endpoint URL + $response->assertSee("api/v1/pengaturan/group/listModul/{$this->team->id}", false); + } + + /** + * Test that menu list is displayed with correct structure + */ + public function test_menu_list_display_structure(): void + { + $response = $this->get("/pengaturan/groups/edit/{$this->team->id}"); + + // Assert that the menu structure is displayed + $response->assertSee('Struktur Menu', false); + $response->assertSee('Sumber Menu URL', false); + + // Assert that the form elements are present + $response->assertSee('frmEdit', false); + $response->assertSee('json_menu', false); + + // Assert that the buttons are present + $response->assertSee('btnUpdate', false); + $response->assertSee('btnAdd', false); + } + + /** + * Test that menu list is loaded with JavaScript functions + */ + public function test_menu_list_javascript_functions(): void + { + $response = $this->get("/pengaturan/groups/edit/{$this->team->id}"); + + // Assert that the Alpine.js directives are present + $response->assertSee('x-data="menu()"', false); + $response->assertSee('x-init="retrieveData()"', false); + + // Assert that the necessary JavaScript functions are present + $response->assertSee('const retrieveData =', false); + $response->assertSee('const buildListModul =', false); + $response->assertSee('const buildEditor =', false); + + // Assert that the fetch API call is present in retrieveData function + $response->assertSee('fetch(', false); + + // Assert that the menu editor initialization is present + $response->assertSee('new MenuEditor', false); + $response->assertSee('editor.setForm', false); + $response->assertSee('editor.setData', false); + $response->assertSee('editor.setUpdateButton', false); + } + + /** + * Test that myEditor element is not empty and contains menu items + */ + public function test_my_editor_element_is_not_empty_and_contains_menu(): void + { + $response = $this->get("/pengaturan/groups/edit/{$this->team->id}"); + // Assert that the page loads successfully + $response->assertStatus(200); + + // Assert that the myEditor element exists in the HTML + $response->assertSee('', false); + + // Assert that the Alpine.js x-data and x-init directives are present to call retrieveData after page load + $response->assertSee('x-data="menu()"', false); + $response->assertSee('x-init="retrieveData()"', false); + + // Assert that the JavaScript code for calling the API endpoint after page load is present + $response->assertSee("api/v1/pengaturan/group/listModul/{$this->team->id}", false); + + // Assert that the retrieveData function exists and makes the API call + $response->assertSee('const retrieveData =', false); + $response->assertSee('fetch(', false); + + // Assert that the JavaScript code that executes after API call to populate the editor is present + $response->assertSee('buildEditor(', false); + $response->assertSee('editor.setData(', false); + + // Verify that the necessary JavaScript functions exist for loading menu after page load + $response->assertSee('retrieveData', false); + $response->assertSee('buildListModul', false); + + // Check that the editor initialization code is present + $response->assertSee('new MenuEditor', false); + $response->assertSee('editor.setForm', false); + $response->assertSee('editor.setUpdateButton', false); + } + + /** + * Test that myEditor element contains menu items after data is loaded + */ + public function test_my_editor_contains_menu_items_after_data_load(): void + { + $response = $this->get("/pengaturan/groups/edit/{$this->team->id}"); + + // Assert that the page loads successfully + $response->assertStatus(200); + + // Assert that the Alpine.js directives trigger the data loading + $response->assertSee('x-data="menu()"', false); + $response->assertSee('x-init="retrieveData()"', false); + + // Assert that the JavaScript code for handling nested menu items is present + $response->assertSee('MenuEditor', false); + $response->assertSee('editor.setForm', false); + $response->assertSee('editor.setUpdateButton', false); + $response->assertSee('editor.setData', false); + + // Assert that the menu editor is properly initialized with options + $response->assertSee('iconPickerOptions', false); + $response->assertSee('sortableListOptions', false); + + // Assert that the API call and data processing functions are present + $response->assertSee('const retrieveData =', false); + $response->assertSee('buildEditor', false); + $response->assertSee('buildListModul', false); + } + + /** + * Test that myEditor element handles empty menu gracefully + */ + public function test_my_editor_handles_empty_menu_gracefully(): void + { + // Create a team with empty menu + $teamWithEmptyMenu = Team::forceCreate([ + 'name' => 'Empty Test Group 2 ' . time(), + 'menu' => json_encode([]), + 'menu_order' => json_encode([]) + ]); + + $response = $this->get("/pengaturan/groups/edit/{$teamWithEmptyMenu->id}"); + + // Assert that the page loads successfully + $response->assertStatus(200); + + // Assert that the myEditor element exists even with empty data + $response->assertSee('', false); + + // Assert that the Alpine.js directives are present to trigger data loading + $response->assertSee('x-data="menu()"', false); + $response->assertSee('x-init="retrieveData()"', false); + + // Assert that the editor is still initialized with empty data + $response->assertSee('editor.setData(', false); + + // Assert that the retrieveData function exists to handle the API call + $response->assertSee('const retrieveData =', false); + $response->assertSee('fetch(', false); + + // Clean up - only delete if it's a test record + $teamWithEmptyMenu->delete(); + } + + protected function tearDown(): void + { + // Clean up any created data + parent::tearDown(); + } +} \ No newline at end of file