Skip to content

Commit

Permalink
Update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Jul 11, 2022
1 parent e4e3151 commit e0c2b79
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ exports[`EditPostPreferencesModal should match snapshot when the modal is active
panelName="post-excerpt"
/>
</PostExcerptCheck>
<WithSelect(PostTypeSupportCheck)
<PostTypeSupportCheck
supportKeys={
Array [
"comments",
Expand All @@ -118,7 +118,7 @@ exports[`EditPostPreferencesModal should match snapshot when the modal is active
label="Discussion"
panelName="discussion-panel"
/>
</WithSelect(PostTypeSupportCheck)>
</PostTypeSupportCheck>
<PageAttributesCheck>
<WithSelect(IfCondition(WithDispatch(BaseOption)))
label="Page attributes"
Expand Down Expand Up @@ -237,7 +237,7 @@ exports[`EditPostPreferencesModal should match snapshot when the modal is active
panelName="post-excerpt"
/>
</PostExcerptCheck>
<WithSelect(PostTypeSupportCheck)
<PostTypeSupportCheck
supportKeys={
Array [
"comments",
Expand All @@ -249,7 +249,7 @@ exports[`EditPostPreferencesModal should match snapshot when the modal is active
label="Discussion"
panelName="discussion-panel"
/>
</WithSelect(PostTypeSupportCheck)>
</PostTypeSupportCheck>
<PageAttributesCheck>
<WithSelect(IfCondition(WithDispatch(BaseOption)))
label="Page attributes"
Expand Down
67 changes: 50 additions & 17 deletions packages/editor/src/components/post-last-revision/test/check.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,74 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';
import { create } from 'react-test-renderer';

/**
* WordPress dependencies
*/
import { createRegistry, RegistryProvider } from '@wordpress/data';

/**
* Internal dependencies
*/
import { PostLastRevisionCheck } from '../check';
import PostLastRevisionCheck from '../check';

function createMockRegistry(
currentPostLastRevisionId,
currentPostRevisionsCount
) {
return createRegistry( {
'core/editor': {
selectors: {
getCurrentPostLastRevisionId: () => currentPostLastRevisionId,
getCurrentPostRevisionsCount: () => currentPostRevisionsCount,
getEditedPostAttribute: () => 'post',
},
reducer: ( state = {} ) => state,
},
core: {
selectors: {
getPostType: () => ( { supports: { revisions: true } } ),
},
reducer: ( state = {} ) => state,
},
} );
}

describe( 'PostLastRevisionCheck', () => {
it( 'should not render anything if the last revision ID is unknown', () => {
const wrapper = shallow(
<PostLastRevisionCheck revisionsCount={ 2 }>
Children
</PostLastRevisionCheck>
const registry = createMockRegistry( undefined, 2 );

const tree = create(
<RegistryProvider value={ registry }>
<PostLastRevisionCheck>Children</PostLastRevisionCheck>
</RegistryProvider>
);

expect( wrapper.type() ).toBe( null );
expect( tree.toJSON() ).toBe( null );
} );

it( 'should not render anything if there is only one revision', () => {
const wrapper = shallow(
<PostLastRevisionCheck lastRevisionId={ 1 } revisionsCount={ 1 }>
Children
</PostLastRevisionCheck>
const registry = createMockRegistry( 1, 1 );

const tree = create(
<RegistryProvider value={ registry }>
<PostLastRevisionCheck>Children</PostLastRevisionCheck>
</RegistryProvider>
);

expect( wrapper.type() ).toBe( null );
expect( tree.toJSON() ).toBe( null );
} );

it( 'should render if there are two revisions', () => {
const wrapper = shallow(
<PostLastRevisionCheck lastRevisionId={ 1 } revisionsCount={ 2 }>
Children
</PostLastRevisionCheck>
const registry = createMockRegistry( 1, 2 );

const tree = create(
<RegistryProvider value={ registry }>
<PostLastRevisionCheck>Children</PostLastRevisionCheck>
</RegistryProvider>
);

expect( wrapper.text() ).not.toBe( null );
expect( tree.toJSON() ).toBe( 'Children' );
} );
} );
61 changes: 47 additions & 14 deletions packages/editor/src/components/post-pending-status/test/check.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,62 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';
import { create } from 'react-test-renderer';

/**
* WordPress dependencies
*/
import { createRegistry, RegistryProvider } from '@wordpress/data';

/**
* Internal dependencies
*/
import { PostPendingStatusCheck } from '../check';
import PostPendingStatusCheck from '../check';

function createMockRegistry( isCurrentPostPublished, currentPost ) {
return createRegistry( {
'core/editor': {
selectors: {
isCurrentPostPublished: () => isCurrentPostPublished,
getCurrentPost: () => currentPost,
},
reducer: ( state = {} ) => state,
},
} );
}

describe( 'PostPendingStatusCheck', () => {
it( "should not render anything if the user doesn't have the right capabilities", () => {
const wrapper = shallow(
<PostPendingStatusCheck hasPublishAction={ false }>
status
</PostPendingStatusCheck>
it( 'should not render anything if the post is published', () => {
const registry = createMockRegistry( true, null );
const tree = create(
<RegistryProvider value={ registry }>
<PostPendingStatusCheck>status</PostPendingStatusCheck>
</RegistryProvider>
);
expect( tree.toJSON() ).toBe( null );
} );

it( 'should not render anything if the post has no publish action', () => {
const registry = createMockRegistry( false, {
_links: {},
} );
const tree = create(
<RegistryProvider value={ registry }>
<PostPendingStatusCheck>status</PostPendingStatusCheck>
</RegistryProvider>
);
expect( wrapper.type() ).toBe( null );
expect( tree.toJSON() ).toBe( null );
} );

it( 'should render if the user has the correct capability', () => {
const wrapper = shallow(
<PostPendingStatusCheck hasPublishAction={ true }>
status
</PostPendingStatusCheck>
it( 'should render if the post has a publish action', () => {
const registry = createMockRegistry( false, {
_links: { 'wp:action-publish': 'https://' },
} );
const tree = create(
<RegistryProvider value={ registry }>
<PostPendingStatusCheck>status</PostPendingStatusCheck>
</RegistryProvider>
);
expect( wrapper.type() ).not.toBe( null );
expect( tree.toJSON() ).toBe( 'status' );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,111 @@
*/
import { create } from 'react-test-renderer';

/**
* WordPress dependencies
*/
import { createRegistry, RegistryProvider } from '@wordpress/data';

/**
* Internal dependencies
*/
import { PostTypeSupportCheck } from '../';
import PostTypeSupportCheck from '../';

function createMockRegistry( postType ) {
return createRegistry( {
'core/editor': {
selectors: {
getEditedPostAttribute: () => 'post',
},
reducer: ( state = {} ) => state,
},
core: {
selectors: {
getPostType: () => postType,
},
reducer: ( state = {} ) => state,
},
} );
}

describe( 'PostTypeSupportCheck', () => {
it( 'renders its children when post type is not known', () => {
let postType;
const registry = createMockRegistry( null );

const tree = create(
<PostTypeSupportCheck postType={ postType } supportKeys="title">
Supported
</PostTypeSupportCheck>
<RegistryProvider value={ registry }>
<PostTypeSupportCheck supportKeys="title">
Supported
</PostTypeSupportCheck>
</RegistryProvider>
);

expect( tree.toJSON() ).toBe( 'Supported' );
} );

it( 'does not render its children when post type is known and not supports', () => {
const postType = {
const registry = createMockRegistry( {
supports: {},
};
} );

const tree = create(
<PostTypeSupportCheck postType={ postType } supportKeys="title">
Supported
</PostTypeSupportCheck>
<RegistryProvider value={ registry }>
<PostTypeSupportCheck supportKeys="title">
Supported
</PostTypeSupportCheck>
</RegistryProvider>
);

expect( tree.toJSON() ).toBe( null );
} );

it( 'renders its children when post type is known and supports', () => {
const postType = {
const registry = createMockRegistry( {
supports: {
title: true,
},
};
} );

const tree = create(
<PostTypeSupportCheck postType={ postType } supportKeys="title">
Supported
</PostTypeSupportCheck>
<RegistryProvider value={ registry }>
<PostTypeSupportCheck supportKeys="title">
Supported
</PostTypeSupportCheck>
</RegistryProvider>
);

expect( tree.toJSON() ).toBe( 'Supported' );
} );

it( 'renders its children if some of keys supported', () => {
const postType = {
const registry = createMockRegistry( {
supports: {
title: true,
},
};
} );

const tree = create(
<PostTypeSupportCheck
postType={ postType }
supportKeys={ [ 'title', 'thumbnail' ] }
>
Supported
</PostTypeSupportCheck>
<RegistryProvider value={ registry }>
<PostTypeSupportCheck supportKeys={ [ 'title', 'thumbnail' ] }>
Supported
</PostTypeSupportCheck>
</RegistryProvider>
);

expect( tree.toJSON() ).toBe( 'Supported' );
} );

it( 'does not render its children if none of keys supported', () => {
const postType = {
const registry = createMockRegistry( {
supports: {},
};
} );

const tree = create(
<PostTypeSupportCheck
postType={ postType }
supportKeys={ [ 'title', 'thumbnail' ] }
>
Supported
</PostTypeSupportCheck>
<RegistryProvider value={ registry }>
<PostTypeSupportCheck supportKeys={ [ 'title', 'thumbnail' ] }>
Supported
</PostTypeSupportCheck>
</RegistryProvider>
);

expect( tree.toJSON() ).toBe( null );
Expand Down

0 comments on commit e0c2b79

Please sign in to comment.