File size: 8,433 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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
/**
* @jest-environment jsdom
*/
// @ts-nocheck - TODO: Fix TypeScript issues
import { screen } from '@testing-library/react';
import React, { useEffect } from 'react';
import { MemoryRouter, useNavigate, useLocation } from 'react-router';
import { Primitive } from 'utility-types';
import themeReducer from 'calypso/state/themes/reducer';
import { addQueryArgs } from '../../../../../lib/url';
import { renderWithProvider } from '../../../../../test-helpers/testing-library';
import type { Flow, FlowV2, ProvidedDependencies, StepperStep } from '../../internals/types';
export const getFlowLocation = () => {
return {
path: screen.getByTestId( 'pathname' ).textContent,
state: JSON.parse( screen.getByTestId( 'state' ).textContent || '{}' ),
};
};
export const getAssertionConditionResult = () => {
return JSON.parse( screen.getByTestId( 'assertionConditionResult' ).textContent || '{}' );
};
interface RenderFlowParams {
currentStep: string;
dependencies?: ProvidedDependencies;
currentURL?: string;
method: 'submit' | 'goBack' | null;
cancelDestination?: string;
}
/** Utility to render a flow for testing purposes */
export const renderFlow = ( flow: Flow | FlowV2< any > ) => {
const FakeStepRender = ( { currentStep, dependencies, method } ) => {
const navigate = useNavigate();
const location = useLocation();
const fakeNavigate = ( pathname, state ) => navigate( pathname, { state } );
const nav = flow.useStepNavigation( currentStep, fakeNavigate );
const { submit } = nav as any;
// FlowV2 doesn't have a goBack method.
const goBack = 'goBack' in nav ? nav.goBack : null;
const assertionConditionResult = flow.useAssertConditions?.() || {};
useEffect( () => {
switch ( method ) {
case 'submit':
// FlowV2 has a different signature for the submit handler.
if ( 'initialize' in flow ) {
submit?.( { slug: currentStep, providedDependencies: dependencies } );
} else {
submit?.( dependencies );
}
break;
case 'goBack':
goBack?.();
break;
}
}, [] );
const pathname = location.pathname.replace( `${ flow.name }/`, '' );
return (
<>
<p data-testid="pathname">{ `${ pathname }${ location.search }` }</p>
<p data-testid="search">{ location.search }</p>
<p data-testid="state">{ JSON.stringify( location.state ) }</p>
{ assertionConditionResult && (
<p data-testid="assertionConditionResult">
{ JSON.stringify( assertionConditionResult ) }
</p>
) }
</>
);
};
// The Flow>useStepNavigation>submit function needs to be called from inside a component
const runUseStepNavigation = ( {
currentURL = '/some-path?siteSlug=example.wordpress.com',
currentStep,
dependencies,
method,
}: RenderFlowParams ) => {
renderWithProvider(
<MemoryRouter initialEntries={ [ currentURL ] }>
<FakeStepRender
currentStep={ currentStep }
dependencies={ dependencies }
method={ method }
/>
</MemoryRouter>,
{
initialState: { themes: { queries: [] }, currentUser: { id: 'some-id' } },
reducers: {
themes: themeReducer,
},
}
);
};
const runUseStepNavigationSubmit = ( params: Omit< RenderFlowParams, 'method' > ) =>
runUseStepNavigation( { ...params, method: 'submit' } );
const runUseStepNavigationGoBack = ( params: Omit< RenderFlowParams, 'method' > ) =>
runUseStepNavigation( { ...params, method: 'goBack' } );
const runUseAssertionCondition = ( params: Omit< RenderFlowParams, 'method' > ) => {
runUseStepNavigation( { ...params, method: null } );
return getAssertionConditionResult();
};
return {
runUseStepNavigationSubmit,
runUseStepNavigationGoBack,
runUseAssertionCondition,
};
};
export const runFlowNavigation = (
flow: FlowV1,
{ from, dependencies = {}, query = {} },
direction = 'forward'
) => {
const { runUseStepNavigationSubmit, runUseStepNavigationGoBack } = renderFlow( flow );
if ( direction === 'forward' ) {
runUseStepNavigationSubmit( {
currentStep: from.slug,
dependencies: dependencies,
currentURL: addQueryArgs( query, `/${ flow.name }/${ from.slug }` ),
} );
}
if ( direction === 'back' ) {
runUseStepNavigationGoBack( {
currentStep: from.slug,
dependencies: dependencies,
currentURL: addQueryArgs( query, `/${ flow.name }/${ from.slug }` ),
} );
}
const destination = getFlowLocation();
const [ pathname, searchParams ] = destination?.path?.split( '?' ) ?? [ '', '' ];
return {
step: pathname.replace( /^\/+/, '' ),
query: new URLSearchParams( searchParams ),
};
};
interface MatchDestinationParams {
step: StepperStep;
query: URLSearchParams | Record< string, Primitive > | null;
}
interface MatchExternalFlowParams {
path: string;
query: URLSearchParams | Record< string, Primitive > | null;
}
declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
interface Matchers {
toMatchDestination( expected: MatchDestinationParams ): CustomMatcherResult;
}
}
}
const differenceWith = ( a: URLSearchParams, b: URLSearchParams ) => {
const aEntries = Array.from( a.entries() );
return aEntries.filter( ( [ key ] ) => ! b.has( key ) || b.get( key ) !== a.get( key ) );
};
const toObject = ( a: URLSearchParams | Record< string, Primitive > ) => {
if ( a instanceof URLSearchParams ) {
const obj = Object.fromEntries( a.entries() );
if ( Object.keys( obj ).length === 0 ) {
return null;
}
return obj;
}
return a;
};
expect.extend( {
toMatchURL(
destination: jest.Spied< typeof window.location.assign >,
expected: MatchExternalFlowParams
) {
const results = destination.mock.calls.map( ( [ destination ] ) => {
const [ flow, queryString ] = destination.toString().split( '?' );
const expectedFlow = expected.path;
if ( expected.query instanceof URLSearchParams === false ) {
expected.query = new URLSearchParams( expected.query as Record< string, string > );
}
const receivedQuery = new URLSearchParams( queryString );
const diff = differenceWith( expected.query, receivedQuery );
const isSameFlow = flow === expectedFlow;
const isSameQuery = diff.length === 0;
if ( ! isSameQuery || ! isSameFlow ) {
return {
received: {
path: flow,
query: toObject( receivedQuery ),
},
pass: false,
};
}
return {
message: () => `expected ${ destination } to match ${ expected }`,
pass: true,
};
} );
const okResult = results.find( ( result ) => result.pass === true );
if ( results.length === 0 ) {
return {
message: () => 'number of results of is 0',
pass: false,
};
}
if ( ! okResult ) {
return {
message: () =>
`Expected: \n\tpath: ${ this.utils.printExpected(
expected.path
) }\n\tquery: ${ this.utils.printExpected( toObject( expected.query ) ) }
\nReceived: ${ results.map(
( result, index ) =>
`\n[${ index }]\tpath: ${ this.utils.printReceived(
result.received?.path
) } \n\tquery: ${ this.utils.printReceived( result.received?.query ) }`
) }`,
pass: false,
};
}
return okResult;
},
toMatchDestination( destination, expected: MatchDestinationParams ) {
const isSameStep = destination.step === expected.step.slug;
if ( ! isSameStep ) {
return {
message: () =>
`Expected step: ${ this.utils.printExpected(
decodeURIComponent( expected.step.slug )
) } \nReceived step: ${ this.utils.printReceived(
decodeURIComponent( destination.step )
) }`,
pass: false,
};
}
if ( expected.query instanceof URLSearchParams === false && expected.query !== null ) {
expected.query = new URLSearchParams( expected.query as Record< string, string > );
}
const diff = expected.query ? differenceWith( expected.query, destination.query ) : [];
const isSameQuery = expected.query ? diff.length === 0 : true;
const pass = isSameStep && isSameQuery;
if ( pass ) {
return {
message: () => `expected ${ destination } not to match ${ expected }`,
pass: true,
};
}
if ( ! isSameQuery ) {
return {
message: () =>
`Expected query: ${ this.utils.printExpected(
toObject( expected.query )
) } \nReceived query: ${ this.utils.printReceived( toObject( destination.query ) ) }`,
pass: false,
};
}
return {
message: () => `expected ${ destination } to match ${ expected }`,
pass: false,
};
},
} );
|