Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit test for /editor/actions.js #3787

Merged
merged 7 commits into from
Dec 8, 2017
Merged

Add Unit test for /editor/actions.js #3787

merged 7 commits into from
Dec 8, 2017

Conversation

hideokamoto
Copy link
Contributor

Description

Related to progress on the issue -> Unit Testing Components #641

Added some unit test scripts for actions.

How Has This Been Tested?

Run unit test command.

$ npm test


PASS  editor/test/actions.js
 actions
   setupEditor
     ✓ should return the SETUP_EDITOR action (5ms)
   resetPost
     ✓ should return the RESET_POST action (2ms)
   setupNewPost
     ✓ should return the SETUP_NEW_POST action (3ms)
   resetBlocks
     ✓ should return the RESET_BLOCKS actions (3ms)
   updateBlockAttributes
     ✓ should return the UPDATE_BLOCK_ATTRIBUTES action (14ms)
   updateBlock
     ✓ should return the UPDATE_BLOCK action (5ms)
   focusBlock
     ✓ should return the UPDATE_FOCUS action (2ms)
   selectBlock
     ✓ should return the SELECT_BLOCK action (1ms)
   startMultiSelect
     ✓ should return the START_MULTI_SELECT (2ms)
   stopMultiSelect
     ✓ should return the Stop_MULTI_SELECT (2ms)
   multiSelect
     ✓ should return MULTI_SELECT action (2ms)
   clearSelectedBlock
     ✓ should return CLEAR_SELECTED_BLOCK action (2ms)
   replaceBlock
     ✓ should return the REPLACE_BLOCKS action (1ms)
   replaceBlocks
     ✓ should return the REPLACE_BLOCKS action (1ms)
   insertBlock
     ✓ should return the INSERT_BLOCKS action (1ms)
   insertBlocks
     ✓ should return the INSERT_BLOCKS action (1ms)
   showInsertionPoint
     ✓ should return the SHOW_INSERTION_POINT action (2ms)
   hideInsertionPoint
     ✓ should return the HIDE_INSERTION_POINT action (2ms)
   setBlockInsertionPoint
     ✓ should return the SET_BLOCK_INSERTION_POINT action (1ms)
   clearBlockInsertionPoint
     ✓ should return the CLEAR_BLOCK_INSERTION_POINT action (1ms)
   editPost
     ✓ should return EDIT_POST action (1ms)
   savePost
     ✓ should return REQUEST_POST_UPDATE action (2ms)
   trashPost
     ✓ should return TRASH_POST action (2ms)
   mergeBlocks
     ✓ should return MERGE_BLOCKS action (1ms)
   autosave
     ✓ should return AUTOSAVE action (7ms)
   redo
     ✓ should return REDO action (2ms)
   undo
     ✓ should return UNDO action (1ms)
   removeBlocks
     ✓ should return REMOVE_BLOCKS action (2ms)
   removeBlock
     ✓ should return REMOVE_BLOCKS action (2ms)
   toggleBlockMode
     ✓ should return TOGGLE_BLOCK_MODE action (2ms)
   startTyping
     ✓ should return the START_TYPING action (1ms)
   stopTyping
     ✓ should return the STOP_TYPING action (1ms)
   toggleSidebar
     ✓ should return TOGGLE_SIDEBAR action (1ms)
   toggleSidebarPanel
     ✓ should return TOGGLE_SIDEBAR_PANEL action (1ms)
   createNotice
     ✓ should return CREATE_NOTICE action when options is empty (3ms)
     ✓ should return CREATE_NOTICE action when options is desined (2ms)
   createSuccessNotice
     ✓ should return CREATE_NOTICE action (3ms)
   createInfoNotice
     ✓ should return CREATE_NOTICE action (2ms)
   createErrorNotice
     ✓ should return CREATE_NOTICE action (3ms)
   createWarningNotice
     ✓ should return CREATE_NOTICE action (3ms)
   removeNotice
     ✓ should return REMOVE_NOTICE actions (1ms)
   metaBoxLoaded
     ✓ should return META_BOX_LOADED action (2ms)
   toggleFeature
     ✓ should return TOGGLE_FEATURE action (1ms)
   requestMetaBoxUpdates
     ✓ should return the REQUEST_META_BOX_UPDATES action (3ms)
   handleMetaBoxReload
     ✓ should return the HANDLE_META_BOX_RELOAD action with a location and node (1ms)
   metaBoxStateChanged
     ✓ should return the META_BOX_STATE_CHANGED action with a hasChanged flag (1ms)
   initializeMetaBoxState
     ✓ should return the META_BOX_STATE_CHANGED action with a hasChanged flag (1ms)
   fetchReusableBlocks
     ✓ should return the FETCH_REUSABLE_BLOCKS action (1ms)
     ✓ should take an optional id argument (1ms)
   updateReusableBlock
     ✓ should return the UPDATE_REUSABLE_BLOCK action (2ms)

----------------------------------------------------|----------|----------|----------|----------|----------------|
File                                                |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------------------------------------------------|----------|----------|----------|----------|----------------|
editor                                             |    80.55 |    79.02 |    87.76 |    84.01 |                |
 actions.js                                        |      100 |      100 |      100 |      100 |                |

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows has proper inline documentation.

@gziolo gziolo added the [Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests. label Dec 4, 2017
convertBlockToStatic,
convertBlockToReusable,
} from '../actions';
import * as actions from '../actions';
Copy link
Member

@gziolo gziolo Dec 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think using wildcard makes it easier as you need to prefix all method with actions.. It's also a common pattern used to import everything explicitly. It isn't as important in the tests context but can make a difference when bundling code for the browser (it would prevent using tree-shaking feature).

} );
describe( 'removeNotice', () => {
it( 'should return REMOVE_NOTICE actions', () => {
const id = 'id';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id is used only once. It should be either inlined or reused in the toEqual call. The same applies to location and feature in the 2 next tests.

const content = <p>element</p>;
it( 'should return CREATE_NOTICE action when options is empty', () => {
const result = actions.createNotice( status, content );
expect( result.type ).toEqual( 'CREATE_NOTICE' );
Copy link
Member

@gziolo gziolo Dec 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toMatchObject would be a perfect fit here and for the similar tests.

expect( result).toMatchObject( {
    type: 'CREATE_NOTICE',
    notice: {
        status, // it's defined above
        content, // it's also defined above
        isDismissible: true,
        id: expect.any( Number ),
    },
} );

} );
describe( 'replaceBlock', () => {
it( 'should return the REPLACE_BLOCKS action', () => {
const blocks = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a single block so the name should be block.

@gziolo
Copy link
Member

gziolo commented Dec 4, 2017

Awesome work adding all those missing tests. I left some comments where we could consider changes. In general most of the tests define const at the beginning of the test which is used only once. It could be reused when composing expected result. Alternatively, they could be inlined. I prefer the former, as we are only interested whether the output shape is properly created.

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marking as needs author's feedback before proceeding. Let me know what do you think about my comments left.

@hideokamoto
Copy link
Contributor Author

Thanks for feedback !
Now I'm resolving these issues.

Please wait for a while 👍

@hideokamoto
Copy link
Contributor Author

hideokamoto commented Dec 7, 2017

Hi @gziolo .
I've fixed your reviewed points.

Could you review again my PR ?
thanks !

@gziolo
Copy link
Member

gziolo commented Dec 8, 2017

All actions are covered with tests with this PR. Awesome work 👍

@gziolo gziolo merged commit 5c886ce into WordPress:master Dec 8, 2017
@gziolo
Copy link
Member

gziolo commented Dec 8, 2017

Thanks for addressing my comments and your contribution :)

@hideokamoto hideokamoto deleted the add/test/editor/actions branch December 21, 2017 05:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Automated Testing Testing infrastructure changes impacting the execution of end-to-end (E2E) and/or unit tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants