File size: 3,830 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 config from '@automattic/calypso-config';
import { render } from '@testing-library/react';
import WpcomLoginForm from '..';

jest.mock( '@automattic/calypso-config' );

describe( 'WpcomLoginForm', () => {
	const props = {
		log: 'log_text',
		pwd: 'secret',
		authorization: 'authorization_token',
		redirectTo: 'https://test.wordpress.com',
	};

	test( 'should render default fields as expected.', () => {
		const { container, rerender } = render( <WpcomLoginForm { ...props } /> );

		// should render root form element
		const form = container.firstChild;
		expect( form ).toBeVisible();
		expect( form ).toHaveAttribute( 'method', 'post' );
		expect( form ).toHaveAttribute( 'action', 'https://test.wordpress.com/wp-login.php' );

		// form should include default hidden elements
		expect( container.querySelector( 'input[name="log"]' ) ).toHaveValue( 'log_text' );
		expect( container.querySelector( 'input[name="pwd"]' ) ).toHaveValue( 'secret' );
		expect( container.querySelector( 'input[name="authorization"]' ) ).toHaveValue(
			'authorization_token'
		);
		expect( container.querySelector( 'input[name="redirect_to"]' ) ).toHaveValue(
			'https://test.wordpress.com'
		);

		// when update a prop
		rerender( <WpcomLoginForm { ...props } log="another_log" /> );
		const el = container.querySelector( 'input[name="log"]' );
		expect( el ).toHaveValue( 'another_log' );
	} );

	test( 'its action should be under the wpcom subdomain that `redirectTo` prop contains.', () => {
		const { container, rerender } = render(
			<WpcomLoginForm { ...props } redirectTo="https://foo.wordpress.com" />
		);

		expect( container.firstChild ).toHaveAttribute(
			'action',
			'https://foo.wordpress.com/wp-login.php'
		);

		rerender( <WpcomLoginForm { ...props } redirectTo="https://bar.wordpress.com" /> );
		expect( container.firstChild ).toHaveAttribute(
			'action',
			'https://bar.wordpress.com/wp-login.php'
		);
	} );

	test( 'its action should has no subdomain when `hostname` is wpcalypso.wpcom or horizon.wpcom.', () => {
		const myProps = { ...props, redirectTo: 'https://foo.wordpress.com' };
		const { container, rerender } = render( <WpcomLoginForm { ...myProps } /> );

		// should has the same hostname with redirectTo prop.
		expect( container.firstChild ).toHaveAttribute(
			'action',
			'https://foo.wordpress.com/wp-login.php'
		);

		// should be default url
		config.mockReturnValueOnce( 'wpcalypso.wordpress.com' );
		rerender( <WpcomLoginForm { ...myProps } /> );
		expect( container.firstChild ).toHaveAttribute(
			'action',
			'https://wordpress.com/wp-login.php'
		);

		// should has the same hostname with redirectTo prop.
		config.mockReturnValueOnce( 'bar.wordpress.com' );
		rerender( <WpcomLoginForm { ...myProps } /> );
		expect( container.firstChild ).toHaveAttribute(
			'action',
			'https://foo.wordpress.com/wp-login.php'
		);

		// should be default url
		config.mockReturnValueOnce( 'horizon.wordpress.com' );
		rerender( <WpcomLoginForm { ...myProps } /> );
		expect( container.firstChild ).toHaveAttribute(
			'action',
			'https://wordpress.com/wp-login.php'
		);
	} );

	test( 'its action should has no subdomain when `redirectTo` prop is not a subdomain of wpcom.', () => {
		const { container } = render(
			<WpcomLoginForm { ...props } redirectTo="https://wordpress.org" />
		);

		expect( container.firstChild ).toHaveAttribute(
			'action',
			'https://wordpress.com/wp-login.php'
		);
	} );

	test( 'its action should has no subdomain when `redirectTo` prop contains public-api.wordpress.com', () => {
		const { container } = render(
			<WpcomLoginForm { ...props } redirectTo="https://public-api.wordpress.com/" />
		);

		expect( container.firstChild ).toHaveAttribute(
			'action',
			'https://wordpress.com/wp-login.php'
		);
	} );
} );