mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Add unit test
This commit is contained in:
parent
b8225fc8c8
commit
34349e925c
5 changed files with 111 additions and 16 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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'])
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue