Kaynağa Gözat

devide useViewerMinJsUrl hook

Yuki Takei 1 yıl önce
ebeveyn
işleme
f0e039d465

+ 3 - 7
apps/app/src/components/Script/DrawioViewerScript.tsx → apps/app/src/components/Script/DrawioViewerScript/DrawioViewerScript.tsx

@@ -3,7 +3,7 @@ import { useCallback } from 'react';
 import type { IGraphViewerGlobal } from '@growi/remark-drawio';
 import Head from 'next/head';
 
-import { useRendererConfig } from '~/stores/context';
+import { useViewerMinJsUrl } from './use-viewer-min-js-url';
 
 declare global {
   // eslint-disable-next-line vars-on-top, no-var
@@ -11,7 +11,7 @@ declare global {
 }
 
 export const DrawioViewerScript = (): JSX.Element => {
-  const { data: rendererConfig } = useRendererConfig();
+  const viewerMinJsSrc = useViewerMinJsUrl();
 
   const loadedHandler = useCallback(() => {
     // disable useResizeSensor and checkVisibleState
@@ -32,16 +32,12 @@ export const DrawioViewerScript = (): JSX.Element => {
     GraphViewer.processElements();
   }, []);
 
-  if (rendererConfig == null) {
-    return <></>;
-  }
-
   return (
     <Head>
       <script
         type="text/javascript"
         async
-        src={(new URL('/js/viewer.min.js', rendererConfig.drawioUri)).toString()}
+        src={viewerMinJsSrc}
         onLoad={loadedHandler}
       />
     </Head>

+ 1 - 0
apps/app/src/components/Script/DrawioViewerScript/index.ts

@@ -0,0 +1 @@
+export * from './DrawioViewerScript';

+ 35 - 0
apps/app/src/components/Script/DrawioViewerScript/use-viewer-min-js-url.spec.ts

@@ -0,0 +1,35 @@
+import { useViewerMinJsUrl } from './use-viewer-min-js-url';
+
+const mocks = vi.hoisted(() => {
+  return {
+    useRendererConfigMock: vi.fn(),
+  };
+});
+
+vi.mock('~/stores/context', () => ({
+  useRendererConfig: mocks.useRendererConfigMock,
+}));
+
+describe('useViewerMinJsUrl', () => {
+  it('should return the URL when rendererConfig is undefined', () => {
+    // Arrange
+    mocks.useRendererConfigMock.mockImplementation(() => {
+      return { data: undefined };
+    });
+
+    // Act
+    const url = useViewerMinJsUrl();
+
+    // Assert
+    expect(url).toBe('http://localhost/js/viewer.min.js');
+  });
+
+  it.each`
+    drawioUri  | expected
+    ${undefined}      | ${'http://localhost/js/viewer.min.js'}
+  `('should return the expected URL "$expected" when drawioUri is "$drawioUrk"', () => {
+    // Arrange
+    // Act
+    // Assert
+  });
+});

+ 7 - 0
apps/app/src/components/Script/DrawioViewerScript/use-viewer-min-js-url.ts

@@ -0,0 +1,7 @@
+import { useRendererConfig } from '~/stores/context';
+
+export const useViewerMinJsUrl = (): string => {
+  const { data: rendererConfig } = useRendererConfig();
+
+  return (new URL('/js/viewer.min.js', rendererConfig?.drawioUri ?? 'http://localhost')).toString();
+};