Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BC-6807 - Copy collaborative text editor #4961

Merged
merged 9 commits into from
Apr 30, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { ContextExternalToolService } from '@modules/tool/context-external-tool/service';
import { IToolFeatures, ToolFeatures } from '@modules/tool/tool-config';
import { Test, TestingModule } from '@nestjs/testing';
import { LinkElement } from '@shared/domain/domainobject';
import { CollaborativeTextEditorElement, LinkElement } from '@shared/domain/domainobject';
import {
collaborativeTextEditorElementFactory,
linkElementFactory,
Expand Down Expand Up @@ -53,6 +53,10 @@ describe(RecursiveCopyVisitor.name, () => {
await setupEntities();
});

afterEach(() => {
jest.resetAllMocks();
});

describe('visitLinkElementAsync', () => {
const setup = (options: { withFileCopy: boolean } = { withFileCopy: false }) => {
const fileCopyServiceMock = createMock<SchoolSpecificFileCopyService>();
Expand Down Expand Up @@ -182,20 +186,46 @@ describe(RecursiveCopyVisitor.name, () => {
contextExternalToolService,
toolFeatures
);
const nowMock = new Date(2020, 1, 1, 0, 0, 0);

jest.useFakeTimers();
jest.setSystemTime(nowMock);

return {
collaborativeTextEditorElement,
visitor,
nowMock,
};
};

it('should throw an error', async () => {
const { visitor, collaborativeTextEditorElement } = setup();
const error = new Error(`Cannot copy element of type: '${collaborativeTextEditorElement.constructor.name}'`);
it('should add status to the resultMap', async () => {
const { visitor, collaborativeTextEditorElement, nowMock } = setup();

await visitor.visitCollaborativeTextEditorElementAsync(collaborativeTextEditorElement);

const status = visitor.resultMap.get(collaborativeTextEditorElement.id);
const expectedCopyEntity = expect.objectContaining({
id: expect.any(String),
createdAt: nowMock,
updatedAt: nowMock,
}) as CollaborativeTextEditorElement;

expect(status).toEqual<CopyStatus>({
copyEntity: expectedCopyEntity,
type: CopyElementType.COLLABORATIVE_TEXT_EDITOR_ELEMENT,
status: CopyStatusEnum.SUCCESS,
});
});

it('should add the element to the copyMap', async () => {
const { visitor, collaborativeTextEditorElement, nowMock } = setup();

await visitor.visitCollaborativeTextEditorElementAsync(collaborativeTextEditorElement);

await expect(() =>
visitor.visitCollaborativeTextEditorElementAsync(collaborativeTextEditorElement)
).rejects.toThrow(error);
const copyMapElement = visitor.copyMap.get(collaborativeTextEditorElement.id);
expect(copyMapElement?.id).toBeDefined();
expect(copyMapElement?.createdAt).toEqual(nowMock);
expect(copyMapElement?.updatedAt).toEqual(nowMock);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,22 @@ export class RecursiveCopyVisitor implements BoardCompositeVisitorAsync {
return Promise.resolve();
}

async visitCollaborativeTextEditorElementAsync(
collaborativeTextEditorElement: CollaborativeTextEditorElement
): Promise<void> {
return Promise.reject(
new Error(`Cannot copy element of type: '${collaborativeTextEditorElement.constructor.name}'`)
);
async visitCollaborativeTextEditorElementAsync(original: CollaborativeTextEditorElement): Promise<void> {
const now = new Date();
const copy = new CollaborativeTextEditorElement({
id: new ObjectId().toHexString(),
createdAt: now,
updatedAt: now,
});

this.resultMap.set(original.id, {
copyEntity: copy,
type: CopyElementType.COLLABORATIVE_TEXT_EDITOR_ELEMENT,
status: CopyStatusEnum.SUCCESS,
});
this.copyMap.set(original.id, copy);

return Promise.resolve();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Die Zeile hat eigentlich keine Auswirkungen. Es ist eine Async Funktion und diese liefert immer ein Promise zurück. Ich habe aber gesehen, das an allen anderen Stellen es genauso gemacht wurde.

}

async visitMediaBoardAsync(original: MediaBoard): Promise<void> {
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/modules/copy-helper/types/copy.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type CopyStatus = {
export enum CopyElementType {
BOARD = 'BOARD',
CARD = 'CARD',
COLLABORATIVE_TEXT_EDITOR_ELEMENT = 'COLLABORATIVE_TEXT_EDITOR_ELEMENT',
COLUMN = 'COLUMN',
COLUMNBOARD = 'COLUMNBOARD',
CONTENT = 'CONTENT',
Expand Down
Loading