Add test for setLabelsForHighlight resolver

This commit is contained in:
Hongbo Wu 2022-06-08 15:25:11 +08:00
parent 4894b3cfd9
commit 145867fe05
2 changed files with 94 additions and 2 deletions

View file

@ -583,5 +583,4 @@ export const functionResolvers = {
...resultResolveTypeResolver('Webhook'),
...resultResolveTypeResolver('ApiKeys'),
...resultResolveTypeResolver('RevokeApiKey'),
...resultResolveTypeResolver('SetLabelsForHighlight'),
}

View file

@ -9,11 +9,12 @@ import { Label } from '../../src/entity/label'
import { expect } from 'chai'
import 'mocha'
import { User } from '../../src/entity/user'
import { Page, PageContext } from '../../src/elastic/types'
import { Highlight, 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'
import { addHighlightToPage } from '../../src/elastic/highlights'
describe('Labels API', () => {
const username = 'fakeUser'
@ -440,4 +441,96 @@ describe('Labels API', () => {
})
})
})
describe('Set labels for highlight', () => {
let query: string
let highlightId: string
let labelIds: string[] = []
beforeEach(() => {
query = `
mutation {
setLabelsForHighlight(
input: {
highlightId: "${highlightId}",
labelIds: [
"${labelIds[0]}",
"${labelIds[1]}"
]
}
) {
... on SetLabelsSuccess {
labels {
id
name
}
}
... on SetLabelsError {
errorCodes
}
}
}
`
})
context('when labels exists', () => {
before(async () => {
highlightId = 'highlight-id'
const highlight: Highlight = {
createdAt: new Date(),
id: highlightId,
patch: 'test patch',
quote: 'test quote',
shortId: 'test shortId',
userId: user.id,
}
await addHighlightToPage(page.id, highlight, ctx)
labelIds = [labels[0].id, labels[1].id]
})
it('should set labels for highlight', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
expect(
res.body.data.setLabelsForHighlight.labels.map((l: any) => l.id)
).to.eql(labelIds)
})
})
context('when labels not exist', () => {
before(async () => {
highlightId = 'highlight-id-2'
const highlight: Highlight = {
createdAt: new Date(),
id: highlightId,
patch: 'test patch',
quote: 'test quote',
shortId: 'test shortId',
userId: user.id,
}
await addHighlightToPage(page.id, highlight, ctx)
labelIds = [generateFakeUuid(), generateFakeUuid()]
})
it('should return error code NOT_FOUND', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
expect(res.body.data.setLabelsForHighlight.errorCodes).to.eql([
'NOT_FOUND',
])
})
})
context('when highlight not exist', () => {
before(() => {
highlightId = generateFakeUuid()
labelIds = [labels[0].id, labels[1].id]
})
it('should return error code NOT_FOUND', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
expect(res.body.data.setLabelsForHighlight.errorCodes).to.eql([
'NOT_FOUND',
])
})
})
})
})