Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/stores/__tests__/conversationTags.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ const favoritesTag = {
collapsed: false,
}

const otherTag = {
id: 'other',
name: 'Other',
type: 'other',
sortOrder: 2,
collapsed: false,
}

const customTagOne = {
id: 'tag-1',
name: 'Alpha',
Expand Down Expand Up @@ -115,6 +123,7 @@ describe('conversationTagsStore', () => {
})

it('creates a tag and persists it', async () => {
BrowserStorage.getItem.mockReturnValueOnce(JSON.stringify([favoritesTag, otherTag]))
conversationTagsStore = useConversationTagsStore()
createTagApi.mockResolvedValue(generateOCSResponse({ payload: customTagOne }))

Expand All @@ -123,8 +132,9 @@ describe('conversationTagsStore', () => {

expect(createTagApi).toHaveBeenCalledWith(customTagOne.name)
expect(tag).toEqual(customTagOne)
expect(conversationTagsStore.tags[customTagOne.id]).toEqual(customTagOne)
expect(getPersistedTags()).toEqual([customTagOne])
// Other's sortOrder bumped from 2 to 3, so new tag (2) appears before Other (3)
expect(conversationTagsStore.sortedTags.map((t) => t.id)).toEqual([favoritesTag.id, customTagOne.id, otherTag.id])
expect(conversationTagsStore.tags[otherTag.id].sortOrder).toBe(3)
})

it('updates a tag name', async () => {
Expand Down
5 changes: 5 additions & 0 deletions src/stores/conversationTags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ export const useConversationTagsStore = defineStore('conversationTags', () => {
async function createTag(name: string) {
const response = await createTagApi(name)
const tag = response.data.ocs.data
// Mirror backend logic: if Other's sortOrder collides with the new tag's, bump it by 1.
const otherTag = Object.values(tags).find((t) => t.type === 'other')
if (otherTag && otherTag.sortOrder === tag.sortOrder) {
otherTag.sortOrder += 1
}
tags[tag.id] = tag
return tag
}
Expand Down
Loading