File size: 4,786 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 |
/**
* @jest-environment jsdom
*/
// @ts-nocheck - TODO: Fix TypeScript issues
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { MemoryRouter } from 'react-router';
import CaptureInput from '../capture-input';
const mockedRecordTracksEvent = jest.fn();
jest.mock( '@automattic/calypso-analytics', () => ( {
recordTracksEvent: ( ...args ) => mockedRecordTracksEvent( ...args ),
} ) );
describe( 'CaptureInput', () => {
beforeEach( () => jest.resetAllMocks() );
it( 'captures the site url', async () => {
const onInputEnter = jest.fn();
render(
<MemoryRouter>
<CaptureInput onInputEnter={ onInputEnter } />
</MemoryRouter>
);
await userEvent.type(
screen.getByLabelText( /Enter the URL of the site/ ),
'https://example.wordpress.com'
);
await userEvent.click( screen.getByRole( 'button', { name: /Continue/ } ) );
expect( onInputEnter ).toHaveBeenCalledWith( 'https://example.wordpress.com' );
} );
it( 'shows an custom input label', async () => {
render(
<MemoryRouter>
<CaptureInput onInputEnter={ jest.fn() } label="A custom label text" />
</MemoryRouter>
);
await userEvent.click( screen.getByRole( 'button', { name: /Continue/ } ) );
expect( screen.getByLabelText( /A custom label text/ ) ).toBeInTheDocument();
} );
it( 'only records invalid urls once', async () => {
const onInputEnter = jest.fn();
render(
<MemoryRouter>
<CaptureInput onInputEnter={ onInputEnter } />
</MemoryRouter>
);
// enter an invalid URL
await userEvent.type( screen.getByLabelText( /Enter the URL of the site/ ), 'not a url' );
// try to submit the same value twice
await userEvent.click( screen.getByRole( 'button', { name: /Continue/ } ) );
await userEvent.click( screen.getByRole( 'button', { name: /Continue/ } ) );
// Now, enter a valid URL and confirm we can submit ok
await userEvent.clear( screen.getByLabelText( /Enter the URL of the site/ ) );
await userEvent.type(
screen.getByLabelText( /Enter the URL of the site/ ),
'https://example.wordpress.com'
);
await userEvent.click( screen.getByRole( 'button', { name: /Continue/ } ) );
expect( onInputEnter ).toHaveBeenCalledWith( 'https://example.wordpress.com' );
expect( mockedRecordTracksEvent.mock.calls.length ).toBe( 1 );
} );
it( 'shows a custom CTA label', async () => {
render(
<MemoryRouter>
<CaptureInput onInputEnter={ jest.fn() } label="A custom label text" nextLabelText="XYZ" />
</MemoryRouter>
);
expect( screen.getByRole( 'button', { name: /XYZ/ } ) ).toBeInTheDocument();
} );
} );
describe( 'URL Validation', () => {
it.each( [
{
input: 'myblog',
error:
"Looks like your site address is missing its domain extension. Please try again with something like 'example.com' or 'example.net'.",
},
{
input: 'https://myblog',
error:
"Looks like your site address is missing its domain extension. Please try again with something like 'example.com' or 'example.net'.",
},
{
input: 'user@example.com',
error:
"Looks like you might have added an email address. Please use a URL instead, like 'example.com'.",
},
{
input: 'http://my^blog.com',
error:
'Looks like your URL has some invalid characters (like ~ or ^). Please delete them and try again.',
},
{
input: 'ftp://example.com',
error:
"URLs usually start with http:// or https:// (rather than file:/, ftp://, or similar). Please try again with a URL like 'https://example.com'.",
},
{
input: 'file:///C:/DEVELOPER/index.html',
error:
"URLs usually start with http:// or https:// (rather than file:/, ftp://, or similar). Please try again with a URL like 'https://example.com'.",
},
{
input: 'https://xn--example.com',
error:
"Looks like you’ve added an internationalized domain name. Please try a standard URL instead (like 'example.com').",
},
{
input: 'www.例子.测试',
error:
"Looks like you’ve added an internationalized domain name. Please try a standard URL instead (like 'example.com').",
},
{
input: 'example.com-',
error:
"Please add a valid website address (like 'example.com'). Feel free to copy and paste it in if that helps.",
},
] )( 'shows error message "$error" when input URL is "$input"', async ( { input, error } ) => {
const onInputEnter = jest.fn();
render(
<MemoryRouter>
<CaptureInput onInputEnter={ onInputEnter } />
</MemoryRouter>
);
await userEvent.type( screen.getByLabelText( /Enter the URL of the site/ ), input );
await userEvent.click( screen.getByRole( 'button', { name: /Continue/ } ) );
expect( screen.getByText( error ) ).toBeInTheDocument();
} );
} );
|