Skip to content

Commit

Permalink
test: imporve folder persist assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Mar 11, 2024
1 parent 7cdf7a7 commit 2f3aac5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../../../../../src/context/virtual-drive/folders/domain/file-systems/RemoteFileSystem';

export class FolderRemoteFileSystemMock implements RemoteFileSystem {
public readonly persistMock = jest.fn();
private readonly persistMock = jest.fn();
public readonly trashMock = jest.fn();
public readonly moveMock = jest.fn();
public readonly renameMock = jest.fn();
Expand All @@ -18,16 +18,36 @@ export class FolderRemoteFileSystemMock implements RemoteFileSystem {
parentId: FolderId,
uuid?: FolderUuid | undefined
): Promise<FolderPersistedDto> {
return this.persistMock(path, parentId, uuid);
expect(this.persistMock).toHaveBeenCalledWith(path, parentId, uuid);

return this.persistMock();
}

trash(id: number): Promise<void> {
return this.trashMock(id);
}

move(folder: Folder): Promise<void> {
return this.moveMock(folder);
}

rename(folder: Folder): Promise<void> {
return this.renameMock(folder);
}

shouldPersists(folder: Folder, includeUuid: boolean) {
this.persistMock(
new FolderPath(folder.path),
new FolderId(folder.parentId as number),
includeUuid ? new FolderUuid(folder.uuid) : undefined
);

this.persistMock.mockResolvedValueOnce({
id: folder.id,
uuid: folder.uuid,
createdAt: folder.createdAt.toISOString(),
updatedAt: folder.updatedAt.toISOString(),
parentId: folder.parentId as number,
} satisfies FolderPersistedDto);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { InvalidArgumentError } from '../../../../../src/context/shared/domain/errors/InvalidArgumentError';
import { FolderCreator } from '../../../../../src/context/virtual-drive/folders/application/FolderCreator';
import { ParentFolderFinder } from '../../../../../src/context/virtual-drive/folders/application/ParentFolderFinder';
import { Folder } from '../../../../../src/context/virtual-drive/folders/domain/Folder';
import { FolderId } from '../../../../../src/context/virtual-drive/folders/domain/FolderId';
import { FolderStatuses } from '../../../../../src/context/virtual-drive/folders/domain/FolderStatus';
import { FolderInPathAlreadyExistsError } from '../../../../../src/context/virtual-drive/folders/domain/errors/FolderInPathAlreadyExistsError';
import { FolderNotFoundError } from '../../../../../src/context/virtual-drive/folders/domain/errors/FolderNotFoundError';
import { FolderPersistedDto } from '../../../../../src/context/virtual-drive/folders/domain/file-systems/RemoteFileSystem';
import { EventBusMock } from '../../shared/__mock__/EventBusMock';
import { FolderRemoteFileSystemMock } from '../__mocks__/FolderRemoteFileSystemMock';
import { FolderRepositoryMock } from '../__mocks__/FolderRepositoryMock';
import { FolderMother } from '../domain/FolderMother';
import { FolderPathMother } from '../domain/FolderPathMother';

const WITH_NO_UUID = false;

describe('Folder Creator', () => {
let repository: FolderRepositoryMock;
let remote: FolderRemoteFileSystemMock;
Expand All @@ -30,16 +29,6 @@ describe('Folder Creator', () => {
SUT = new FolderCreator(repository, parentFolderFinder, remote, eventBus);
});

const mockCorrectPersistance = (folder: Folder) => {
remote.persistMock.mockResolvedValueOnce({
id: folder.id,
uuid: folder.uuid,
createdAt: folder.createdAt.toISOString(),
updatedAt: folder.updatedAt.toISOString(),
parentId: folder.parentId as number,
} satisfies FolderPersistedDto);
};

it('throws an InvalidArgument error if the path is not a valid posix path', async () => {
const nonPosixPath = 'C:\\Users\\Internxt';

Expand Down Expand Up @@ -86,35 +75,29 @@ describe('Folder Creator', () => {
it('persists a folder in the remote fs when the parent folder is found and the path is available', async () => {
const path = FolderPathMother.any();
const parent = FolderMother.fromPartial({ path: path.dirname() });
const folderCreated = FolderMother.fromPartial({
const createdFolder = FolderMother.fromPartial({
path: path.value,
parentId: parent.id,
});

mockCorrectPersistance(folderCreated);
remote.shouldPersists(createdFolder, WITH_NO_UUID);

repository.matchingPartialMock
.mockReturnValueOnce([])
.mockReturnValueOnce([parent]);

await SUT.run(path.value);

expect(remote.persistMock).toBeCalledWith(
path,
new FolderId(parent.id),
undefined // optional parameter
);
});

it('add the folder to the repository', async () => {
const path = FolderPathMother.any();
const parent = FolderMother.fromPartial({ path: path.dirname() });
const folderCreated = FolderMother.fromPartial({
const createdFolder = FolderMother.fromPartial({
path: path.value,
parentId: parent.id,
});

mockCorrectPersistance(folderCreated);
remote.shouldPersists(createdFolder, WITH_NO_UUID);

repository.matchingPartialMock
.mockReturnValueOnce([])
Expand All @@ -123,19 +106,19 @@ describe('Folder Creator', () => {
await SUT.run(path.value);

expect(repository.addMock).toBeCalledWith(
expect.objectContaining(folderCreated)
expect.objectContaining(createdFolder)
);
});

it('publishes folder created event', async () => {
const path = FolderPathMother.any();
const parent = FolderMother.fromPartial({ path: path.dirname() });
const folderCreated = FolderMother.fromPartial({
const createdFolder = FolderMother.fromPartial({
path: path.value,
parentId: parent.id,
});

mockCorrectPersistance(folderCreated);
remote.shouldPersists(createdFolder, WITH_NO_UUID);

repository.matchingPartialMock
.mockReturnValueOnce([])
Expand All @@ -145,7 +128,7 @@ describe('Folder Creator', () => {

expect(eventBus.publishMock).toBeCalledWith(
expect.arrayContaining([
expect.objectContaining({ aggregateId: folderCreated.uuid }),
expect.objectContaining({ aggregateId: createdFolder.uuid }),
])
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { SyncFolderMessengerMock } from '../__mocks__/SyncFolderMessengerMock';
import { FolderMother } from '../domain/FolderMother';
import { OfflineFolderMother } from '../domain/OfflineFolderMother';

const WITH_UUID = true;

describe('Folder Creator from Offline Folder', () => {
let SUT: FolderCreatorFromOfflineFolder;

Expand Down Expand Up @@ -33,7 +35,7 @@ describe('Folder Creator from Offline Folder', () => {
const offlineFolder = OfflineFolderMother.random();
const folder = FolderMother.fromPartial(offlineFolder.attributes());

remote.persistMock.mockResolvedValueOnce(folder.attributes());
remote.shouldPersists(folder, WITH_UUID);

repository.addMock.mockResolvedValueOnce(Promise.resolve());

Expand All @@ -46,11 +48,12 @@ describe('Folder Creator from Offline Folder', () => {
it('sends the message FOLDER_CREATING', async () => {
const offlineFolder = OfflineFolderMother.random();

const resultFolderAttributes = FolderMother.fromPartial(
const expectedFolder = FolderMother.fromPartial(
offlineFolder.attributes()
).attributes();
);

remote.persistMock.mockResolvedValueOnce(resultFolderAttributes);
remote.shouldPersists(expectedFolder, WITH_UUID);
// remote.persistMock.mockResolvedValueOnce(resultFolderAttributes);

repository.addMock.mockImplementationOnce(() => {
// no-op
Expand All @@ -64,15 +67,15 @@ describe('Folder Creator from Offline Folder', () => {
it('sends the message FOLDER_CREATED', async () => {
const offlineFolder = OfflineFolderMother.random();

const resultFolderAttributes = FolderMother.fromPartial(
const expectedFolder = FolderMother.fromPartial(
offlineFolder.attributes()
).attributes();
);

repository.addMock.mockImplementationOnce(() => {
// no-op
});

remote.persistMock.mockResolvedValueOnce(resultFolderAttributes);
remote.shouldPersists(expectedFolder, WITH_UUID);

await SUT.run(offlineFolder);

Expand Down

0 comments on commit 2f3aac5

Please sign in to comment.