From 7af4b1a94d085d3bb4e0e886909dcf187d0d17fb Mon Sep 17 00:00:00 2001 From: Cesar Augusto Date: Sun, 29 Sep 2024 16:27:43 +0200 Subject: [PATCH] Add storage functions unit tests * Add additional unit tests for hastags before a breakline --- src/__tests__/fixtures.ts | 45 ++++++++++++++++++ src/__tests__/parser.test.ts | 90 ++++++++++++++++++----------------- src/__tests__/storage.test.ts | 43 +++++++++++++++++ 3 files changed, 135 insertions(+), 43 deletions(-) create mode 100644 src/__tests__/fixtures.ts create mode 100644 src/__tests__/storage.test.ts diff --git a/src/__tests__/fixtures.ts b/src/__tests__/fixtures.ts new file mode 100644 index 0000000..8a685c4 --- /dev/null +++ b/src/__tests__/fixtures.ts @@ -0,0 +1,45 @@ +/* eslint-disable no-empty-pattern, */ +import { Editor } from '@tiptap/core' +import StarterKit from '@tiptap/starter-kit' +import { Markdown as MarkdownExtension } from 'tiptap-markdown' +import { test as base } from 'vitest' +import { NostrExtension } from '../extensions/NostrExtension' + +const extensions = [StarterKit.configure({ history: false }), NostrExtension] + +const editor = new Editor({ extensions }) + +type Fixtures = { + editor: typeof editor + editorMarkdown: Editor + editorUserAbout: Editor +} + +// We ideally want to have a single editor instance to parse markdown and user abouts, +// But currently no ideal way to dynamically load extensions +export const test = base.extend({ + editor: ({}, use) => { + return use(editor) + }, + editorMarkdown: ({}, use) => { + return use( + new Editor({ + extensions: [ + StarterKit.configure({ history: false }), + NostrExtension, + MarkdownExtension.configure({ breaks: true }), + ], + }), + ) + }, + editorUserAbout: ({}, use) => { + return use( + new Editor({ + extensions: [ + StarterKit.configure({ history: false }), + NostrExtension.configure({ image: false, video: false }), + ], + }), + ) + }, +}) diff --git a/src/__tests__/parser.test.ts b/src/__tests__/parser.test.ts index 7f6a8c4..3742d49 100644 --- a/src/__tests__/parser.test.ts +++ b/src/__tests__/parser.test.ts @@ -1,49 +1,7 @@ // @vitest-environment happy-dom -/* eslint-disable no-empty-pattern, */ -import { Editor } from '@tiptap/core' -import StarterKit from '@tiptap/starter-kit' import { nip19 } from 'nostr-tools' -import { Markdown as MarkdownExtension } from 'tiptap-markdown' -import { test as base } from 'vitest' -import { NostrExtension } from '../extensions/NostrExtension' import { fakeEvent } from './testUtils' - -type Fixtures = { - editor: Editor - editorMarkdown: Editor - editorUserAbout: Editor -} - -const extensions = [StarterKit.configure({ history: false }), NostrExtension] - -// We ideally want to have a single editor instance to parse markdown and user abouts, -// But currently no ideal way to dynamically load extensions -const test = base.extend({ - editor: ({}, use) => { - return use(new Editor({ extensions })) - }, - editorMarkdown: ({}, use) => { - return use( - new Editor({ - extensions: [ - StarterKit.configure({ history: false }), - NostrExtension.configure(), - MarkdownExtension.configure({ breaks: true }), - ], - }), - ) - }, - editorUserAbout: ({}, use) => { - return use( - new Editor({ - extensions: [ - StarterKit.configure({ history: false }), - NostrExtension.configure({ image: false, video: false }), - ], - }), - ) - }, -}) +import { test } from './fixtures' describe('parseNote()', () => { test('Should assert simple text', ({ editor }) => { @@ -814,4 +772,50 @@ https://host.com/2.jpeg } `) }) + + test('hashtag before a breakline', ({ editor }) => { + const event = fakeEvent({ + content: 'Lorem Ipsum #tag.\n\nNew Line ipsum', + }) + editor.commands.setEventContent(event) + expect(editor.getJSON()).toMatchInlineSnapshot(` + { + "content": [ + { + "content": [ + { + "text": "Lorem Ipsum ", + "type": "text", + }, + { + "marks": [ + { + "attrs": { + "tag": "#tag", + }, + "type": "tag", + }, + ], + "text": "#tag", + "type": "text", + }, + { + "text": ".", + "type": "text", + }, + { + "type": "hardBreak", + }, + { + "text": "New Line ipsum", + "type": "text", + }, + ], + "type": "paragraph", + }, + ], + "type": "doc", + } + `) + }) }) diff --git a/src/__tests__/storage.test.ts b/src/__tests__/storage.test.ts new file mode 100644 index 0000000..a5dda6b --- /dev/null +++ b/src/__tests__/storage.test.ts @@ -0,0 +1,43 @@ +import { nip19 } from 'nostr-tools' +import type { NostrStorage } from '../extensions/NostrExtension' +import { test } from './fixtures' +import { fakeEvent } from './testUtils' + +describe('Storage', () => { + test('assert getTags()', ({ editor }) => { + const event = fakeEvent({ content: `text #123 #456 text #123 text #abc` }) + editor.commands.setEventContent(event) + const storage = editor.storage.nostr as NostrStorage + expect(storage.getTags()).toEqual([{ tag: '#123' }, { tag: '#456' }, { tag: '#123' }, { tag: '#abc' }]) + }) + + test('assert getNprofiles()', ({ editor }) => { + const ref = fakeEvent() + const decoded = { relays: [], pubkey: ref.pubkey } + const nprofile = nip19.nprofileEncode(decoded) + const event = fakeEvent({ content: `${nprofile}` }) + editor.commands.setEventContent(event) + const storage = editor.storage.nostr as NostrStorage + expect(storage.getNprofiles()).toEqual([{ ...decoded, nprofile }]) + }) + + test('assert getNevents()', ({ editor }) => { + const ref = fakeEvent() + const decoded = { kind: 1, id: ref.id, relays: [], author: ref.pubkey } + const nevent = nip19.neventEncode(decoded) + const event = fakeEvent({ content: `${nevent}` }) + editor.commands.setEventContent(event) + const storage = editor.storage.nostr as NostrStorage + expect(storage.getNevents()).toEqual([{ ...decoded, nevent }]) + }) + + test('assert getNAddress()', ({ editor }) => { + const ref = fakeEvent() + const decoded = { kind: 1, identifier: ref.id, relays: [], pubkey: ref.pubkey } + const naddr = nip19.naddrEncode(decoded) + const event = fakeEvent({ content: `${naddr}` }) + editor.commands.setEventContent(event) + const storage = editor.storage.nostr as NostrStorage + expect(storage.getNaddress()).toEqual([{ ...decoded, naddr }]) + }) +})