Skip to content

Commit

Permalink
feat: adding a themes list command
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-almeida committed Jul 31, 2023
1 parent cd49d29 commit 52d8fd7
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/list.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'
import getBrandId from '../../lib/getBrandId'

export default class List extends Command {
static description = 'list installed themes'

static enableJsonFlag = true

static flags = {
brandId: Flags.string({ description: 'The id of the brand where the themes are installed' })
}

static examples = [
'$ zcli themes:list --brandId=123456'
]

static strict = false

async run () {
let { flags: { brandId } } = await this.parse(List)

brandId = brandId || await getBrandId()

try {
CliUx.ux.action.start('Listing themes')
const { data: { themes } } = await request.requestAPI(`/api/v2/guide/theming/themes?brand_id=${brandId}`, {
headers: {
'X-Zendesk-Request-Originator': 'zcli themes:list'
},
validateStatus: (status: number) => status === 200
})
CliUx.ux.action.stop('Ok')
this.log(chalk.green('Themes listed successfully'), themes)
return { themes }
} 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/list.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect, test } from '@oclif/test'
import ListCommand from '../../src/commands/themes/list'
import env from './env'

describe('themes:list', function () {
describe('successful list', () => {
const success = test
.env(env)
.nock('https://z3ntest.zendesk.com', api => api
.get('/api/v2/guide/theming/themes?brand_id=1111')
.reply(200, { themes: [] }))

success
.stdout()
.it('should display success message when thes themes are listed successfully', async ctx => {
await ListCommand.run(['--brandId', '1111'])
expect(ctx.stdout).to.contain('Themes listed successfully []')
})

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

describe('list failure', () => {
test
.env(env)
.nock('https://z3ntest.zendesk.com', api => api
.get('/api/v2/guide/theming/themes?brand_id=1111')
.reply(500, {
errors: [{
code: 'InternalError',
title: 'Something went wrong'
}]
}))
.stderr()
.it('should report publish errors', async ctx => {
try {
await ListCommand.run(['--brandId', '1111'])
} catch (error) {
expect(ctx.stderr).to.contain('!')
expect(error.message).to.contain('InternalError - Something went wrong')
}
})
})
})

0 comments on commit 52d8fd7

Please sign in to comment.