-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding a themes delete command
- Loading branch information
1 parent
52d8fd7
commit 375011d
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
}) | ||
}) | ||
}) |