File size: 4,490 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
import { setShouldServerSideRenderLogin } from '../ssr';

function getSomeCleanLoginContext( queryValues, lang = 'en' ) {
	return {
		query: queryValues,
		lang,
	};
}

describe( 'setShouldServerSideRenderLogin', () => {
	test( 'when lang is Mag-16, then sets context.serverSideRender to TRUE - and calls next()', () => {
		const next = jest.fn();
		const contextWithNonDefaultLang = getSomeCleanLoginContext( {}, 'ar' );

		setShouldServerSideRenderLogin( contextWithNonDefaultLang, next );
		expect( contextWithNonDefaultLang.serverSideRender ).toBe( true );
		expect( next ).toHaveBeenCalledTimes( 1 );
	} );

	test( 'when lang is non-default and non-Mag-16, then sets context.serverSideRender to FALSE - and calls next()', () => {
		const next = jest.fn();
		const contextWithNonDefaultLang = getSomeCleanLoginContext( {}, 'bg' );

		setShouldServerSideRenderLogin( contextWithNonDefaultLang, next );
		expect( contextWithNonDefaultLang.serverSideRender ).toBe( false );
		expect( next ).toHaveBeenCalledTimes( 1 );
	} );

	test( 'when query is empty, then sets context.serverSideRender to TRUE - and calls next()', () => {
		const next = jest.fn();
		const contextWithoutQueryKeys = getSomeCleanLoginContext( {} );

		setShouldServerSideRenderLogin( contextWithoutQueryKeys, next );
		expect( contextWithoutQueryKeys.serverSideRender ).toBe( true );
		expect( next ).toHaveBeenCalledTimes( 1 );
	} );

	test( 'when query has only INVALID keys, then sets context.serverSideRender to FALSE - and calls next()', () => {
		const next = jest.fn();
		const contextWithQueryKeys = {
			query: {
				hello: 'world',
			},
		};

		setShouldServerSideRenderLogin( contextWithQueryKeys, next );
		expect( contextWithQueryKeys.serverSideRender ).toBe( false );
		expect( next ).toHaveBeenCalledTimes( 1 );
	} );

	test( 'when query has only valid keys, then serverSideRender is TRUE, but when invalid keys are added, it fails', () => {
		const next = jest.fn();
		let contextWithQueryKeys = getSomeCleanLoginContext( { client_id: 1234 } );
		setShouldServerSideRenderLogin( contextWithQueryKeys, next );
		expect( contextWithQueryKeys.serverSideRender ).toBe( true );

		// add one valid key, signup_flow
		contextWithQueryKeys = getSomeCleanLoginContext( {
			client_id: 1288,
			signup_flow: 'avbsdaf',
		} );
		setShouldServerSideRenderLogin( contextWithQueryKeys, next );
		expect( contextWithQueryKeys.serverSideRender ).toBe( true );

		// add another valid key, redirect_to
		contextWithQueryKeys = getSomeCleanLoginContext( {
			client_id: 87357,
			signup_flow: 'xsa',
			redirect_to: 'https://wordpress.com/theme',
		} );
		setShouldServerSideRenderLogin( contextWithQueryKeys, next );
		expect( contextWithQueryKeys.serverSideRender ).toBe( true );

		// add another random key and expect SSR to stop
		contextWithQueryKeys = getSomeCleanLoginContext( {
			client_id: 87357,
			signup_flow: 'xsa',
			redirect_to: 'https://wordpress.com/theme',
			hello: 'world',
		} );
		setShouldServerSideRenderLogin( contextWithQueryKeys, next );
		expect( contextWithQueryKeys.serverSideRender ).toBe( false );

		// add do one more test in which only a few of the keys are set and then the invalid key is set
		contextWithQueryKeys = getSomeCleanLoginContext( { client_id: '5678', hello: 'world' } );
		setShouldServerSideRenderLogin( contextWithQueryKeys, next );
		expect( contextWithQueryKeys.serverSideRender ).toBe( false );

		// for all of the above cases, expect that next was called
		expect( next ).toHaveBeenCalledTimes( 5 ); // because we have 5 tests and we did not check for each of them
	} );

	test( 'when query has redirect_to, then only the ones starting with the prefix make SSR true', () => {
		const contextWithThemePrefix = getSomeCleanLoginContext( {
			redirect_to: 'https://wordpress.com/theme/something',
		} );
		setShouldServerSideRenderLogin( contextWithThemePrefix, () => {} );
		expect( contextWithThemePrefix.serverSideRender ).toBe( true );

		const contextWithGoPrefix = getSomeCleanLoginContext( {
			redirect_to: 'https://wordpress.com/go/something',
		} );
		setShouldServerSideRenderLogin( contextWithGoPrefix, () => {} );
		expect( contextWithGoPrefix.serverSideRender ).toBe( true );

		const contextWithInvalidPrefix = getSomeCleanLoginContext( {
			redirect_to: 'https://blue.com/go/something',
		} );
		setShouldServerSideRenderLogin( contextWithInvalidPrefix, () => {} );
		expect( contextWithInvalidPrefix.serverSideRender ).toBe( false );
	} );
} );