WordPress.org

Make WordPress Core

Ticket #25784: 25784.diff

File 25784.diff, 5.6 KB (added by iandunn, 7 years ago)
  • tests/qunit/index.html

     
    33<head>
    44  <title>WordPress QUnit Test Suite</title>
    55
    6   <!-- jQuery -->
     6  <!-- Dependencies -->
    77  <script src="../../src/wp-includes/js/jquery/jquery.js"></script>
    8 
     8  <script src="../../src/wp-includes/js/underscore.min.js"></script>
     9  <script src="../../src/wp-includes/js/zxcvbn.min.js"></script>
     10       
    911  <!-- QUnit -->
    1012  <link rel="stylesheet" href="vendor/qunit.css" type="text/css" media="screen" />
    1113  <script src="vendor/qunit.js"></script>
    1214
    1315  <!-- Tested files -->
    14   <script src="../../src/wp-includes/js/zxcvbn.min.js"></script>
    1516  <script src="../../src/wp-admin/js/password-strength-meter.js"></script>
     17  <script src="../../src/wp-includes/js/shortcode.js"></script>
    1618
    1719  <!-- Unit tests -->
    1820  <script src="wp-admin/js/password-strength-meter.js"></script>
     21  <script src="wp-includes/js/shortcode.js"></script>
    1922
    2023</head>
    2124<body>
  • tests/qunit/wp-includes/js/shortcode.js

     
     1/* global wp, jQuery */
     2jQuery( function() {
     3        module( 'shortcode' );
     4
     5        test( 'next() should find the shortcode', function() {
     6                var result;
     7               
     8                // Basic
     9                result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode' );
     10                equal( result.index, 13, 'foo shortcode found at index 13' );
     11
     12                result = wp.shortcode.next( 'foo', 'this has the [foo param="foo"] shortcode' );
     13                equal( result.index, 13, 'foo shortcode with params found at index 13' );
     14
     15                // Not found
     16                result = wp.shortcode.next( 'bar', 'this has the [foo] shortcode' );
     17                equal( result, undefined, 'bar shortcode not found' );
     18
     19                result = wp.shortcode.next( 'bar', 'this has the [foo param="bar"] shortcode' );
     20                equal( result, undefined, 'bar shortcode not found with params' );
     21               
     22                // Starting at indices
     23                result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 12 );
     24                equal( result.index, 13, 'foo shortcode found before index 13' );
     25
     26                result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 13 );
     27                equal( result.index, 13, 'foo shortcode found at index 13' );
     28
     29                result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 14 );
     30                equal( result, undefined, 'foo shortcode not found after index 13' );
     31
     32                // Multiple instances
     33                result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode [foo] twice', 14 );
     34                equal( result.index, 29, 'foo shortcode found the second foo at index 29' );
     35
     36                result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode [foo] twice' );
     37                equal( result.index, 31, 'foo shortcode found the non-escaped foo at index 31' );
     38
     39                // Escaped
     40                result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode' );
     41                equal( result, undefined, 'foo shortcode not found when escaped' );
     42
     43                result = wp.shortcode.next( 'foo', 'this has the [[foo param="foo"]] shortcode' );
     44                equal( result, undefined, 'foo shortcode not found when escaped with params' );
     45
     46                // Stubs
     47                result = wp.shortcode.next( 'foo', 'this has the [foobar] shortcode' );
     48                equal( result, undefined, "stub doesn't trigger match" );
     49
     50                result = wp.shortcode.next( 'foobar', 'this has the [foo] shortcode' );
     51                equal( result, undefined, "stub doesn't trigger match" );
     52        });
     53
     54        test( 'replace() should replace the shortcode', function() {
     55                var result;
     56               
     57                // Basic
     58                result = wp.shortcode.replace( 'foo', 'this has the [foo] shortcode', shortcodeReplaceCallback );
     59                equal( result, 'this has the bar shortcode', 'foo replaced with bar' );
     60               
     61                result = wp.shortcode.replace( 'foo', 'this has the [foo param="foo"] shortcode', shortcodeReplaceCallback );
     62                equal( result, 'this has the bar shortcode', 'foo and params replaced with bar' );
     63
     64                // Not found
     65                result = wp.shortcode.replace( 'bar', 'this has the [foo] shortcode', shortcodeReplaceCallback );
     66                equal( result, 'this has the [foo] shortcode', 'bar not found' );
     67
     68                result = wp.shortcode.replace( 'bar', 'this has the [foo param="bar"] shortcode', shortcodeReplaceCallback );
     69                equal( result, 'this has the [foo param="bar"] shortcode', 'bar not found with params' );
     70
     71                // Multiple instances
     72                result = wp.shortcode.replace( 'foo', 'this has the [foo] shortcode [foo] twice', shortcodeReplaceCallback );
     73                equal( result, 'this has the bar shortcode bar twice', 'foo replaced with bar twice' );
     74
     75                result = wp.shortcode.replace( 'foo', 'this has the [foo param="foo"] shortcode [foo] twice', shortcodeReplaceCallback );
     76                equal( result, 'this has the bar shortcode bar twice', 'foo and params replaced with bar twice' );
     77               
     78                // Escaped
     79                result = wp.shortcode.replace( 'foo', 'this has the [[foo]] shortcode', shortcodeReplaceCallback );
     80                equal( result, 'this has the [[foo]] shortcode', 'escaped foo not replaced' );
     81
     82                result = wp.shortcode.replace( 'foo', 'this has the [[foo param="bar"]] shortcode', shortcodeReplaceCallback );
     83                equal( result, 'this has the [[foo param="bar"]] shortcode', 'escaped foo with params not replaced' );
     84
     85                // Stubs
     86                result = wp.shortcode.replace( 'foo', 'this has the [foobar] shortcode', shortcodeReplaceCallback )
     87                equal( result, 'this has the [foobar] shortcode', 'stub not replaced' );
     88
     89                result = wp.shortcode.replace( 'foobar', 'this has the [foo] shortcode', shortcodeReplaceCallback );
     90                equal( result, 'this has the [foo] shortcode', 'stub not replaced' );
     91        });
     92
     93        function shortcodeReplaceCallback( shortcode ) {
     94                return 'bar';
     95        }
     96});