Repository: hashicorp/vault
File: ui/tests/acceptance/enterprise-namespaces-test.js (JavaScript)

Code before:
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { runCmd, createNS } from 'vault/tests/helpers/commands';
import { login, loginNs, logout } from 'vault/tests/helpers/auth/auth-helpers';
import { AUTH_FORM } from 'vault/tests/helpers/auth/auth-form-selectors';
import { GENERAL } from '../helpers/general-selectors';
import { NAMESPACE_PICKER_SELECTORS } from '../helpers/namespace-picker';

import sinon from 'sinon';

module('Acceptance | Enterprise | namespaces', function (hooks) {
  setupApplicationTest(hooks);

  let fetchSpy;

  hooks.beforeEach(() => {
    fetchSpy = sinon.spy(window, 'fetch');
    return login();
  });

  hooks.afterEach(() => {
    fetchSpy.restore();
  });

  test('it focuses the search input field when the component is loaded', async function (assert) {
    assert.expect(1);

    await click(NAMESPACE_PICKER_SELECTORS.toggle);

    // Verify that the search input field is focused
    const searchInput = find(NAMESPACE_PICKER_SELECTORS.searchInput);
    assert.strictEqual(
      document.activeElement,
      searchInput,
      'The search input field is focused on component load'
    );
  });

  test('it navigates to the matching namespace when Enter is pressed', async function (assert) {
    assert.expect(2);

    await click(NAMESPACE_PICKER_SELECTORS.toggle);

    // Simulate typing into the search input
    await fillIn(NAMESPACE_PICKER_SELECTORS.searchInput, 'beep/boop');
    assert
      .dom(NAMESPACE_PICKER_SELECTORS.searchInput)
      .hasValue('beep/boop', 'The search input field has the correct value');

    // Simulate pressing Enter
    await triggerKeyEvent(NAMESPACE_PICKER_SELECTORS.searchInput, 'keydown', 'Enter');

Review comment (suggestion):
This is probably a good thing to add to our rules document, but you really only need `assert.expect` if there's a chance the test could run without all the assertions being hit. This is common when using mirage when you have an assertion inside a this.server.x because if that network request doesn't get hit neither will the assertion and the test could still pass. 

In the cases here it appears that all the assertions will be hit, and so we don't need to have the .expect catch. 

let me know if that does/doesn't make sense.

Code after:
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { runCmd, createNS } from 'vault/tests/helpers/commands';
import { login, loginNs, logout } from 'vault/tests/helpers/auth/auth-helpers';
import { AUTH_FORM } from 'vault/tests/helpers/auth/auth-form-selectors';
import { GENERAL } from '../helpers/general-selectors';
import { NAMESPACE_PICKER_SELECTORS } from '../helpers/namespace-picker';

import sinon from 'sinon';

module('Acceptance | Enterprise | namespaces', function (hooks) {
  setupApplicationTest(hooks);

  let fetchSpy;

  hooks.beforeEach(() => {
    fetchSpy = sinon.spy(window, 'fetch');
    return login();
  });

  hooks.afterEach(() => {
    fetchSpy.restore();
  });

  test('it focuses the search input field when the component is loaded', async function (assert) {
    await click(NAMESPACE_PICKER_SELECTORS.toggle);

    // Verify that the search input field is focused
    const searchInput = find(NAMESPACE_PICKER_SELECTORS.searchInput);
    assert.strictEqual(
      document.activeElement,
      searchInput,
      'The search input field is focused on component load'
    );
  });

  test('it navigates to the matching namespace when Enter is pressed', async function (assert) {
    await click(NAMESPACE_PICKER_SELECTORS.toggle);

    // Simulate typing into the search input
    await fillIn(NAMESPACE_PICKER_SELECTORS.searchInput, 'beep/boop');
    assert
      .dom(NAMESPACE_PICKER_SELECTORS.searchInput)
      .hasValue('beep/boop', 'The search input field has the correct value');

    // Simulate pressing Enter
    await triggerKeyEvent(NAMESPACE_PICKER_SELECTORS.searchInput, 'keydown', 'Enter');

    // Verify navigation to the matching namespace
    assert.strictEqual(
      this.owner.lookup('service:router').currentURL,