File size: 6,928 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
 * @jest-environment jsdom
 */

import { findEligibleTour, getGuidedTourState, hasTourJustBeenVisible } from '../selectors';

jest.mock( 'calypso/layout/guided-tours/config', () => {
	return require( 'calypso/state/guided-tours/test/fixtures/config' );
} );

describe( 'selectors', () => {
	describe( '#hasTourJustBeenVisible', () => {
		test( 'should return false when no tour has been seen', () => {
			const state = { ui: { actionLog: [] } };
			expect( hasTourJustBeenVisible( state ) ).toBeUndefined();
		} );

		test( 'should return true when a tour has just been seen', () => {
			const now = 1478623930204;
			const state = {
				ui: {
					actionLog: [
						{
							type: 'GUIDED_TOUR_UPDATE',
							shouldShow: false,
							timestamp: now - 10000, // 10 seconds earlier
						},
					],
				},
			};
			expect( hasTourJustBeenVisible( state, now ) ).toBe( true );
		} );

		test( 'should return false when a tour has been seen longer ago', () => {
			const now = 1478623930204;
			const state = {
				ui: {
					actionLog: [
						{
							type: 'GUIDED_TOUR_UPDATE',
							shouldShow: false,
							timestamp: now - 120000, // 2 minutes earlier
						},
					],
				},
			};
			expect( hasTourJustBeenVisible( state, now ) ).toBe( false );
		} );
	} );

	describe( '#getGuidedTourState()', () => {
		test( 'should return an empty object if no state is present', () => {
			const tourState = getGuidedTourState( {
				route: {
					query: {
						current: {},
						initial: {},
					},
				},
				guidedTours: false,
				ui: {
					shouldShow: false,
					actionLog: [],
				},
				preferences: {
					lastFetchedTimestamp: 1,
					remoteValues: {
						'guided-tours-history': [],
					},
				},
			} );

			expect( tourState ).toEqual( { shouldShow: false } );
		} );
	} );
	describe( '#findEligibleTour()', () => {
		const makeState = ( {
			actionLog = [],
			toursHistory = [],
			queryArguments = {},
			userData = {},
			timestamp = null,
		} ) => ( {
			route: {
				query: {
					current: queryArguments,
					initial: queryArguments,
				},
				timestamp,
			},
			ui: {
				actionLog,
			},
			preferences: {
				lastFetchedTimestamp: 1,
				remoteValues: {
					'guided-tours-history': toursHistory,
				},
			},
			currentUser: {
				id: 1337,
				user: {
					ID: 1337,
					date: '2015-11-20T00:00:00+00:00',
					...userData,
				},
			},
		} );

		const navigateToThemes = {
			type: 'ROUTE_SET',
			path: '/themes/77203074',
		};

		const navigateToTest = {
			type: 'ROUTE_SET',
			path: '/test',
		};

		const mainTourStarted = {
			type: 'GUIDED_TOUR_UPDATE',
			tour: 'main',
		};

		const mainTourAborted = {
			type: 'GUIDED_TOUR_UPDATE',
			shouldShow: false,
			shouldReallyShow: false,
			tour: 'main',
			stepName: 'init',
			finished: false,
		};

		const mainTourSeen = {
			tourName: 'main',
			timestamp: 1337,
			finished: true,
		};

		const themesTourSeen = {
			tourName: 'themes',
			timestamp: 1337,
			finished: true,
		};

		const testTourSeen = {
			tourName: 'test',
			timestamp: 1337,
			finished: true,
		};

		test( 'should return undefined if nothing relevant in log', () => {
			const state = makeState( { actionLog: [ { type: 'IRRELEVANT' } ] } );
			const tour = findEligibleTour( state );

			expect( tour ).toBeUndefined();
		} );
		test( 'should find `themes` when applicable', () => {
			const state = makeState( { actionLog: [ navigateToThemes ] } );
			const tour = findEligibleTour( state );

			expect( tour ).toBe( 'themes' );
		} );
		test( 'should not find `themes` if previously seen', () => {
			const state = makeState( {
				actionLog: [ navigateToThemes ],
				toursHistory: [ themesTourSeen ],
			} );

			const tour = findEligibleTour( state );

			expect( tour ).toBeUndefined();
		} );
		test( 'should see to it that an ongoing tour is selected', () => {
			const havingStartedTour = makeState( {
				actionLog: [ mainTourStarted, navigateToThemes ],
			} );
			const havingQuitTour = makeState( {
				actionLog: [ mainTourStarted, mainTourAborted, navigateToThemes ],
			} );

			expect( findEligibleTour( havingStartedTour ) ).toBe( 'main' );
			expect( findEligibleTour( havingQuitTour ) ).toBe( 'themes' );
		} );
		test( 'should favor a tour launched via query arguments', () => {
			const state = makeState( {
				actionLog: [ navigateToThemes ],
				toursHistory: [ mainTourSeen, themesTourSeen ],
				queryArguments: { tour: 'main' },
			} );
			const tour = findEligibleTour( state );

			expect( tour ).toBe( 'main' );
		} );
		test( 'should dismiss a requested tour at the end', () => {
			const mainTourJustSeen = {
				tourName: 'main',
				timestamp: 1338,
				finished: true,
			};
			const state = makeState( {
				actionLog: [ navigateToThemes ],
				toursHistory: [ themesTourSeen, mainTourJustSeen ],
				queryArguments: { tour: 'main' },
				timestamp: 0,
			} );
			const tour = findEligibleTour( state );

			expect( tour ).toBeUndefined();
		} );
		test( 'should respect tour "when" conditions', () => {
			const state = makeState( {
				actionLog: [ navigateToThemes ],
				userData: { date: new Date().toJSON() }, // user was created just now
			} );
			const tour = findEligibleTour( state );

			// Even though we navigated to `/themes`, this counts as navigating
			// to `/`, and `state` satisfies `main`'s "when" condition that the user
			// should be a new user. In our config, `main` is declared before
			// `themes`, so the selector should prefer the former.
			expect( tour ).toBe( 'main' );
		} );
		test( "shouldn't show a requested tour twice", () => {
			/*
			 * Assume that a lot has happened during a Calypso session, so the
			 * action log doesn't contain actions specific to Guided Tours
			 * anymore.
			 */
			const state = makeState( {
				actionLog: Array.from( { length: 50 }, () => navigateToTest ),
				toursHistory: [ testTourSeen, themesTourSeen ],
				queryArguments: { tour: 'themes' },
				timestamp: 0,
			} );
			const tour = findEligibleTour( state );

			expect( tour ).toBeUndefined();
		} );
		test( 'should bail if user preferences are stale', () => {
			const state = makeState( {
				actionLog: [ navigateToThemes ],
				userData: { date: new Date().toJSON() }, // user was created just now
			} );
			delete state.preferences.lastFetchedTimestamp;
			const tour = findEligibleTour( state );

			expect( tour ).toBeUndefined();
		} );
		describe( 'picking a tour based on the most recent actions', () => {
			test( 'should pick `themes`', () => {
				const state = makeState( {
					actionLog: [ navigateToThemes, navigateToTest ],
				} );
				const tour = findEligibleTour( state );

				expect( tour ).toBe( 'test' );
			} );
			test( 'should pick `test`', () => {
				const state = makeState( {
					actionLog: [ navigateToTest, navigateToThemes ],
				} );
				const tour = findEligibleTour( state );

				expect( tour ).toBe( 'themes' );
			} );
		} );
	} );
} );