Skip to content

Commit 9d053cf

Browse files
luka-nextcloudmejo-
authored andcommitted
feat: add tests for mentions
Signed-off-by: Luka Trovic <luka@nextcloud.com>
1 parent f40c459 commit 9d053cf

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed

cypress/e2e/mentions.spec.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { initUserAndFiles, randHash } from '../utils/index.js'
2+
import 'cypress-file-upload'
3+
4+
const randUser = randHash()
5+
const randUser1 = randHash()
6+
const currentUser = randUser
7+
8+
const refresh = () => {
9+
cy.get('.files-controls .crumb:not(.hidden) a')
10+
.last()
11+
.click({ force: true })
12+
}
13+
14+
const createFileWithMention = (target, userToMention) => {
15+
const mimeType = 'text/markdown'
16+
const content = `Hello @[${userToMention}](mention://user/${userToMention})`
17+
cy.createFile(target, content, mimeType)
18+
.then(refresh)
19+
}
20+
21+
describe('Test mentioning users', () => {
22+
before(() => {
23+
initUserAndFiles(randUser, 'test.md')
24+
cy.nextcloudCreateUser(randUser1, 'password')
25+
})
26+
27+
beforeEach(() => {
28+
cy.login(currentUser, 'password')
29+
})
30+
31+
it('Type @ and see the user list', () => {
32+
const requestAlias = 'fetchUsersList'
33+
cy.intercept({ method: 'POST', url: '**/users' }).as(requestAlias)
34+
35+
cy.openFile('test.md')
36+
cy.get('.text-editor__content div[contenteditable="true"]')
37+
.clear()
38+
.type(`@${randUser1.substring(0, 3)}`)
39+
40+
return cy.wait(`@${requestAlias}`)
41+
.then(() => {
42+
cy.get('.tippy-box .items').children().should(($children) => {
43+
const users = $children.map((i, el) => el.innerText).get()
44+
expect(users.length).to.be.greaterThan(0)
45+
expect(randUser1).to.be.oneOf(users)
46+
})
47+
})
48+
})
49+
50+
it('Select a user will insert the mention', () => {
51+
const autocompleteReauestAlias = 'fetchUsersList'
52+
cy.intercept({ method: 'POST', url: '**/users' }).as(autocompleteReauestAlias)
53+
54+
cy.openFile('test.md')
55+
cy.get('.text-editor__content div[contenteditable="true"]')
56+
.clear()
57+
.type(`@${randUser1.substring(0, 3)}`)
58+
59+
return cy.wait(`@${autocompleteReauestAlias}`)
60+
.then(() => {
61+
cy.get('.tippy-box .items').contains(randUser1).click()
62+
cy.get('span.mention').contains(randUser1).should('be.visible')
63+
})
64+
})
65+
66+
it(' Open a document with an existing mention and properly see the user bubble rendered', () => {
67+
const mentionFilename = 'mention.md'
68+
createFileWithMention(mentionFilename, randUser1)
69+
cy.openFile(mentionFilename)
70+
cy.get('.text-editor__content div[contenteditable="true"] span.mention')
71+
.contains(randUser1)
72+
.should('be.visible')
73+
})
74+
})

src/extensions/Mention.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default TipTapMention.extend({
1111
getAttrs: element => {
1212
return {
1313
id: element.getAttribute('data-id'),
14-
label: element.innerText ?? element.getAttribute('data-id'),
14+
label: element.innerText || element.textContent || element.getAttribute('data-label'),
1515
}
1616
},
1717
priority: 100,

src/tests/markdown.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ describe('Markdown though editor', () => {
152152
expect(markdownThroughEditor(entry)).toBe(entry)
153153
})
154154
})
155+
156+
test('mentions', () => {
157+
expect(markdownThroughEditor('@[username](mention://user/id)')).toBe(' @[username](mention://user/id) ')
158+
})
155159
})
156160

157161
describe('Markdown serializer from html', () => {
@@ -203,6 +207,10 @@ describe('Markdown serializer from html', () => {
203207
// Test --- within front matter is allowed
204208
expect(markdownThroughEditorHtml('<pre id="frontmatter"><code>---</code></pre><h1>Heading</h1>')).toBe('----\n---\n----\n\n# Heading')
205209
})
210+
211+
test('mentions', () => {
212+
expect(markdownThroughEditorHtml('<span class="mention" data-label="username" data-type="user" data-id="id">username</span>')).toBe(' @[username](mention://user/id) ')
213+
})
206214
})
207215

208216
describe('Trailing nodes', () => {

0 commit comments

Comments
 (0)