UNPKG

@zendesk/zcli-themes

Version:

zcli theme commands live here

218 lines (217 loc) 8.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const sinon = require("sinon"); const test_1 = require("@oclif/test"); const getManifest = require("./getManifest"); const getTemplates = require("./getTemplates"); const getVariables = require("./getVariables"); const getAssets = require("./getAssets"); const rewriteManifest = require("./rewriteManifest"); const rewriteTemplates = require("./rewriteTemplates"); const zcli_core_1 = require("@zendesk/zcli-core"); const errors = require("@oclif/core/lib/errors"); const migrate_1 = require("./migrate"); const manifest = { name: 'Copenhagen theme', author: 'Jane Doe', version: '1.0.1', api_version: 1, settings: [ { variables: [ { identifier: 'color', type: 'color', value: '#999' }, { identifier: 'logo', type: 'file' } ] } ] }; const flags = { bind: 'localhost', port: 1000, logs: true, livereload: false }; describe('migrate', () => { beforeEach(() => { sinon.restore(); }); it('calls the migrations endpoint with the correct payload and rewrites files', async () => { const getManifestStub = sinon.stub(getManifest, 'default'); const getTemplatesStub = sinon.stub(getTemplates, 'default'); const getVariablesStub = sinon.stub(getVariables, 'default'); const getAssetsStub = sinon.stub(getAssets, 'default'); const rewriteManifestStub = sinon.stub(rewriteManifest, 'default'); const rewriteTemplatesStub = sinon.stub(rewriteTemplates, 'default'); const requestStub = sinon.stub(zcli_core_1.request, 'requestAPI'); getManifestStub.withArgs('theme/path').returns(manifest); getTemplatesStub.withArgs('theme/path').returns({ home_page: '<h1>Home</h1>', 'article_pages/product_updates': '<h1>Product updates</h1>', 'custom_pages/faq': '<h1>FAQ</h1>' }); getVariablesStub.withArgs('theme/path', manifest.settings, flags).returns([ { identifier: 'color', type: 'color', value: '#999' }, { identifier: 'logo', type: 'file', value: 'http://localhost:1000/guide/settings/logo.png' } ]); getAssetsStub.withArgs('theme/path', flags).returns([ [ { base: 'background.png', dir: '', ext: '.png', name: 'background', root: '' }, 'http://localhost:1000/guide/assets/background.png' ] ]); requestStub.returns(Promise.resolve({ status: 200, statusText: 'OK', data: { metadata: { api_version: 2 }, templates: { home_page: '<h1>Updated Home</h1>', 'article_pages/product_updates': '<h1>Updated Product updates</h1>', 'custom_pages/faq': '<h1>Updated FAQ</h1>' } } })); await (0, migrate_1.default)('theme/path', flags); (0, test_1.expect)(requestStub.calledWith('/hc/api/internal/theming/migrations', sinon.match({ method: 'POST', headers: { 'X-Zendesk-Request-Originator': 'zcli themes:migrate' }, data: { templates: { home_page: '<h1>Home</h1>', 'article_pages/product_updates': '<h1>Product updates</h1>', 'custom_pages/faq': '<h1>FAQ</h1>', assets: { 'background.png': 'http://localhost:1000/guide/assets/background.png' }, variables: { color: '#999', logo: 'http://localhost:1000/guide/settings/logo.png' }, metadata: { api_version: 1 } } } }))).to.equal(true); (0, test_1.expect)(rewriteManifestStub.calledWith('theme/path', 2)).to.equal(true); (0, test_1.expect)(rewriteTemplatesStub.calledWith('theme/path', { home_page: '<h1>Updated Home</h1>', 'article_pages/product_updates': '<h1>Updated Product updates</h1>', 'custom_pages/faq': '<h1>Updated FAQ</h1>' })).to.equal(true); }); it('throws an error when validation fails with template errors', async () => { sinon.stub(getManifest, 'default').returns(manifest); sinon.stub(getTemplates, 'default').returns({}); sinon.stub(getVariables, 'default').returns([]); sinon.stub(getAssets, 'default').returns([]); sinon.stub(zcli_core_1.request, 'requestAPI').throws({ response: { data: { template_errors: { home_page: [ { description: "'articles' does not exist", line: 10, column: 6, length: 7 } ], footer: [ { description: "'alternative_locales' does not exist", line: 6, column: 12, length: 10 } ] } } } }); const errorStub = sinon.stub(errors, 'error').callThrough(); try { await (0, migrate_1.default)('theme/path', flags); } catch (_a) { const [call] = errorStub.getCalls(); const [error] = call.args; (0, test_1.expect)(error).to.contain('theme/path/templates/home_page.hbs:10:6'); (0, test_1.expect)(error).to.contain("'articles' does not exist"); (0, test_1.expect)(error).to.contain('theme/path/templates/footer.hbs:6:12'); (0, test_1.expect)(error).to.contain("'alternative_locales' does not exist"); } }); it('throws an error when there is a general error', async () => { sinon.stub(getManifest, 'default').returns(manifest); sinon.stub(getTemplates, 'default').returns({}); sinon.stub(getVariables, 'default').returns([]); sinon.stub(getAssets, 'default').returns([]); sinon.stub(zcli_core_1.request, 'requestAPI').throws({ response: { data: { general_error: 'Something went wrong' } } }); const errorStub = sinon.stub(errors, 'error').callThrough(); try { await (0, migrate_1.default)('theme/path', flags); } catch (_a) { const [call] = errorStub.getCalls(); const [error] = call.args; (0, test_1.expect)(error).to.equal('Something went wrong'); } }); it('throws an error when there is a response with a message', async () => { sinon.stub(getManifest, 'default').returns(manifest); sinon.stub(getTemplates, 'default').returns({}); sinon.stub(getVariables, 'default').returns([]); sinon.stub(getAssets, 'default').returns([]); sinon.stub(zcli_core_1.request, 'requestAPI').throws({ response: { data: {} }, message: 'Network error' }); const errorStub = sinon.stub(errors, 'error').callThrough(); try { await (0, migrate_1.default)('theme/path', flags); } catch (_a) { const [call] = errorStub.getCalls(); const [error] = call.args; (0, test_1.expect)(error).to.equal('Network error'); } }); it('throws an error when there is no response', async () => { sinon.stub(getManifest, 'default').returns(manifest); sinon.stub(getTemplates, 'default').returns({}); sinon.stub(getVariables, 'default').returns([]); sinon.stub(getAssets, 'default').returns([]); const axiosError = new Error('Connection refused'); sinon.stub(zcli_core_1.request, 'requestAPI').throws(axiosError); const errorStub = sinon.stub(errors, 'error').callThrough(); try { await (0, migrate_1.default)('theme/path', flags); } catch (_a) { const [call] = errorStub.getCalls(); const [error] = call.args; (0, test_1.expect)(error).to.equal(axiosError); } }); });