Skip to content

Commit

Permalink
TST - usecase instrument model
Browse files Browse the repository at this point in the history
  • Loading branch information
juliecoust committed Jul 31, 2024
1 parent 027a606 commit d63133b
Show file tree
Hide file tree
Showing 4 changed files with 374 additions and 4 deletions.
3 changes: 0 additions & 3 deletions src/domain/use-cases/project/update-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,6 @@ export class UpdateProject implements UpdateProjectUseCase {

private async getUpdatedPrivileges(project_id: number): Promise<PublicPrivilege> {
const privileges = await this.privilegeRepository.getPublicPrivileges({ project_id: project_id });
if (!privileges) {
throw new Error("Cannot find privileges");
}
return privileges;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { InstrumentModelRepository } from "../../../../src/domain/interfaces/repositories/instrument_model-repository";
import { MockInstrumentModelRepository } from "../../../mocks/instrumentModel-mock";
import { GetOneInstrumentModel } from '../../../../src/domain/use-cases/instrument_model/get-one-instrument_model';
import { instrument_model_response } from "../../../entities/instrumentModel";


let mockInstrumentModelRepository: InstrumentModelRepository;

beforeEach(() => {
jest.clearAllMocks();
mockInstrumentModelRepository = new MockInstrumentModelRepository()
})

describe("Domain - Use Cases - Get One Instrument Model", () => {
test("Get one instrument model by id : Ok ", async () => {
const getOneInstrumentModelUseCase = new GetOneInstrumentModel(mockInstrumentModelRepository)
jest.spyOn(mockInstrumentModelRepository, 'getOneInstrumentModel').mockResolvedValueOnce(instrument_model_response);

const instrumentModel = await getOneInstrumentModelUseCase.execute(1);
expect(instrumentModel.instrument_model_id).toBe(1);
});
test("Get one instrument model by id : Not found ", async () => {
const getOneInstrumentModelUseCase = new GetOneInstrumentModel(mockInstrumentModelRepository)
jest.spyOn(mockInstrumentModelRepository, 'getOneInstrumentModel').mockResolvedValueOnce(null);
await expect(getOneInstrumentModelUseCase.execute(2)).rejects.toThrow("Cannot find instrument_model");

});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { InstrumentModelRepository } from "../../../../src/domain/interfaces/repositories/instrument_model-repository";
import { MockInstrumentModelRepository } from "../../../mocks/instrumentModel-mock";
import { MockSearchRepository } from "../../../mocks/search-mock";
import { SearchInstrumentModels } from '../../../../src/domain/use-cases/instrument_model/search-instrument_model';
import { instrument_model_response } from "../../../entities/instrumentModel";
import { SearchRepository } from "../../../../src/domain/interfaces/repositories/search-repository";


let mockInstrumentModelRepository: InstrumentModelRepository;
let mockSearchRepository: SearchRepository;

beforeEach(() => {
jest.clearAllMocks();
mockInstrumentModelRepository = new MockInstrumentModelRepository()
mockSearchRepository = new MockSearchRepository()
})

describe("Domain - Use Cases - Search Instrument Models", () => {
test("Search instrument models with filters, should be forced to empty ", async () => {
const options = { filter: [{ field: "instrument_model_name", operator: "=", value: "UVP5" }], sort_by: [], page: 1, limit: 10 }
const instruments = { items: [instrument_model_response], total: 1 }
const search_info_response = { total: 1, page: 1, pages: 1, limit: 10, total_on_page: 1 }

jest.spyOn(mockSearchRepository, 'formatSortBy').mockReturnValueOnce([]);
jest.spyOn(mockInstrumentModelRepository, 'standardGetInstrumentModels').mockResolvedValueOnce(instruments);
jest.spyOn(mockSearchRepository, 'formatSearchInfo').mockReturnValueOnce(search_info_response);


const searchInstrumentModelsUseCase = new SearchInstrumentModels(mockInstrumentModelRepository, mockSearchRepository)
const { instrument_models, search_info } = await searchInstrumentModelsUseCase.execute(options);

expect(instrument_models.length).toBe(1);
expect(search_info.total).toBe(1);
expect(mockSearchRepository.formatSortBy).toHaveBeenCalledWith([]);
expect(mockInstrumentModelRepository.standardGetInstrumentModels).toHaveBeenCalledWith({ filter: [], sort_by: [], page: 1, limit: 10 });
expect(mockSearchRepository.formatSearchInfo).toHaveBeenCalledWith(instruments, options);
});
test("Throw error when searching instrument models", async () => {
const options = { filter: [{ field: "instrument_model_name", operator: "=", value: "UVP5" }], sort_by: [], page: 1, limit: 10 }
const search_info_response = { total: 1, page: 1, pages: 1, limit: 10, total_on_page: 1 }

jest.spyOn(mockSearchRepository, 'formatSortBy').mockReturnValueOnce([]);
jest.spyOn(mockInstrumentModelRepository, 'standardGetInstrumentModels').mockRejectedValueOnce(new Error());
jest.spyOn(mockSearchRepository, 'formatSearchInfo').mockReturnValueOnce(search_info_response);

try {
const searchInstrumentModelsUseCase = new SearchInstrumentModels(mockInstrumentModelRepository, mockSearchRepository)
await searchInstrumentModelsUseCase.execute(options);
} catch (error) {
expect(error.message).toBe("Cannot search instrument models");

expect(mockSearchRepository.formatSortBy).toHaveBeenCalledWith([]);
expect(mockInstrumentModelRepository.standardGetInstrumentModels).toHaveBeenCalledWith({ filter: [], sort_by: [], page: 1, limit: 10 });
expect(mockSearchRepository.formatSearchInfo).not.toHaveBeenCalled();
}
});
});
Loading

0 comments on commit d63133b

Please sign in to comment.