diff --git a/tests/phpunit/unit/components/VectorComponentPinnableHeaderTest.php b/tests/phpunit/unit/components/VectorComponentPinnableHeaderTest.php new file mode 100644 index 000000000..27df3bd29 --- /dev/null +++ b/tests/phpunit/unit/components/VectorComponentPinnableHeaderTest.php @@ -0,0 +1,118 @@ + [ + // The header should start in a "pinned" state. + 'pinned' => true, + // ID of the pinnable header element. + 'id' => 'vector-example', + // Feature name for persistent state tracking. + 'featureName' => 'example-pinned', + // Indicates the header is expected to move in the DOM. + 'moveElement' => true, + // The type of label tag to be used. + 'labelTagName' => 'div' + ], + // Test case with the header element fixed and not intended to be moved. + 'Pinnable Header Without Moving Element' => [ + // The header should start in an "unpinned" state. + 'pinned' => false, + // ID of the pinnable header element. + 'id' => 'vector-another-example', + // Feature name for tracking state. + 'featureName' => 'another-example-pinned', + // Indicates the header will not move in the DOM. + 'moveElement' => false, + // The type of label tag to be used, in this case, an h2. + 'labelTagName' => 'h2' + ], + ]; + } + + /** + * Tests that the getTemplateData method returns the correct data. + * Uses data provided by provideTestCases to run the same test with different configurations. + * @covers ::getTemplateData + * @dataProvider provideTestCases + */ + public function testGetTemplateData( $pinned, $id, $featureName, $moveElement, $labelTagName ) { + // Mocking the MessageLocalizer to provide predictable responses for given message keys. + $localizer = $this->createMock( MessageLocalizer::class ); + $localizer->method( 'msg' )->willReturnCallback( function ( $key ) { + return $this->createConfiguredMock( Message::class, [ + // Simulated localization output. + '__toString' => $key . '-mocked-label', + ] ); + } ); + + // Instantiating the component with the provided test parameters. + $pinnableHeader = new VectorComponentPinnableHeader( + $localizer, + $pinned, + $id, + $featureName, + $moveElement, + $labelTagName + ); + + // Acquiring the template data from the component. + $templateData = $pinnableHeader->getTemplateData(); + + // Assertions to verify each piece of expected template data. + $this->assertEquals( $pinned, $templateData['is-pinned'] ); + $this->assertStringEndsWith( '-mocked-label', $templateData['label'] ); + $this->assertEquals( $labelTagName, $templateData['label-tag-name'] ); + $this->assertStringEndsWith( '-mocked-label', $templateData['pin-label'] ); + $this->assertStringEndsWith( '-mocked-label', $templateData['unpin-label'] ); + $this->assertEquals( $id, $templateData['data-pinnable-element-id'] ); + $this->assertEquals( $featureName, $templateData['data-feature-name'] ); + + // Additional checks for elements' IDs when the element can move. + if ( $moveElement ) { + $this->assertEquals( $id . '-unpinned-container', $templateData['data-unpinned-container-id'] ); + $this->assertEquals( $id . '-pinned-container', $templateData['data-pinned-container-id'] ); + } else { + $this->assertArrayNotHasKey( 'data-unpinned-container-id', $templateData ); + $this->assertArrayNotHasKey( 'data-pinned-container-id', $templateData ); + } + } +}