feat: persist notes on realtime note unload and on interval

The realtime note map has been moved into its own class
to separate the realtime note business logic from the storing logic.

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2022-07-22 23:05:38 +02:00 committed by Yannick Bungers
parent 49b4d2e070
commit 4746c30c26
22 changed files with 619 additions and 102 deletions

View file

@ -6,6 +6,7 @@
import { ConfigModule } from '@nestjs/config';
import { Test, TestingModule } from '@nestjs/testing';
import { getRepositoryToken } from '@nestjs/typeorm';
import { Mock } from 'ts-mockery';
import { Repository } from 'typeorm';
import { AuthToken } from '../auth/auth-token.entity';
@ -99,7 +100,7 @@ describe('RevisionsService', () => {
describe('getRevision', () => {
it('returns a revision', async () => {
const note = {} as Note;
const note = Mock.of<Note>({});
const revision = Revision.create('', '', note) as Revision;
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revision);
expect(await service.getRevision({} as Note, 1)).toEqual(revision);
@ -192,4 +193,48 @@ describe('RevisionsService', () => {
expect(userInfo.anonymousUserCount).toEqual(2);
});
});
describe('createRevision', () => {
it('creates a new revision', async () => {
const note = Mock.of<Note>({});
const oldContent = 'old content\n';
const newContent = 'new content\n';
const oldRevision = Mock.of<Revision>({ content: oldContent });
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(oldRevision);
jest
.spyOn(revisionRepo, 'save')
.mockImplementation((revision) =>
Promise.resolve(revision as Revision),
);
const createdRevision = await service.createRevision(note, newContent);
expect(createdRevision).not.toBeUndefined();
expect(createdRevision?.content).toBe(newContent);
await expect(createdRevision?.note).resolves.toBe(note);
expect(createdRevision?.patch).toMatchInlineSnapshot(`
"Index: markdownContent
===================================================================
--- markdownContent
+++ markdownContent
@@ -1,1 +1,1 @@
-old content
+new content
"
`);
});
it("won't create a revision if content is unchanged", async () => {
const note = Mock.of<Note>({});
const oldContent = 'old content\n';
const oldRevision = Mock.of<Revision>({ content: oldContent });
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(oldRevision);
const saveSpy = jest.spyOn(revisionRepo, 'save').mockImplementation();
const createdRevision = await service.createRevision(note, oldContent);
expect(createdRevision).toBeUndefined();
expect(saveSpy).not.toBeCalled();
});
});
});