File size: 6,443 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import {
	acceptDomainTransfer,
	acceptDomainTransferCompleted,
	cancelDomainTransferRequest,
	cancelDomainTransferRequestCompleted,
	cancelDomainTransferRequestFailed,
	declineDomainTransferCompleted,
	fetchWapiDomainInfo,
	fetchWapiDomainInfoFailure,
	fetchWapiDomainInfoSuccess,
	requestDomainTransferCode,
	requestDomainTransferCodeCompleted,
	requestDomainTransferCodeFailed,
	updateDomainTransfer,
} from '../actions';
import { createDomainObject } from '../assembler';
import reducer, { items } from '../reducer';

const selectedRegistrar = {
	tag: 'UNIT-TEST-TAG',
	registrarName: 'Unit Test',
	registrarUrl: 'https://automattic.com',
};
const testDomain = 'unit-test-transfer-reducer.com';

describe( 'reducer', () => {
	test( 'should include expected keys in return value', () => {
		expect( reducer( undefined, {} ) ).toEqual( {
			items: {},
		} );
	} );

	test( 'should build a IPS tag transfer payload', () => {
		const options = { selectedRegistrar, saveStatus: 'saving' };
		const response = items( undefined, updateDomainTransfer( testDomain, options ) );

		expect( response ).toMatchObject( {
			[ testDomain ]: options,
		} );
	} );

	test( 'should mark requesting transfer code as true when requesting a transfer code', () => {
		const state = items( undefined, requestDomainTransferCode( testDomain, {} ) );

		expect( state[ testDomain ].isRequestingTransferCode ).toBe( true );
	} );

	test( 'should mark requesting transfer code as false when requesting a transfer code succeeded', () => {
		const initialState = {
			[ testDomain ]: {
				isRequestingTransferCode: true,
				data: {},
			},
		};
		const state = items( initialState, requestDomainTransferCodeCompleted( testDomain, {} ) );

		expect( state[ testDomain ].isRequestingTransferCode ).toBe( false );
	} );

	test( 'should mark requesting transfer code as false when requesting a transfer code failed', () => {
		const initialState = {
			[ testDomain ]: {
				isRequestingTransferCode: true,
			},
		};
		const state = items( initialState, requestDomainTransferCodeFailed( testDomain ) );

		expect( state[ testDomain ].isRequestingTransferCode ).toBe( false );
	} );

	test( 'should set needsUpdate to true when requesting a transfer code succeeded', () => {
		const initialState = {
			[ testDomain ]: {
				data: {},
			},
		};
		const state = items( initialState, requestDomainTransferCodeCompleted( testDomain, {} ) );

		expect( state[ testDomain ].needsUpdate ).toBe( true );
	} );

	test( 'should update locked state when requesting a transfer code succeeded and unlock requested', () => {
		const initialState = {
			[ testDomain ]: {
				data: {
					locked: true,
				},
			},
		};
		const state = items(
			initialState,
			requestDomainTransferCodeCompleted( testDomain, { unlock: true } )
		);

		expect( state[ testDomain ].data.locked ).toBe( false );
	} );

	test( 'should mark canceling transfer as true when canceling a transfer', () => {
		const state = items( undefined, cancelDomainTransferRequest( testDomain, {} ) );

		expect( state[ testDomain ].isCancelingTransfer ).toBe( true );
	} );

	test( 'should mark canceling transfer as true when canceling a transfer succeeded', () => {
		const initialState = {
			[ testDomain ]: {
				isCancelingTransfer: true,
				data: {},
			},
		};
		const state = items( initialState, cancelDomainTransferRequestCompleted( testDomain, {} ) );

		expect( state[ testDomain ].isCancelingTransfer ).toBe( false );
	} );

	test( 'should mark canceling transfer as false when canceling a transfer failed', () => {
		const initialState = {
			[ testDomain ]: {
				isCancelingTransfer: true,
			},
		};
		const state = items( initialState, cancelDomainTransferRequestFailed( testDomain ) );

		expect( state[ testDomain ].isCancelingTransfer ).toBe( false );
	} );

	test( 'should update locked and pendingTransfer state when canceling a transfer succeeded', () => {
		const initialState = {
			[ testDomain ]: {
				data: {
					locked: true,
				},
			},
		};
		const state = items(
			initialState,
			cancelDomainTransferRequestCompleted( testDomain, { locked: false } )
		);

		expect( state[ testDomain ].data.locked ).toBe( false );
		expect( state[ testDomain ].data.pendingTransfer ).toBe( false );
	} );

	test( 'should mark accepting transfer as true when accepting a transfer', () => {
		const state = items( undefined, acceptDomainTransfer( testDomain ) );

		expect( state[ testDomain ].isAcceptingTransfer ).toBe( true );
	} );

	test( 'should mark accepting transfer as false when accepting a transfer succeeded', () => {
		const initialState = {
			[ testDomain ]: {
				isAcceptingTransfer: true,
			},
		};
		const state = items( initialState, acceptDomainTransferCompleted( testDomain ) );

		expect( state[ testDomain ].isAcceptingTransfer ).toBe( false );
	} );

	test( 'should mark pending transfer as false when accepting a transfer succeeded', () => {
		const initialState = {
			[ testDomain ]: {
				isAcceptingTransfer: true,
			},
		};
		const state = items( initialState, acceptDomainTransferCompleted( testDomain ) );

		expect( state[ testDomain ].data.pendingTransfer ).toBe( false );
	} );

	test( 'should mark pending transfer as false when declining a transfer succeeded', () => {
		const initialState = {
			[ testDomain ]: {},
		};
		const state = items( initialState, declineDomainTransferCompleted( testDomain ) );

		expect( state[ testDomain ].data.pendingTransfer ).toBe( false );
	} );

	test( 'should set needsUpdate to false when requesting domain information', () => {
		const state = items( {}, fetchWapiDomainInfo( testDomain ) );

		expect( state[ testDomain ].needsUpdate ).toBe( false );
	} );

	test( 'should set needsUpdate to false when domain information request failed', () => {
		const state = items( {}, fetchWapiDomainInfoFailure( testDomain ) );

		expect( state[ testDomain ].needsUpdate ).toBe( false );
	} );

	test( 'should disable needsUpdate, enable hasLoadedFromServer and set domain status when domain info request succeeded', () => {
		const status = {
			locked: false,
			pending_transfer: false,
			transfer_prohibited: false,
		};
		const state = items( {}, fetchWapiDomainInfoSuccess( testDomain, status ) );

		expect( state[ testDomain ].hasLoadedFromServer ).toBe( true );
		expect( state[ testDomain ].data ).toEqual( createDomainObject( status ) );
		expect( state[ testDomain ].needsUpdate ).toBe( false );
	} );
} );