| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Skins\Vector\Tests\Unit\Components; |
| 24 |
|
| 25 |
use MediaWiki\Skins\Vector\Components\VectorComponentButton; |
| 26 |
use MediaWikiUnitTestCase; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class VectorComponentButtonTest extends MediaWikiUnitTestCase { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public function provideButtonData(): array { |
| 42 |
return [ |
| 43 |
'Basic Button' => [ |
| 44 |
|
| 45 |
'label' => 'Click Me', |
| 46 |
|
| 47 |
'expectedClasses' => 'cdx-button', |
| 48 |
|
| 49 |
'weight' => 'normal', |
| 50 |
|
| 51 |
'iconOnly' => false, |
| 52 |
|
| 53 |
'href' => null, |
| 54 |
], |
| 55 |
'Button With Primary Weight' => [ |
| 56 |
|
| 57 |
'label' => 'Primary Action', |
| 58 |
|
| 59 |
'expectedClasses' => |
| 60 |
'cdx-button cdx-button--fake-button cdx-button--fake-button--enabled cdx-button--weight-primary', |
| 61 |
|
| 62 |
'weight' => 'primary', |
| 63 |
|
| 64 |
'iconOnly' => false, |
| 65 |
|
| 66 |
'href' => '/mock-link', |
| 67 |
], |
| 68 |
'Icon Only Button' => [ |
| 69 |
|
| 70 |
'label' => '', |
| 71 |
|
| 72 |
'expectedClasses' => 'cdx-button cdx-button--icon-only', |
| 73 |
|
| 74 |
'weight' => 'normal', |
| 75 |
|
| 76 |
'iconOnly' => true, |
| 77 |
|
| 78 |
'href' => null, |
| 79 |
], |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function testGetClasses() { |
| 90 |
$basicButton = new VectorComponentButton( 'Label' ); |
| 91 |
$templateData = $basicButton->getTemplateData(); |
| 92 |
$this->assertStringContainsString( 'cdx-button', $templateData['class'], |
| 93 |
'Basic button should have cdx-button class.' ); |
| 94 |
|
| 95 |
$primaryButton = new VectorComponentButton( 'Label', null, null, null, [], 'primary' ); |
| 96 |
$templateData = $primaryButton->getTemplateData(); |
| 97 |
$this->assertStringContainsString( 'cdx-button--weight-primary', $templateData['class'], |
| 98 |
'Primary button should have primary weight class.' ); |
| 99 |
|
| 100 |
$iconOnlyButton = new VectorComponentButton( |
| 101 |
'Label', null, null, null, [], 'normal', 'default', true ); |
| 102 |
$templateData = $iconOnlyButton->getTemplateData(); |
| 103 |
$this->assertStringContainsString( 'cdx-button--icon-only', $templateData['class'], |
| 104 |
'Icon-only button should have icon-only class.' ); |
| 105 |
|
| 106 |
$destructiveButton = new VectorComponentButton( 'Label', null, null, null, [], |
| 107 |
'normal', 'destructive' ); |
| 108 |
$templateData = $destructiveButton->getTemplateData(); |
| 109 |
$this->assertStringContainsString( 'cdx-button--action-destructive', $templateData['class'], |
| 110 |
'Destructive button should have destructive action class.' ); |
| 111 |
|
| 112 |
$progressiveButton = new VectorComponentButton( 'Label', null, null, null, [], |
| 113 |
'normal', 'progressive' ); |
| 114 |
$templateData = $progressiveButton->getTemplateData(); |
| 115 |
$this->assertStringContainsString( 'cdx-button--action-progressive', $templateData['class'], |
| 116 |
'Progressive button should have progressive action class.' ); |
| 117 |
|
| 118 |
$quietButton = new VectorComponentButton( 'Label', null, null, null, [], 'quiet' ); |
| 119 |
$templateData = $quietButton->getTemplateData(); |
| 120 |
$this->assertStringContainsString( 'cdx-button--weight-quiet', $templateData['class'], |
| 121 |
'Quiet button should have quiet weight class.' ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function testGetTemplateData( |
| 132 |
string $label, |
| 133 |
string $expectedClasses, |
| 134 |
string $weight, |
| 135 |
bool $iconOnly, |
| 136 |
?string $href |
| 137 |
) { |
| 138 |
|
| 139 |
$button = new VectorComponentButton( |
| 140 |
$label, |
| 141 |
'icon-sample', |
| 142 |
'btn-id', |
| 143 |
'additional-class', |
| 144 |
|
| 145 |
[ 'data-test' => 'true' ], |
| 146 |
$weight, |
| 147 |
|
| 148 |
'default', |
| 149 |
$iconOnly, |
| 150 |
$href |
| 151 |
); |
| 152 |
|
| 153 |
|
| 154 |
$templateData = $button->getTemplateData(); |
| 155 |
|
| 156 |
|
| 157 |
$this->assertEquals( $label, $templateData['label'] ); |
| 158 |
$this->assertEquals( 'icon-sample', $templateData['icon'] ); |
| 159 |
$this->assertEquals( 'btn-id', $templateData['id'] ); |
| 160 |
|
| 161 |
$this->assertStringContainsString( $expectedClasses, $templateData['class'] ); |
| 162 |
$this->assertEquals( $href, $templateData['href'] ); |
| 163 |
|
| 164 |
$this->assertContains( [ 'key' => 'data-test', 'value' => 'true' ], $templateData['array-attributes'] ); |
| 165 |
} |
| 166 |
} |