Skip to content

Commit

Permalink
more tests for deleting funding source (#314)
Browse files Browse the repository at this point in the history
* more tests for deleting funding source

* fixed test

Co-authored-by: Shreyas Devalapurkar <[email protected]>
  • Loading branch information
anissa-agahchen and sdevalapurkar authored May 13, 2021
1 parent b0e8107 commit a9a9a32
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ describe('add a funding source', () => {
await result({ ...sampleReq, body: null }, (null as unknown) as any, (null as unknown) as any);
expect.fail();
} catch (actualError) {
console.log(actualError);
expect(actualError.status).to.equal(400);
expect(actualError.message).to.equal('Missing funding source data');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import sinonChai from 'sinon-chai';
import * as deleteFundingSource from './delete';
import * as db from '../../../../../database/db';
import * as deleteFundingSource_queries from '../../../../../queries/project/project-delete-queries';
import SQL from 'sql-template-strings';

chai.use(sinonChai);

Expand Down Expand Up @@ -39,6 +40,18 @@ describe('delete a funding source', () => {
}
} as any;

let actualResult: any = null;

const sampleRes = {
status: () => {
return {
json: (result: any) => {
actualResult = result;
}
};
}
};

afterEach(() => {
sinon.restore();
});
Expand Down Expand Up @@ -97,4 +110,52 @@ describe('delete a funding source', () => {
expect(actualError.message).to.equal('Failed to build SQL delete statement');
}
});

it('should return the row count of the removed funding source on success', async () => {
const mockQuery = sinon.stub();

mockQuery.resolves({ rowCount: 1 });

sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
},
query: mockQuery
});

sinon.stub(deleteFundingSource_queries, 'deleteFundingSourceSQL').returns(SQL`something`);

const result = deleteFundingSource.deleteFundingSource();

await result(sampleReq, sampleRes as any, (null as unknown) as any);

expect(actualResult).to.eql(1);
});

it('throws a 400 error when delete fundingSource fails, because the response has no rows', async () => {
const mockQuery = sinon.stub();

mockQuery.resolves({ rows: [], rowCount: 0 });

sinon.stub(db, 'getDBConnection').returns({
...dbConnectionObj,
systemUserId: () => {
return 20;
},
query: mockQuery
});

sinon.stub(deleteFundingSource_queries, 'deleteFundingSourceSQL').returns(SQL`some query`);

try {
const result = deleteFundingSource.deleteFundingSource();

await result(sampleReq, (null as unknown) as any, (null as unknown) as any);
expect.fail();
} catch (actualError) {
expect(actualError.status).to.equal(400);
expect(actualError.message).to.equal('Failed to delete project funding source');
}
});
});

0 comments on commit a9a9a32

Please sign in to comment.