|
| 1 | +/** |
| 2 | + * External dependencies |
| 3 | + */ |
| 4 | +import { render, screen } from '@testing-library/react'; |
| 5 | + |
| 6 | +/** |
| 7 | + * WordPress dependencies |
| 8 | + */ |
| 9 | +import { useSelect, useDispatch } from '@wordpress/data'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Internal dependencies |
| 13 | + */ |
| 14 | +import PostRevisionsTimeline from '../'; |
| 15 | + |
| 16 | +jest.mock( '@wordpress/data/src/components/use-select', () => jest.fn() ); |
| 17 | +jest.mock( '@wordpress/data/src/components/use-dispatch', () => ( { |
| 18 | + useDispatch: jest.fn(), |
| 19 | +} ) ); |
| 20 | + |
| 21 | +jest.mock( '@wordpress/fields', () => ( { |
| 22 | + authorField: { |
| 23 | + id: 'author', |
| 24 | + label: 'Author', |
| 25 | + getValue: ( { item } ) => item?._embedded?.author?.[ 0 ]?.name, |
| 26 | + render: ( { item } ) => { |
| 27 | + const { createElement } = require( '@wordpress/element' ); |
| 28 | + const authorName = item?._embedded?.author?.[ 0 ]?.name; |
| 29 | + |
| 30 | + return createElement( |
| 31 | + 'span', |
| 32 | + null, |
| 33 | + createElement( |
| 34 | + 'span', |
| 35 | + { 'aria-hidden': 'true' }, |
| 36 | + 'Author icon' |
| 37 | + ), |
| 38 | + createElement( 'span', null, authorName ) |
| 39 | + ); |
| 40 | + }, |
| 41 | + }, |
| 42 | +} ) ); |
| 43 | + |
| 44 | +jest.mock( '@wordpress/dataviews', () => { |
| 45 | + const DataViewsPicker = ( { data, fields, getItemId, selection } ) => { |
| 46 | + const titleField = fields.find( ( field ) => field.id === 'date' ); |
| 47 | + |
| 48 | + return ( |
| 49 | + <div> |
| 50 | + { data.map( ( item ) => ( |
| 51 | + <div |
| 52 | + key={ getItemId( item ) } |
| 53 | + role="option" |
| 54 | + aria-label={ titleField.getValue( { item } ) } |
| 55 | + aria-selected={ selection.includes( |
| 56 | + getItemId( item ) |
| 57 | + ) } |
| 58 | + > |
| 59 | + { fields.map( ( field ) => { |
| 60 | + const normalizedField = { |
| 61 | + id: field.id, |
| 62 | + label: field.label, |
| 63 | + render: field.render, |
| 64 | + }; |
| 65 | + |
| 66 | + return normalizedField.render ? ( |
| 67 | + <div key={ normalizedField.id }> |
| 68 | + <normalizedField.render |
| 69 | + item={ item } |
| 70 | + field={ normalizedField } |
| 71 | + /> |
| 72 | + </div> |
| 73 | + ) : null; |
| 74 | + } ) } |
| 75 | + </div> |
| 76 | + ) ) } |
| 77 | + </div> |
| 78 | + ); |
| 79 | + }; |
| 80 | + DataViewsPicker.Layout = () => null; |
| 81 | + DataViewsPicker.Footer = () => null; |
| 82 | + |
| 83 | + return { |
| 84 | + DataViewsPicker, |
| 85 | + filterSortAndPaginate: ( data ) => ( { |
| 86 | + data, |
| 87 | + paginationInfo: { totalItems: data.length }, |
| 88 | + } ), |
| 89 | + }; |
| 90 | +} ); |
| 91 | + |
| 92 | +jest.mock( '../../../lock-unlock', () => ( { |
| 93 | + unlock: ( object ) => { |
| 94 | + return { |
| 95 | + ...object, |
| 96 | + registerPrivateActions: jest.fn(), |
| 97 | + registerPrivateSelectors: jest.fn(), |
| 98 | + }; |
| 99 | + }, |
| 100 | +} ) ); |
| 101 | + |
| 102 | +jest.mock( '../../post-content-information', () => ( { |
| 103 | + PostContentInformationUI: () => null, |
| 104 | +} ) ); |
| 105 | + |
| 106 | +describe( 'PostRevisionsTimeline', () => { |
| 107 | + let getCurrentRevision; |
| 108 | + |
| 109 | + beforeEach( () => { |
| 110 | + const revisions = [ |
| 111 | + { |
| 112 | + id: 3, |
| 113 | + date: '2026-07-07T12:00:00', |
| 114 | + author: 1, |
| 115 | + slug: '10-autosave-v1', |
| 116 | + content: { raw: 'Autosaved content' }, |
| 117 | + _embedded: { author: [ { id: 1, name: 'Alice' } ] }, |
| 118 | + }, |
| 119 | + { |
| 120 | + id: 2, |
| 121 | + date: '2026-07-07T11:00:00', |
| 122 | + author: 2, |
| 123 | + slug: '10-revision-v1', |
| 124 | + content: { raw: 'Current revision content' }, |
| 125 | + _embedded: { author: [ { id: 2, name: 'Bob' } ] }, |
| 126 | + }, |
| 127 | + { |
| 128 | + id: 1, |
| 129 | + date: '2026-07-07T10:00:00', |
| 130 | + author: 1, |
| 131 | + slug: '10-revision-v1', |
| 132 | + content: { raw: 'Older revision content' }, |
| 133 | + _embedded: { author: [ { id: 1, name: 'Alice' } ] }, |
| 134 | + }, |
| 135 | + ]; |
| 136 | + const users = { |
| 137 | + 1: { name: 'Alice' }, |
| 138 | + 2: { name: 'Bob' }, |
| 139 | + }; |
| 140 | + |
| 141 | + getCurrentRevision = jest.fn( () => revisions[ 0 ] ); |
| 142 | + |
| 143 | + useSelect.mockImplementation( ( mapSelect ) => |
| 144 | + mapSelect( () => ( { |
| 145 | + getCurrentPostType: () => 'post', |
| 146 | + getCurrentRevisionId: () => 3, |
| 147 | + getCurrentRevision, |
| 148 | + getRevisionPage: () => 1, |
| 149 | + getPageRevisions: () => revisions, |
| 150 | + getCurrentPostLastRevisionId: () => 2, |
| 151 | + getEntityConfig: () => ( { revisionKey: 'id' } ), |
| 152 | + getEntityRecord: ( _kind, _name, id ) => users[ id ], |
| 153 | + } ) ) |
| 154 | + ); |
| 155 | + useDispatch.mockReturnValue( { setCurrentRevisionId: jest.fn() } ); |
| 156 | + } ); |
| 157 | + |
| 158 | + it( 'keeps the author field intact and labels autosaves', () => { |
| 159 | + render( <PostRevisionsTimeline /> ); |
| 160 | + |
| 161 | + expect( screen.getAllByText( 'Author icon' ) ).toHaveLength( 3 ); |
| 162 | + expect( screen.getAllByText( 'Alice' ) ).toHaveLength( 2 ); |
| 163 | + expect( screen.getByText( 'Bob' ) ).toBeVisible(); |
| 164 | + expect( screen.getByText( 'Autosave' ) ).toBeVisible(); |
| 165 | + expect( screen.getAllByRole( 'option' )[ 0 ] ).toHaveAccessibleName( |
| 166 | + /Autosave/ |
| 167 | + ); |
| 168 | + expect( screen.getAllByRole( 'option' )[ 1 ] ).not.toHaveAccessibleName( |
| 169 | + /Autosave/ |
| 170 | + ); |
| 171 | + expect( screen.queryByText( 'Current' ) ).not.toBeInTheDocument(); |
| 172 | + expect( |
| 173 | + screen.queryByText( 'Revision by Alice' ) |
| 174 | + ).not.toBeInTheDocument(); |
| 175 | + expect( |
| 176 | + screen.queryByText( 'Current Revision by Bob' ) |
| 177 | + ).not.toBeInTheDocument(); |
| 178 | + expect( getCurrentRevision ).not.toHaveBeenCalled(); |
| 179 | + } ); |
| 180 | +} ); |
0 commit comments