File size: 2,924 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
import { DEFAULT_VIEWPORT_HEIGHT } from '../../constants';
import { Design, DesignPreviewOptions } from '../../types';
import { getDesignPreviewUrl } from '../designs';

const mergeDesign = ( currentDesign, newDesign ) => ( {
	...currentDesign,
	...newDesign,
	recipe: {
		...currentDesign.recipe,
		...newDesign.recipe,
	},
} );

describe( 'Design Picker designs utils', () => {
	describe( 'getDesignPreviewUrl', () => {
		const design = {
			title: 'Zoologist',
			recipe: {
				stylesheet: 'pub/zoologist',
				pattern_ids: [ 12, 34 ],
			},
		} as Design;

		it( 'should return the block-previews/site endpoint with the correct query params', () => {
			expect( getDesignPreviewUrl( design, {} ) ).toEqual(
				`https://public-api.wordpress.com/wpcom/v2/block-previews/site?stylesheet=pub%2Fzoologist&pattern_ids=12%2C34&viewport_height=${ DEFAULT_VIEWPORT_HEIGHT }&site_title=Zoologist`
			);
		} );

		it( 'should append the header_pattern_ids to the query params', () => {
			const customizedDesign = mergeDesign( design, {
				recipe: { header_pattern_ids: [ 56, 78 ] },
			} );

			expect( getDesignPreviewUrl( customizedDesign, {} ) ).toEqual(
				`https://public-api.wordpress.com/wpcom/v2/block-previews/site?stylesheet=pub%2Fzoologist&pattern_ids=12%2C34&header_pattern_ids=56%2C78&viewport_height=${ DEFAULT_VIEWPORT_HEIGHT }&site_title=Zoologist`
			);
		} );

		it( 'should append the footer_pattern_ids to the query params', () => {
			const customizedDesign = mergeDesign( design, {
				recipe: { footer_pattern_ids: [ 56, 78 ] },
			} );

			expect( getDesignPreviewUrl( customizedDesign, {} ) ).toEqual(
				`https://public-api.wordpress.com/wpcom/v2/block-previews/site?stylesheet=pub%2Fzoologist&pattern_ids=12%2C34&footer_pattern_ids=56%2C78&viewport_height=${ DEFAULT_VIEWPORT_HEIGHT }&site_title=Zoologist`
			);
		} );

		it( 'should append the preview options to the query params', () => {
			const options: DesignPreviewOptions = {
				language: 'id',
				site_title: 'Design Title',
				viewport_height: 700,
			};

			expect( getDesignPreviewUrl( design, options ) ).toEqual(
				'https://public-api.wordpress.com/wpcom/v2/block-previews/site?stylesheet=pub%2Fzoologist&pattern_ids=12%2C34&language=id&viewport_height=700&site_title=Design%20Title'
			);
		} );

		// Parentheses in uri components don't usually need to be escaped, but because the design url sometimes appears
		// in a `background-url: url( ... )` CSS rule the parentheses will break it.
		it( 'should escape parentheses within the site title', () => {
			const options: DesignPreviewOptions = {
				site_title: 'Mock(Design)(Title)',
			};

			expect( getDesignPreviewUrl( design, options ) ).toEqual(
				`https://public-api.wordpress.com/wpcom/v2/block-previews/site?stylesheet=pub%2Fzoologist&pattern_ids=12%2C34&viewport_height=${ DEFAULT_VIEWPORT_HEIGHT }&site_title=Mock%28Design%29%28Title%29`
			);
		} );
	} );
} );