File size: 4,587 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
116
117
118
119
120
121
122
123
124
125
126
import { last } from 'lodash';
import { REASONS_FOR_MANUAL_RENEWAL } from '../constants';
import { createPathWithoutImmediateLoginInformation, createImmediateLoginMessage } from '../utils';

describe( 'immediate-login/utils', () => {
	describe( 'createPathWithoutImmediateLoginInformation', () => {
		// eslint-disable-next-line jest/expect-expect
		test( 'should be possible to call', () => {
			createPathWithoutImmediateLoginInformation( '', {} );
		} );
		test( 'should return the same path if no parameters are present [1]', () => {
			expect( createPathWithoutImmediateLoginInformation( '/', {} ) ).toBe( '/' );
		} );
		test( 'should return the same path if no parameters are present [2]', () => {
			expect( createPathWithoutImmediateLoginInformation( '/root/', {} ) ).toBe( '/root/' );
		} );
		test( 'should return the same path if no parameters are present [3]', () => {
			expect( createPathWithoutImmediateLoginInformation( '/test/another_segment/', {} ) ).toBe(
				'/test/another_segment/'
			);
		} );

		test( 'should return the same path if no login-related parameters are present [1]', () => {
			expect( createPathWithoutImmediateLoginInformation( '/', { url: 'test' } ) ).toBe(
				'/?url=test'
			);
		} );
		test( 'should return the same path if no login-related parameters are present [2]', () => {
			expect(
				createPathWithoutImmediateLoginInformation( '/root/', { page: 15, offset: 32 } )
			).toBe( '/root/?page=15&offset=32' );
		} );
		test( 'should properly encode query parameters keys and values', () => {
			expect(
				createPathWithoutImmediateLoginInformation( '/test/another_segment/', {
					'the title': 'Tom & Jerry',
					another: 'param',
				} )
			).toBe( '/test/another_segment/?the%20title=Tom%20%26%20Jerry&another=param' );
		} );

		test( 'should remove immediate-login-related query params [1]', () => {
			expect(
				createPathWithoutImmediateLoginInformation( '/test/another_segment/', {
					immediate_login_attempt: 'true',
				} )
			).toBe( '/test/another_segment/' );
		} );

		test( 'should remove immediate-login-related query params [2]', () => {
			expect(
				createPathWithoutImmediateLoginInformation( '/test/another_segment/', {
					login_reason: 'some reason',
				} )
			).toBe( '/test/another_segment/' );
		} );

		test( 'should remove immediate-login-related query params [3]', () => {
			expect(
				createPathWithoutImmediateLoginInformation( '/test/another_segment/', {
					immediate_login_attempt: 'true',
					login_reason: 'some reason',
				} )
			).toBe( '/test/another_segment/' );
		} );

		test( 'should remove immediate-login-related query params [4]', () => {
			expect(
				createPathWithoutImmediateLoginInformation( '/test/another_segment/', {
					immediate_login_attempt: 'true',
					immediate_login_success: 'true',
					login_reason: 'some reason',
					login_email: 'test@example.com',
					login_locale: 'fr',
				} )
			).toBe( '/test/another_segment/' );
		} );

		test( 'should remove immediate-login-related query params [5]', () => {
			expect(
				createPathWithoutImmediateLoginInformation( '/test/another_segment/', {
					immediate_login_attempt: 'true',
					login_reason: 'some reason',
					unrelated: 'value1',
					unrelated2: 'value2',
				} )
			).toBe( '/test/another_segment/?unrelated=value1&unrelated2=value2' );
		} );

		test( 'should remove immediate-login-related query params [6]', () => {
			expect(
				createPathWithoutImmediateLoginInformation( '/test/another_segment/', {
					immediate_login_attempt: 'true',
					immediate_login_success: 'true',
					unrelated: 'value1',
					unrelated2: 'value2',
					login_reason: 'some reason',
					login_email: 'test@example.com',
					login_locale: 'fr',
				} )
			).toBe( '/test/another_segment/?unrelated=value1&unrelated2=value2' );
		} );
	} );

	describe( 'createImmediateLoginMessage', () => {
		// eslint-disable-next-line jest/expect-expect
		test( 'should be possible to call', () => {
			createImmediateLoginMessage( '', '' );
		} );
		test( 'should return a string', () => {
			expect( typeof createImmediateLoginMessage( '', '' ) ).toBe( 'string' );
		} );

		const messages = [
			createImmediateLoginMessage( '', 'test@wordpress.com' ),
			createImmediateLoginMessage( REASONS_FOR_MANUAL_RENEWAL[ 0 ], 'test@wordpress.com' ),
			createImmediateLoginMessage( last( REASONS_FOR_MANUAL_RENEWAL ), 'test@wordpress.com' ),
		];
		test( 'should put an email in every message', () => {
			for ( const m of messages ) {
				expect( m.indexOf( 'test@wordpress.com' ) ).not.toBe( -1 );
			}
		} );
	} );
} );