mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 01:35:18 -04:00
fix: improve and adjust tests
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
eb986b1504
commit
bb355feddc
4 changed files with 490 additions and 342 deletions
|
@ -104,9 +104,9 @@ describe('RevisionsService', () => {
|
|||
describe('getRevision', () => {
|
||||
it('returns a revision', async () => {
|
||||
const note = Mock.of<Note>({});
|
||||
const revision = Revision.create('', '', note) as Revision;
|
||||
const revision = Mock.of<Revision>({});
|
||||
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revision);
|
||||
expect(await service.getRevision({} as Note, 1)).toEqual(revision);
|
||||
expect(await service.getRevision(note, 1)).toBe(revision);
|
||||
});
|
||||
it('throws if the revision is not in the databse', async () => {
|
||||
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(null);
|
||||
|
@ -117,54 +117,55 @@ describe('RevisionsService', () => {
|
|||
});
|
||||
|
||||
describe('purgeRevisions', () => {
|
||||
let revisions: Revision[];
|
||||
let note: Note;
|
||||
|
||||
beforeEach(() => {
|
||||
note = Mock.of<Note>({});
|
||||
revisions = [];
|
||||
|
||||
jest
|
||||
.spyOn(revisionRepo, 'remove')
|
||||
.mockImplementation(
|
||||
<T extends Revision | Revision[]>(deleteEntities: T): Promise<T> => {
|
||||
const newRevisions = revisions.filter((item: Revision) =>
|
||||
Array.isArray(deleteEntities)
|
||||
? !deleteEntities.includes(item)
|
||||
: deleteEntities !== item,
|
||||
);
|
||||
revisions = newRevisions;
|
||||
note.revisions = Promise.resolve(newRevisions);
|
||||
return Promise.resolve(deleteEntities);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('purges the revision history', async () => {
|
||||
const note = {} as Note;
|
||||
note.id = 4711;
|
||||
let revisions: Revision[] = [];
|
||||
const revision1 = Revision.create('a', 'a', note) as Revision;
|
||||
revision1.id = 1;
|
||||
const revision2 = Revision.create('b', 'b', note) as Revision;
|
||||
revision2.id = 2;
|
||||
const revision3 = Revision.create('c', 'c', note) as Revision;
|
||||
revision3.id = 3;
|
||||
revisions.push(revision1, revision2, revision3);
|
||||
const revision1 = Mock.of<Revision>({ id: 1 });
|
||||
const revision2 = Mock.of<Revision>({ id: 2 });
|
||||
const revision3 = Mock.of<Revision>({ id: 3 });
|
||||
revisions = [revision1, revision2, revision3];
|
||||
note.revisions = Promise.resolve(revisions);
|
||||
|
||||
jest.spyOn(revisionRepo, 'find').mockResolvedValueOnce(revisions);
|
||||
jest.spyOn(service, 'getLatestRevision').mockResolvedValueOnce(revision3);
|
||||
revisionRepo.remove = jest
|
||||
.fn()
|
||||
.mockImplementation((deleteList: Revision[]) => {
|
||||
revisions = revisions.filter(
|
||||
(item: Revision) => !deleteList.includes(item),
|
||||
);
|
||||
return Promise.resolve(deleteList);
|
||||
});
|
||||
|
||||
// expected to return all the purged revisions
|
||||
expect(await service.purgeRevisions(note)).toHaveLength(2);
|
||||
expect(await service.purgeRevisions(note)).toStrictEqual([
|
||||
revision1,
|
||||
revision2,
|
||||
]);
|
||||
|
||||
// expected to have only the latest revision
|
||||
const updatedRevisions: Revision[] = [revision3];
|
||||
expect(revisions).toEqual(updatedRevisions);
|
||||
expect(revisions).toStrictEqual([revision3]);
|
||||
});
|
||||
it('has no effect on revision history when a single revision is present', async () => {
|
||||
const note = {} as Note;
|
||||
note.id = 4711;
|
||||
let revisions: Revision[] = [];
|
||||
const revision1 = Revision.create('a', 'a', note) as Revision;
|
||||
revision1.id = 1;
|
||||
revisions.push(revision1);
|
||||
const revision1 = Mock.of<Revision>({ id: 1 });
|
||||
revisions = [revision1];
|
||||
note.revisions = Promise.resolve(revisions);
|
||||
|
||||
jest.spyOn(revisionRepo, 'find').mockResolvedValueOnce(revisions);
|
||||
jest.spyOn(service, 'getLatestRevision').mockResolvedValueOnce(revision1);
|
||||
revisionRepo.remove = jest
|
||||
.fn()
|
||||
.mockImplementation((deleteList: Revision[]) => {
|
||||
revisions = revisions.filter(
|
||||
(item: Revision) => !deleteList.includes(item),
|
||||
);
|
||||
return Promise.resolve(deleteList);
|
||||
});
|
||||
|
||||
// expected to return all the purged revisions
|
||||
expect(await service.purgeRevisions(note)).toHaveLength(0);
|
||||
|
@ -188,7 +189,7 @@ describe('RevisionsService', () => {
|
|||
edits.push(Edit.create(anonAuthor, 29, 20) as Edit);
|
||||
edits.push(Edit.create(anonAuthor, 29, 20) as Edit);
|
||||
edits.push(Edit.create(anonAuthor2, 29, 20) as Edit);
|
||||
const revision = Revision.create('', '', {} as Note) as Revision;
|
||||
const revision = Mock.of<Revision>({});
|
||||
revision.edits = Promise.resolve(edits);
|
||||
|
||||
const userInfo = await service.getRevisionUserInfo(revision);
|
||||
|
@ -197,13 +198,122 @@ describe('RevisionsService', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('toRevisionMetadataDto', () => {
|
||||
it('converts a revision', async () => {
|
||||
const revision = Mock.of<Revision>({
|
||||
id: 3246,
|
||||
content: 'mockContent',
|
||||
length: 1854,
|
||||
createdAt: new Date('2020-05-20T09:58:00.000Z'),
|
||||
title: 'mockTitle',
|
||||
tags: Promise.resolve([Mock.of<Tag>({ name: 'mockTag' })]),
|
||||
description: 'mockDescription',
|
||||
patch: 'mockPatch',
|
||||
edits: Promise.resolve([
|
||||
Mock.of<Edit>({
|
||||
endPos: 93,
|
||||
startPos: 34,
|
||||
createdAt: new Date('2020-03-04T20:12:00.000Z'),
|
||||
updatedAt: new Date('2021-12-10T09:45:00.000Z'),
|
||||
author: Promise.resolve(
|
||||
Mock.of<Author>({
|
||||
user: Promise.resolve(
|
||||
Mock.of<User>({
|
||||
username: 'mockusername',
|
||||
}),
|
||||
),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
]),
|
||||
});
|
||||
expect(await service.toRevisionMetadataDto(revision))
|
||||
.toMatchInlineSnapshot(`
|
||||
{
|
||||
"anonymousAuthorCount": 0,
|
||||
"authorUsernames": [
|
||||
"mockusername",
|
||||
],
|
||||
"createdAt": 2020-05-20T09:58:00.000Z,
|
||||
"description": "mockDescription",
|
||||
"id": 3246,
|
||||
"length": 1854,
|
||||
"tags": [
|
||||
"mockTag",
|
||||
],
|
||||
"title": "mockTitle",
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toRevisionDto', () => {
|
||||
it('converts a revision', async () => {
|
||||
const revision = Mock.of<Revision>({
|
||||
id: 3246,
|
||||
content: 'mockContent',
|
||||
length: 1854,
|
||||
createdAt: new Date('2020-05-20T09:58:00.000Z'),
|
||||
title: 'mockTitle',
|
||||
tags: Promise.resolve([Mock.of<Tag>({ name: 'mockTag' })]),
|
||||
description: 'mockDescription',
|
||||
patch: 'mockPatch',
|
||||
edits: Promise.resolve([
|
||||
Mock.of<Edit>({
|
||||
endPos: 93,
|
||||
startPos: 34,
|
||||
createdAt: new Date('2020-03-04T22:32:00.000Z'),
|
||||
updatedAt: new Date('2021-02-10T12:23:00.000Z'),
|
||||
author: Promise.resolve(
|
||||
Mock.of<Author>({
|
||||
user: Promise.resolve(
|
||||
Mock.of<User>({
|
||||
username: 'mockusername',
|
||||
}),
|
||||
),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
]),
|
||||
});
|
||||
expect(await service.toRevisionDto(revision)).toMatchInlineSnapshot(`
|
||||
{
|
||||
"anonymousAuthorCount": 0,
|
||||
"authorUsernames": [
|
||||
"mockusername",
|
||||
],
|
||||
"content": "mockContent",
|
||||
"createdAt": 2020-05-20T09:58:00.000Z,
|
||||
"description": "mockDescription",
|
||||
"edits": [
|
||||
{
|
||||
"createdAt": 2020-03-04T22:32:00.000Z,
|
||||
"endPos": 93,
|
||||
"startPos": 34,
|
||||
"updatedAt": 2021-02-10T12:23:00.000Z,
|
||||
"username": "mockusername",
|
||||
},
|
||||
],
|
||||
"id": 3246,
|
||||
"length": 1854,
|
||||
"patch": "mockPatch",
|
||||
"tags": [
|
||||
"mockTag",
|
||||
],
|
||||
"title": "mockTitle",
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
|
||||
describe('createRevision', () => {
|
||||
it('creates a new revision', async () => {
|
||||
const note = Mock.of<Note>({ publicId: 'test-note' });
|
||||
const note = Mock.of<Note>({ publicId: 'test-note', id: 1 });
|
||||
const oldContent = 'old content\n';
|
||||
const newContent = 'new content\n';
|
||||
const newContent =
|
||||
'---\ntitle: new title\ndescription: new description\ntags: [ "tag1" ]\n---\nnew content\n';
|
||||
|
||||
const oldRevision = Mock.of<Revision>({ content: oldContent });
|
||||
const oldRevision = Mock.of<Revision>({ content: oldContent, id: 1 });
|
||||
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(oldRevision);
|
||||
jest
|
||||
.spyOn(revisionRepo, 'save')
|
||||
|
@ -214,24 +324,38 @@ describe('RevisionsService', () => {
|
|||
const createdRevision = await service.createRevision(note, newContent);
|
||||
expect(createdRevision).not.toBeUndefined();
|
||||
expect(createdRevision?.content).toBe(newContent);
|
||||
await expect(createdRevision?.tags).resolves.toMatchInlineSnapshot(`
|
||||
[
|
||||
Tag {
|
||||
"name": "tag1",
|
||||
},
|
||||
]
|
||||
`);
|
||||
expect(createdRevision?.title).toBe('new title');
|
||||
expect(createdRevision?.description).toBe('new description');
|
||||
await expect(createdRevision?.note).resolves.toBe(note);
|
||||
expect(createdRevision?.patch).toMatchInlineSnapshot(`
|
||||
"Index: test-note
|
||||
===================================================================
|
||||
--- test-note
|
||||
+++ test-note
|
||||
@@ -1,1 +1,1 @@
|
||||
@@ -1,1 +1,6 @@
|
||||
-old content
|
||||
+---
|
||||
+title: new title
|
||||
+description: new description
|
||||
+tags: [ "tag1" ]
|
||||
+---
|
||||
+new content
|
||||
"
|
||||
`);
|
||||
});
|
||||
|
||||
it("won't create a revision if content is unchanged", async () => {
|
||||
const note = Mock.of<Note>({});
|
||||
const note = Mock.of<Note>({ id: 1 });
|
||||
const oldContent = 'old content\n';
|
||||
|
||||
const oldRevision = Mock.of<Revision>({ content: oldContent });
|
||||
const oldRevision = Mock.of<Revision>({ content: oldContent, id: 1 });
|
||||
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(oldRevision);
|
||||
const saveSpy = jest.spyOn(revisionRepo, 'save').mockImplementation();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue