From f82e1c5d234a9926f61c09e810a8078ca0848859 Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Fri, 15 Nov 2013 20:40:54 +0000 Subject: [PATCH] Add JavaScript tests for shortcode.js. props jorbin, iandunn. see #25784. git-svn-id: https://develop.svn.wordpress.org/trunk@26222 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/qunit/index.html | 9 +- tests/qunit/wp-includes/js/shortcode.js | 140 ++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 3 deletions(-) create mode 100644 tests/qunit/wp-includes/js/shortcode.js diff --git a/tests/qunit/index.html b/tests/qunit/index.html index 65b772a338..56d64a4862 100644 --- a/tests/qunit/index.html +++ b/tests/qunit/index.html @@ -3,19 +3,22 @@ WordPress QUnit Test Suite - + - + + + - + + diff --git a/tests/qunit/wp-includes/js/shortcode.js b/tests/qunit/wp-includes/js/shortcode.js new file mode 100644 index 0000000000..c49f3f103f --- /dev/null +++ b/tests/qunit/wp-includes/js/shortcode.js @@ -0,0 +1,140 @@ +/* global wp, jQuery */ +jQuery( function() { + module( 'shortcode' ); + + test( 'next() should find the shortcode', function() { + var result; + + // Basic + result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode' ); + equal( result.index, 13, 'foo shortcode found at index 13' ); + + result = wp.shortcode.next( 'foo', 'this has the [foo param="foo"] shortcode' ); + equal( result.index, 13, 'foo shortcode with params found at index 13' ); + }); + + test( 'next() should not shortcodes that are not there', function() { + var result; + + // Not found + result = wp.shortcode.next( 'bar', 'this has the [foo] shortcode' ); + equal( result, undefined, 'bar shortcode not found' ); + + result = wp.shortcode.next( 'bar', 'this has the [foo param="bar"] shortcode' ); + equal( result, undefined, 'bar shortcode not found with params' ); + }); + + test( 'next() should find the shortcode when told to start looking beyond the start of the string', function() { + var result; + + // Starting at indices + result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 12 ); + equal( result.index, 13, 'foo shortcode found before index 13' ); + + result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 13 ); + equal( result.index, 13, 'foo shortcode found at index 13' ); + + result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode', 14 ); + equal( result, undefined, 'foo shortcode not found after index 13' ); + }); + + test( 'next() should find the second instances of the shortcode when the starting indice is after the start of the first one', function() { + var result; + + result = wp.shortcode.next( 'foo', 'this has the [foo] shortcode [foo] twice', 14 ); + equal( result.index, 29, 'foo shortcode found the second foo at index 29' ); + }); + + + test( 'next() should not find escaped shortcodes', function() { + var result; + + // Escaped + result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode' ); + equal( result, undefined, 'foo shortcode not found when escaped' ); + + result = wp.shortcode.next( 'foo', 'this has the [[foo param="foo"]] shortcode' ); + equal( result, undefined, 'foo shortcode not found when escaped with params' ); + }); + + test( 'next() should find the second instances of the shortcode when the first one is escaped', function() { + var result; + + + result = wp.shortcode.next( 'foo', 'this has the [[foo]] shortcode [foo] twice' ); + equal( result.index, 31, 'foo shortcode found the non-escaped foo at index 31' ); + }); + + test( 'next() should not find shortcodes that are not full matches', function() { + var result; + + // Stubs + result = wp.shortcode.next( 'foo', 'this has the [foobar] shortcode' ); + equal( result, undefined, 'stub does not trigger match' ); + + result = wp.shortcode.next( 'foobar', 'this has the [foo] shortcode' ); + equal( result, undefined, 'stub does not trigger match' ); + }); + + test( 'replace() should replace the shortcode', function() { + var result; + + // Basic + result = wp.shortcode.replace( 'foo', 'this has the [foo] shortcode', shortcodeReplaceCallback ); + equal( result, 'this has the bar shortcode', 'foo replaced with bar' ); + + result = wp.shortcode.replace( 'foo', 'this has the [foo param="foo"] shortcode', shortcodeReplaceCallback ); + equal( result, 'this has the bar shortcode', 'foo and params replaced with bar' ); + }); + + test( 'replace() should not replace the shortcode when it does not match', function() { + var result; + + // Not found + result = wp.shortcode.replace( 'bar', 'this has the [foo] shortcode', shortcodeReplaceCallback ); + equal( result, 'this has the [foo] shortcode', 'bar not found' ); + + result = wp.shortcode.replace( 'bar', 'this has the [foo param="bar"] shortcode', shortcodeReplaceCallback ); + equal( result, 'this has the [foo param="bar"] shortcode', 'bar not found with params' ); + }); + + test( 'replace() should replace the shortcode in all instances of its use', function() { + var result; + + // Multiple instances + result = wp.shortcode.replace( 'foo', 'this has the [foo] shortcode [foo] twice', shortcodeReplaceCallback ); + equal( result, 'this has the bar shortcode bar twice', 'foo replaced with bar twice' ); + + result = wp.shortcode.replace( 'foo', 'this has the [foo param="foo"] shortcode [foo] twice', shortcodeReplaceCallback ); + equal( result, 'this has the bar shortcode bar twice', 'foo and params replaced with bar twice' ); + }); + + test( 'replace() should not replace the escaped shortcodes', function() { + var result; + + // Escaped + result = wp.shortcode.replace( 'foo', 'this has the [[foo]] shortcode', shortcodeReplaceCallback ); + equal( result, 'this has the [[foo]] shortcode', 'escaped foo not replaced' ); + + result = wp.shortcode.replace( 'foo', 'this has the [[foo param="bar"]] shortcode', shortcodeReplaceCallback ); + equal( result, 'this has the [[foo param="bar"]] shortcode', 'escaped foo with params not replaced' ); + + result = wp.shortcode.replace( 'foo', 'this [foo] has the [[foo param="bar"]] shortcode escaped', shortcodeReplaceCallback ); + equal( result, 'this bar has the [[foo param="bar"]] shortcode escaped', 'escaped foo with params not replaced but unescaped foo replaced' ); + }); + + test( 'replace() should not replace the shortcode when it is an incomplete match', function() { + var result; + + // Stubs + result = wp.shortcode.replace( 'foo', 'this has the [foobar] shortcode', shortcodeReplaceCallback ); + equal( result, 'this has the [foobar] shortcode', 'stub not replaced' ); + + result = wp.shortcode.replace( 'foobar', 'this has the [foo] shortcode', shortcodeReplaceCallback ); + equal( result, 'this has the [foo] shortcode', 'stub not replaced' ); + }); + + function shortcodeReplaceCallback( ) { + return 'bar'; + } +});