(frontend) assert DocToolBox depends the licence

Thanks to Vitest we can now assert more complicated
parts of the code without too much mocking.
This commit is contained in:
Anthony LC
2025-08-28 11:20:12 +02:00
parent 1eee24dc19
commit 64f967cd29
5 changed files with 92 additions and 27 deletions

View File

@@ -1,13 +1,6 @@
import { afterAll, afterEach, describe, expect, it, vi } from 'vitest';
const originalEnv = process.env.NEXT_PUBLIC_PUBLISH_AS_MIT;
vi.mock('@/features/docs/doc-export/utils', () => ({
anything: true,
}));
vi.mock('@/features/docs/doc-export/components/ModalExport', () => ({
ModalExport: () => <span>ModalExport</span>,
}));
describe('useModuleExport', () => {
afterAll(() => {
process.env.NEXT_PUBLIC_PUBLISH_AS_MIT = originalEnv;
@@ -23,7 +16,7 @@ describe('useModuleExport', () => {
const Export = await import('@/features/docs/doc-export/');
expect(Export.default).toBeUndefined();
});
}, 10000);
it('should load modules when NEXT_PUBLIC_PUBLISH_AS_MIT is false', async () => {
process.env.NEXT_PUBLIC_PUBLISH_AS_MIT = 'false';

View File

@@ -0,0 +1,69 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { afterAll, beforeEach, describe, expect, vi } from 'vitest';
import { AppWrapper } from '@/tests/utils';
const originalEnv = process.env.NEXT_PUBLIC_PUBLISH_AS_MIT;
vi.mock('next/router', async () => ({
...(await vi.importActual('next/router')),
useRouter: () => ({
push: vi.fn(),
}),
}));
const doc = {
nb_accesses: 1,
abilities: {
versions_list: true,
destroy: true,
},
};
describe('DocToolBox - Licence', () => {
afterAll(() => {
process.env.NEXT_PUBLIC_PUBLISH_AS_MIT = originalEnv;
});
beforeEach(() => {
vi.clearAllMocks();
vi.resetModules();
});
test('The export button is rendered when MIT version is deactivated', async () => {
process.env.NEXT_PUBLIC_PUBLISH_AS_MIT = 'false';
const { DocToolBox } = await import('../components/DocToolBox');
render(<DocToolBox doc={doc as any} />, {
wrapper: AppWrapper,
});
const optionsButton = await screen.findByLabelText('Export the document');
await userEvent.click(optionsButton);
expect(
await screen.findByText(
'Download your document in a .docx or .pdf format.',
),
).toBeInTheDocument();
}, 10000);
test('The export button is not rendered when MIT version is activated', async () => {
process.env.NEXT_PUBLIC_PUBLISH_AS_MIT = 'true';
const { DocToolBox } = await import('../components/DocToolBox');
render(<DocToolBox doc={doc as any} />, {
wrapper: AppWrapper,
});
expect(
screen.getByLabelText('Open the document options'),
).toBeInTheDocument();
expect(
screen.queryByLabelText('Export the document'),
).not.toBeInTheDocument();
});
});

View File

@@ -241,6 +241,7 @@ export const DocToolBox = ({ doc }: DocToolBoxProps) => {
setIsModalExportOpen(true);
}}
size={isSmallMobile ? 'small' : 'medium'}
aria-label={t('Export the document')}
/>
)}
<DropdownMenu options={options}>