File size: 10,052 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 | /** @jest-environment jsdom */
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { NavigationLink } from '../';
const signupUtils = require( 'calypso/signup/utils' );
jest.mock( 'calypso/signup/utils', () => ( {
getStepUrl: jest.fn(),
getFilteredSteps: jest.fn(),
getPreviousStepName: jest.fn(),
isFirstStepInFlow: jest.fn(),
} ) );
jest.mock( '@automattic/components/src/gridicon', () => {
return ( { icon } ) => <div data-testid={ `gridicon-${ icon }` } />;
} );
const noop = () => {};
const { getStepUrl, getFilteredSteps, getPreviousStepName, isFirstStepInFlow } = signupUtils;
describe( 'NavigationLink', () => {
const props = {
flowName: 'test:flow',
stepName: 'test:step2',
positionInFlow: 1,
stepSectionName: 'test:section',
signupProgress: {
'test:step1': { stepName: 'test:step1', stepSectionName: 'test:section1', wasSkipped: false },
'test:step2': { stepName: 'test:step2', stepSectionName: 'test:section2', wasSkipped: false },
'test:step3': { stepName: 'test:step3', stepSectionName: 'test:section3', wasSkipped: false },
},
recordTracksEvent: noop,
submitSignupStep: noop,
goToPreviousStep: jest.fn(),
goToNextStep: jest.fn(),
translate: ( str ) => `translated:${ str }`,
userLoggedIn: true,
};
beforeEach( () => {
getPreviousStepName.mockReturnValue( 'test:step1' );
isFirstStepInFlow.mockReturnValue( false );
getFilteredSteps.mockReturnValue( Object.values( props.signupProgress ) );
} );
afterEach( () => {
getStepUrl.mockReset();
props.goToPreviousStep.mockReset();
props.goToNextStep.mockReset();
} );
test( 'should render Button element', () => {
render( <NavigationLink { ...props } /> );
expect( screen.queryByRole( 'button' ) ).toHaveClass( 'navigation-link' );
} );
test( 'should render no icons when the direction prop is undefined', () => {
render( <NavigationLink { ...props } /> );
expect( screen.queryByTestId( /gridicon-/ ) ).not.toBeInTheDocument();
} );
test( 'should render right-arrow icon when the direction prop is "forward".', () => {
render( <NavigationLink { ...props } direction="forward" /> );
expect( screen.queryByText( 'translated:Skip for now' ) ).toBeVisible();
expect( screen.queryAllByTestId( /gridicon-/ ) ).toHaveLength( 1 );
expect( screen.queryByTestId( 'gridicon-arrow-right' ) ).toBeVisible();
} );
test( 'should render left-arrow icon when the direction prop is "back".', () => {
render( <NavigationLink { ...props } direction="back" /> );
expect( screen.queryByText( 'translated:Back' ) ).toBeVisible();
expect( screen.queryAllByTestId( /gridicon-/ ) ).toHaveLength( 1 );
expect( screen.queryByTestId( 'gridicon-arrow-left' ) ).toBeVisible();
} );
test( 'should set href prop to undefined when the direction is not "back".', () => {
render( <NavigationLink { ...props } direction="forward" /> );
expect( screen.getByRole( 'button' ) ).not.toHaveAttribute( 'href' );
} );
test( 'should set a proper url as href prop when the direction is "back".', () => {
expect( getStepUrl ).not.toHaveBeenCalled();
const { rerender } = render(
// We're mocking a logged-out user to ensure locale is being considered
<NavigationLink { ...props } userLoggedIn={ false } direction="back" />
);
// It should call getStepUrl()
expect( getStepUrl ).toHaveBeenCalled();
expect( getStepUrl ).toHaveBeenCalledWith(
'test:flow',
'test:step1',
'test:section1',
'en',
undefined
);
// when it is the first step
getStepUrl.mockReset();
getPreviousStepName.mockReturnValue( 'test:step1' );
isFirstStepInFlow.mockReturnValue( true );
getFilteredSteps.mockReturnValue( [
{ stepName: 'test:step1', stepSectionName: 'test:section1', wasSkipped: false },
] );
rerender(
<NavigationLink { ...props } userLoggedIn={ false } direction="back" stepName="test:step1" />
);
expect( getStepUrl ).toHaveBeenCalled();
expect( getStepUrl ).toHaveBeenCalledWith( 'test:flow', null, '', 'en', undefined );
// The href should be backUrl when exist.
rerender(
<NavigationLink
{ ...props }
userLoggedIn={ false }
direction="back"
backUrl="test:back-url"
/>
);
expect( screen.getByRole( 'link' ) ).toHaveAttribute( 'href', 'test:back-url' );
} );
test( 'should call goToNextStep() only when the direction is forward and clicked', async () => {
const user = userEvent.setup();
render( <NavigationLink { ...props } direction="forward" /> );
expect( props.goToNextStep ).not.toHaveBeenCalled();
await user.click( screen.getByRole( 'button' ) );
expect( props.goToNextStep ).toHaveBeenCalled();
} );
test( 'should not call goToNextStep() when the direction is back', async () => {
const user = userEvent.setup();
render( <NavigationLink { ...props } direction="back" /> );
expect( props.goToNextStep ).not.toHaveBeenCalled();
await user.click( screen.getByRole( 'button' ) );
expect( props.goToNextStep ).not.toHaveBeenCalled();
} );
test( 'should call goToPreviousStep() only when the direction is back and clicked', async () => {
const user = userEvent.setup();
render( <NavigationLink { ...props } direction="back" /> );
expect( props.goToPreviousStep ).not.toHaveBeenCalled();
await user.click( screen.getByRole( 'button' ) );
expect( props.goToPreviousStep ).toHaveBeenCalled();
} );
test( 'should not call goToPreviousStep() when the direction is forward', async () => {
const user = userEvent.setup();
render( <NavigationLink { ...props } direction="forward" /> );
expect( props.goToPreviousStep ).not.toHaveBeenCalled();
await user.click( screen.getByRole( 'button' ) );
expect( props.goToPreviousStep ).not.toHaveBeenCalled();
} );
test( 'getPreviousStep() When in 2nd step should return 1st step', () => {
const navigationLink = new NavigationLink( props );
const { flowName, signupProgress, stepName } = props;
getPreviousStepName.mockReturnValue( 'test:step1' );
const previousStep = navigationLink.getPreviousStep( flowName, signupProgress, stepName );
expect( previousStep.stepName ).toBe( 'test:step1' );
} );
test( 'getPreviousStep() When in 1st step should return nullish step', () => {
const navigationLink = new NavigationLink( props );
const { flowName, signupProgress } = props;
isFirstStepInFlow.mockReturnValue( true );
const previousStep = navigationLink.getPreviousStep( flowName, signupProgress, 'test:step1' );
expect( previousStep.stepName ).toBe( null );
} );
test( 'getPreviousStep() When in 3rd step should return 2nd step', () => {
const navigationLink = new NavigationLink( props );
const { flowName, signupProgress } = props;
getPreviousStepName.mockReturnValue( 'test:step2' );
isFirstStepInFlow.mockReturnValue( false );
const previousStep = navigationLink.getPreviousStep( flowName, signupProgress, 'test:step3' );
expect( previousStep.stepName ).toBe( 'test:step2' );
} );
test( 'getPreviousStep() When current steps is unknown, step should return last step in progress which belong to the current flow', () => {
const navigationLink = new NavigationLink( props );
const { flowName, signupProgress } = props;
const singupProgressWithSomeStepsThatDoNotBelongToThisFlow = {
...signupProgress,
'some-random-step': {
stepName: 'test:step4',
stepSectionName: 'test:section4',
wasSkipped: false,
},
};
isFirstStepInFlow.mockReturnValue( false );
//Mock getPreviousStepName() Will return undefined since 'some-other-random-step' does not belong to this flow
getPreviousStepName.mockReturnValue( undefined );
const currentStepName = 'some-other-random-step';
const previousStep = navigationLink.getPreviousStep(
flowName,
singupProgressWithSomeStepsThatDoNotBelongToThisFlow,
currentStepName
);
const progressStepNames = Object.keys( signupProgress );
expect( previousStep.stepName ).toBe( progressStepNames[ progressStepNames.length - 1 ] );
} );
test( 'getPreviousStep() When current progress does not contain any step of the current flow return nullish step', () => {
const navigationLink = new NavigationLink( props );
const { flowName } = props;
const singupProgressWithOnlyStepsThatDoNotBelongToThisFlow = {
'test:some-random-step': {
stepName: 'test:some-random-step',
stepSectionName: 'test:some-random-step:section',
wasSkipped: false,
},
'test:some-other-random-step': {
stepName: 'test:some-other-random-step',
stepSectionName: 'test:some-other-random-step',
wasSkipped: false,
},
};
//This simulates not having relevant steps in current flow
getFilteredSteps.mockReturnValue( [] );
isFirstStepInFlow.mockReturnValue( false );
const currentStepName = 'test:whatever-current-step';
const previousStep = navigationLink.getPreviousStep(
flowName,
singupProgressWithOnlyStepsThatDoNotBelongToThisFlow,
currentStepName
);
expect( previousStep.stepName ).toBe( null );
} );
test( 'getPreviousStep() When there are skipped steps they should be ignored', () => {
const { flowName, signupProgress } = props;
const navigationLink = new NavigationLink( props );
const stepsWithSkippedSteps = {
'test:step1': { stepName: 'test:step1', stepSectionName: 'test:section1', wasSkipped: false },
'test:step2': { stepName: 'test:step2', stepSectionName: 'test:section2', wasSkipped: true },
'test:step3': { stepName: 'test:step3', stepSectionName: 'test:section3', wasSkipped: true },
'test:step4': { stepName: 'test:step4', stepSectionName: 'test:section4', wasSkipped: false },
};
//According to step definitions the last step would return as mocked here
getPreviousStepName.mockReturnValue( 'test:step3' );
getFilteredSteps.mockReturnValue( Object.values( stepsWithSkippedSteps ) );
isFirstStepInFlow.mockReturnValue( false );
const previousStep = navigationLink.getPreviousStep( flowName, signupProgress, 'test:step4' );
expect( previousStep.stepName ).toBe( 'test:step1' );
} );
} );
|