From 32fbe7bc9a5651bc0a3bc8743f36742d01e6fd16 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 2 Feb 2018 11:22:44 -0500 Subject: [PATCH 1/5] Effects: Restore new post setup as non-dirtying change --- editor/store/effects.js | 8 ++++---- editor/store/reducer.js | 2 +- editor/store/test/effects.js | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/editor/store/effects.js b/editor/store/effects.js index 5a6c69c2af330b..8415234f70cb13 100644 --- a/editor/store/effects.js +++ b/editor/store/effects.js @@ -305,6 +305,10 @@ export default { effects.push( resetBlocks( blocks ) ); } + // Resetting post should occur after blocks have been reset, since it's + // the post reset that restarts history (used in dirty detection). + effects.push( resetPost( post ) ); + // Include auto draft title in edits while not flagging post as dirty if ( post.status === 'auto-draft' ) { effects.push( setupNewPost( { @@ -312,10 +316,6 @@ export default { } ) ); } - // Resetting post should occur after blocks have been reset, since it's - // the post reset that restarts history (used in dirty detection). - effects.push( resetPost( post ) ); - return effects; }, FETCH_REUSABLE_BLOCKS( action, store ) { diff --git a/editor/store/reducer.js b/editor/store/reducer.js index 861c5b1defe0b3..47a923a84d0430 100644 --- a/editor/store/reducer.js +++ b/editor/store/reducer.js @@ -118,7 +118,7 @@ export const editor = flow( [ // Track whether changes exist, resetting at each post save. Relies on // editor initialization firing post reset as an effect. - partialRight( withChangeDetection, { resetTypes: [ 'RESET_POST' ] } ), + partialRight( withChangeDetection, { resetTypes: [ 'SETUP_NEW_POST', 'RESET_POST' ] } ), ] )( { edits( state = {}, action ) { switch ( action.type ) { diff --git a/editor/store/test/effects.js b/editor/store/test/effects.js index a940ac0dcac494..d515d18792ff19 100644 --- a/editor/store/test/effects.js +++ b/editor/store/test/effects.js @@ -469,8 +469,8 @@ describe( 'effects', () => { const result = handler( { post, settings: {} } ); expect( result ).toEqual( [ - setupNewPost( { title: 'A History of Pork' } ), resetPost( post ), + setupNewPost( { title: 'A History of Pork' } ), ] ); } ); } ); From a4fff32c034289091f284501e04187943e131ac7 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 2 Feb 2018 11:29:31 -0500 Subject: [PATCH 2/5] Store: Reset history when setting up new post Avoids "Undo" present on new post --- editor/store/reducer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor/store/reducer.js b/editor/store/reducer.js index 47a923a84d0430..81450ebf56fb02 100644 --- a/editor/store/reducer.js +++ b/editor/store/reducer.js @@ -114,7 +114,7 @@ export const editor = flow( [ combineReducers, // Track undo history, starting at editor initialization. - partialRight( withHistory, { resetTypes: [ 'SETUP_EDITOR' ] } ), + partialRight( withHistory, { resetTypes: [ 'SETUP_NEW_POST', 'SETUP_EDITOR' ] } ), // Track whether changes exist, resetting at each post save. Relies on // editor initialization firing post reset as an effect. From 8b1d6f12e0890dcd6b8e7798163518a8da0d0f9f Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 2 Feb 2018 12:03:03 -0500 Subject: [PATCH 3/5] Tests: Configure Cypress for Mocha/Chai See: https://docs.cypress.io/guides/references/assertions.html#Chai --- .eslintrc.json | 1 - test/e2e/.eslintrc.json | 171 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 170 insertions(+), 2 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index f529be3eb28c26..59fb1e9a419a2f 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -11,7 +11,6 @@ "browser": false, "es6": true, "node": true, - "mocha": true, "jest/globals": true }, "parserOptions": { diff --git a/test/e2e/.eslintrc.json b/test/e2e/.eslintrc.json index 60e3c131aeac94..ccae7f5e561ae8 100644 --- a/test/e2e/.eslintrc.json +++ b/test/e2e/.eslintrc.json @@ -1,6 +1,175 @@ { + "root": true, + "parser": "babel-eslint", + "extends": [ + "wordpress" + ], + "env": { + "browser": false, + "es6": true, + "node": true, + "mocha": true + }, + "parserOptions": { + "sourceType": "module" + }, "globals": { "cy": true, - "Cypress": true + "Cypress": true, + "expect": true + }, + "plugins": [ + "wordpress" + ], + "rules": { + "array-bracket-spacing": [ "error", "always" ], + "brace-style": [ "error", "1tbs" ], + "camelcase": [ "error", { "properties": "never" } ], + "comma-dangle": [ "error", "always-multiline" ], + "comma-spacing": "error", + "comma-style": "error", + "computed-property-spacing": [ "error", "always" ], + "constructor-super": "error", + "dot-notation": "error", + "eol-last": "error", + "eqeqeq": "error", + "func-call-spacing": "error", + "indent": [ "error", "tab", { "SwitchCase": 1 } ], + "key-spacing": "error", + "keyword-spacing": "error", + "lines-around-comment": "off", + "no-alert": "error", + "no-bitwise": "error", + "no-caller": "error", + "no-console": "error", + "no-const-assign": "error", + "no-debugger": "error", + "no-dupe-args": "error", + "no-dupe-class-members": "error", + "no-dupe-keys": "error", + "no-duplicate-case": "error", + "no-duplicate-imports": "error", + "no-else-return": "error", + "no-eval": "error", + "no-extra-semi": "error", + "no-fallthrough": "error", + "no-lonely-if": "error", + "no-mixed-operators": "error", + "no-mixed-spaces-and-tabs": "error", + "no-multiple-empty-lines": [ "error", { "max": 1 } ], + "no-multi-spaces": "error", + "no-multi-str": "off", + "no-negated-in-lhs": "error", + "no-nested-ternary": "error", + "no-redeclare": "error", + "no-restricted-syntax": [ + "error", + { + "selector": "ImportDeclaration[source.value=/^@wordpress\\u002F.+\\u002F/]", + "message": "Path access on WordPress dependencies is not allowed." + }, + { + "selector": "ImportDeclaration[source.value=/^blocks$/]", + "message": "Use @wordpress/blocks as import path instead." + }, + { + "selector": "ImportDeclaration[source.value=/^components$/]", + "message": "Use @wordpress/components as import path instead." + }, + { + "selector": "ImportDeclaration[source.value=/^date$/]", + "message": "Use @wordpress/date as import path instead." + }, + { + "selector": "ImportDeclaration[source.value=/^editor$/]", + "message": "Use @wordpress/editor as import path instead." + }, + { + "selector": "ImportDeclaration[source.value=/^element$/]", + "message": "Use @wordpress/element as import path instead." + }, + { + "selector": "ImportDeclaration[source.value=/^i18n$/]", + "message": "Use @wordpress/i18n as import path instead." + }, + { + "selector": "ImportDeclaration[source.value=/^data$/]", + "message": "Use @wordpress/data as import path instead." + }, + { + "selector": "ImportDeclaration[source.value=/^utils$/]", + "message": "Use @wordpress/utils as import path instead." + }, + { + "selector": "ImportDeclaration[source.value=/^edit-poost$/]", + "message": "Use @wordpress/edit-post as import path instead." + }, + { + "selector": "CallExpression[callee.name=/^__|_n|_x$/]:not([arguments.0.type=/^Literal|BinaryExpression$/])", + "message": "Translate function arguments must be string literals." + }, + { + "selector": "CallExpression[callee.name=/^_n|_x$/]:not([arguments.1.type=/^Literal|BinaryExpression$/])", + "message": "Translate function arguments must be string literals." + }, + { + "selector": "CallExpression[callee.name=_nx]:not([arguments.2.type=/^Literal|BinaryExpression$/])", + "message": "Translate function arguments must be string literals." + } + ], + "no-shadow": "error", + "no-undef": "error", + "no-undef-init": "error", + "no-unreachable": "error", + "no-unsafe-negation": "error", + "no-unused-expressions": "error", + "no-unused-vars": "error", + "no-useless-computed-key": "error", + "no-useless-constructor": "error", + "no-useless-return": "error", + "no-var": "error", + "no-whitespace-before-property": "error", + "object-curly-spacing": [ "error", "always" ], + "padded-blocks": [ "error", "never" ], + "prefer-const": "error", + "quote-props": [ "error", "as-needed" ], + "semi": "error", + "semi-spacing": "error", + "space-before-blocks": [ "error", "always" ], + "space-before-function-paren": [ "error", "never" ], + "space-in-parens": [ "error", "always" ], + "space-infix-ops": [ "error", { "int32Hint": false } ], + "space-unary-ops": [ "error", { + "overrides": { + "!": true + } + } ], + "template-curly-spacing": [ "error", "always" ], + "valid-jsdoc": [ "error", { + "prefer": { + "arg": "param", + "argument": "param", + "extends": "augments", + "returns": "return" + }, + "preferType": { + "array": "Array", + "bool": "boolean", + "Boolean": "boolean", + "float": "number", + "Float": "number", + "int": "number", + "integer": "number", + "Integer": "number", + "Number": "number", + "object": "Object", + "String": "string", + "Void": "void" + }, + "requireParamDescription": false, + "requireReturn": false + } ], + "valid-typeof": "error", + "yoda": "off" } } From 2566c71d42247fa676e35772c23940bf1446dd78 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Fri, 2 Feb 2018 12:03:56 -0500 Subject: [PATCH 4/5] Tests: Add integration tests for history, confirm prompt --- editor/components/editor-history/redo.js | 1 + editor/components/editor-history/undo.js | 1 + test/e2e/integration/001-hello-gutenberg.js | 26 +++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/editor/components/editor-history/redo.js b/editor/components/editor-history/redo.js index 89b032eb6875fb..88ecb3d6434126 100644 --- a/editor/components/editor-history/redo.js +++ b/editor/components/editor-history/redo.js @@ -21,6 +21,7 @@ function EditorHistoryRedo( { hasRedo, redo } ) { label={ __( 'Redo' ) } disabled={ ! hasRedo } onClick={ redo } + className="editor-history__undo" /> ); } diff --git a/editor/components/editor-history/undo.js b/editor/components/editor-history/undo.js index 0c6361e6ad1700..6dd3d6e8e928e3 100644 --- a/editor/components/editor-history/undo.js +++ b/editor/components/editor-history/undo.js @@ -21,6 +21,7 @@ function EditorHistoryUndo( { hasUndo, undo } ) { label={ __( 'Undo' ) } disabled={ ! hasUndo } onClick={ undo } + className="editor-history__undo" /> ); } diff --git a/test/e2e/integration/001-hello-gutenberg.js b/test/e2e/integration/001-hello-gutenberg.js index fed6862bcb1ddc..37e21e5ec84acd 100644 --- a/test/e2e/integration/001-hello-gutenberg.js +++ b/test/e2e/integration/001-hello-gutenberg.js @@ -8,4 +8,30 @@ describe( 'Hello Gutenberg', () => { cy.url().should( 'include', 'post-new.php' ); cy.get( '[placeholder="Add title"]' ).should( 'exist' ); } ); + + it( 'Should have no history', () => { + cy.get( '.editor-history__undo:not( :disabled )' ).should( 'not.exist' ); + cy.get( '.editor-history__redo:not( :disabled )' ).should( 'not.exist' ); + } ); + + it( 'Should not prompt to confirm unsaved changes', ( done ) => { + const timeout = setTimeout( () => { + done( new Error( 'Expected page reload' ) ); + }, 5000 ); + + cy.window().then( ( window ) => { + function verify( event ) { + expect( event.returnValue ).to.equal( '' ); + + window.removeEventListener( 'beforeunload', verify ); + clearTimeout( timeout ); + + done(); + } + + window.addEventListener( 'beforeunload', verify ); + + cy.reload(); + } ); + } ); } ); From 88f922ecfb967b5cec7bba2013303ebce5a8f7a4 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Tue, 6 Feb 2018 16:24:04 -0500 Subject: [PATCH 5/5] Testing: Document reason for custom E2E ESLint ruleset --- test/e2e/{.eslintrc.json => .eslintrc.js} | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) rename test/e2e/{.eslintrc.json => .eslintrc.js} (92%) diff --git a/test/e2e/.eslintrc.json b/test/e2e/.eslintrc.js similarity index 92% rename from test/e2e/.eslintrc.json rename to test/e2e/.eslintrc.js index ccae7f5e561ae8..df03ebfead0ccd 100644 --- a/test/e2e/.eslintrc.json +++ b/test/e2e/.eslintrc.js @@ -1,4 +1,10 @@ -{ +// For the most part, this file mirrors the same configuration from the root +// `.eslintrc.json`, the exceptions being that since Cypress is preconfigured +// with Mocha & Chai, we need to broadly disable the Jest rules, which is +// otherwise difficult to do. This file could be made more minimal once all of +// the Gutenberg-specific rules are migrated to a common WordPress config. + +module.exports = { "root": true, "parser": "babel-eslint", "extends": [ @@ -172,4 +178,4 @@ "valid-typeof": "error", "yoda": "off" } -} +};