diff --git a/api/src/repositories/technique-vantage-repository.test.ts b/api/src/repositories/technique-vantage-repository.test.ts index 195d2cad56..3f2c1fc570 100644 --- a/api/src/repositories/technique-vantage-repository.test.ts +++ b/api/src/repositories/technique-vantage-repository.test.ts @@ -11,14 +11,6 @@ import { VantagePostData } from './vantage-mode-repository'; chai.use(sinonChai); describe('TechniqueVantageRepository', () => { - let dbConnection: any; - let repository: TechniqueVantageRepository; - - beforeEach(() => { - dbConnection = getMockDBConnection(); - repository = new TechniqueVantageRepository(dbConnection); - }); - afterEach(() => { sinon.restore(); }); @@ -33,20 +25,25 @@ describe('TechniqueVantageRepository', () => { description: 'test description' } ]; - const mockResponse = { rows: mockRecord } as QueryResult; - const knexStub = sinon.stub(dbConnection, 'knex').resolves(mockResponse); + const dbConnection = getMockDBConnection({ knex: sinon.stub().resolves(mockResponse) }); + + const repository = new TechniqueVantageRepository(dbConnection); const surveyId = 1; const methodTechniqueId = 2; const result = await repository.getVantagesForTechnique(surveyId, methodTechniqueId); - expect(knexStub).to.have.been.calledOnce; + expect(dbConnection.knex).to.have.been.calledOnce; expect(result).to.deep.equal(mockRecord); }); it('should throw an error if the query fails', async () => { + const dbConnection = getMockDBConnection(); + + const repository = new TechniqueVantageRepository(dbConnection); + sinon.stub(dbConnection, 'knex').throws(new Error('Query error')); try { @@ -61,22 +58,27 @@ describe('TechniqueVantageRepository', () => { describe('insertVantagesForTechnique', () => { it('should insert the vantages successfully', async () => { const mockRecord = [{ method_technique_vantage_id: 1 }]; + const mockResponse = { rows: mockRecord } as QueryResult; + const dbConnection = getMockDBConnection({ knex: sinon.stub().resolves(mockResponse) }); - const vantageMethods: VantagePostData[] = [{ vantage_method_id: 3 }]; + const repository = new TechniqueVantageRepository(dbConnection); - const mockResponse = { rows: mockRecord } as QueryResult; - const knexStub = sinon.stub(dbConnection, 'knex').resolves(mockResponse); + const vantageMethods: VantagePostData[] = [{ vantage_method_id: 3 }]; const surveyId = 1; const methodTechniqueId = 2; const result = await repository.insertVantagesForTechnique(surveyId, methodTechniqueId, vantageMethods); - expect(knexStub).to.have.been.calledOnce; + expect(dbConnection.knex).to.have.been.calledOnce; expect(result).to.deep.equal(mockRecord); }); it('should throw an error if insertion fails', async () => { + const dbConnection = getMockDBConnection(); + + const repository = new TechniqueVantageRepository(dbConnection); + const vantageMethods: VantagePostData[] = [{ vantage_method_id: 3 }]; sinon.stub(dbConnection, 'knex').throws(new Error('Insert error')); @@ -92,20 +94,26 @@ describe('TechniqueVantageRepository', () => { describe('deleteVantagesForTechnique', () => { it('should delete the vantages successfully', async () => { - const vantageMethods: VantagePostData[] = [{ vantage_method_id: 3 }]; - const mockResponse = { rows: [], rowCount: 0 } as any as Promise>; - const knexStub = sinon.stub(dbConnection, 'knex').resolves(mockResponse); + const dbConnection = getMockDBConnection({ knex: sinon.stub().resolves(mockResponse) }); + + const repository = new TechniqueVantageRepository(dbConnection); + + const vantageMethods: VantagePostData[] = [{ vantage_method_id: 3 }]; const surveyId = 1; const methodTechniqueId = 2; await repository.deleteVantagesForTechnique(surveyId, methodTechniqueId, vantageMethods); - expect(knexStub).to.have.been.calledOnce; + expect(dbConnection.knex).to.have.been.calledOnce; }); it('should throw an error if deletion fails', async () => { + const dbConnection = getMockDBConnection(); + + const repository = new TechniqueVantageRepository(dbConnection); + const vantageMethods: VantagePostData[] = [{ vantage_method_id: 3 }]; sinon.stub(dbConnection, 'knex').throws(new Error('Delete error')); @@ -119,6 +127,10 @@ describe('TechniqueVantageRepository', () => { }); it('should do nothing if no vantage methods are provided', async () => { + const dbConnection = getMockDBConnection(); + + const repository = new TechniqueVantageRepository(dbConnection); + const knexStub = sinon.stub(dbConnection, 'knex'); await repository.deleteVantagesForTechnique(1, 2, []); diff --git a/api/src/services/technique-service.test.ts b/api/src/services/technique-service.test.ts index 85a16dfce0..f8767599d4 100644 --- a/api/src/services/technique-service.test.ts +++ b/api/src/services/technique-service.test.ts @@ -116,6 +116,9 @@ describe('TechniqueService', () => { const insertQuantitativeAttributesForTechniqueStub = sinon .stub(TechniqueAttributeService.prototype, 'insertQuantitativeAttributesForTechnique') .resolves(); + const insertVantagesForTechniqueStub = sinon + .stub(TechniqueVantageService.prototype, 'insertVantagesForTechnique') + .resolves(); const dbConnection = getMockDBConnection(); @@ -136,20 +139,24 @@ describe('TechniqueService', () => { attributes: { quantitative_attributes: [ { - method_technique_attribute_quantitative_id: 44, method_lookup_attribute_quantitative_id: '123-456-55', value: 66 } ], qualitative_attributes: [ { - method_technique_attribute_qualitative_id: 77, method_lookup_attribute_qualitative_id: '123-456-88', method_lookup_attribute_qualitative_option_id: '123-456-99' } ] }, - vantage_methods: [] + vantage_methods: [ + { + method_technique_vantage_id: 1, + vantage_method_id: 2, + vantage_category_id: 3 + } + ] } ]; @@ -170,6 +177,13 @@ describe('TechniqueService', () => { 11, techniques[0].attributes.quantitative_attributes ); + expect(insertVantagesForTechniqueStub).to.have.been.calledOnceWith(surveyId, 11, [ + { + method_technique_vantage_id: 1, + vantage_method_id: 2, + vantage_category_id: 3 + } + ]); expect(response).to.eql([mockRecord]); });