diff --git a/packages/zcli-themes/src/commands/themes/delete.ts b/packages/zcli-themes/src/commands/themes/delete.ts new file mode 100644 index 00000000..196d09c9 --- /dev/null +++ b/packages/zcli-themes/src/commands/themes/delete.ts @@ -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}`) + } + } +} diff --git a/packages/zcli-themes/tests/functional/delete.test.ts b/packages/zcli-themes/tests/functional/delete.test.ts new file mode 100644 index 00000000..ce792d51 --- /dev/null +++ b/packages/zcli-themes/tests/functional/delete.test.ts @@ -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') + } + }) + }) +})