From 2b9ee8eda8b9735dd3685611ea0f5ec11a12af38 Mon Sep 17 00:00:00 2001 From: szchenghuang <6309412+szchenghuang@users.noreply.github.com> Date: Fri, 11 Oct 2024 15:45:20 +0800 Subject: [PATCH] Tests for register command upgraded. --- packages/cli/src/tests/register.test.js | 327 +++++++++++++----------- 1 file changed, 174 insertions(+), 153 deletions(-) diff --git a/packages/cli/src/tests/register.test.js b/packages/cli/src/tests/register.test.js index 1dc4470bc..3d795b8cd 100644 --- a/packages/cli/src/tests/register.test.js +++ b/packages/cli/src/tests/register.test.js @@ -1,5 +1,7 @@ const fs = require('fs'); -const oclif = require('@oclif/test'); +const nock = require('nock'); +const { expect } = require('chai'); +const { captureOutput, runCommand } = require('@oclif/test'); const { BASE_ENDPOINT, MIN_TITLE_LENGTH, @@ -36,128 +38,144 @@ describe('RegisterCommand', () => { restoreDeployKey(); }); - function getTestObj() { - return oclif.test.nock(BASE_ENDPOINT, (mockApi) => - mockApi - .get('/api/platform/cli/apps/fields-choices') - .reply(200, registerFieldChoices) - ); + function setup() { + nock(BASE_ENDPOINT) + .get('/api/platform/cli/apps/fields-choices') + .reply(200, registerFieldChoices); } describe('zapier register should enforce character minimum on title', function () { - getTestObj() - .command(['register', 't']) - .catch((ctx) => { - oclif - .expect(ctx.message) - .to.contain( - `Please provide a title that is ${MIN_TITLE_LENGTH} characters or more.` - ); - }) - .it('zapier register should enforce character minimum on title flag'); + it('zapier register should enforce character minimum on title flag', async function () { + setup(); + + await captureOutput(async function () { + const { error } = await runCommand(['register', 't']); + expect(error.message).to.contain( + `Please provide a title that is ${MIN_TITLE_LENGTH} characters or more.` + ); + }); + }); }); describe('zapier register should enforce character limits on flags', function () { - getTestObj() - .command([ - 'register', - '--desc', - 'Cupidatat non elit non enim enim cupidatat ea in consequat exercitation do nisi occaecat amet id deserunt nostrud quis aliqua id fugiat sit elit.', - ]) - .catch((ctx) => { - oclif - .expect(ctx.message) - .to.contain( - `Please provide a description that is ${MAX_DESCRIPTION_LENGTH} characters or less.` - ); - }) - .it('zapier register should enforce character limit on desc flag'); + it('zapier register should enforce character limit on desc flag', async function () { + setup(); + + await captureOutput(async function () { + const { error } = await runCommand([ + 'register', + '--desc', + 'Cupidatat non elit non enim enim cupidatat ea in consequat exercitation do nisi occaecat amet id deserunt nostrud quis aliqua id fugiat sit elit.', + ]); + expect(error.message).to.contain( + `Please provide a description that is ${MAX_DESCRIPTION_LENGTH} characters or less.` + ); + }); + }); }); describe('zapier register should validate enum fields that are passed in as flags', function () { - getTestObj() - .command(['register', '--role', 'invalidRole']) - .catch((ctx) => { - oclif - .expect(ctx.message) - .to.contain('invalidRole is not a valid value for role'); - }) - .it('zapier register should throw error for invalid role'); - - getTestObj() - .command(['register', '--category', 'invalidCategory']) - .catch((ctx) => { - oclif - .expect(ctx.message) - .to.contain('invalidCategory is not a valid value for category'); - }) - .it('zapier register should throw error for invalid category'); - - getTestObj() - .command(['register', '--audience', 'invalidAudience']) - .catch((ctx) => { - oclif - .expect(ctx.message) - .to.contain('invalidAudience is not a valid value for audience'); - }) - .it('zapier register should throw error for invalid audience'); + it('zapier register should throw error for invalid role', async function () { + setup(); + + await captureOutput(async function () { + const { error } = await runCommand([ + 'register', + '--role', + 'invalidRole', + ]); + expect(error.message).to.contain( + 'invalidRole is not a valid value for role' + ); + }); + }); + + it('zapier register should throw error for invalid category', async function () { + setup(); + + await captureOutput(async function () { + const { error } = await runCommand([ + 'register', + '--category', + 'invalidCategory', + ]); + expect(error.message).to.contain( + 'invalidCategory is not a valid value for category' + ); + }); + }); + + it('zapier register should throw error for invalid audience', async function () { + setup(); + + await captureOutput(async function () { + const { error } = await runCommand([ + 'register', + '--audience', + 'invalidAudience', + ]); + expect(error.message).to.contain( + 'invalidAudience is not a valid value for audience' + ); + }); + }); }); describe('zapier register should accept all data via flags', function () { - function getTestObj() { - return oclif.test.nock(BASE_ENDPOINT, (mockApi) => - mockApi - .get('/api/platform/cli/apps/fields-choices') - .reply(200, registerFieldChoices) - .post('/api/platform/cli/apps') - .query({ formId: 'create' }) - .reply(201, privateApp) - ); + function setup() { + return nock(BASE_ENDPOINT) + .get('/api/platform/cli/apps/fields-choices') + .reply(200, registerFieldChoices) + .post('/api/platform/cli/apps') + .query({ formId: 'create' }) + .reply(201, privateApp); } - getTestObj() - .stdout() - .stderr() - .command([ - 'register', - 'My Cool Integration', - '--desc', - 'My Cool Integration helps you integrate your apps with the apps that you need.', - '--url', - 'https://www.zapier.com', - '--audience', - 'private', - '--role', - 'employee', - '--category', - 'marketing-automation', - '--subscribe', - ]) - .it( - 'zapier register should successfully register an app with all data provided' - ); + it('zapier register should successfully register an app with all data provided', async function () { + setup(); + + await captureOutput(async function () { + const { error } = await runCommand([ + 'register', + 'My Cool Integration', + '--desc', + 'My Cool Integration helps you integrate your apps with the apps that you need.', + '--url', + 'https://www.zapier.com', + '--audience', + 'private', + '--role', + 'employee', + '--category', + 'marketing-automation', + '--subscribe', + ]); + + expect(error.message).to.contain( + 'invalidAudience is not a valid value for audience' + ); + }); + }); }); describe('zapier register should update existing app', function () { - function getTestObj(isPublic) { + function setup(isPublic) { const exportedApp = isPublic ? publicApp : privateApp; - return oclif.test.nock(BASE_ENDPOINT, (mockApi) => - mockApi - .get('/api/platform/cli/apps/fields-choices') - .reply(200, registerFieldChoices) - .get(`/api/platform/cli/apps/${exportedApp.id}`) - .reply(200, exportedApp) - .put(`/api/platform/cli/apps/${exportedApp.id}`, { - title: 'Hello', - description: 'Helps you in some way.', - homepage_url: 'https://example.com', - intention: 'global', - role: 'contractor', - app_category: 'productivity', - }) - .optionally() - .reply(201, exportedApp) - ); + return nock(BASE_ENDPOINT) + .get('/api/platform/cli/apps/fields-choices') + .reply(200, registerFieldChoices) + .get(`/api/platform/cli/apps/${exportedApp.id}`) + .reply(200, exportedApp) + .put(`/api/platform/cli/apps/${exportedApp.id}`, { + title: 'Hello', + description: 'Helps you in some way.', + homepage_url: 'https://example.com', + intention: 'global', + role: 'contractor', + app_category: 'productivity', + }) + .optionally() + .reply(201, exportedApp); } fs.writeFileSync( @@ -165,53 +183,56 @@ describe('RegisterCommand', () => { `{"id":${privateApp.id},"key":"App${privateApp.id}"}` ); - getTestObj() - .stdout() - .stderr() - .command([ - 'register', - 'Hello', - '-D', - 'Helps you in some way.', - '-u', - 'https://example.com', - '-a', - 'global', - '-r', - 'contractor', - '-c', - 'productivity', - '--yes', - ]) - .it('zapier register --yes should update an app without prompts'); - - getTestObj(true) - .stdout() - .stderr() - .command([ - 'register', - 'Hello', - '-D', - 'Helps you in some way.', - '-u', - 'https://example.com', - '-a', - 'global', - '-r', - 'contractor', - '-c', - 'productivity', - '--yes', - ]) - .catch((ctx) => { - oclif - .expect(ctx.message) - .to.contain( - "You can't edit settings for this integration. To edit your integration details on Zapier's public app directory, email partners@zapier.com." - ); - }) - .it( - 'zapier register should not allow a user to update a pre-existing public app' - ); + it('zapier register should successfully register an app with all data provided', async function () { + setup(); + + await captureOutput(async function () { + const { error } = await runCommand([ + 'register', + 'Hello', + '-D', + 'Helps you in some way.', + '-u', + 'https://example.com', + '-a', + 'global', + '-r', + 'contractor', + '-c', + 'productivity', + '--yes', + ]); + + expect(error.message).to.contain( + 'zapier register --yes should update an app without prompts' + ); + }); + }); + + it('zapier register should not allow a user to update a pre-existing public app', async function () { + setup(true); + + await captureOutput(async function () { + const { error } = await runCommand([ + 'register', + 'Hello', + '-D', + 'Helps you in some way.', + '-u', + 'https://example.com', + '-a', + 'global', + '-r', + 'contractor', + '-c', + 'productivity', + '--yes', + ]); + + expect(error.message).to.contain( + "You can't edit settings for this integration. To edit your integration details on Zapier's public app directory, email partners@zapier.com." + ); + }); + }); }); });