Skip to content

Commit

Permalink
Add storage functions unit tests
Browse files Browse the repository at this point in the history
* Add additional unit tests for hastags before a breakline
  • Loading branch information
cesardeazevedo committed Sep 29, 2024
1 parent 9fa7224 commit 7af4b1a
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 43 deletions.
45 changes: 45 additions & 0 deletions src/__tests__/fixtures.ts
Original file line number Diff line number Diff line change
@@ -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<Fixtures>({
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 }),
],
}),
)
},
})
90 changes: 47 additions & 43 deletions src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
@@ -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<Fixtures>({
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 }) => {
Expand Down Expand Up @@ -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",
}
`)
})
})
43 changes: 43 additions & 0 deletions src/__tests__/storage.test.ts
Original file line number Diff line number Diff line change
@@ -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 }])
})
})

0 comments on commit 7af4b1a

Please sign in to comment.