Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka committed Nov 10, 2021
1 parent d3c748d commit e572aa0
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/components/collaborative-editing/use-yjs/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,62 @@ describe( 'CollaborativeEditing', () => {
expect( bobScreen.getByRole( 'document', { name: 'Paragraph block' } ) ).toHaveTextContent( 'initialtyped' );
} );
} );

describe( 'CollaborativeEditing: Undo/Redo', () => {
beforeEach( () => {
// Real timers are used so Yjs can merge undo stack items
jest.useRealTimers();
} );

afterEach( () => {
jest.useFakeTimers();
} );

it( 'should undo/redo single user edits', async () => {
const [ transport ] = getTransports( 1 );
const onSave = jest.fn();

render(
<IsolatedBlockEditor settings={ {} } onSaveContent={ onSave }>
<CollaborativeEditing settings={ { ...collabSettings, transport } } />
</IsolatedBlockEditor>
);

expect( screen.getByRole( 'button', { name: 'Undo' } ) ).toHaveAttribute( 'aria-disabled', 'true' );
expect( screen.getByRole( 'button', { name: 'Redo' } ) ).toHaveAttribute( 'aria-disabled', 'true' );

userEvent.click( screen.getByText( /^Start writing.+/ ) );

userEvent.keyboard( 'hello' );
expect( screen.getByRole( 'document', { name: 'Paragraph block' } ) ).toHaveTextContent( 'hello' );

// Emulate pause for Yjs to make a new undo stack item
screen.getByRole( 'document', { name: 'Paragraph block' } ).blur();
await waitFor( () => new Promise( ( resolve ) => setTimeout( resolve, 500 ) ) );
userEvent.click( screen.getByRole( 'document', { name: 'Paragraph block' } ) );

userEvent.keyboard( 'world' );
expect( screen.getByRole( 'document', { name: 'Paragraph block' } ) ).toHaveTextContent( 'helloworld' );

expect( screen.getByRole( 'button', { name: 'Undo' } ) ).toHaveAttribute( 'aria-disabled', 'false' );
userEvent.click( screen.getByRole( 'button', { name: 'Undo' } ) );

expect( screen.getByRole( 'document', { name: 'Paragraph block' } ) ).toHaveTextContent( 'hello' );

expect( screen.getByRole( 'button', { name: 'Undo' } ) ).toHaveAttribute( 'aria-disabled', 'false' );
expect( screen.getByRole( 'button', { name: 'Redo' } ) ).toHaveAttribute( 'aria-disabled', 'false' );
userEvent.click( screen.getByRole( 'button', { name: 'Undo' } ) );

expect( screen.getByText( /^Start writing.+/ ) ).toBeInTheDocument();
expect( onSave ).toHaveBeenLastCalledWith( '' );

expect( screen.getByRole( 'button', { name: 'Undo' } ) ).toHaveAttribute( 'aria-disabled', 'true' );
expect( screen.getByRole( 'button', { name: 'Redo' } ) ).toHaveAttribute( 'aria-disabled', 'false' );

userEvent.click( screen.getByRole( 'button', { name: 'Redo' } ) );
expect( screen.getByRole( 'document', { name: 'Paragraph block' } ) ).toHaveTextContent( 'hello' );

userEvent.click( screen.getByRole( 'button', { name: 'Redo' } ) );
expect( screen.getByRole( 'document', { name: 'Paragraph block' } ) ).toHaveTextContent( 'helloworld' );
} );
} );

0 comments on commit e572aa0

Please sign in to comment.