Skip to content

Commit

Permalink
Update usage of block-validation store in test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
thelovekesh committed May 15, 2023
1 parent 3c8720e commit 45e9ac9
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 174 deletions.
111 changes: 55 additions & 56 deletions assets/src/block-validation/components/error/test/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { registerBlockType, createBlock } from '@wordpress/blocks';
* Internal dependencies
*/
import { Error } from '../index';
import { createStore } from '../../../store';
import { store as blockValidationStore } from '../../../store';

let pluginBlock, muPluginBlock, themeBlock, coreBlock, unknownBlock;

Expand Down Expand Up @@ -84,66 +84,65 @@ function createTestStoreAndBlocks() {
unknownBlock,
]);

createStore({
reviewLink: 'http://site.test/wp-admin',
validationErrors: [
{
clientId: pluginBlock.clientId,
code: 'DISALLOWED_TAG',
status: 3,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
error: {
type: 'js_error',
sources: [],
},
dispatch(blockValidationStore).setReviewLink('http://site.test/wp-admin');

dispatch(blockValidationStore).setValidationErrors([
{
clientId: pluginBlock.clientId,
code: 'DISALLOWED_TAG',
status: 3,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
error: {
type: 'js_error',
sources: [],
},
{
clientId: muPluginBlock.clientId,
code: 'DISALLOWED_TAG',
status: 3,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
error: {
type: 'js_error',
sources: [],
},
},
{
clientId: muPluginBlock.clientId,
code: 'DISALLOWED_TAG',
status: 3,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
error: {
type: 'js_error',
sources: [],
},
{
clientId: themeBlock.clientId,
code: 'DISALLOWED_TAG',
status: 3,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
error: {
type: 'js_error',
sources: [],
},
},
{
clientId: themeBlock.clientId,
code: 'DISALLOWED_TAG',
status: 3,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
error: {
type: 'js_error',
sources: [],
},
{
clientId: coreBlock.clientId,
code: 'DISALLOWED_TAG',
status: 3,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
error: {
type: 'js_error',
sources: [],
},
},
{
clientId: coreBlock.clientId,
code: 'DISALLOWED_TAG',
status: 3,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
error: {
type: 'js_error',
sources: [],
},
{
clientId: unknownBlock.clientId,
code: 'DISALLOWED_TAG',
status: 3,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
error: {
type: 'js_error',
sources: [],
},
},
{
clientId: unknownBlock.clientId,
code: 'DISALLOWED_TAG',
status: 3,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
error: {
type: 'js_error',
sources: [],
},
],
});
},
]);
}

function getTestBlock(type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import '@wordpress/block-editor'; // Block editor data store needed.
/**
* Internal dependencies
*/
import { createStore } from '../../../store';
import { store as blockValidationStore } from '../../../store';
import { withAMPToolbarButton } from '../index';

let block;
Expand All @@ -38,29 +38,18 @@ describe('withAMPToolbarButton: filtering with errors', () => {
block = createBlock(TEST_BLOCK, {});
dispatch('core/block-editor').insertBlock(block);

createStore({
reviewLink: 'http://review-link.test',
unreviewedValidationErrors: [
{
clientId: block.clientId,
code: 'DISALLOWED_TAG',
status: 3,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
type: 'js_error',
},
],
validationErrors: [
{
clientId: block.clientId,
code: 'DISALLOWED_TAG',
status: 3,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
type: 'js_error',
},
],
});
dispatch(blockValidationStore).setReviewLink('http://review-link.test');

dispatch(blockValidationStore).setValidationErrors([
{
clientId: block.clientId,
code: 'DISALLOWED_TAG',
status: 1,
term_id: 12,
title: 'Invalid script: <code>jquery.js</code>',
type: 'js_error',
},
]);
});

it('is filtered correctly with a class component', () => {
Expand Down Expand Up @@ -105,10 +94,9 @@ describe('withAMPToolbarButton: filtering without errors', () => {
block = createBlock(TEST_BLOCK, {});
dispatch('core/block-editor').insertBlock(block);

createStore({
reviewLink: 'http://review-link.test',
validationErrors: [],
});
dispatch(blockValidationStore).setReviewLink('http://review-link.test');

dispatch(blockValidationStore).setValidationErrors([]);
});

it('is not filtered with a class component and no errors', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
* Internal dependencies
*/
import { usePostDirtyStateChanges } from '../use-post-dirty-state-changes';
import { BLOCK_VALIDATION_STORE_KEY, createStore } from '../../store';
import { store as blockValidationStore } from '../../store';

jest.mock('@wordpress/data/build/components/use-select', () => jest.fn());
jest.mock('@wordpress/compose/build/hooks/use-debounce', () => (fn) => fn);
Expand All @@ -40,16 +40,12 @@ describe('usePostDirtyStateChanges', () => {
useSelect.mockImplementation(() => ({
getEditedPostContent,
isSavingOrPreviewingPost: false,
isPostDirty: select(BLOCK_VALIDATION_STORE_KEY).getIsPostDirty(),
isPostDirty: select(blockValidationStore).getIsPostDirty(),
...overrides,
}));
}

beforeAll(() => {
createStore({
isPostDirty: false,
});

register(
createReduxStore('test/use-post-dirty-state-updates', {
reducer: (state = {}) => ({ ...state }),
Expand All @@ -67,7 +63,7 @@ describe('usePostDirtyStateChanges', () => {

renderComponentContainingHook();

expect(select(BLOCK_VALIDATION_STORE_KEY).getIsPostDirty()).toBe(false);
expect(select(blockValidationStore).getIsPostDirty()).toBe(false);

// Change content - post should become dirty.
getEditedPostContent.mockReturnValue('foo');
Expand All @@ -76,7 +72,7 @@ describe('usePostDirtyStateChanges', () => {
dispatch('test/use-post-dirty-state-updates').change();
});

expect(select(BLOCK_VALIDATION_STORE_KEY).getIsPostDirty()).toBe(true);
expect(select(blockValidationStore).getIsPostDirty()).toBe(true);

// Save post - dirty state should get cleared.
setupUseSelect({
Expand All @@ -85,7 +81,7 @@ describe('usePostDirtyStateChanges', () => {

renderComponentContainingHook();

expect(select(BLOCK_VALIDATION_STORE_KEY).getIsPostDirty()).toBe(false);
expect(select(blockValidationStore).getIsPostDirty()).toBe(false);

// Change content - getEditedPostContent() should be called again
getEditedPostContent.mockReturnValue('baz');
Expand All @@ -99,6 +95,6 @@ describe('usePostDirtyStateChanges', () => {
dispatch('test/use-post-dirty-state-updates').change();
});

expect(select(BLOCK_VALIDATION_STORE_KEY).getIsPostDirty()).toBe(true);
expect(select(blockValidationStore).getIsPostDirty()).toBe(true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
maybeAddClientIdToValidationError,
useValidationErrorStateUpdates,
} from '../use-validation-error-state-updates';
import { BLOCK_VALIDATION_STORE_KEY, createStore } from '../../store';
import { store as blockValidationStore } from '../../store';

// This allows us to tweak the returned value on each test
jest.mock('@wordpress/data/build/components/use-select', () => jest.fn());
Expand Down Expand Up @@ -60,12 +60,6 @@ describe('useValidationErrorStateUpdates', () => {
}));
}

beforeAll(() => {
createStore({
validationErrors: [],
});
});

it('does not trigger validation on an autosave', async () => {
// Initial render should trigger validation.
setupUseSelect({
Expand All @@ -77,13 +71,13 @@ describe('useValidationErrorStateUpdates', () => {

await waitFor(() => {
expect(
select(BLOCK_VALIDATION_STORE_KEY).getValidationErrors()
select(blockValidationStore).getValidationErrors()
).toHaveLength(0);
});

await waitFor(() => {
expect(
select(BLOCK_VALIDATION_STORE_KEY).getValidationErrors()
select(blockValidationStore).getValidationErrors()
).toHaveLength(0);
});
});
Expand All @@ -95,13 +89,13 @@ describe('useValidationErrorStateUpdates', () => {

await waitFor(() => {
expect(
select(BLOCK_VALIDATION_STORE_KEY).getValidationErrors()
select(blockValidationStore).getValidationErrors()
).toHaveLength(0);
});

await waitFor(() => {
expect(
select(BLOCK_VALIDATION_STORE_KEY).getValidationErrors()
select(blockValidationStore).getValidationErrors()
).toHaveLength(8);
});
});
Expand All @@ -118,7 +112,7 @@ describe('useValidationErrorStateUpdates', () => {

await waitFor(() => {
expect(
select(BLOCK_VALIDATION_STORE_KEY).getValidationErrors()
select(blockValidationStore).getValidationErrors()
).toHaveLength(0);
});

Expand All @@ -134,7 +128,7 @@ describe('useValidationErrorStateUpdates', () => {

await waitFor(() => {
expect(
select(BLOCK_VALIDATION_STORE_KEY).getValidationErrors()
select(blockValidationStore).getValidationErrors()
).toHaveLength(0);
});

Expand All @@ -151,7 +145,7 @@ describe('useValidationErrorStateUpdates', () => {

await waitFor(() => {
expect(
select(BLOCK_VALIDATION_STORE_KEY).getValidationErrors()
select(blockValidationStore).getValidationErrors()
).toHaveLength(8);
});
});
Expand Down
Loading

0 comments on commit 45e9ac9

Please sign in to comment.