function AjaxColorFormTest::testAjaxColorForm

Same name and namespace in other branches
  1. 3.x modules/form_api_example/tests/src/FunctionalJavascript/AjaxColorFormTest.php \Drupal\Tests\form_api_example\FunctionalJavascript\AjaxColorFormTest::testAjaxColorForm()

Functional test of the color temperature AJAX dropdown form.

File

modules/form_api_example/tests/src/FunctionalJavascript/AjaxColorFormTest.php, line 32

Class

AjaxColorFormTest
Tests the behavior of the color temperature AJAX dropdown form.

Namespace

Drupal\Tests\form_api_example\FunctionalJavascript

Code

public function testAjaxColorForm() {
  // Visit form route.
  $this->drupalGet(Url::fromRoute('form_api_example.ajax_color_demo'));
  // Get Mink stuff.
  $assert = $this->assertSession();
  // Before the color temperature dropdown is selected, we should not have a
  // color dropdown.
  $assert->fieldNotExists('color');
  $color_matrix = [
    'warm' => [
      'red',
      'orange',
      'yellow',
    ],
    'cool' => [
      'blue',
      'purple',
      'green',
    ],
  ];
  foreach ($color_matrix as $temperature => $colors) {
    // Submit all the colors.
    foreach ($colors as $color) {
      $assert->selectExists('temperature')
        ->selectOption($temperature);
      $assert->assertWaitOnAjaxRequest();
      $assert->selectExists('color')
        ->selectOption($color);
      $assert->buttonExists('Submit')
        ->press();
      $assert->pageTextContains("Value for Temperature: {$temperature}");
      $assert->pageTextContains("Value for color: {$color}");
    }
  }
  $assert->selectExists('temperature')
    ->selectOption('');
  $assert->assertWaitOnAjaxRequest();
  $assert->fieldNotExists('color');
}