File size: 3,790 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * @jest-environment jsdom
 */

import { capitalPDangit, parseHtml, preventWidows } from '../index';

describe( 'formatting', () => {
	describe( '#capitalPDangtest()', function () {
		test( 'should error when input is not a string', function () {
			const types = [ {}, undefined, 1, true, [], function () {} ];

			types.forEach( function ( type ) {
				expect( function () {
					capitalPDangit( type );
				} ).toThrow( Error );
			} );
		} );

		test( 'should not modify wordpress', function () {
			const strings = [ 'wordpress', 'I love wordpress' ];

			strings.forEach( function ( string ) {
				expect( capitalPDangit( string ) ).toBe( string );
			} );
		} );

		test( 'should return WordPress with a capital P when passed Wordpress', function () {
			expect( capitalPDangit( 'Wordpress' ) ).toBe( 'WordPress' );
			expect( capitalPDangit( 'I love Wordpress' ) ).toBe( 'I love WordPress' );
		} );

		test( 'should replace all instances of Wordpress', function () {
			expect( capitalPDangit( 'Wordpress Wordpress' ) ).toBe( 'WordPress WordPress' );
			expect( capitalPDangit( 'I love Wordpress and Wordpress loves me' ) ).toBe(
				'I love WordPress and WordPress loves me'
			);
		} );
	} );

	describe( '#parseHtml()', function () {
		test( 'should equal to null when input is not a string', function () {
			const types = [ {}, undefined, 1, true, [], function () {} ];

			types.forEach( function ( type ) {
				expect( parseHtml( type ) ).toBeNull();
			} );
		} );

		test( 'should return a DOM element if you pass in DOM element', function () {
			const div = document.createElement( 'div' );
			expect( div ).toBe( parseHtml( div ) );
		} );

		test( 'should return a document fragment if we pass in a string', function () {
			const fragment = parseHtml( 'hello' );
			expect( typeof fragment.querySelector ).toBe( 'function' );
			expect( fragment.querySelectorAll( '*' ) ).toHaveLength( 0 );
		} );

		test( 'should return a document fragment if we pass in a HTML string', function () {
			const fragment = parseHtml( '<div>hello</div>' );
			expect( fragment.querySelectorAll( 'div' ) ).toHaveLength( 1 );
		} );

		test( 'should parseHtml and return document fragment that can be queried', function () {
			const strings = [
				'<span><a href="stuff">hello world</a></span>',
				'<div><span></span><a href="stuff">hello world</a></div>',
			];

			strings.forEach( function ( string ) {
				const link = parseHtml( string ).querySelectorAll( 'a' );
				expect( link[ 0 ].innerHTML ).toBe( 'hello world' );
			} );
		} );
	} );

	describe( '#preventWidows()', () => {
		test( 'should not modify input if type is not string', () => {
			const types = [ {}, undefined, 1, true, [], function () {} ];

			types.forEach( ( type ) => {
				expect( preventWidows( type ) ).toBe( type );
			} );
		} );

		test( 'should return empty string when input is all whitespace', () => {
			const inputs = [ ' ', '\t', '\n' ];

			inputs.forEach( ( input ) => {
				expect( preventWidows( input ) ).toBe( '' );
			} );
		} );

		test( 'should return input when only one word', () => {
			expect( preventWidows( 'test' ) ).toBe( 'test' );
		} );

		test( 'should trim whitespace', () => {
			expect( preventWidows( 'test ' ) ).toBe( 'test' );
			expect( preventWidows( '\ntest string ' ) ).toBe( 'test\xA0string' );
		} );

		test( 'should add non-breaking space between words to keep', () => {
			const input = 'I really love BBQ. It is one of my favorite foods. Beef ribs are amazing.';
			expect( preventWidows( input ) ).toBe(
				'I really love BBQ. It is one of my favorite foods. Beef ribs are\xA0amazing.'
			);
			expect( preventWidows( input, 4 ) ).toBe(
				'I really love BBQ. It is one of my favorite foods. Beef\xA0ribs\xA0are\xA0amazing.'
			);
		} );
	} );
} );