Skip to content

Commit da78cc8

Browse files
committed
♻️(frontend) Simplify AGPL export pattern
We were maintaining two separate components for AGPL and MIT license exports. This commit consolidates the functionality into a single component that handles both licenses, simplifying the codebase and reducing duplication.
1 parent a99c813 commit da78cc8

File tree

11 files changed

+260
-441
lines changed

11 files changed

+260
-441
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { renderHook, waitFor } from '@testing-library/react';
2+
3+
import { AppWrapper } from '@/tests/utils';
4+
5+
// Mock the environment variable
6+
const originalEnv = process.env.NEXT_PUBLIC_PUBLISH_AS_MIT;
7+
8+
// Mock the libAGPL module
9+
jest.mock(
10+
'../../libAGPL',
11+
() => ({
12+
exportToPdf: jest.fn(),
13+
exportToDocx: jest.fn(),
14+
}),
15+
{ virtual: true },
16+
);
17+
18+
describe('useModuleExport', () => {
19+
afterAll(() => {
20+
process.env.NEXT_PUBLIC_PUBLISH_AS_MIT = originalEnv;
21+
});
22+
23+
it('should load modules when NEXT_PUBLIC_PUBLISH_AS_MIT is false', async () => {
24+
process.env.NEXT_PUBLIC_PUBLISH_AS_MIT = 'false';
25+
const { useModuleExport } = await import('../useModuleExport');
26+
27+
const { result } = renderHook(() => useModuleExport(), {
28+
wrapper: AppWrapper,
29+
});
30+
31+
// Initial state should be undefined
32+
expect(result.current).toBeUndefined();
33+
34+
// After effects run, it should contain the modules from libAGPL
35+
await waitFor(() => {
36+
expect(result.current).toBeDefined();
37+
});
38+
39+
expect(result.current).toHaveProperty('exportToPdf');
40+
expect(result.current).toHaveProperty('exportToDocx');
41+
});
42+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { renderHook } from '@testing-library/react';
2+
3+
import { AppWrapper } from '@/tests/utils';
4+
import { sleep } from '@/utils';
5+
6+
const originalEnv = process.env.NEXT_PUBLIC_PUBLISH_AS_MIT;
7+
8+
describe('useModuleExport', () => {
9+
afterAll(() => {
10+
process.env.NEXT_PUBLIC_PUBLISH_AS_MIT = originalEnv;
11+
});
12+
13+
it('should return null when NEXT_PUBLIC_PUBLISH_AS_MIT is true', async () => {
14+
const { useModuleExport } = await import('../useModuleExport');
15+
16+
const { result } = renderHook(() => useModuleExport(), {
17+
wrapper: AppWrapper,
18+
});
19+
20+
expect(result.current).toBeUndefined();
21+
22+
await sleep(1000);
23+
24+
expect(result.current).toBeUndefined();
25+
});
26+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useEffect, useState } from 'react';
2+
3+
const modulesAGPL =
4+
process.env.NEXT_PUBLIC_PUBLISH_AS_MIT === 'false'
5+
? import('../libAGPL')
6+
: Promise.resolve(null);
7+
8+
export const useModuleExport = () => {
9+
const [modules, setModules] = useState<Awaited<typeof modulesAGPL>>();
10+
11+
useEffect(() => {
12+
const resolveModule = async () => {
13+
const resolvedModules = await modulesAGPL;
14+
if (!resolvedModules) {
15+
return;
16+
}
17+
setModules(resolvedModules);
18+
};
19+
void resolveModule();
20+
}, []);
21+
22+
return modules;
23+
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './api';
2-
export * from './components';
2+
export * from './hooks/useModuleExport';
33
export * from './utils';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { ModalExport } from './components';

src/frontend/apps/impress/src/features/docs/doc-header/__tests__/DocToolBox.spec.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class TestAnalytic extends AbstractAnalytic {
3030

3131
jest.mock('@/features/docs/doc-export/', () => ({
3232
ModalExport: () => <span>ModalExport</span>,
33+
useModuleExport: () => ({
34+
exportToPdf: jest.fn(),
35+
exportToDocx: jest.fn(),
36+
}),
3337
}));
3438

3539
const doc = {

src/frontend/apps/impress/src/features/docs/doc-header/__tests__/DocToolBoxAGPL.spec.tsx

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/frontend/apps/impress/src/features/docs/doc-header/__tests__/DocToolBoxMIT.spec.tsx

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)