1 import type { CommentInterface, CommentThreadInterface, InternalEventBusInterface } from '@proton/docs-shared'
2 import { CommentThreadState } from '@proton/docs-shared'
3 import { LocalCommentsState } from './LocalCommentsState'
5 describe('LocalCommentsState', () => {
6 let state: LocalCommentsState
7 let eventBus: InternalEventBusInterface
12 } as unknown as InternalEventBusInterface
13 state = new LocalCommentsState(eventBus)
16 describe('addThread', () => {
17 it('should add a thread', () => {
18 state.addThread({ id: '1' } as CommentThreadInterface)
20 expect(state.getAllThreads()).toEqual([{ id: '1' }])
23 it('should mark a thread as unread', () => {
24 state.addThread({ id: '1' } as CommentThreadInterface, true)
26 expect(state.hasUnreadThreads()).toBe(true)
29 it('should prevent duplicates', () => {
30 state.addThread({ id: '1' } as CommentThreadInterface)
31 state.addThread({ id: '1' } as CommentThreadInterface)
33 expect(state.getAllThreads()).toEqual([{ id: '1' }])
37 describe('deleteThread', () => {
38 it('should delete a thread', () => {
39 state.addThread({ id: '1' } as CommentThreadInterface)
40 state.deleteThread('1')
42 expect(state.getAllThreads()).toEqual([])
45 it('should remove thread from unread list', () => {
46 state.addThread({ id: '1' } as CommentThreadInterface, true)
47 state.deleteThread('1')
49 expect(state.hasUnreadThreads()).toBe(false)
53 describe('resolveThread', () => {
54 it('should resolve a thread', () => {
55 state.addThread({ id: '1', state: CommentThreadState.Active } as CommentThreadInterface)
56 state.resolveThread('1')
58 expect(state.findThreadById('1')?.state).toBe(CommentThreadState.Resolved)
62 describe('unresolveThread', () => {
63 it('should unresolve a thread', () => {
64 state.addThread({ id: '1', state: CommentThreadState.Resolved } as CommentThreadInterface)
65 state.unresolveThread('1')
67 expect(state.findThreadById('1')?.state).toBe(CommentThreadState.Active)
71 describe('changeThreadState', () => {
72 it('should change the state of a thread', () => {
73 state.addThread({ id: '1', state: CommentThreadState.Active } as CommentThreadInterface)
74 state.changeThreadState('1', CommentThreadState.Resolved)
76 expect(state.findThreadById('1')?.state).toBe(CommentThreadState.Resolved)
80 describe('addComment', () => {
81 it('should add a comment to a thread', () => {
82 state.addThread({ id: '1', comments: [] as CommentInterface[] } as CommentThreadInterface)
83 state.addComment({ id: '1', content: 'test' } as CommentInterface, '1')
85 expect(state.findThreadById('1')?.comments).toEqual([{ id: '1', content: 'test' }])
88 it('should mark thread as unread when adding a comment', () => {
89 state.addThread({ id: '1', comments: [] as CommentInterface[] } as CommentThreadInterface)
90 state.addComment({ id: '1', content: 'test' } as CommentInterface, '1', true)
92 expect(state.hasUnreadThreads()).toBe(true)
96 describe('editComment', () => {
97 it('should edit a comment in a thread', () => {
98 state.addThread({ id: '1', comments: [{ id: '1', content: 'test' }] } as CommentThreadInterface)
99 state.editComment({ commentID: '1', threadID: '1', content: 'edited' })
101 expect(state.findThreadById('1')?.comments).toEqual([{ id: '1', content: 'edited' }])
104 it('should mark thread as unread when editing a comment', () => {
105 state.addThread({ id: '1', comments: [{ id: '1', content: 'test' }] } as CommentThreadInterface)
106 state.editComment({ commentID: '1', threadID: '1', content: 'edited', markThreadUnread: true })
108 expect(state.hasUnreadThreads()).toBe(true)
112 describe('deleteComment', () => {
113 it('should delete a comment from a thread', () => {
114 state.addThread({ id: '1', comments: [{ id: '1', content: 'test' }] } as CommentThreadInterface)
115 state.deleteComment({ commentID: '1', threadID: '1' })
117 expect(state.findThreadById('1')?.comments).toEqual([])
121 describe('replacePlaceholderThread', () => {
122 it('should replace a placeholder thread', () => {
123 state.addThread({ id: 'placeholder', isPlaceholder: true } as CommentThreadInterface)
124 state.replacePlaceholderThread('placeholder', { id: '1', isPlaceholder: false } as CommentThreadInterface)
126 expect(state.findThreadById('1')?.isPlaceholder).toBe(false)
130 describe('replacePlaceholderComment', () => {
131 it('should replace a placeholder comment', () => {
132 state.addThread({ id: '1', comments: [{ id: 'placeholder', isPlaceholder: true }] } as CommentThreadInterface)
133 state.replacePlaceholderComment('placeholder', { id: '1', isPlaceholder: false } as CommentInterface)
135 expect(state.findThreadById('1')?.comments).toEqual([{ id: '1', isPlaceholder: false }])