|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests verifying behaviors for supporting interoperability with JavaScript. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @group js-interop |
| 7 | + */ |
| 8 | +class Tests_JS_Interop extends WP_UnitTestCase { |
| 9 | + /** |
| 10 | + * Ensures proper recognition of a data attribute and how to transform its |
| 11 | + * name into what JavaScript code would read from an element's `dataset`. |
| 12 | + * |
| 13 | + * @ticket 61501 |
| 14 | + * |
| 15 | + * @dataProvider data_possible_custom_data_attributes_and_transformed_names |
| 16 | + * |
| 17 | + * @param string $attribute_name Raw HTML attribute name. |
| 18 | + * @param string|null $dataset_name_if_any Transformed attribute name, or `null` |
| 19 | + * if not a custom data attribute. |
| 20 | + */ |
| 21 | + public function test_transforms_custom_attributes_to_proper_dataset_name( string $attribute_name, ?string $dataset_name_if_any ) { |
| 22 | + $transformed_name = wp_js_dataset_name( $attribute_name ); |
| 23 | + |
| 24 | + if ( isset( $dataset_name_if_any ) ) { |
| 25 | + $this->assertNotNull( |
| 26 | + $transformed_name, |
| 27 | + "Failed to recognize '{$attribute_name}' as a custom data attribute." |
| 28 | + ); |
| 29 | + |
| 30 | + $this->assertSame( |
| 31 | + $dataset_name_if_any, |
| 32 | + $transformed_name, |
| 33 | + 'Improperly transformed custom data attribute name.' |
| 34 | + ); |
| 35 | + } else { |
| 36 | + $this->assertNull( |
| 37 | + $transformed_name, |
| 38 | + "Should not have identified '{$attribute_name}' as a custom data attribute." |
| 39 | + ); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Data provider. |
| 45 | + * |
| 46 | + * @return array[]. |
| 47 | + */ |
| 48 | + public static function data_possible_custom_data_attributes_and_transformed_names() { |
| 49 | + return array( |
| 50 | + // Non-custom-data attributes. |
| 51 | + 'Normal attribute' => array( 'post-id', null ), |
| 52 | + 'Single word' => array( 'id', null ), |
| 53 | + |
| 54 | + // Normative custom data attributes. |
| 55 | + 'Normal custom data attribute' => array( 'data-post-id', 'postId' ), |
| 56 | + 'Leading dash' => array( 'data--before', 'Before' ), |
| 57 | + 'Trailing dash' => array( 'data-after-', 'after-' ), |
| 58 | + 'Double-dashes' => array( 'data-wp-bind--enabled', 'wpBind-Enabled' ), |
| 59 | + 'Double-dashes everywhere' => array( 'data--one--two--', 'One-Two--' ), |
| 60 | + 'Triple-dashes' => array( 'data---one---two---', '-One--Two---' ), |
| 61 | + |
| 62 | + // Unexpected but recognized custom data attributes. |
| 63 | + 'Only comprising a prefix' => array( 'data-', '' ), |
| 64 | + 'With upper case ASCII' => array( 'data-Post-ID', 'postId' ), |
| 65 | + 'With medial upper casing' => array( 'data-uPPer-cAsE', 'upperCase' ), |
| 66 | + 'With Unicode whitespace' => array( "data-\u{2003}", "\u{2003}" ), |
| 67 | + 'With Emoji' => array( 'data-🐄-pasture', '🐄Pasture' ), |
| 68 | + 'Brackets and colon' => array( 'data-[wish:granted]', '[wish:granted]' ), |
| 69 | + |
| 70 | + // Pens and Pencils: a collection of interesting combinations of dash and underscore. |
| 71 | + 'data-pens-and-pencils' => array( 'data-pens-and-pencils', 'pensAndPencils' ), |
| 72 | + 'data-pens--and--pencils' => array( 'data-pens--and--pencils', 'pens-And-Pencils' ), |
| 73 | + 'data--pens--and--pencils' => array( 'data--pens--and--pencils', 'Pens-And-Pencils' ), |
| 74 | + 'data---pens---and---pencils' => array( 'data---pens---and---pencils', '-Pens--And--Pencils' ), |
| 75 | + 'data-pens-and-pencils-' => array( 'data-pens-and-pencils-', 'pensAndPencils-' ), |
| 76 | + 'data-pens-and-pencils--' => array( 'data-pens-and-pencils--', 'pensAndPencils--' ), |
| 77 | + 'data-pens_and_pencils__' => array( 'data-pens_and_pencils__', 'pens_and_pencils__' ), |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
0 commit comments