Add unit test

This commit is contained in:
Hongbo Wu 2022-05-19 16:36:35 +08:00
parent b8225fc8c8
commit 34349e925c
5 changed files with 111 additions and 16 deletions

View file

@ -136,9 +136,8 @@ export const deleteLabelInPages = async (
refresh: ctx.refresh,
})
console.log('updated pages', body.updated)
await ctx.pubsub.entityDeleted(EntityType.LABEL, label, ctx.uid)
body.updated > 0 &&
(await ctx.pubsub.entityDeleted(EntityType.LABEL, label, ctx.uid))
return true
} catch (e) {
@ -156,7 +155,7 @@ export const updateLabelInPage = async (
index: INDEX_ALIAS,
body: {
script: {
source: `ctx._source.labels.removeIf(h -> h.name == params.label.name);
source: `ctx._source.labels.removeIf(l -> l.id == params.label.id);
ctx._source.labels.add(params.label)`,
lang: 'painless',
params: {
@ -176,7 +175,7 @@ export const updateLabelInPage = async (
path: 'labels',
query: {
term: {
'labels.name': label.name,
'labels.id': label.id,
},
},
},
@ -186,11 +185,11 @@ export const updateLabelInPage = async (
},
},
refresh: ctx.refresh,
conflicts: 'proceed', // ignore conflicts
})
console.log('updated pages', body.updated)
await ctx.pubsub.entityUpdated<Label>(EntityType.LABEL, label, ctx.uid)
body.updated > 0 &&
(await ctx.pubsub.entityUpdated(EntityType.LABEL, label, ctx.uid))
return true
} catch (e) {

View file

@ -226,7 +226,7 @@ export const setLabelsResolver = authorized<
const labels = await getRepository(Label).find({
where: { id: In(labelIds), user: { id: user.id } },
relations: ['user'],
select: ['id', 'name', 'color', 'description', 'createdAt'],
})
if (labels.length !== labelIds.length) {
return {
@ -282,9 +282,9 @@ export const updateLabelResolver = authorized<
}
}
const label = await getRepository(Label).findOneBy({
id: labelId,
user: { id: uid },
const label = await getRepository(Label).findOne({
where: { id: labelId, user: { id: uid } },
select: ['id', 'name', 'color', 'description', 'createdAt'],
})
if (!label) {
return {

View file

@ -20,6 +20,7 @@ describe('Labels API', () => {
let authToken: string
let page: Page
let labels: Label[]
let existingLabelOfLink: Label
before(async () => {
// create test user and login
@ -36,12 +37,18 @@ describe('Labels API', () => {
labels = [label1, label2]
// create a page with label
const existingLabelOfLink = await createTestLabel(
existingLabelOfLink = await createTestLabel(
user,
'different_label',
'#dddddd'
)
page = await createTestElasticPage(user, [existingLabelOfLink])
page = await createTestElasticPage(user, [
{
id: existingLabelOfLink.id,
name: existingLabelOfLink.name,
color: existingLabelOfLink.color,
},
])
})
after(async () => {
@ -319,4 +326,82 @@ describe('Labels API', () => {
return graphqlRequest(query, invalidAuthToken).expect(500)
})
})
describe('Update label', () => {
let query: string
let labelId: string
let name: string
let color: string
beforeEach(() => {
query = `
mutation {
updateLabel(
input: {
labelId: "${labelId}",
name: "${name}",
color: "${color}"
}
) {
... on UpdateLabelSuccess {
label {
id
name
color
}
}
... on UpdateLabelError {
errorCodes
}
}
}
`
})
context('when labels exists', () => {
before(() => {
labelId = existingLabelOfLink.id
name = 'Updated label'
color = '#aabbcc'
})
it('should return the updated label', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
expect(res.body.data.updateLabel.label).to.eql({
id: labelId,
name,
color,
})
})
it('should update the label in db', async () => {
await graphqlRequest(query, authToken).expect(200)
const newLabel = await getRepository(Label).findOne({
where: { id: labelId },
})
expect(newLabel?.name).to.eql(name)
expect(newLabel?.color).to.eql(color)
})
it('should update the label', async () => {
await graphqlRequest(query, authToken).expect(200)
const updatedPage = await getPageById(page.id)
expect(updatedPage?.labels?.[0].name).to.eql(name)
expect(updatedPage?.labels?.[0].color).to.eql(color)
})
})
context('when labels not exist', () => {
before(() => {
labelId = generateFakeUuid()
})
it('should return error code NOT_FOUND', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
expect(res.body.data.updateLabel.errorCodes).to.eql(['NOT_FOUND'])
})
})
})
})

View file

@ -2,10 +2,9 @@ import { createApp } from '../src/server'
import supertest from 'supertest'
import { v4 } from 'uuid'
import { corsConfig } from '../src/utils/corsConfig'
import { ArticleSavingRequestStatus, Page } from '../src/elastic/types'
import { ArticleSavingRequestStatus, Label, Page } from '../src/elastic/types'
import { PageType } from '../src/generated/graphql'
import { User } from '../src/entity/user'
import { Label } from '../src/entity/label'
import { createPubSubClient } from '../src/datalayer/pubsub'
import { createPage, getPageById } from '../src/elastic/pages'

View file

@ -50,9 +50,21 @@
"labels": {
"type": "nested",
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "keyword",
"normalizer": "lowercase_normalizer"
},
"color": {
"type": "keyword"
},
"description": {
"type": "text"
},
"createdAt": {
"type": "date"
}
}
},