Skip to content

Commit

Permalink
test to ensure entities POST runs as a transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed Feb 3, 2025
1 parent a6f4bd8 commit ae3052f
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/api/entities/specs/routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { AccessLevels, PermissionType } from 'shared/types/permissionSchema';
import { UserRole } from 'shared/types/userSchema';
import { ObjectId } from 'mongodb';
import fixtures, { permissions } from './fixtures';
import { storage } from 'api/files';

Check failure on line 19 in app/api/entities/specs/routes.spec.ts

View workflow job for this annotation

GitHub Actions / eslint

'storage' is defined but never used
import entities from '../entities';
import { appContext } from 'api/utils/AppContext';

jest.mock(
'../../auth/authMiddleware.ts',
Expand Down Expand Up @@ -244,5 +247,28 @@ describe('entities routes', () => {

expect(legacyLogger.error).toHaveBeenCalledWith(expect.stringContaining('Deprecation'));
});

it('should run saveEntity process as a transaction', async () => {
jest.restoreAllMocks();
jest.spyOn(entities, 'getUnrestrictedWithDocuments').mockImplementationOnce(() => {
throw new Error('error at the end of the saveEntity');
});
new UserInContextMockFactory().mock(user);
const response: SuperTestResponse = await request(app)
.post('/api/entities')
.field('entity', JSON.stringify(entityToSave))
.attach('documents[0]', path.join(__dirname, 'Hello, World.pdf'), 'Nombre en español')
.field('documents_originalname[0]', 'Nombre en español')
.expect(500);

expect(response.body).toMatchObject({
error: expect.any(String),
});

await appContext.run(async () => {
const myEntity = await entities.get({ title: 'my entity' });
expect(myEntity.length).toBe(0);
});
});
});
});

0 comments on commit ae3052f

Please sign in to comment.