Same name and namespace in other branches
  1. 8.x-2.x tests/src/FunctionalJavascript/ColorFieldWidgetJavascriptTests.php \Drupal\Tests\color_field\FunctionalJavascript\ColorFieldWidgetJavascriptTests::testColorFieldSpectrum() 1 comment

Test color_field_widget_spectrum widget.

Unfortunately since the spectrum library uses clickable divs instead of buttons, we can't use full interaction of clicks with elements. So instead we just confirm that the right html has been generated and assume that the library tests itself.

Ensure that our handling of the palette is correctly handling different types of color values. Like don't break if using commas in rgba values.

File

tests/src/FunctionalJavascript/ColorFieldWidgetJavascriptTests.php, line 145

Class

ColorFieldWidgetJavascriptTests
Tests for form grouping elements.

Namespace

Drupal\Tests\color_field\FunctionalJavascript

Code

public function testColorFieldSpectrum() : void {
    $this->form
        ->setComponent('field_color_repeat', [
        'type' => 'color_field_widget_spectrum',
        'settings' => [
            'palette' => '["#0678BE","#53B0EB", "#96BC44"]',
            'show_palette' => FALSE,
        ],
    ])
        ->setComponent('field_color', [
        'type' => 'color_field_widget_spectrum',
        'settings' => [
            'palette' => '["#005493","#F5AA1C","#C63527","002754", hsv 0 100 100, "rgba(0,255,255,0.5)", green,hsl(0 100 50)]',
            'show_palette' => TRUE,
        ],
    ])
        ->save();
    // Disable alpha on second field.
    FieldConfig::load('node.article.field_color_repeat')->setSetting('opacity', 0)
        ->save();
    $session = $this->getSession();
    $web_assert = $this->assertSession();
    $this->drupalGet('node/add/article');
    $page = $session->getPage();
    // Confirm that help text/label are present.
    $web_assert->responseContains('Freeform Color');
    $web_assert->responseContains('Color field description');
    // Wait for elements to be generated.
    $web_assert->waitForElementVisible('css', '.sp-preview');
    // Confirm that two fields aren't sharing settings.
    // Also confirms that custom palette is being used correctly and that the
    // one field's palette isn't shown. 4 for the one palette plus one each for
    // the widget and the current color value.
    $boxes = $page->findAll('css', '.sp-thumb-el');
    $this->assertEquals(13, count($boxes));
    // Confirm that alpha slider is hidden if the field doesn't support opacity.
    $alpha = $page->findAll('css', '.sp-alpha-enabled');
    $this->assertEquals(1, count($alpha));
}