Skip to content

Commit

Permalink
feat: adding a themes delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-almeida committed Jul 31, 2023
1 parent 52d8fd7 commit 375011d
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
42 changes: 42 additions & 0 deletions packages/zcli-themes/src/commands/themes/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Command, Flags, CliUx } from '@oclif/core'
import { request } from '@zendesk/zcli-core'
import * as chalk from 'chalk'

export default class Delete extends Command {
static description = 'delete a theme'

static enableJsonFlag = true

static flags = {
themeId: Flags.string({ description: 'The id of the theme to delete' })
}

static examples = [
'$ zcli themes:delete --themeId=abcd'
]

static strict = false

async run () {
let { flags: { themeId } } = await this.parse(Delete)

themeId = themeId || await CliUx.ux.prompt('Theme ID')

try {
CliUx.ux.action.start('Deleting theme')
await request.requestAPI(`/api/v2/guide/theming/themes/${themeId}`, {
method: 'delete',
headers: {
'X-Zendesk-Request-Originator': 'zcli themes:delete'
},
validateStatus: (status: number) => status === 204
})
CliUx.ux.action.stop('Ok')
this.log(chalk.green('Theme deleted successfully'), `theme ID: ${themeId}`)
return { themeId }
} catch (e: any) {
const [error] = e.response.data.errors
this.error(`${error.code} - ${error.title}`)
}
}
}
49 changes: 49 additions & 0 deletions packages/zcli-themes/tests/functional/delete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect, test } from '@oclif/test'
import DeleteCommand from '../../src/commands/themes/delete'
import env from './env'

describe('themes:delete', function () {
describe('successful deletion', () => {
const success = test
.env(env)
.nock('https://z3ntest.zendesk.com', api => api
.delete('/api/v2/guide/theming/themes/1234')
.reply(204))

success
.stdout()
.it('should display success message when the theme is deleted successfully', async ctx => {
await DeleteCommand.run(['--themeId', '1234'])
expect(ctx.stdout).to.contain('Theme deleted successfully theme ID: 1234')
})

success
.stdout()
.it('should return an object containing the theme ID when ran with --json', async ctx => {
await DeleteCommand.run(['--themeId', '1234', '--json'])
expect(ctx.stdout).to.equal(JSON.stringify({ themeId: '1234' }, null, 2) + '\n')
})
})

describe('delete failure', () => {
test
.env(env)
.nock('https://z3ntest.zendesk.com', api => api
.delete('/api/v2/guide/theming/themes/1234')
.reply(400, {
errors: [{
code: 'ThemeNotFound',
title: 'Invalid id'
}]
}))
.stderr()
.it('should report delete errors', async ctx => {
try {
await DeleteCommand.run(['--themeId', '1234'])
} catch (error) {
expect(ctx.stderr).to.contain('!')
expect(error.message).to.contain('ThemeNotFound - Invalid id')
}
})
})
})

0 comments on commit 375011d

Please sign in to comment.