|
| 1 | +import * as contentstackSdk from '@contentstack/management' |
| 2 | +import connectStack from '../../src/utils/connect-stack' |
| 3 | +import {cli} from 'cli-ux' |
| 4 | +import processStack from '../../src/utils/process-stack' |
| 5 | +const regexMessages = require('../../messages/index.json').validateRegex |
| 6 | + |
| 7 | +jest.mock('@contentstack/management', () => ({client: jest.fn()})) |
| 8 | +jest.mock('../../src/utils/output.ts') |
| 9 | +jest.mock('../../src/utils/process-stack.ts') |
| 10 | + |
| 11 | +/* @ts-ignore */ |
| 12 | +cli = ({ |
| 13 | + debug: jest.fn(), |
| 14 | + error: jest.fn(), |
| 15 | + action: { |
| 16 | + start: jest.fn(), |
| 17 | + stop: jest.fn(), |
| 18 | + }}) |
| 19 | + |
| 20 | +describe('Get Client from Management SDK, connect with Stack & process Stack', () => { |
| 21 | + beforeEach(() => { |
| 22 | + jest.restoreAllMocks() |
| 23 | + }) |
| 24 | + |
| 25 | + test('API Key is Valid', async () => { |
| 26 | + const apiKey = 'blt1234' |
| 27 | + const authToken = 'blt1234' |
| 28 | + const flags = { |
| 29 | + contentType: true, |
| 30 | + globalField: true, |
| 31 | + } |
| 32 | + contentstackSdk.client.mockImplementation(() => { |
| 33 | + return { |
| 34 | + stack: jest.fn().mockImplementation(() => { |
| 35 | + return { |
| 36 | + fetch: jest.fn().mockResolvedValue(Promise.resolve({stack: {}})), |
| 37 | + } |
| 38 | + }), |
| 39 | + } |
| 40 | + }) |
| 41 | + await connectStack(flags, authToken, apiKey) |
| 42 | + expect(cli.action.start).toHaveBeenCalled() |
| 43 | + expect(processStack).toHaveBeenCalled() |
| 44 | + }) |
| 45 | + |
| 46 | + test('API Key is Invalid', async () => { |
| 47 | + try { |
| 48 | + const apiKey = 'blt1234' |
| 49 | + const authToken = 'blt1234' |
| 50 | + const flags = { |
| 51 | + contentType: true, |
| 52 | + globalField: true, |
| 53 | + } |
| 54 | + contentstackSdk.client.mockImplementation(() => { |
| 55 | + return { |
| 56 | + stack: jest.fn().mockImplementation(() => { |
| 57 | + return { |
| 58 | + fetch: jest.fn().mockRejectedValue(Promise.resolve(Error)), |
| 59 | + } |
| 60 | + }), |
| 61 | + } |
| 62 | + }) |
| 63 | + await connectStack(flags, authToken, apiKey) |
| 64 | + expect(cli.action.start).toHaveBeenCalled() |
| 65 | + } catch (error) { |
| 66 | + expect(error.message).toBe(regexMessages.errors.stack.apiKey) |
| 67 | + } |
| 68 | + }) |
| 69 | +}) |
0 commit comments