Skip to content

Commit

Permalink
fix mock functions
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Aug 8, 2024
1 parent 0231ad2 commit bfa812a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/api/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ app.use(function* (next) {
try {
yield next;
} catch (err) {
console.error('Sending error as a body', err);
this.status = err.status || 500;
this.body = err.message;
this.app.emit('error', err, this);
Expand Down
3 changes: 2 additions & 1 deletion src/api/controller/api/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export const setup = async (ctx, next) => {
try {
await next();
} catch (error) {
console.error('Sending error as a body', error);
ctx.status = 500;
ctx.body = { error: error.message };
}
Expand Down Expand Up @@ -314,8 +315,8 @@ export const importFields = (asyncBusboyImpl) => async (ctx) => {
};
ctx.status = 200;
} catch (e) {
console.error('Sending error as a body', e);
ctx.status = 500;
console.error(e);
ctx.body = e.message;
}
};
Expand Down
1 change: 1 addition & 0 deletions src/api/controller/api/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const prepareUpload = async (ctx, next) => {
await next();
} catch (error) {
progress.throw(ctx.tenant, error);
console.error('Sending error as a body', error);
ctx.status = 500;
ctx.body = error.message;
}
Expand Down
30 changes: 15 additions & 15 deletions src/api/services/updateFacetValue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ describe('updatefacetValue', () => {
findOneAndUpdate: jest.fn().mockImplementation(() => ({
value: { count: 10 },
})),
update: jest.fn(),
remove: jest.fn(),
updateOne: jest.fn(),
deleteOne: jest.fn(),
};
await updateFacetValue(publishedFacet)({
field: 'fieldName',
Expand All @@ -20,9 +20,9 @@ describe('updatefacetValue', () => {
{ returnOriginal: false },
);

expect(publishedFacet.remove).not.toHaveBeenCalled();
expect(publishedFacet.deleteOne).not.toHaveBeenCalled();

expect(publishedFacet.update).toHaveBeenCalledWith(
expect(publishedFacet.updateOne).toHaveBeenCalledWith(
{
field: 'fieldName',
value: 'new',
Expand All @@ -32,7 +32,7 @@ describe('updatefacetValue', () => {
);
});

it('should call remove if findOneAndUpdate returned an updated facet with count at 0 or less', async () => {
it('should call deleteOne if findOneAndUpdate returned an updated facet with count at 0 or less', async () => {
const publishedFacet = {
findOneAndUpdate: jest.fn().mockImplementation(() => ({
value: {
Expand All @@ -41,8 +41,8 @@ describe('updatefacetValue', () => {
count: 0,
},
})),
update: jest.fn(),
remove: jest.fn(),
updateOne: jest.fn(),
deleteOne: jest.fn(),
};
await updateFacetValue(publishedFacet)({
field: 'fieldName',
Expand All @@ -58,12 +58,12 @@ describe('updatefacetValue', () => {
{ returnOriginal: false },
);

expect(publishedFacet.remove).toHaveBeenCalledWith({
expect(publishedFacet.deleteOne).toHaveBeenCalledWith({
field: 'fieldName',
value: 'old',
count: 0,
});
expect(publishedFacet.update).toHaveBeenCalledWith(
expect(publishedFacet.updateOne).toHaveBeenCalledWith(
{
field: 'fieldName',
value: 'new',
Expand All @@ -80,8 +80,8 @@ describe('updatefacetValue', () => {
findOneAndUpdate: jest.fn().mockImplementation(() => ({
value: { count: 10 },
})),
update: jest.fn(),
remove: jest.fn(),
updateOne: jest.fn(),
deleteOne: jest.fn(),
};
await updateFacetValue(publishedFacet)({
field: 'fieldName',
Expand All @@ -100,11 +100,11 @@ describe('updatefacetValue', () => {
{ returnOriginal: false },
);

expect(publishedFacet.remove).not.toHaveBeenCalled();
expect(publishedFacet.deleteOne).not.toHaveBeenCalled();

expect(publishedFacet.update).toHaveBeenCalledTimes(2);
expect(publishedFacet.updateOne).toHaveBeenCalledTimes(2);

expect(publishedFacet.update).toHaveBeenCalledWith(
expect(publishedFacet.updateOne).toHaveBeenCalledWith(
{
field: 'fieldName',
value: 'new1',
Expand All @@ -113,7 +113,7 @@ describe('updatefacetValue', () => {
{ upsert: true },
);

expect(publishedFacet.update).toHaveBeenCalledWith(
expect(publishedFacet.updateOne).toHaveBeenCalledWith(
{
field: 'fieldName',
value: 'new2',
Expand Down

0 comments on commit bfa812a

Please sign in to comment.