function ComponentRenderTest::checkAttributeMerging

Same name in this branch
  1. 11.x core/modules/sdc/tests/src/Kernel/ComponentRenderTest.php \Drupal\Tests\sdc\Kernel\ComponentRenderTest::checkAttributeMerging()
Same name in other branches
  1. 10 core/modules/sdc/tests/src/Kernel/ComponentRenderTest.php \Drupal\Tests\sdc\Kernel\ComponentRenderTest::checkAttributeMerging()
  2. 10 core/tests/Drupal/KernelTests/Components/ComponentRenderTest.php \Drupal\KernelTests\Components\ComponentRenderTest::checkAttributeMerging()

Ensures the attributes are merged properly.

1 call to ComponentRenderTest::checkAttributeMerging()
ComponentRenderTest::testRender in core/tests/Drupal/KernelTests/Components/ComponentRenderTest.php
Test that components render correctly.

File

core/tests/Drupal/KernelTests/Components/ComponentRenderTest.php, line 218

Class

ComponentRenderTest
Tests the correct rendering of components.

Namespace

Drupal\KernelTests\Components

Code

protected function checkAttributeMerging() : void {
    $content = [
        'label' => 'I am a labels',
    ];
    // 1. Check that if it exists Attribute object in the 'attributes' prop, you
    // get them merged.
    $build = [
        '#type' => 'inline_template',
        '#context' => [
            'content' => $content,
            'attributes' => new Attribute([
                'data-merged-attributes' => 'yes',
            ]),
        ],
        '#template' => "{{ include('sdc_test:my-button', { text: content.label, iconType: 'external', attributes: attributes }, with_context = false) }}",
    ];
    $crawler = $this->renderComponentRenderArray($build);
    $this->assertNotEmpty($crawler->filter('#sdc-wrapper [data-merged-attributes="yes"][data-component-id="sdc_test:my-button"]'), $crawler->outerHtml());
    // 2. Check that if the 'attributes' exists, but there is some other data
    // type, then we don't touch it.
    $build = [
        '#type' => 'inline_template',
        '#context' => [
            'content' => $content,
            'attributes' => 'hard-coded-attr',
        ],
        '#template' => "{{ include('sdc_theme_test_base:my-card-no-schema', { header: content.label, attributes: attributes }, with_context = false) }}",
    ];
    $crawler = $this->renderComponentRenderArray($build);
    // The default data attribute should be missing.
    $this->assertEmpty($crawler->filter('#sdc-wrapper [data-component-id="sdc_theme_test_base:my-card-no-schema"]'), $crawler->outerHtml());
    $this->assertNotEmpty($crawler->filter('#sdc-wrapper [hard-coded-attr]'), $crawler->outerHtml());
    // 3. Check that if the 'attributes' is empty, we get the defaults.
    $build = [
        '#type' => 'inline_template',
        '#context' => [
            'content' => $content,
        ],
        '#template' => "{{ include('sdc_theme_test_base:my-card-no-schema', { header: content.label }, with_context = false) }}",
    ];
    $crawler = $this->renderComponentRenderArray($build);
    // The default data attribute should not be missing.
    $this->assertNotEmpty($crawler->filter('#sdc-wrapper [data-component-id="sdc_theme_test_base:my-card-no-schema"]'), $crawler->outerHtml());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.