| 1 | /* global wp */ |
| 2 | |
| 3 | jQuery( function ( $ ) { |
| 4 | module( 'Remove QueryString Args: InList' ); |
| 5 | var removableQueryArgs = [ |
| 6 | 'user_switched', |
| 7 | 'switched_off', |
| 8 | 'switched_back', |
| 9 | 'message', |
| 10 | 'updated', |
| 11 | 'settings-updated', |
| 12 | 'saved', |
| 13 | 'activated', |
| 14 | 'activate', |
| 15 | 'deactivate', |
| 16 | 'locked', |
| 17 | 'skipped', |
| 18 | 'deleted', |
| 19 | 'trashed', |
| 20 | 'untrashed' |
| 21 | ]; |
| 22 | |
| 23 | function removeParam( key, sourceURL ) { |
| 24 | var rtn = sourceURL.split( '?' )[0], param, params = [], |
| 25 | queryString = (sourceURL.indexOf( '?' ) !== - 1) ? sourceURL.split( '?' )[1] : ''; |
| 26 | if ( queryString !== '' ) { |
| 27 | params = queryString.split( '&' ); |
| 28 | for ( var i = params.length - 1; i >= 0; i -= 1 ) { |
| 29 | param = params[i].split( '=' )[0]; |
| 30 | if ( $.inArray( param, key ) > - 1 ) { |
| 31 | params.splice( i, 1 ); |
| 32 | } |
| 33 | } |
| 34 | rtn = rtn + '?' + params.join( '&' ); |
| 35 | } |
| 36 | return rtn; |
| 37 | } |
| 38 | |
| 39 | test( 'should remove listed arg from the URL', function () { |
| 40 | var testUrl = 'http://src.wordpress-develop.dev/wp-admin/plugins.php?deactivate=true&plugin_status=all&paged=1&s=', |
| 41 | finalUrl = 'http://src.wordpress-develop.dev/wp-admin/plugins.php?plugin_status=all&paged=1&s='; |
| 42 | |
| 43 | equal( removeParam( removableQueryArgs, testUrl ), finalUrl, 'deactivate removed from URL' ); |
| 44 | } ); |
| 45 | |
| 46 | test( 'should not remove unlisted arg from the URL', function () { |
| 47 | var testUrl = 'http://src.wordpress-develop.dev/wp-admin/plugins.php?deactivated=true&plugin_status=all&paged=1&s=', |
| 48 | finalUrl = 'http://src.wordpress-develop.dev/wp-admin/plugins.php?deactivated=true&plugin_status=all&paged=1&s='; |
| 49 | |
| 50 | equal( removeParam( removableQueryArgs, testUrl ), finalUrl, 'deactivated not removed from URL' ); |
| 51 | } ); |
| 52 | } ); |
| 53 | No newline at end of file |