(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

@@ -25,7 +25,7 @@ test.describe('Doc Export', () => {
await createDoc(page, 'doc-editor', browserName, 1); await createDoc(page, 'doc-editor', browserName, 1);
await page await page
.getByRole('button', { .getByRole('button', {
name: 'download', name: 'Export the document',
}) })
.click(); .click();
@@ -78,8 +78,7 @@ test.describe('Doc Export', () => {
await page await page
.getByRole('button', { .getByRole('button', {
name: 'download', name: 'Export the document',
exact: true,
}) })
.click(); .click();
@@ -130,8 +129,7 @@ test.describe('Doc Export', () => {
await page await page
.getByRole('button', { .getByRole('button', {
name: 'download', name: 'Export the document',
exact: true,
}) })
.click(); .click();
@@ -200,7 +198,7 @@ test.describe('Doc Export', () => {
await page await page
.getByRole('button', { .getByRole('button', {
name: 'download', name: 'Export the document',
}) })
.click(); .click();
@@ -277,7 +275,7 @@ test.describe('Doc Export', () => {
await page await page
.getByRole('button', { .getByRole('button', {
name: 'download', name: 'Export the document',
}) })
.click(); .click();
@@ -327,8 +325,7 @@ test.describe('Doc Export', () => {
await page await page
.getByRole('button', { .getByRole('button', {
name: 'download', name: 'Export the document',
exact: true,
}) })
.click(); .click();
@@ -390,8 +387,7 @@ test.describe('Doc Export', () => {
await page await page
.getByRole('button', { .getByRole('button', {
name: 'download', name: 'Export the document',
exact: true,
}) })
.click(); .click();
@@ -465,8 +461,7 @@ test.describe('Doc Export', () => {
await page await page
.getByRole('button', { .getByRole('button', {
name: 'download', name: 'Export the document',
exact: true,
}) })
.click(); .click();
@@ -537,8 +532,7 @@ test.describe('Doc Export', () => {
await page await page
.getByRole('button', { .getByRole('button', {
name: 'download', name: 'Export the document',
exact: true,
}) })
.click(); .click();

View File

@@ -44,7 +44,9 @@ test.describe('Doc Header', () => {
await expect(card.getByText('Owner ·')).toBeVisible(); await expect(card.getByText('Owner ·')).toBeVisible();
await expect(page.getByRole('button', { name: 'Share' })).toBeVisible(); await expect(page.getByRole('button', { name: 'Share' })).toBeVisible();
await expect(page.getByRole('button', { name: 'download' })).toBeVisible(); await expect(
page.getByRole('button', { name: 'Export the document' }),
).toBeVisible();
await expect( await expect(
page.getByRole('button', { name: 'Open the document options' }), page.getByRole('button', { name: 'Open the document options' }),
).toBeVisible(); ).toBeVisible();
@@ -115,7 +117,9 @@ test.describe('Doc Header', () => {
await goToGridDoc(page); await goToGridDoc(page);
await expect(page.getByRole('button', { name: 'download' })).toBeVisible(); await expect(
page.getByRole('button', { name: 'Export the document' }),
).toBeVisible();
await page.getByLabel('Open the document options').click(); await page.getByLabel('Open the document options').click();
@@ -185,7 +189,9 @@ test.describe('Doc Header', () => {
await goToGridDoc(page); await goToGridDoc(page);
await expect(page.getByRole('button', { name: 'download' })).toBeVisible(); await expect(
page.getByRole('button', { name: 'Export the document' }),
).toBeVisible();
await page.getByLabel('Open the document options').click(); await page.getByLabel('Open the document options').click();
await expect(page.getByLabel('Delete document')).toBeDisabled(); await expect(page.getByLabel('Delete document')).toBeDisabled();
@@ -245,7 +251,9 @@ test.describe('Doc Header', () => {
await goToGridDoc(page); await goToGridDoc(page);
await expect(page.getByRole('button', { name: 'download' })).toBeVisible(); await expect(
page.getByRole('button', { name: 'Export the document' }),
).toBeVisible();
await page.getByLabel('Open the document options').click(); await page.getByLabel('Open the document options').click();
await expect(page.getByLabel('Delete document')).toBeDisabled(); await expect(page.getByLabel('Delete document')).toBeDisabled();

View File

@@ -1,13 +1,6 @@
import { afterAll, afterEach, describe, expect, it, vi } from 'vitest'; import { afterAll, afterEach, describe, expect, it, vi } from 'vitest';
const originalEnv = process.env.NEXT_PUBLIC_PUBLISH_AS_MIT; 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', () => { describe('useModuleExport', () => {
afterAll(() => { afterAll(() => {
process.env.NEXT_PUBLIC_PUBLISH_AS_MIT = originalEnv; process.env.NEXT_PUBLIC_PUBLISH_AS_MIT = originalEnv;
@@ -23,7 +16,7 @@ describe('useModuleExport', () => {
const Export = await import('@/features/docs/doc-export/'); const Export = await import('@/features/docs/doc-export/');
expect(Export.default).toBeUndefined(); expect(Export.default).toBeUndefined();
}); }, 10000);
it('should load modules when NEXT_PUBLIC_PUBLISH_AS_MIT is false', async () => { it('should load modules when NEXT_PUBLIC_PUBLISH_AS_MIT is false', async () => {
process.env.NEXT_PUBLIC_PUBLISH_AS_MIT = 'false'; 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); setIsModalExportOpen(true);
}} }}
size={isSmallMobile ? 'small' : 'medium'} size={isSmallMobile ? 'small' : 'medium'}
aria-label={t('Export the document')}
/> />
)} )}
<DropdownMenu options={options}> <DropdownMenu options={options}>