From cf887d38095cd76a402b103e8c8dbedb0257e348 Mon Sep 17 00:00:00 2001 From: Teresa Gobble Date: Fri, 1 Sep 2023 12:00:54 -0500 Subject: [PATCH] MERL-1004: fixed missing edit button in the Faust toolbar when previewing a post (#1556) * Added preview typeName query to edit functionality Co-authored-by: Joe Fusco Co-authored-by: Blake Wilson * Fixed TypeScript error * Create funny-starfishes-melt.md * Update postID and postType to accept edge case of undefined Co-authored-by: Blake Wilson * Fixed moderate severity vulnerability in css-tools * Revert test for failing E2E toolbar.test.tsx * Revert "Revert test for failing E2E toolbar.test.tsx" This reverts commit d8e86dfd5fc929425f3f9798756d15486d1f2960. * Update .changeset/funny-starfishes-melt.md Co-authored-by: John Parris * Test implementation of mocked useRouter call * Added mocked useRouter call to additional tests * Added databaseId to failing tests * Added regression testing for edit button in preview * Added databaseId to test * Reordered tests to fix test package check on GitHub * Merged updated package-lock.json --------- Co-authored-by: Joe Fusco Co-authored-by: Blake Wilson Co-authored-by: Blake Wilson Co-authored-by: John Parris --- .changeset/funny-starfishes-melt.md | 5 + package-lock.json | 180 +++++++++++++----- .../src/components/Toolbar/nodes/Edit.tsx | 20 +- .../tests/components/Toolbar/Toolbar.test.tsx | 98 +++++++++- 4 files changed, 245 insertions(+), 58 deletions(-) create mode 100644 .changeset/funny-starfishes-melt.md diff --git a/.changeset/funny-starfishes-melt.md b/.changeset/funny-starfishes-melt.md new file mode 100644 index 000000000..c3dfaa14c --- /dev/null +++ b/.changeset/funny-starfishes-melt.md @@ -0,0 +1,5 @@ +--- +"@faustwp/core": patch +--- + +Fixed bug where the "Edit Post" link was missing in the Faust Toolbar diff --git a/package-lock.json b/package-lock.json index ac0e02b05..d32574e9d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18586,9 +18586,9 @@ "license": "MIT" }, "packages/experimental-app-router/node_modules/@next/swc-darwin-arm64": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.13.tgz", - "integrity": "sha512-ZptVhHjzUuivnXMNCJ6lER33HN7lC+rZ01z+PM10Ows21NHFYMvGhi5iXkGtBDk6VmtzsbqnAjnx4Oz5um0FjA==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-12.3.0.tgz", + "integrity": "sha512-9s4d3Mhii+WFce8o8Jok7WC3Bawkr9wEUU++SJRptjU1L5tsfYJMrSYCACHLhZujziNDLyExe4Hwwsccps1sfg==", "cpu": [ "arm64" ], @@ -18616,9 +18616,9 @@ } }, "packages/experimental-app-router/node_modules/@next/swc-linux-arm64-gnu": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.13.tgz", - "integrity": "sha512-xEHUqC8eqR5DHe8SOmMnDU1K3ggrJ28uIKltrQAwqFSSSmzjnN/XMocZkcVhuncuxYrpbri0iMQstRyRVdQVWg==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-12.3.0.tgz", + "integrity": "sha512-RqJHDKe0WImeUrdR0kayTkRWgp4vD/MS7g0r6Xuf8+ellOFH7JAAJffDW3ayuVZeMYOa7RvgNFcOoWnrTUl9Nw==", "cpu": [ "arm64" ], @@ -18631,9 +18631,9 @@ } }, "packages/experimental-app-router/node_modules/@next/swc-linux-arm64-musl": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.13.tgz", - "integrity": "sha512-sNf3MnLAm8rquSSAoeD9nVcdaDeRYOeey4stOWOyWIgbBDtP+C93amSgH/LPTDoUV7gNiU6f+ghepTjTjRgIUQ==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-12.3.0.tgz", + "integrity": "sha512-nvNWoUieMjvDjpYJ/4SQe9lQs2xMj6ZRs8N+bmTrVu9leY2Fg3WD6W9p/1uU9hGO8u+OdF13wc4iRShu/WYIHg==", "cpu": [ "arm64" ], @@ -18646,9 +18646,9 @@ } }, "packages/experimental-app-router/node_modules/@next/swc-linux-x64-gnu": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.13.tgz", - "integrity": "sha512-WhcRaJJSHyx9OWmKjjz+OWHumiPZWRqmM/09Bt7Up4UqUJFFhGExeztR4trtv3rflvULatu9IH/nTV8fUUgaMA==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-12.3.0.tgz", + "integrity": "sha512-4ajhIuVU9PeQCMMhdDgZTLrHmjbOUFuIyg6J19hZqwEwDTSqQyrSLkbJs2Nd7IRiM6Ul/XyrtEFCpk4k+xD2+w==", "cpu": [ "x64" ], @@ -18661,9 +18661,9 @@ } }, "packages/experimental-app-router/node_modules/@next/swc-linux-x64-musl": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.13.tgz", - "integrity": "sha512-+Y4LLhOWWZQIDKVwr2R17lq2KSN0F1c30QVgGIWfnjjHpH8nrIWHEndhqYU+iFuW8It78CiJjQKTw4f51HD7jA==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-12.3.0.tgz", + "integrity": "sha512-U092RBYbaGxoMAwpauePJEu2PuZSEoUCGJBvsptQr2/2XIMwAJDYM4c/M5NfYEsBr+yjvsYNsOpYfeQ88D82Yg==", "cpu": [ "x64" ], @@ -18676,9 +18676,9 @@ } }, "packages/experimental-app-router/node_modules/@next/swc-win32-arm64-msvc": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.13.tgz", - "integrity": "sha512-rWurdOR20uxjfqd1X9vDAgv0Jb26KjyL8akF9CBeFqX8rVaBAnW/Wf6A2gYEwyYY4Bai3T7p1kro6DFrsvBAAw==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-12.3.0.tgz", + "integrity": "sha512-pzSzaxjDEJe67bUok9Nxf9rykbJfHXW0owICFsPBsqHyc+cr8vpF7g9e2APTCddtVhvjkga9ILoZJ9NxWS7Yiw==", "cpu": [ "arm64" ], @@ -18691,9 +18691,9 @@ } }, "packages/experimental-app-router/node_modules/@next/swc-win32-ia32-msvc": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.13.tgz", - "integrity": "sha512-E8bSPwRuY5ibJ3CzLQmJEt8qaWrPYuUTwnrwygPUEWoLzD5YRx9SD37oXRdU81TgGwDzCxpl7z5Nqlfk50xAog==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-12.3.0.tgz", + "integrity": "sha512-MQGUpMbYhQmTZ06a9e0hPQJnxFMwETo2WtyAotY3GEzbNCQVbCGhsvqEKcl+ZEHgShlHXUWvSffq1ZscY6gK7A==", "cpu": [ "ia32" ], @@ -18706,9 +18706,9 @@ } }, "packages/experimental-app-router/node_modules/@next/swc-win32-x64-msvc": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.13.tgz", - "integrity": "sha512-4KlyC6jWRubPnppgfYsNTPeWfGCxtWLh5vaOAW/kdzAk9widqho8Qb5S4K2vHmal1tsURi7Onk2MMCV1phvyqA==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-12.3.0.tgz", + "integrity": "sha512-C/nw6OgQpEULWqs+wgMHXGvlJLguPRFFGqR2TAqWBerQ8J+Sg3z1ZTqwelkSi4FoqStGuZ2UdFHIDN1ySmR1xA==", "cpu": [ "x64" ], @@ -20575,6 +20575,66 @@ "plugins/faustwp": { "name": "@faustwp/wordpress-plugin", "version": "1.0.3" + }, + "packages/experimental-app-router/node_modules/@next/swc-android-arm-eabi": { + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-android-arm-eabi/-/swc-android-arm-eabi-12.3.0.tgz", + "integrity": "sha512-/PuirPnAKsYBw93w/7Q9hqy+KGOU9mjYprZ/faxMUJh/dc6v3rYLxkZKNG9nFPIW4QKNTCnhP40xF9hLnxO+xg==", + "cpu": [ + "arm" + ], + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 10" + } + }, + "packages/experimental-app-router/node_modules/@next/swc-android-arm64": { + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-android-arm64/-/swc-android-arm64-12.3.0.tgz", + "integrity": "sha512-OaI+FhAM6P9B6Ybwbn0Zl8YwWido0lLwhDBi9WiYCh4RQmIXAyVIoIJPHo4fP05+mXaJ/k1trvDvuURvHOq2qw==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 10" + } + }, + "packages/experimental-app-router/node_modules/@next/swc-freebsd-x64": { + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-freebsd-x64/-/swc-freebsd-x64-12.3.0.tgz", + "integrity": "sha512-xAlruUREij/bFa+qsE1tmsP28t7vz02N4ZDHt2lh3uJUniE0Ne9idyIDLc1Ed0IF2RjfgOp4ZVunuS3OM0sngw==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">= 10" + } + }, + "packages/experimental-app-router/node_modules/@next/swc-linux-arm-gnueabihf": { + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm-gnueabihf/-/swc-linux-arm-gnueabihf-12.3.0.tgz", + "integrity": "sha512-jin2S4VT/cugc2dSZEUIabhYDJNgrUh7fufbdsaAezgcQzqfdfJqfxl4E9GuafzB4cbRPTaqA0V5uqbp0IyGkQ==", + "cpu": [ + "arm" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10" + } } }, "dependencies": { @@ -34078,52 +34138,76 @@ "zod": { "version": "3.21.4" }, + "@next/swc-android-arm-eabi": { + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-android-arm-eabi/-/swc-android-arm-eabi-12.3.0.tgz", + "integrity": "sha512-/PuirPnAKsYBw93w/7Q9hqy+KGOU9mjYprZ/faxMUJh/dc6v3rYLxkZKNG9nFPIW4QKNTCnhP40xF9hLnxO+xg==", + "optional": true + }, + "@next/swc-android-arm64": { + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-android-arm64/-/swc-android-arm64-12.3.0.tgz", + "integrity": "sha512-OaI+FhAM6P9B6Ybwbn0Zl8YwWido0lLwhDBi9WiYCh4RQmIXAyVIoIJPHo4fP05+mXaJ/k1trvDvuURvHOq2qw==", + "optional": true + }, "@next/swc-darwin-arm64": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-13.4.13.tgz", - "integrity": "sha512-ZptVhHjzUuivnXMNCJ6lER33HN7lC+rZ01z+PM10Ows21NHFYMvGhi5iXkGtBDk6VmtzsbqnAjnx4Oz5um0FjA==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-darwin-arm64/-/swc-darwin-arm64-12.3.0.tgz", + "integrity": "sha512-9s4d3Mhii+WFce8o8Jok7WC3Bawkr9wEUU++SJRptjU1L5tsfYJMrSYCACHLhZujziNDLyExe4Hwwsccps1sfg==", + "optional": true + }, + "@next/swc-freebsd-x64": { + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-freebsd-x64/-/swc-freebsd-x64-12.3.0.tgz", + "integrity": "sha512-xAlruUREij/bFa+qsE1tmsP28t7vz02N4ZDHt2lh3uJUniE0Ne9idyIDLc1Ed0IF2RjfgOp4ZVunuS3OM0sngw==", + "optional": true + }, + "@next/swc-linux-arm-gnueabihf": { + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm-gnueabihf/-/swc-linux-arm-gnueabihf-12.3.0.tgz", + "integrity": "sha512-jin2S4VT/cugc2dSZEUIabhYDJNgrUh7fufbdsaAezgcQzqfdfJqfxl4E9GuafzB4cbRPTaqA0V5uqbp0IyGkQ==", "optional": true }, "@next/swc-linux-arm64-gnu": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-13.4.13.tgz", - "integrity": "sha512-xEHUqC8eqR5DHe8SOmMnDU1K3ggrJ28uIKltrQAwqFSSSmzjnN/XMocZkcVhuncuxYrpbri0iMQstRyRVdQVWg==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-gnu/-/swc-linux-arm64-gnu-12.3.0.tgz", + "integrity": "sha512-RqJHDKe0WImeUrdR0kayTkRWgp4vD/MS7g0r6Xuf8+ellOFH7JAAJffDW3ayuVZeMYOa7RvgNFcOoWnrTUl9Nw==", "optional": true }, "@next/swc-linux-arm64-musl": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-13.4.13.tgz", - "integrity": "sha512-sNf3MnLAm8rquSSAoeD9nVcdaDeRYOeey4stOWOyWIgbBDtP+C93amSgH/LPTDoUV7gNiU6f+ghepTjTjRgIUQ==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-linux-arm64-musl/-/swc-linux-arm64-musl-12.3.0.tgz", + "integrity": "sha512-nvNWoUieMjvDjpYJ/4SQe9lQs2xMj6ZRs8N+bmTrVu9leY2Fg3WD6W9p/1uU9hGO8u+OdF13wc4iRShu/WYIHg==", "optional": true }, "@next/swc-linux-x64-gnu": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-13.4.13.tgz", - "integrity": "sha512-WhcRaJJSHyx9OWmKjjz+OWHumiPZWRqmM/09Bt7Up4UqUJFFhGExeztR4trtv3rflvULatu9IH/nTV8fUUgaMA==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-gnu/-/swc-linux-x64-gnu-12.3.0.tgz", + "integrity": "sha512-4ajhIuVU9PeQCMMhdDgZTLrHmjbOUFuIyg6J19hZqwEwDTSqQyrSLkbJs2Nd7IRiM6Ul/XyrtEFCpk4k+xD2+w==", "optional": true }, "@next/swc-linux-x64-musl": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-13.4.13.tgz", - "integrity": "sha512-+Y4LLhOWWZQIDKVwr2R17lq2KSN0F1c30QVgGIWfnjjHpH8nrIWHEndhqYU+iFuW8It78CiJjQKTw4f51HD7jA==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-linux-x64-musl/-/swc-linux-x64-musl-12.3.0.tgz", + "integrity": "sha512-U092RBYbaGxoMAwpauePJEu2PuZSEoUCGJBvsptQr2/2XIMwAJDYM4c/M5NfYEsBr+yjvsYNsOpYfeQ88D82Yg==", "optional": true }, "@next/swc-win32-arm64-msvc": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-13.4.13.tgz", - "integrity": "sha512-rWurdOR20uxjfqd1X9vDAgv0Jb26KjyL8akF9CBeFqX8rVaBAnW/Wf6A2gYEwyYY4Bai3T7p1kro6DFrsvBAAw==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-win32-arm64-msvc/-/swc-win32-arm64-msvc-12.3.0.tgz", + "integrity": "sha512-pzSzaxjDEJe67bUok9Nxf9rykbJfHXW0owICFsPBsqHyc+cr8vpF7g9e2APTCddtVhvjkga9ILoZJ9NxWS7Yiw==", "optional": true }, "@next/swc-win32-ia32-msvc": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-13.4.13.tgz", - "integrity": "sha512-E8bSPwRuY5ibJ3CzLQmJEt8qaWrPYuUTwnrwygPUEWoLzD5YRx9SD37oXRdU81TgGwDzCxpl7z5Nqlfk50xAog==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-win32-ia32-msvc/-/swc-win32-ia32-msvc-12.3.0.tgz", + "integrity": "sha512-MQGUpMbYhQmTZ06a9e0hPQJnxFMwETo2WtyAotY3GEzbNCQVbCGhsvqEKcl+ZEHgShlHXUWvSffq1ZscY6gK7A==", "optional": true }, "@next/swc-win32-x64-msvc": { - "version": "13.4.13", - "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-13.4.13.tgz", - "integrity": "sha512-4KlyC6jWRubPnppgfYsNTPeWfGCxtWLh5vaOAW/kdzAk9widqho8Qb5S4K2vHmal1tsURi7Onk2MMCV1phvyqA==", + "version": "12.3.0", + "resolved": "https://registry.npmjs.org/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-12.3.0.tgz", + "integrity": "sha512-C/nw6OgQpEULWqs+wgMHXGvlJLguPRFFGqR2TAqWBerQ8J+Sg3z1ZTqwelkSi4FoqStGuZ2UdFHIDN1ySmR1xA==", "optional": true } } diff --git a/packages/faustwp-core/src/components/Toolbar/nodes/Edit.tsx b/packages/faustwp-core/src/components/Toolbar/nodes/Edit.tsx index 145c17d24..002022fcb 100644 --- a/packages/faustwp-core/src/components/Toolbar/nodes/Edit.tsx +++ b/packages/faustwp-core/src/components/Toolbar/nodes/Edit.tsx @@ -1,4 +1,5 @@ import React from 'react'; +import { useRouter } from 'next/router.js'; import { SeedNode } from '../../../queries/seedQuery.js'; import { getAdminUrl } from '../../../lib/getAdminUrl.js'; import { ToolbarItem } from '../index.js'; @@ -8,17 +9,22 @@ type Props = { }; export function Edit({ seedNode }: Props) { - if ( - seedNode === undefined || - seedNode?.isFrontPage || - seedNode?.isPostsPage - ) { + const { + query: { p, typeName }, + } = useRouter(); + + if (seedNode?.isFrontPage || seedNode?.isPostsPage) { return null; } // eslint-disable-next-line no-underscore-dangle - const postType = seedNode?.__typename || ''; - const postId = seedNode?.databaseId || ''; + // eslint-disable-next-line no-underscore-dangle + const postType = seedNode?.__typename ?? (typeName as string | undefined); + const postId = seedNode?.databaseId ?? (p as string | undefined); + + if (!postId || !postType) { + return null; + } const editPostUrl = getAdminUrl(`post.php?post=${postId}&action=edit`); diff --git a/packages/faustwp-core/tests/components/Toolbar/Toolbar.test.tsx b/packages/faustwp-core/tests/components/Toolbar/Toolbar.test.tsx index d289967cb..724f9760b 100644 --- a/packages/faustwp-core/tests/components/Toolbar/Toolbar.test.tsx +++ b/packages/faustwp-core/tests/components/Toolbar/Toolbar.test.tsx @@ -7,7 +7,7 @@ import { Hooks } from '@wordpress/hooks/build-types'; import { FaustToolbarNodes } from '../../../src'; import { setConfig } from '../../../src/config/index'; import * as apollo from '@apollo/client'; - +import * as nextRouter from 'next/router'; import { Toolbar } from '../../../src/components/Toolbar'; import { OperationVariables, QueryResult } from '@apollo/client'; @@ -47,6 +47,10 @@ test('renders the toolbar if user preference is true', async () => { .spyOn(apollo, 'useQuery') .mockReturnValue(mockUseQuery); + const useRouterSpy = jest.spyOn(nextRouter, 'useRouter').mockReturnValue({ + query: {}, + } as any as nextRouter.NextRouter); + const dom = render(); const navElement = await waitFor(() => @@ -77,6 +81,7 @@ test('doesnt render the toolbar if user preference is false', async () => { const navElement = await waitFor(() => queryByAttribute('id', dom.container, 'wpadminbar'), ); + expect(navElement).not.toBeInTheDocument(); }); @@ -96,6 +101,10 @@ test('render the toolbar if user preference request throws an error/fails', asyn .spyOn(apollo, 'useQuery') .mockReturnValue(mockUseQuery); + const useRouterSpy = jest.spyOn(nextRouter, 'useRouter').mockReturnValue({ + query: {}, + } as any as nextRouter.NextRouter); + const dom = render(); const navElement = await waitFor(() => @@ -108,6 +117,11 @@ test('render the toolbar if user preference request throws an error/fails', asyn test('renders a default list of nodes in the primary section if seedNode is not provided', async () => { expect.assertions(2); mockIsAuthenticated = true; + + const useRouterSpy = jest.spyOn(nextRouter, 'useRouter').mockReturnValue({ + query: {}, + } as any as nextRouter.NextRouter); + const dom = render(); await waitFor(() => queryByAttribute('id', dom.container, 'wpadminbar')); const toolBars = screen.getAllByRole('list', { name: /toolbar/i }); @@ -128,9 +142,15 @@ test('renders a default list of nodes in the primary section if seedNode is not test('renders an Edit Post Node, in the primary section if seedNode is provided', async () => { expect.assertions(2); mockIsAuthenticated = true; + + const useRouterSpy = jest.spyOn(nextRouter, 'useRouter').mockReturnValue({ + query: {}, + } as any as nextRouter.NextRouter); + const dom = render( - , + , ); + await waitFor(() => queryByAttribute('id', dom.container, 'wpadminbar')); const toolBars = screen.getAllByRole('list', { name: /toolbar/i }); // Primary Toolbar @@ -151,6 +171,11 @@ test('renders an Account Node in the secondary section', async () => { expect.assertions(2); mockIsAuthenticated = true; mockIsReady = true; + + const useRouterSpy = jest.spyOn(nextRouter, 'useRouter').mockReturnValue({ + query: {}, + } as any as nextRouter.NextRouter); + const dom = render( , ); @@ -171,6 +196,67 @@ test('renders an Account Node in the secondary section', async () => { ); }); +test('renders an Edit Post Node, if seedNode is not provided and is preview', async () => { + expect.assertions(2); + mockIsAuthenticated = true; + + const useRouterSpy = jest.spyOn(nextRouter, 'useRouter').mockReturnValue({ + query: { + p: '123', + typeName: 'Post', + preview: true, + }, + } as any as nextRouter.NextRouter); + + const dom = render( + , + ); + + await waitFor(() => queryByAttribute('id', dom.container, 'wpadminbar')); + const toolBars = screen.getAllByRole('list', { name: /toolbar/i }); + // Primary Toolbar + testToolBarNode( + toolBars[0], + 3, + ` + Array [ + "WordPress", + "Edit Post", + "GraphiQL IDE", + ] + `, + ); +}); + +test('does not render an Edit Post Node, if there is no seedNode and it is not a preview', async () => { + expect.assertions(2); + mockIsAuthenticated = true; + + const useRouterSpy = jest.spyOn(nextRouter, 'useRouter').mockReturnValue({ + query: { + }, + } as any as nextRouter.NextRouter); + + const dom = render( + , + ); + + await waitFor(() => queryByAttribute('id', dom.container, 'wpadminbar')); + const toolBars = screen.getAllByRole('list', { name: /toolbar/i }); + // Primary Toolbar + testToolBarNode( + toolBars[0], + 3, + ` + Array [ + "WordPress", + "", + "GraphiQL IDE", + ] +`, + ); +}); + test('Uses `toolbarNodes` hook to add nodes', async () => { setConfig({ // @ts-ignore @@ -180,9 +266,15 @@ test('Uses `toolbarNodes` hook to add nodes', async () => { }); expect.assertions(2); mockIsAuthenticated = true; + + const useRouterSpy = jest.spyOn(nextRouter, 'useRouter').mockReturnValue({ + query: { + }, + } as any as nextRouter.NextRouter); + mockIsReady = true; const dom = render( - , + , ); await waitFor(() => queryByAttribute('id', dom.container, 'wpadminbar')); const toolBars = screen.getAllByRole('list', { name: /toolbar/i });