mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-24 02:36:31 -05:00
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:
parent
f9b6f6851b
commit
e99ba0615c
4 changed files with 50 additions and 5 deletions
|
@ -122,3 +122,7 @@ License: CC0-1.0
|
|||
Files: frontend/**/__snapshots__/*.snap
|
||||
Copyright: 2022 The HedgeDoc developers (see AUTHORS file)
|
||||
License: AGPL-3.0-only
|
||||
|
||||
Files: backend/**/__snapshots__/*.snap
|
||||
Copyright: 2024 The HedgeDoc developers (see AUTHORS file)
|
||||
License: AGPL-3.0-only
|
||||
|
|
|
@ -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
|
||||
"
|
||||
`;
|
|
@ -34,6 +34,7 @@ import { EditService } from './edit.service';
|
|||
import { Revision } from './revision.entity';
|
||||
import { RevisionsService } from './revisions.service';
|
||||
|
||||
|
||||
describe('RevisionsService', () => {
|
||||
let service: RevisionsService;
|
||||
let revisionRepo: Repository<Revision>;
|
||||
|
@ -121,7 +122,7 @@ describe('RevisionsService', () => {
|
|||
let note: Note;
|
||||
|
||||
beforeEach(() => {
|
||||
note = Mock.of<Note>({});
|
||||
note = Mock.of<Note>({ publicId: 'test-note', id: 1 });
|
||||
revisions = [];
|
||||
|
||||
jest
|
||||
|
@ -141,23 +142,36 @@ describe('RevisionsService', () => {
|
|||
});
|
||||
|
||||
it('purges the revision history', async () => {
|
||||
const revision1 = Mock.of<Revision>({ id: 1 });
|
||||
const revision2 = Mock.of<Revision>({ id: 2 });
|
||||
const revision3 = Mock.of<Revision>({ id: 3 });
|
||||
const revision1 = Mock.of<Revision>({
|
||||
id: 1,
|
||||
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];
|
||||
note.revisions = Promise.resolve(revisions);
|
||||
|
||||
jest.spyOn(revisionRepo, 'find').mockResolvedValueOnce(revisions);
|
||||
jest.spyOn(service, 'getLatestRevision').mockResolvedValueOnce(revision3);
|
||||
|
||||
jest.spyOn(revisionRepo, 'save').mockResolvedValue(Mock.of<Revision>());
|
||||
|
||||
// expected to return all the purged revisions
|
||||
expect(await service.purgeRevisions(note)).toStrictEqual([
|
||||
revision1,
|
||||
revision2,
|
||||
]);
|
||||
|
||||
// expected to have only the latest revision
|
||||
expect(revisions).toStrictEqual([revision3]);
|
||||
expect(revision3.patch).toMatchSnapshot();
|
||||
});
|
||||
it('has no effect on revision history when a single revision is present', async () => {
|
||||
const revision1 = Mock.of<Revision>({ id: 1 });
|
||||
|
|
|
@ -59,6 +59,17 @@ export class RevisionsService {
|
|||
const oldRevisions = revisions.filter(
|
||||
(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
|
||||
return await this.revisionRepository.remove(oldRevisions);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue