refactor: test mocks (#16008)

This commit is contained in:
Jason Rasmussen
2025-02-10 18:47:42 -05:00
committed by GitHub
parent 8794c84e9d
commit 735f8d661e
74 changed files with 3820 additions and 4043 deletions
+41 -46
View File
@@ -1,22 +1,17 @@
import { BadRequestException } from '@nestjs/common';
import { MemoryType } from 'src/enum';
import { MemoryService } from 'src/services/memory.service';
import { IMemoryRepository } from 'src/types';
import { authStub } from 'test/fixtures/auth.stub';
import { memoryStub } from 'test/fixtures/memory.stub';
import { userStub } from 'test/fixtures/user.stub';
import { IAccessRepositoryMock } from 'test/repositories/access.repository.mock';
import { newTestService } from 'test/utils';
import { Mocked } from 'vitest';
import { newTestService, ServiceMocks } from 'test/utils';
describe(MemoryService.name, () => {
let sut: MemoryService;
let accessMock: IAccessRepositoryMock;
let memoryMock: Mocked<IMemoryRepository>;
let mocks: ServiceMocks;
beforeEach(() => {
({ sut, accessMock, memoryMock } = newTestService(MemoryService));
({ sut, mocks } = newTestService(MemoryService));
});
it('should be defined', () => {
@@ -25,7 +20,7 @@ describe(MemoryService.name, () => {
describe('search', () => {
it('should search memories', async () => {
memoryMock.search.mockResolvedValue([memoryStub.memory1, memoryStub.empty]);
mocks.memory.search.mockResolvedValue([memoryStub.memory1, memoryStub.empty]);
await expect(sut.search(authStub.admin)).resolves.toEqual(
expect.arrayContaining([
expect.objectContaining({ id: 'memory1', assets: expect.any(Array) }),
@@ -45,22 +40,22 @@ describe(MemoryService.name, () => {
});
it('should throw an error when the memory is not found', async () => {
accessMock.memory.checkOwnerAccess.mockResolvedValue(new Set(['race-condition']));
mocks.access.memory.checkOwnerAccess.mockResolvedValue(new Set(['race-condition']));
await expect(sut.get(authStub.admin, 'race-condition')).rejects.toBeInstanceOf(BadRequestException);
});
it('should get a memory by id', async () => {
memoryMock.get.mockResolvedValue(memoryStub.memory1);
accessMock.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
mocks.memory.get.mockResolvedValue(memoryStub.memory1);
mocks.access.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
await expect(sut.get(authStub.admin, 'memory1')).resolves.toMatchObject({ id: 'memory1' });
expect(memoryMock.get).toHaveBeenCalledWith('memory1');
expect(accessMock.memory.checkOwnerAccess).toHaveBeenCalledWith(userStub.admin.id, new Set(['memory1']));
expect(mocks.memory.get).toHaveBeenCalledWith('memory1');
expect(mocks.access.memory.checkOwnerAccess).toHaveBeenCalledWith(userStub.admin.id, new Set(['memory1']));
});
});
describe('create', () => {
it('should skip assets the user does not have access to', async () => {
memoryMock.create.mockResolvedValue(memoryStub.empty);
mocks.memory.create.mockResolvedValue(memoryStub.empty);
await expect(
sut.create(authStub.admin, {
type: MemoryType.ON_THIS_DAY,
@@ -69,7 +64,7 @@ describe(MemoryService.name, () => {
memoryAt: new Date(2024),
}),
).resolves.toMatchObject({ assets: [] });
expect(memoryMock.create).toHaveBeenCalledWith(
expect(mocks.memory.create).toHaveBeenCalledWith(
{
ownerId: 'admin_id',
memoryAt: expect.any(Date),
@@ -83,8 +78,8 @@ describe(MemoryService.name, () => {
});
it('should create a memory', async () => {
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset1']));
memoryMock.create.mockResolvedValue(memoryStub.memory1);
mocks.access.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset1']));
mocks.memory.create.mockResolvedValue(memoryStub.memory1);
await expect(
sut.create(authStub.admin, {
type: MemoryType.ON_THIS_DAY,
@@ -93,7 +88,7 @@ describe(MemoryService.name, () => {
memoryAt: new Date(2024, 0, 1),
}),
).resolves.toBeDefined();
expect(memoryMock.create).toHaveBeenCalledWith(
expect(mocks.memory.create).toHaveBeenCalledWith(
expect.objectContaining({
ownerId: userStub.admin.id,
}),
@@ -102,7 +97,7 @@ describe(MemoryService.name, () => {
});
it('should create a memory without assets', async () => {
memoryMock.create.mockResolvedValue(memoryStub.memory1);
mocks.memory.create.mockResolvedValue(memoryStub.memory1);
await expect(
sut.create(authStub.admin, {
type: MemoryType.ON_THIS_DAY,
@@ -118,27 +113,27 @@ describe(MemoryService.name, () => {
await expect(sut.update(authStub.admin, 'not-found', { isSaved: true })).rejects.toBeInstanceOf(
BadRequestException,
);
expect(memoryMock.update).not.toHaveBeenCalled();
expect(mocks.memory.update).not.toHaveBeenCalled();
});
it('should update a memory', async () => {
accessMock.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
memoryMock.update.mockResolvedValue(memoryStub.memory1);
mocks.access.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
mocks.memory.update.mockResolvedValue(memoryStub.memory1);
await expect(sut.update(authStub.admin, 'memory1', { isSaved: true })).resolves.toBeDefined();
expect(memoryMock.update).toHaveBeenCalledWith('memory1', expect.objectContaining({ isSaved: true }));
expect(mocks.memory.update).toHaveBeenCalledWith('memory1', expect.objectContaining({ isSaved: true }));
});
});
describe('remove', () => {
it('should require access', async () => {
await expect(sut.remove(authStub.admin, 'not-found')).rejects.toBeInstanceOf(BadRequestException);
expect(memoryMock.delete).not.toHaveBeenCalled();
expect(mocks.memory.delete).not.toHaveBeenCalled();
});
it('should delete a memory', async () => {
accessMock.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
mocks.access.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
await expect(sut.remove(authStub.admin, 'memory1')).resolves.toBeUndefined();
expect(memoryMock.delete).toHaveBeenCalledWith('memory1');
expect(mocks.memory.delete).toHaveBeenCalledWith('memory1');
});
});
@@ -147,36 +142,36 @@ describe(MemoryService.name, () => {
await expect(sut.addAssets(authStub.admin, 'not-found', { ids: ['asset1'] })).rejects.toBeInstanceOf(
BadRequestException,
);
expect(memoryMock.addAssetIds).not.toHaveBeenCalled();
expect(mocks.memory.addAssetIds).not.toHaveBeenCalled();
});
it('should require asset access', async () => {
accessMock.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
memoryMock.get.mockResolvedValue(memoryStub.memory1);
mocks.access.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
mocks.memory.get.mockResolvedValue(memoryStub.memory1);
await expect(sut.addAssets(authStub.admin, 'memory1', { ids: ['not-found'] })).resolves.toEqual([
{ error: 'no_permission', id: 'not-found', success: false },
]);
expect(memoryMock.addAssetIds).not.toHaveBeenCalled();
expect(mocks.memory.addAssetIds).not.toHaveBeenCalled();
});
it('should skip assets already in the memory', async () => {
accessMock.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
memoryMock.get.mockResolvedValue(memoryStub.memory1);
memoryMock.getAssetIds.mockResolvedValue(new Set(['asset1']));
mocks.access.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
mocks.memory.get.mockResolvedValue(memoryStub.memory1);
mocks.memory.getAssetIds.mockResolvedValue(new Set(['asset1']));
await expect(sut.addAssets(authStub.admin, 'memory1', { ids: ['asset1'] })).resolves.toEqual([
{ error: 'duplicate', id: 'asset1', success: false },
]);
expect(memoryMock.addAssetIds).not.toHaveBeenCalled();
expect(mocks.memory.addAssetIds).not.toHaveBeenCalled();
});
it('should add assets', async () => {
accessMock.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset1']));
memoryMock.get.mockResolvedValue(memoryStub.memory1);
mocks.access.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
mocks.access.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset1']));
mocks.memory.get.mockResolvedValue(memoryStub.memory1);
await expect(sut.addAssets(authStub.admin, 'memory1', { ids: ['asset1'] })).resolves.toEqual([
{ id: 'asset1', success: true },
]);
expect(memoryMock.addAssetIds).toHaveBeenCalledWith('memory1', ['asset1']);
expect(mocks.memory.addAssetIds).toHaveBeenCalledWith('memory1', ['asset1']);
});
});
@@ -185,25 +180,25 @@ describe(MemoryService.name, () => {
await expect(sut.removeAssets(authStub.admin, 'not-found', { ids: ['asset1'] })).rejects.toBeInstanceOf(
BadRequestException,
);
expect(memoryMock.removeAssetIds).not.toHaveBeenCalled();
expect(mocks.memory.removeAssetIds).not.toHaveBeenCalled();
});
it('should skip assets not in the memory', async () => {
accessMock.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
mocks.access.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
await expect(sut.removeAssets(authStub.admin, 'memory1', { ids: ['not-found'] })).resolves.toEqual([
{ error: 'not_found', id: 'not-found', success: false },
]);
expect(memoryMock.removeAssetIds).not.toHaveBeenCalled();
expect(mocks.memory.removeAssetIds).not.toHaveBeenCalled();
});
it('should remove assets', async () => {
accessMock.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
accessMock.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset1']));
memoryMock.getAssetIds.mockResolvedValue(new Set(['asset1']));
mocks.access.memory.checkOwnerAccess.mockResolvedValue(new Set(['memory1']));
mocks.access.asset.checkOwnerAccess.mockResolvedValue(new Set(['asset1']));
mocks.memory.getAssetIds.mockResolvedValue(new Set(['asset1']));
await expect(sut.removeAssets(authStub.admin, 'memory1', { ids: ['asset1'] })).resolves.toEqual([
{ id: 'asset1', success: true },
]);
expect(memoryMock.removeAssetIds).toHaveBeenCalledWith('memory1', ['asset1']);
expect(mocks.memory.removeAssetIds).toHaveBeenCalledWith('memory1', ['asset1']);
});
});
});