Repository: hashicorp/vault
File: ui/tests/integration/components/namespace-picker-test.js (JavaScript)

Code before:
      'After refreshing, four namespaces are displayed'
    );

    // Verify the new namespace is specifically shown
    assert
      .dom(NAMESPACE_PICKER_SELECTORS.link('new-namespace'))
      .exists('The new namespace "new-namespace" is displayed after refreshing');
  });

  test('it displays the "Manage" button when the user has permissions', async function (assert) {
    // Mock `hasListPermissions` to be true
    this.owner.lookup('service:namespace').set('hasListPermissions', true);

    await render(hbs`<NamespacePicker />`);

    await click(NAMESPACE_PICKER_SELECTORS.toggle);

    // Find the "Manage" button
    const manageButton = findAll('a').find((el) => {
      const spans = el.querySelectorAll('span');
      return spans[1]?.textContent.trim() === 'Manage';
    });

    // Verify the "Manage" button is rendered
    assert.ok(manageButton, 'The "Manage" button is displayed');
  });
});


Review comment (suggestion):
🗒️ Note: This test case is now covered by the three new tests added above: 
```
test('it shows both action buttons when canList is true', async function (assert) {
test('it hides the refresh button when canList is false', async function (assert) {
test('it hides both action buttons when the capabilities store throws an error', async function (assert) {
```

Code after:
      }
      return Promise.reject();
    });

    await render(hbs`<NamespacePicker />`);
    await click(NAMESPACE_PICKER_SELECTORS.toggle);

    // Verify that the buttons are hidden
    assert.dom(NAMESPACE_PICKER_SELECTORS.refreshList).doesNotExist('Refresh List button is hidden');
    assert.dom(NAMESPACE_PICKER_SELECTORS.manageButton).exists('Manage button is hidden');
  });

  test('it hides both action buttons when the capabilities store throws an error', async function (assert) {
    const storeStub = this.owner.lookup('service:store');
    sinon.stub(storeStub, 'findRecord').callsFake(() => {
      return Promise.reject();
    });

    await render(hbs`<NamespacePicker />`);
    await click(NAMESPACE_PICKER_SELECTORS.toggle);

    // Verify that the buttons are hidden
    assert.dom(NAMESPACE_PICKER_SELECTORS.refreshList).doesNotExist('Refresh List button is hidden');
    assert.dom(NAMESPACE_PICKER_SELECTORS.manageButton).doesNotExist('Manage button is hidden');
  });

  test('it updates the namespace list after clicking "Refresh list"', async function (assert) {
    this.owner.lookup('service:namespace').set('hasListPermissions', true);

    const storeStub = this.owner.lookup('service:store');
    sinon.stub(storeStub, 'findRecord').callsFake((modelType, id) => {
      if (modelType === 'capabilities' && id === 'sys/namespaces/') {
        return Promise.resolve(getMockCapabilitiesModel(true)); // Return the mock model
      }
      return Promise.reject();
    });

    await render(hbs`<NamespacePicker />`);
    await click(NAMESPACE_PICKER_SELECTORS.toggle);

    // Dynamically modify the `findNamespacesForUser.perform` method for this test
    const namespaceService = this.owner.lookup('service:namespace');
    namespaceService.set('findNamespacesForUser', {
      perform: () => {
        namespaceService.set('accessibleNamespaces', [
          'parent1',
          'parent1/child1',
          'new-namespace', // Add a new namespace
        ]);
        return Promise.resolve();
      },