Skip to content

Commit

Permalink
Added unit tests for the escape and unescape functions in core/code b…
Browse files Browse the repository at this point in the history
…lock
  • Loading branch information
David Aguilera committed Apr 26, 2019
1 parent d86123a commit 1cb6ff8
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions packages/block-library/src/code/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Internal dependencies
*/
import { escape, unescape } from '../utils';

describe( 'core/code', () => {
describe( 'escape()', () => {
it( 'should escape ampersands', () => {
const text = escape( '&' );
expect( text ).toBe( '&' );
} );

it( 'should escape opening square brackets', () => {
const text = escape( '[shortcode][/shortcode]' );
expect( text ).toBe( '[shortcode][/shortcode]' );
} );

it( 'should escape the protocol of an isolated url', () => {
const text = escape( 'https://example.com/test/' );
expect( text ).toBe( 'https://example.com/test/' );
} );

it( 'should not escape the protocol of a non isolated url', () => {
const text = escape( 'Text https://example.com/test/' );
expect( text ).toBe( 'Text https://example.com/test/' );
} );

it( 'should escape ampersands last', () => {
const text = escape( '[shortcode][/shortcode]' );
expect( text ).toBe( '[shortcode][/shortcode]' );
expect( text ).not.toBe( '[shortcode][/shortcode]' );
} );
} );

describe( 'unescape()', () => {
it( 'should unescape escaped ampersands', () => {
const text = unescape( '&' );
expect( text ).toBe( '&' );
} );

it( 'should unescape escaped opening square brackets', () => {
const text = unescape( '[shortcode][/shortcode]' );
expect( text ).toBe( '[shortcode][/shortcode]' );
} );

it( 'should unescape the escaped protocol of an isolated url', () => {
const text = unescape( 'https://example.com/test/' );
expect( text ).toBe( 'https://example.com/test/' );
} );

it( 'should revert the result of escape()', () => {
const ampersand = unescape( escape( '&' ) );
expect( ampersand ).toBe( '&' );

const squareBracket = unescape( escape( '[shortcode][/shortcode]' ) );
expect( squareBracket ).toBe( '[shortcode][/shortcode]' );

const url = unescape( escape( 'https://example.com/test/' ) );
expect( url ).toBe( 'https://example.com/test/' );
} );
} );
} );

0 comments on commit 1cb6ff8

Please sign in to comment.