|
| 1 | +/* |
| 2 | +Copyright 2021 Adobe. All rights reserved. |
| 3 | +This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | +of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +
|
| 7 | +Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | +the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | +OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | +governing permissions and limitations under the License. |
| 11 | +*/ |
| 12 | + |
| 13 | +const { cli } = require('cli-ux') |
| 14 | +const { init, mockSdk } = require('@adobe/aio-lib-cloudmanager') |
| 15 | +const { resetCurrentOrgId, setCurrentOrgId } = require('@adobe/aio-lib-ims') |
| 16 | +const ListCommandExecutionsCommand = require('../../../src/commands/cloudmanager/commerce/list-command-executions') |
| 17 | + |
| 18 | +beforeEach(() => { |
| 19 | + resetCurrentOrgId() |
| 20 | +}) |
| 21 | + |
| 22 | +test('list-command-executions (commerce) - missing arg', async () => { |
| 23 | + expect.assertions(2) |
| 24 | + |
| 25 | + const runResult = ListCommandExecutionsCommand.run([]) |
| 26 | + await expect(runResult instanceof Promise).toBeTruthy() |
| 27 | + await expect(runResult).rejects.toSatisfy( |
| 28 | + (err) => err.message.indexOf('Missing 1 required arg') === 0, |
| 29 | + ) |
| 30 | +}) |
| 31 | + |
| 32 | +test('list-command-executions (commerce) - missing config', async () => { |
| 33 | + expect.assertions(2) |
| 34 | + |
| 35 | + const runResult = ListCommandExecutionsCommand.run([ |
| 36 | + '--programId', |
| 37 | + '5', |
| 38 | + '10', |
| 39 | + ]) |
| 40 | + await expect(runResult instanceof Promise).toBeTruthy() |
| 41 | + await expect(runResult).rejects.toSatisfy( |
| 42 | + (err) => |
| 43 | + err.message === |
| 44 | + '[CloudManagerCLI:NO_IMS_CONTEXT] Unable to find IMS context aio-cli-plugin-cloudmanager.', |
| 45 | + ) |
| 46 | +}) |
| 47 | + |
| 48 | +test('list-command-executions (commerce)', async () => { |
| 49 | + setCurrentOrgId('good') |
| 50 | + const result = [ |
| 51 | + { |
| 52 | + id: 100, |
| 53 | + type: 'bin/magento', |
| 54 | + command: 'maintenance:status', |
| 55 | + options: [], |
| 56 | + |
| 57 | + startedAt: '2021-08-17T17:03:18.858+0000', |
| 58 | + completedAt: '2021-08-17T17:03:43.000+0000', |
| 59 | + name: 'magento-cli-952', |
| 60 | + status: 'COMPLETED', |
| 61 | + environmentId: 177253, |
| 62 | + }, |
| 63 | + { |
| 64 | + id: 101, |
| 65 | + type: 'bin/magento', |
| 66 | + command: 'cache:clean', |
| 67 | + options: [], |
| 68 | + |
| 69 | + startedAt: '2021-08-17T17:03:18.858+0000', |
| 70 | + completedAt: '2021-08-17T17:03:43.000+0000', |
| 71 | + name: 'magento-cli-952', |
| 72 | + status: 'COMPLETED', |
| 73 | + environmentId: 177253, |
| 74 | + }, |
| 75 | + ] |
| 76 | + mockSdk.getCommerceCommandExecutions = jest.fn(() => { |
| 77 | + return Promise.resolve(result) |
| 78 | + }) |
| 79 | + |
| 80 | + // expect.assertions(9) |
| 81 | + |
| 82 | + const runResult = ListCommandExecutionsCommand.run([ |
| 83 | + '--programId', |
| 84 | + '5', |
| 85 | + '10', |
| 86 | + ]) |
| 87 | + await expect(runResult instanceof Promise).toBeTruthy() |
| 88 | + await runResult |
| 89 | + await expect(init.mock.calls.length).toEqual(1) |
| 90 | + await expect(init).toHaveBeenCalledWith( |
| 91 | + 'good', |
| 92 | + 'test-client-id', |
| 93 | + 'fake-token', |
| 94 | + 'https://cloudmanager.adobe.io', |
| 95 | + ) |
| 96 | + await expect(mockSdk.getCommerceCommandExecutions.mock.calls.length).toEqual( |
| 97 | + 1, |
| 98 | + ) |
| 99 | + await expect(mockSdk.getCommerceCommandExecutions).toHaveBeenCalledWith( |
| 100 | + '5', |
| 101 | + '10', |
| 102 | + undefined, |
| 103 | + undefined, |
| 104 | + undefined, |
| 105 | + ) |
| 106 | + await expect(cli.action.start.mock.calls[0][0]).toEqual( |
| 107 | + 'Getting Commerce Command Executions', |
| 108 | + ) |
| 109 | + await expect(cli.action.start.mock.calls[1][0]).toEqual( |
| 110 | + 'Retrieved Commerce Command Executions', |
| 111 | + ) |
| 112 | + await expect(cli.table.mock.calls[0][0]).toEqual(result) |
| 113 | + await expect(cli.action.stop).toHaveBeenCalled() |
| 114 | +}) |
| 115 | + |
| 116 | +test('list-command-executions (commerce) - api error', async () => { |
| 117 | + setCurrentOrgId('good') |
| 118 | + mockSdk.getCommerceCommandExecutions = jest.fn(() => |
| 119 | + Promise.reject(new Error('Command failed.')), |
| 120 | + ) |
| 121 | + const runResult = ListCommandExecutionsCommand.run([ |
| 122 | + '--programId', |
| 123 | + '5', |
| 124 | + '10', |
| 125 | + ]) |
| 126 | + await expect(runResult instanceof Promise).toBeTruthy() |
| 127 | + await expect(runResult).rejects.toEqual(new Error('Command failed.')) |
| 128 | +}) |
0 commit comments