File size: 4,235 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 |
/**
* @jest-environment jsdom
*/
import { GravatarQuickEditorCore } from '@gravatar-com/quick-editor';
import { screen, fireEvent } from '@testing-library/react';
import { EditGravatar } from 'calypso/blocks/edit-gravatar';
import { renderWithProvider } from 'calypso/test-helpers/testing-library';
jest.mock( '@gravatar-com/quick-editor', () => ( {
GravatarQuickEditorCore: jest.fn().mockImplementation( ( options ) => {
let _isOpen = false;
return {
// Fire the callback to simulate the profile update.
open: jest.fn( () => {
_isOpen = true;
options.onProfileUpdated?.();
} ),
close: jest.fn( () => {
_isOpen = false;
} ),
isOpen: jest.fn( () => _isOpen ),
};
} ),
} ) );
jest.mock( 'calypso/state/selectors/get-user-settings', () => jest.fn( () => {} ) );
const FIXED_NOW = 1_706_790_000_000; // 2025-03-05 00:00 UTC
jest.spyOn( Date, 'now' ).mockReturnValue( FIXED_NOW );
const user = {
email_verified: true,
display_name: 'arbitrary-user-display-name',
};
const baseProps = {
user,
translate: ( i ) => i,
recordClickButtonEvent: jest.fn(),
recordAvatarUpdatedEvent: jest.fn(),
setCurrentUser: jest.fn(),
receiveGravatarDetails: jest.fn(),
};
const setup = ( overrides = {} ) =>
renderWithProvider( <EditGravatar { ...baseProps } { ...overrides } /> );
const clickEditAvatarLink = () =>
fireEvent.click( screen.getByRole( 'button', { name: /Edit your public avatar/i } ) );
describe( 'EditGravatar', () => {
afterEach( jest.clearAllMocks );
describe( 'renders', () => {
test( 'editable Gravatar', () => {
setup();
expect( screen.getByAltText( user.display_name ) ).toBeVisible();
expect( screen.getByRole( 'button', { name: /Edit your public avatar/i } ) ).toBeVisible();
} );
test( 'unverified email', () => {
setup( { user: { ...user, email_verified: false } } );
expect( screen.getByTestId( 'caution-icon' ) ).toBeVisible();
expect(
screen.getByRole( 'button', { name: /Verify your email to edit your avatar/i } )
).toBeVisible();
} );
test( 'Gravatar disabled', () => {
setup( { isGravatarProfileHidden: true } );
expect( screen.getByTestId( 'hidden-avatar' ) ).toBeVisible();
expect( screen.getByText( /Your avatar is hidden\./ ) ).toBeVisible();
} );
} );
describe( 'actions', () => {
test( 'opens quick editor and updates avatar URL and Gravatar details', () => {
setup();
clickEditAvatarLink();
const quickEditor = GravatarQuickEditorCore.mock.results.at( -1 ).value;
expect( quickEditor.open ).toHaveBeenCalledTimes( 1 );
expect( baseProps.setCurrentUser ).toHaveBeenCalledWith(
expect.objectContaining( { avatar_URL: expect.stringContaining( `ver=${ FIXED_NOW }` ) } )
);
expect( baseProps.receiveGravatarDetails ).toHaveBeenCalledWith( { has_gravatar: true } );
} );
test( 'shows email‑verification dialog', () => {
setup( { user: { ...user, email_verified: false } } );
fireEvent.click(
screen.getByRole( 'button', { name: /Verify your email to edit your avatar/i } )
);
// Check for dialog modal copy to ensure it appeared.
expect( screen.getByText( /Secure your account and access more features\./ ) ).toBeVisible();
} );
test( 'closes the previous quick editor before opening a new one', () => {
setup();
clickEditAvatarLink();
const quickEditor = GravatarQuickEditorCore.mock.results.at( -1 ).value;
expect( quickEditor.isOpen ).toBeTruthy();
clickEditAvatarLink();
expect( quickEditor.close ).toHaveBeenCalledTimes( 1 );
} );
test( 'closes quick editor on page refresh', () => {
setup();
clickEditAvatarLink();
const quickEditor = GravatarQuickEditorCore.mock.results.at( -1 ).value;
expect( quickEditor.isOpen ).toBeTruthy();
window.dispatchEvent( new Event( 'pagehide' ) );
expect( quickEditor.close ).toHaveBeenCalledTimes( 1 );
} );
test( 'closes quick editor when navigating away from the page', () => {
const { unmount } = setup();
clickEditAvatarLink();
const quickEditor = GravatarQuickEditorCore.mock.results.at( -1 ).value;
expect( quickEditor.isOpen ).toBeTruthy();
unmount();
expect( quickEditor.close ).toHaveBeenCalledTimes( 1 );
} );
} );
} );
|