Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
* External dependencies
*/
import { act } from 'react-dom/test-utils';
import { render, fireEvent } from '@testing-library/react';

/**
* WordPress dependencies
*/
import { render, unmountComponentAtNode } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';

/**
Expand All @@ -28,8 +27,6 @@ jest.mock('../../../hooks/use-errors-fetching-state-changes', () => ({
}));

describe('AMPDocumentStatusNotification', () => {
let container;

const openGeneralSidebar = jest.fn();
const closePublishSidebar = jest.fn();

Expand Down Expand Up @@ -65,18 +62,6 @@ describe('AMPDocumentStatusNotification', () => {
}));
});

beforeEach(() => {
// jest.clearAllMocks();
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
unmountComponentAtNode(container);
container.remove();
container = null;
});

it('renders only a toggle if AMP is disabled', () => {
setupHooks(
{},
Expand All @@ -86,9 +71,7 @@ describe('AMPDocumentStatusNotification', () => {
}
);

act(() => {
render(<AMPDocumentStatusNotification />, container);
});
const { container } = render(<AMPDocumentStatusNotification />);

expect(container.children).toHaveLength(1);
expect(container.innerHTML).toContain('Enable AMP');
Expand All @@ -103,9 +86,7 @@ describe('AMPDocumentStatusNotification', () => {
}
);

act(() => {
render(<AMPDocumentStatusNotification />, container);
});
const { container } = render(<AMPDocumentStatusNotification />);

expect(container.innerHTML).toContain('Enable AMP');
expect(
Expand All @@ -119,9 +100,7 @@ describe('AMPDocumentStatusNotification', () => {
isPostDirty: true,
});

act(() => {
render(<AMPDocumentStatusNotification />, container);
});
let { container } = render(<AMPDocumentStatusNotification />);

expect(container.innerHTML).toContain('Enable AMP');
expect(container.innerHTML).toContain('Content has changed.');
Expand All @@ -133,14 +112,13 @@ describe('AMPDocumentStatusNotification', () => {
maybeIsPostDirty: true,
});

act(() => {
render(<AMPDocumentStatusNotification />, container);
});
({ container } = render(<AMPDocumentStatusNotification />));

expect(container.innerHTML).toContain('Content may have changed.');

// Simulate button click.
container.querySelector('button').click();
fireEvent.click(container.querySelector('button'));

expect(openGeneralSidebar).toHaveBeenCalledTimes(1);
expect(closePublishSidebar).toHaveBeenCalledTimes(1);
});
Expand All @@ -150,9 +128,7 @@ describe('AMPDocumentStatusNotification', () => {
keptMarkupValidationErrorCount: 3,
});

act(() => {
render(<AMPDocumentStatusNotification />, container);
});
const { container } = render(<AMPDocumentStatusNotification />);

expect(container.innerHTML).toContain('Enable AMP');
expect(container.innerHTML).toContain(
Expand All @@ -169,9 +145,7 @@ describe('AMPDocumentStatusNotification', () => {
unreviewedValidationErrorCount: 1,
});

act(() => {
render(<AMPDocumentStatusNotification />, container);
});
const { container } = render(<AMPDocumentStatusNotification />);

expect(container.innerHTML).toContain('Enable AMP');
expect(container.innerHTML).toContain(
Expand All @@ -186,9 +160,7 @@ describe('AMPDocumentStatusNotification', () => {
it('renders a correct message if there are no errors', () => {
setupHooks();

act(() => {
render(<AMPDocumentStatusNotification />, container);
});
const { container } = render(<AMPDocumentStatusNotification />);

expect(container.innerHTML).toContain('Enable AMP');
expect(container.innerHTML).toContain(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
/**
* External dependencies
*/
import { act } from 'react-dom/test-utils';

/**
* WordPress dependencies
*/
import { render, unmountComponentAtNode } from '@wordpress/element';
import { render, fireEvent } from '@testing-library/react';

/**
* Internal dependencies
Expand All @@ -19,8 +14,6 @@ jest.mock('../../../hooks/use-amp-document-toggle', () => ({
}));

describe('AMPToggle', () => {
let container;

const toggleAMP = jest.fn();

function setupHooks(overrides) {
Expand All @@ -33,25 +26,14 @@ describe('AMPToggle', () => {

beforeEach(() => {
jest.clearAllMocks();

container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
unmountComponentAtNode(container);
container.remove();
container = null;
});

it('renders a toggle that reacts to changes', () => {
setupHooks({
isAMPEnabled: true,
});

act(() => {
render(<AMPToggle />, container);
});
const { container } = render(<AMPToggle />);

expect(
container.querySelector('input[type="checkbox"]')
Expand All @@ -60,7 +42,8 @@ describe('AMPToggle', () => {
true
);

container.querySelector('input[type="checkbox"]').click();
fireEvent.click(container.querySelector('input[type="checkbox"]'));

expect(toggleAMP).toHaveBeenCalledTimes(1);
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
* External dependencies
*/
import { act } from 'react-dom/test-utils';
import { render, fireEvent } from '@testing-library/react';

/**
* WordPress dependencies
*/
import { render, unmountComponentAtNode } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';

/**
Expand All @@ -24,8 +23,6 @@ jest.mock('../../../hooks/use-errors-fetching-state-changes', () => ({
}));

describe('AMPRevalidateNotification', () => {
let container;

const autosave = jest.fn();
const savePost = jest.fn();

Expand All @@ -45,27 +42,16 @@ describe('AMPRevalidateNotification', () => {
});

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);

useErrorsFetchingStateChanges.mockImplementation(() => ({
isFetchingErrors: false,
fetchingErrorsMessage: '',
}));
});

afterEach(() => {
unmountComponentAtNode(container);
container.remove();
container = null;
});

it('does not render revalidate message if post is not dirty', () => {
setupUseSelect();

act(() => {
render(<AMPRevalidateNotification />, container);
});
const { container } = render(<AMPRevalidateNotification />);

expect(container.children).toHaveLength(0);
});
Expand All @@ -76,9 +62,7 @@ describe('AMPRevalidateNotification', () => {
fetchingErrorsMessage: 'Loading',
}));

act(() => {
render(<AMPRevalidateNotification />, container);
});
const { container } = render(<AMPRevalidateNotification />);

expect(
container.querySelector('.amp-spinner-container')
Expand All @@ -91,9 +75,7 @@ describe('AMPRevalidateNotification', () => {
isPostDirty: true,
});

act(() => {
render(<AMPRevalidateNotification />, container);
});
const { container } = render(<AMPRevalidateNotification />);

expect(container.innerHTML).toMatchSnapshot();
expect(container.children).toHaveLength(1);
Expand All @@ -103,7 +85,8 @@ describe('AMPRevalidateNotification', () => {
'Re-validate'
);

container.querySelector('button').click();
fireEvent.click(container.querySelector('button'));

expect(autosave).toHaveBeenCalledWith({ isPreview: true });
});

Expand All @@ -113,17 +96,16 @@ describe('AMPRevalidateNotification', () => {
isPostDirty: true,
});

act(() => {
render(<AMPRevalidateNotification />, container);
});
const { container } = render(<AMPRevalidateNotification />);

expect(container.innerHTML).toMatchSnapshot();
expect(container.innerHTML).toContain('has changed');
expect(container.querySelector('button').textContent).toContain(
'Save draft'
);

container.querySelector('button').click();
fireEvent.click(container.querySelector('button'));

expect(savePost).toHaveBeenCalledWith({ isPreview: true });
});

Expand All @@ -134,9 +116,7 @@ describe('AMPRevalidateNotification', () => {
maybeIsPostDirty: true,
});

act(() => {
render(<AMPRevalidateNotification />, container);
});
const { container } = render(<AMPRevalidateNotification />);

expect(container.innerHTML).toMatchSnapshot();
expect(container.children).toHaveLength(1);
Expand Down
Loading