Skip to content

Commit

Permalink
label.set/description.set: allow to remove a term by passing an empty…
Browse files Browse the repository at this point in the history
… string as value
  • Loading branch information
maxlath committed Oct 16, 2022
1 parent defabeb commit 612eed6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 8 deletions.
16 changes: 16 additions & 0 deletions docs/how_to.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ wbEdit.label.set({
value: 'Bac à sable bulgroz'
})
```
This can also be used to remove a label
```js
wbEdit.label.set({
id: 'Q4115189',
language: 'fr',
value: ''
})
```

### Description
#### set description
Expand All @@ -336,6 +344,14 @@ wbEdit.description.set({
value: 'description du Bac à sable bulgroz'
})
```
This can also be used to remove a description
```js
wbEdit.description.set({
id: 'Q4115189',
language: 'fr',
value: ''
})
```

### Alias
#### add aliases
Expand Down
7 changes: 4 additions & 3 deletions lib/label_or_description/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ const validate = require('../validate')
const error_ = require('../error')

module.exports = name => params => {
const { id, language, value } = params
const { id, language } = params
let { value } = params
const action = `wbset${name}`

validate.entity(id)
validate.language(language)
if (value == null) throw error_.new(`missing ${name}`, params)
validate.labelOrDescription(name, value)
if (value === undefined) throw error_.new(`missing ${name}`, params)
if (value === null) value = ''

return {
action,
Expand Down
28 changes: 25 additions & 3 deletions tests/integration/description/set.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
require('should')
const should = require('should')
const config = require('config')
const wbEdit = require('root')(config)
const { randomString } = require('tests/unit/utils')
const { getSandboxItemId } = require('tests/integration/utils/sandbox_entities')
const { getSandboxItemId, getRefreshedEntity } = require('tests/integration/utils/sandbox_entities')
const language = 'fr'

describe('description set', function () {
this.timeout(20 * 1000)
before('wait for instance', require('tests/integration/utils/wait_for_instance'))

it('should set a label', async () => {
it('should set a description', async () => {
const id = await getSandboxItemId()
const value = `Bac à Sable (${randomString()})`
const res = await wbEdit.description.set({ id, language, value })
res.success.should.equal(1)
const item = await getRefreshedEntity(id)
item.descriptions[language].value.should.equal(value)
})

it('should remove a description when passed value=null', async () => {
const id = await getSandboxItemId()
const value = `Bac à Sable (${randomString()})`
await wbEdit.description.set({ id, language, value })
const res = await wbEdit.description.set({ id, language, value: null })
res.success.should.equal(1)
const item = await getRefreshedEntity(id)
should(item.descriptions[language]).not.be.ok()
})

it('should remove a description when passed value=""', async () => {
const id = await getSandboxItemId()
const value = `Bac à Sable (${randomString()})`
await wbEdit.description.set({ id, language, value })
const res = await wbEdit.description.set({ id, language, value: '' })
res.success.should.equal(1)
const item = await getRefreshedEntity(id)
should(item.descriptions[language]).not.be.ok()
})
})
26 changes: 24 additions & 2 deletions tests/integration/label/set.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require('should')
const should = require('should')
const config = require('config')
const wbEdit = require('root')(config)
const { randomString } = require('tests/unit/utils')
const { getSandboxItemId } = require('tests/integration/utils/sandbox_entities')
const { getSandboxItemId, getRefreshedEntity } = require('tests/integration/utils/sandbox_entities')
const language = 'fr'

describe('label set', function () {
Expand All @@ -14,5 +14,27 @@ describe('label set', function () {
const value = `Bac à Sable (${randomString()})`
const res = await wbEdit.label.set({ id, language, value })
res.success.should.equal(1)
const item = await getRefreshedEntity(id)
item.labels[language].value.should.equal(value)
})

it('should remove a label when passed value=null', async () => {
const id = await getSandboxItemId()
const value = `Bac à Sable (${randomString()})`
await wbEdit.label.set({ id, language, value })
const res = await wbEdit.label.set({ id, language, value: '' })
res.success.should.equal(1)
const item = await getRefreshedEntity(id)
should(item.labels[language]).not.be.ok()
})

it('should remove a label when passed value=""', async () => {
const id = await getSandboxItemId()
const value = `Bac à Sable (${randomString()})`
await wbEdit.label.set({ id, language, value })
const res = await wbEdit.label.set({ id, language, value: '' })
res.success.should.equal(1)
const item = await getRefreshedEntity(id)
should(item.labels[language]).not.be.ok()
})
})

0 comments on commit 612eed6

Please sign in to comment.