amaannawab923 commented on code in PR #45522:
URL: https://github.com/apache/superset/pull/45522#discussion_r4401950576


##########
superset-frontend/src/core/pages/navigation.test.tsx:
##########
@@ -0,0 +1,154 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * @fileoverview `useSubPath` / `useNavigate` under a subdirectory deployment.
+ *
+ * Superset mounts its router as `<Router basename={applicationRoot()}>`, so
+ * `location.pathname` arrives with the app root already removed and
+ * `history.push` has it added back. These tests run against a real `Router`
+ * carrying a basename, because that is the only way to tell a correct
+ * implementation from one that happens to work when the basename is empty.
+ */
+
+import { ReactNode } from 'react';
+import { BrowserRouter } from 'react-router-dom';
+import { logging } from '@apache-superset/core/utils';
+import { render, screen } from 'spec/helpers/testing-library';
+import { PageRootContext, useNavigate, useSubPath } from './index';
+
+const APP_ROOT = '/analytics';
+
+// The app root is read once at module evaluation, so it cannot be changed
+// per test without resetting modules -- which would hand this module a second
+// React instance and break hooks. The two helpers are mocked instead, with
+// the behaviour they have under a subdirectory deployment.
+jest.mock('src/utils/navigationUtils', () => ({
+  ...jest.requireActual('src/utils/navigationUtils'),
+  ensureAppRoot: (path: string) =>
+    path.startsWith(`${APP_ROOT}/`) || path === APP_ROOT
+      ? path
+      : `${APP_ROOT}${path}`,
+  stripAppRoot: (path: string) =>
+    path.startsWith(`${APP_ROOT}/`) ? path.slice(APP_ROOT.length) : path,
+}));
+
+const renderAt = (pathname: string, root = '/review-queue', target = '/42') => 
{
+  window.history.replaceState({}, '', `${APP_ROOT}${pathname}`);
+
+  const Probe = () => {
+    const subPath = useSubPath();
+    const navigate = useNavigate();
+    return (
+      <>
+        <span data-test="sub-path">{subPath}</span>
+        <button type="button" onClick={() => navigate(target)}>
+          go
+        </button>
+      </>
+    );
+  };
+
+  const wrapper = ({ children }: { children: ReactNode }) => (
+    <BrowserRouter basename={APP_ROOT}>
+      <PageRootContext.Provider value={root}>
+        {children}
+      </PageRootContext.Provider>
+    </BrowserRouter>
+  );
+
+  render(<Probe />, { wrapper });
+};
+
+afterEach(() => {
+  window.history.replaceState({}, '', '/');
+  jest.restoreAllMocks();
+});
+
+test('useSubPath reads the path beneath the page under an app root', () => {

Review Comment:
   Added in 26c667d, as `renderAt('/analytics/review-queue/42', 
'/analytics/review-queue')` expecting `/42`, plus the page-root case and the 
`useNavigate` counterpart, since the same ambiguity applies to the push.
   
   Checked that it earns its place the way you described it: with `useSubPath` 
stripping the app root a second time, the new case fails and all eleven 
existing ones pass.
   
   One interaction worth flagging, because it changes what the test is for. 
b65a24e now refuses a page path whose first segment is the application root, at 
registration, for a neighbouring reason -- `ensureAppRoot` cannot tell "already 
rooted" from "starts with the root segment" either, so the menu entry for such 
a page could not be built correctly. So `/analytics/review-queue` under 
`/analytics` should no longer reach these hooks in a running deployment.
   
   I kept the tests anyway rather than calling them moot. The refusal is the 
server's, and these two hooks are the client's; they should be correct on their 
own rather than by arrangement with a check somewhere else. The comment on the 
block says that, so nobody later reads it as dead weight and deletes it.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to