test: fix update patch when removing old revisions

Signed-off-by: yamashush <38120991+yamashush@users.noreply.github.com>
Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
yamashush 2024-05-09 17:16:41 +09:00 committed by Erik Michelson
parent f9b6f6851b
commit e99ba0615c
4 changed files with 50 additions and 5 deletions

View file

@ -122,3 +122,7 @@ License: CC0-1.0
Files: frontend/**/__snapshots__/*.snap Files: frontend/**/__snapshots__/*.snap
Copyright: 2022 The HedgeDoc developers (see AUTHORS file) Copyright: 2022 The HedgeDoc developers (see AUTHORS file)
License: AGPL-3.0-only License: AGPL-3.0-only
Files: backend/**/__snapshots__/*.snap
Copyright: 2024 The HedgeDoc developers (see AUTHORS file)
License: AGPL-3.0-only

View file

@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`RevisionsService purgeRevisions purges the revision history 1`] = `
"Index: test-note
===================================================================
--- test-note
+++ test-note
@@ -0,0 +1,6 @@
+---
+title: new title
+description: new description
+tags: [ "tag1" ]
+---
+new content
"
`;

View file

@ -34,6 +34,7 @@ import { EditService } from './edit.service';
import { Revision } from './revision.entity'; import { Revision } from './revision.entity';
import { RevisionsService } from './revisions.service'; import { RevisionsService } from './revisions.service';
describe('RevisionsService', () => { describe('RevisionsService', () => {
let service: RevisionsService; let service: RevisionsService;
let revisionRepo: Repository<Revision>; let revisionRepo: Repository<Revision>;
@ -121,7 +122,7 @@ describe('RevisionsService', () => {
let note: Note; let note: Note;
beforeEach(() => { beforeEach(() => {
note = Mock.of<Note>({}); note = Mock.of<Note>({ publicId: 'test-note', id: 1 });
revisions = []; revisions = [];
jest jest
@ -141,23 +142,36 @@ describe('RevisionsService', () => {
}); });
it('purges the revision history', async () => { it('purges the revision history', async () => {
const revision1 = Mock.of<Revision>({ id: 1 }); const revision1 = Mock.of<Revision>({
const revision2 = Mock.of<Revision>({ id: 2 }); id: 1,
const revision3 = Mock.of<Revision>({ id: 3 }); note: Promise.resolve(note),
});
const revision2 = Mock.of<Revision>({
id: 2,
note: Promise.resolve(note),
});
const revision3 = Mock.of<Revision>({
id: 3,
note: Promise.resolve(note),
content:
'---\ntitle: new title\ndescription: new description\ntags: [ "tag1" ]\n---\nnew content\n',
});
revisions = [revision1, revision2, revision3]; revisions = [revision1, revision2, revision3];
note.revisions = Promise.resolve(revisions); note.revisions = Promise.resolve(revisions);
jest.spyOn(revisionRepo, 'find').mockResolvedValueOnce(revisions); jest.spyOn(revisionRepo, 'find').mockResolvedValueOnce(revisions);
jest.spyOn(service, 'getLatestRevision').mockResolvedValueOnce(revision3); jest.spyOn(service, 'getLatestRevision').mockResolvedValueOnce(revision3);
jest.spyOn(revisionRepo, 'save').mockResolvedValue(Mock.of<Revision>());
// expected to return all the purged revisions // expected to return all the purged revisions
expect(await service.purgeRevisions(note)).toStrictEqual([ expect(await service.purgeRevisions(note)).toStrictEqual([
revision1, revision1,
revision2, revision2,
]); ]);
// expected to have only the latest revision
expect(revisions).toStrictEqual([revision3]); expect(revisions).toStrictEqual([revision3]);
expect(revision3.patch).toMatchSnapshot();
}); });
it('has no effect on revision history when a single revision is present', async () => { it('has no effect on revision history when a single revision is present', async () => {
const revision1 = Mock.of<Revision>({ id: 1 }); const revision1 = Mock.of<Revision>({ id: 1 });

View file

@ -59,6 +59,17 @@ export class RevisionsService {
const oldRevisions = revisions.filter( const oldRevisions = revisions.filter(
(item) => item.id !== latestRevision.id, (item) => item.id !== latestRevision.id,
); );
// update content diff
if (oldRevisions.length > 0) {
latestRevision.patch = createPatch(
note.publicId,
'',
latestRevision.content,
);
await this.revisionRepository.save(latestRevision);
}
// delete the old revisions // delete the old revisions
return await this.revisionRepository.remove(oldRevisions); return await this.revisionRepository.remove(oldRevisions);
} }