diff --git a/packages/api/src/services/labels.ts b/packages/api/src/services/labels.ts index 7b908dd7f..bf9577b74 100644 --- a/packages/api/src/services/labels.ts +++ b/packages/api/src/services/labels.ts @@ -54,5 +54,15 @@ export const addLabelToPage = async ( console.log('adding label to page', label.name, pageId) - return addLabelInPage(pageId, labelEntity, ctx) + return addLabelInPage( + pageId, + { + id: labelEntity.id, + name: labelEntity.name, + color: labelEntity.color, + description: labelEntity.description, + createdAt: labelEntity.createdAt, + }, + ctx + ) } diff --git a/packages/api/test/resolvers/labels.test.ts b/packages/api/test/resolvers/labels.test.ts index 893d4b634..6d7814fc7 100644 --- a/packages/api/test/resolvers/labels.test.ts +++ b/packages/api/test/resolvers/labels.test.ts @@ -9,9 +9,11 @@ import { Label } from '../../src/entity/label' import { expect } from 'chai' import 'mocha' import { User } from '../../src/entity/user' -import { Page } from '../../src/elastic/types' +import { Page, PageContext } from '../../src/elastic/types' import { getRepository } from '../../src/entity/utils' import { getPageById } from '../../src/elastic/pages' +import { addLabelInPage } from '../../src/elastic/labels' +import { createPubSubClient } from '../../src/datalayer/pubsub' describe('Labels API', () => { const username = 'fakeUser' @@ -20,7 +22,7 @@ describe('Labels API', () => { let authToken: string let page: Page let labels: Label[] - let existingLabelOfLink: Label + let ctx: PageContext before(async () => { // create test user and login @@ -37,7 +39,7 @@ describe('Labels API', () => { labels = [label1, label2] // create a page with label - existingLabelOfLink = await createTestLabel( + const existingLabelOfLink = await createTestLabel( user, 'different_label', '#dddddd' @@ -49,6 +51,12 @@ describe('Labels API', () => { color: existingLabelOfLink.color, }, ]) + + ctx = { + pubsub: createPubSubClient(), + refresh: true, + uid: user.id, + } }) after(async () => { @@ -205,8 +213,10 @@ describe('Labels API', () => { }) context('when label exists', () => { + let toDeleteLabel: Label + before(async () => { - const toDeleteLabel = await createTestLabel(user, 'label4', '#ffffff') + toDeleteLabel = await createTestLabel(user, 'label4', '#ffffff') labelId = toDeleteLabel.id }) @@ -215,6 +225,19 @@ describe('Labels API', () => { const label = await getRepository(Label).findOneBy({ id: labelId }) expect(label).to.not.exist }) + + context('when a page has this label', () => { + before(async () => { + await addLabelInPage(page.id, toDeleteLabel, ctx) + }) + + it('should update page', async () => { + await graphqlRequest(query, authToken).expect(200) + const updatedPage = await getPageById(page.id) + + expect(updatedPage?.labels).not.to.include(toDeleteLabel) + }) + }) }) context('when label not exist', () => { @@ -300,7 +323,7 @@ describe('Labels API', () => { }) }) - context('when link not exist', () => { + context('when page not exist', () => { before(() => { pageId = generateFakeUuid() labelIds = [labels[0].id, labels[1].id] @@ -359,8 +382,11 @@ describe('Labels API', () => { }) context('when labels exists', () => { - before(() => { - labelId = existingLabelOfLink.id + let toUpdateLabel: Label + + before(async () => { + toUpdateLabel = await createTestLabel(user, 'label5', '#ffffff') + labelId = toUpdateLabel.id name = 'Updated label' color = '#aabbcc' }) @@ -376,20 +402,30 @@ describe('Labels API', () => { it('should update the label in db', async () => { await graphqlRequest(query, authToken).expect(200) - const newLabel = await getRepository(Label).findOne({ + const updatedLabel = await getRepository(Label).findOne({ where: { id: labelId }, }) - expect(newLabel?.name).to.eql(name) - expect(newLabel?.color).to.eql(color) + expect(updatedLabel?.name).to.eql(name) + expect(updatedLabel?.color).to.eql(color) }) - it('should update the label', async () => { - await graphqlRequest(query, authToken).expect(200) - const updatedPage = await getPageById(page.id) + context('when a page has the label', () => { + before(async () => { + await addLabelInPage(page.id, toUpdateLabel, ctx) + }) - expect(updatedPage?.labels?.[0].name).to.eql(name) - expect(updatedPage?.labels?.[0].color).to.eql(color) + it('should update the page with the label', async () => { + await graphqlRequest(query, authToken).expect(200) + + const updatedPage = await getPageById(page.id) + const updatedLabel = updatedPage?.labels?.filter( + (l) => l.id === labelId + )?.[0] + + expect(updatedLabel?.name).to.eql(name) + expect(updatedLabel?.color).to.eql(color) + }) }) })