|
| 1 | +import { jest } from '@jest/globals' |
| 2 | + |
| 3 | +import type { SchemaAppRequest } from '@smartthings/core-sdk' |
| 4 | + |
| 5 | +import { awsHelpText } from '../../../../lib/aws-util.js' |
| 6 | +import type { |
| 7 | + booleanDef, |
| 8 | + createFromUserInput, |
| 9 | + InputDefinition, |
| 10 | + listSelectionDef, |
| 11 | + objectDef, |
| 12 | + optionalDef, |
| 13 | + optionalStringDef, |
| 14 | + selectDef, |
| 15 | + staticDef, |
| 16 | + stringDef, |
| 17 | + undefinedDef, |
| 18 | + updateFromUserInput, |
| 19 | +} from '../../../../lib/item-input/index.js' |
| 20 | + |
| 21 | + |
| 22 | +const booleanDefMock = jest.fn<typeof booleanDef>() |
| 23 | +const createFromUserInputMock = jest.fn<typeof createFromUserInput>() |
| 24 | +const listSelectionDefMock = jest.fn<typeof listSelectionDef>() |
| 25 | +const objectDefMock = jest.fn<typeof objectDef>() |
| 26 | +const optionalDefMock = jest.fn<typeof optionalDef>() |
| 27 | +const optionalStringDefMock = jest.fn<typeof optionalStringDef>() |
| 28 | +const selectDefMock = jest.fn<typeof selectDef>() |
| 29 | +const staticDefMock = jest.fn<typeof staticDef>() |
| 30 | +const stringDefMock = jest.fn<typeof stringDef>() |
| 31 | +const undefinedDefMock = {} as typeof undefinedDef |
| 32 | +const updateFromUserInputMock = jest.fn<typeof updateFromUserInput>() |
| 33 | +jest.unstable_mockModule('../../../../lib/item-input/index.js', () => ({ |
| 34 | + booleanDef: booleanDefMock, |
| 35 | + createFromUserInput: createFromUserInputMock, |
| 36 | + listSelectionDef: listSelectionDefMock, |
| 37 | + maxItemValueLength: 16, |
| 38 | + objectDef: objectDefMock, |
| 39 | + optionalDef: optionalDefMock, |
| 40 | + optionalStringDef: optionalStringDefMock, |
| 41 | + selectDef: selectDefMock, |
| 42 | + staticDef: staticDefMock, |
| 43 | + stringDef: stringDefMock, |
| 44 | + undefinedDef: undefinedDefMock, |
| 45 | + updateFromUserInput: updateFromUserInputMock, |
| 46 | +})) |
| 47 | + |
| 48 | + |
| 49 | +const { |
| 50 | + arnDef, |
| 51 | + webHookUrlDef, |
| 52 | +} = await import('../../../../lib/command/util/schema-util-input-primitives.js') |
| 53 | + |
| 54 | + |
| 55 | +const generatedStringDef = { name: 'Generated String Def' } as InputDefinition<string> |
| 56 | +const generatedARNDef = { name: 'Generated ARN Def' } as InputDefinition<string | undefined> |
| 57 | +const lambdaInitialValue = { appName: 'Schema App', hostingType: 'lambda' } as SchemaAppRequest |
| 58 | +const webhookInitialValue = { appName: 'Schema App', hostingType: 'webhook' } as SchemaAppRequest |
| 59 | + |
| 60 | +describe('arnDef', () => { |
| 61 | + it('uses undefinedDef when in China but ARN is not for China', () => { |
| 62 | + expect(arnDef('ARN', true, undefined)).toBe(undefinedDefMock) |
| 63 | + expect(arnDef('ARN', true, undefined, { forChina: false })).toBe(undefinedDefMock) |
| 64 | + }) |
| 65 | + |
| 66 | + it('uses undefinedDef when not in China but for China', () => { |
| 67 | + expect(arnDef('ARN', false, undefined, { forChina: true })).toBe(undefinedDefMock) |
| 68 | + }) |
| 69 | + |
| 70 | + it('uses required stringDef in China', () => { |
| 71 | + stringDefMock.mockReturnValueOnce(generatedStringDef) |
| 72 | + optionalDefMock.mockReturnValueOnce(generatedARNDef) |
| 73 | + |
| 74 | + expect(arnDef('ARN', true, lambdaInitialValue, { forChina: true })).toBe(generatedARNDef) |
| 75 | + |
| 76 | + expect(stringDefMock).toHaveBeenCalledExactlyOnceWith('ARN', { helpText: awsHelpText }) |
| 77 | + expect(optionalDefMock).toHaveBeenCalledExactlyOnceWith( |
| 78 | + generatedStringDef, |
| 79 | + expect.any(Function), |
| 80 | + { initiallyActive: true }, |
| 81 | + ) |
| 82 | + |
| 83 | + expect(optionalStringDefMock).not.toHaveBeenCalled() |
| 84 | + }) |
| 85 | + |
| 86 | + it('uses optional string def for global', () => { |
| 87 | + optionalStringDefMock.mockReturnValueOnce(generatedStringDef) |
| 88 | + optionalDefMock.mockReturnValueOnce(generatedARNDef) |
| 89 | + |
| 90 | + expect(arnDef('ARN', false)).toBe(generatedARNDef) |
| 91 | + |
| 92 | + expect(optionalStringDefMock) |
| 93 | + .toHaveBeenCalledExactlyOnceWith('ARN', { helpText: awsHelpText }) |
| 94 | + expect(optionalDefMock).toHaveBeenCalledExactlyOnceWith( |
| 95 | + generatedStringDef, |
| 96 | + expect.any(Function), |
| 97 | + { initiallyActive: false }, |
| 98 | + ) |
| 99 | + |
| 100 | + expect(stringDefMock).not.toHaveBeenCalled() |
| 101 | + }) |
| 102 | + |
| 103 | + it.each` |
| 104 | + initialValue | initiallyActive |
| 105 | + ${{ hostingType: 'lambda' }} | ${true} |
| 106 | + ${{ hostingType: 'webhook' }} | ${false} |
| 107 | + ${undefined} | ${false} |
| 108 | + `('uses initiallyActive $initiallyActive for initialValue $initialValue', ({ initialValue, initiallyActive }) => { |
| 109 | + optionalStringDefMock.mockReturnValueOnce(generatedStringDef) |
| 110 | + optionalDefMock.mockReturnValueOnce(generatedARNDef) |
| 111 | + |
| 112 | + expect(arnDef('ARN', false, initialValue)).toBe(generatedARNDef) |
| 113 | + |
| 114 | + expect(optionalDefMock).toHaveBeenCalledExactlyOnceWith(generatedStringDef, expect.any(Function), { initiallyActive }) |
| 115 | + }) |
| 116 | + |
| 117 | + it('predicate function tests for hostingType of "lambda"', async () => { |
| 118 | + optionalStringDefMock.mockReturnValueOnce(generatedStringDef) |
| 119 | + optionalDefMock.mockReturnValueOnce(generatedARNDef) |
| 120 | + |
| 121 | + expect(arnDef('ARN', false, lambdaInitialValue)).toBe(generatedARNDef) |
| 122 | + |
| 123 | + const checkIfActiveFunction = optionalDefMock.mock.calls[0][1] |
| 124 | + |
| 125 | + expect(checkIfActiveFunction([{ hostingType: 'lambda' }])).toBe(true) |
| 126 | + expect(checkIfActiveFunction([{ hostingType: 'webhook' }])).toBe(false) |
| 127 | + expect(checkIfActiveFunction()).toBe(false) |
| 128 | + }) |
| 129 | +}) |
| 130 | + |
| 131 | +describe('webHookUrlDef', () => { |
| 132 | + it('uses undefinedDef when in China', () => { |
| 133 | + expect(webHookUrlDef(true, undefined)).toBe(undefinedDefMock) |
| 134 | + }) |
| 135 | + |
| 136 | + it('uses stringDef when not in China', () => { |
| 137 | + stringDefMock.mockReturnValueOnce(generatedStringDef) |
| 138 | + optionalDefMock.mockReturnValueOnce(generatedARNDef) |
| 139 | + |
| 140 | + expect(webHookUrlDef(false, webhookInitialValue)).toBe(generatedARNDef) |
| 141 | + |
| 142 | + expect(stringDefMock).toHaveBeenCalledExactlyOnceWith('Webhook URL') |
| 143 | + expect(optionalDefMock).toHaveBeenCalledExactlyOnceWith( |
| 144 | + generatedStringDef, |
| 145 | + expect.any(Function), |
| 146 | + { initiallyActive: true }, |
| 147 | + ) |
| 148 | + |
| 149 | + expect(optionalStringDefMock).not.toHaveBeenCalled() |
| 150 | + }) |
| 151 | + |
| 152 | + it.each` |
| 153 | + initialValue | initiallyActive |
| 154 | + ${{ hostingType: 'webhook' }} | ${true} |
| 155 | + ${{ hostingType: 'lambda' }} | ${false} |
| 156 | + ${undefined} | ${false} |
| 157 | + `('uses initiallyActive $initiallyActive for initialValue $initialValue', ({ initialValue, initiallyActive }) => { |
| 158 | + stringDefMock.mockReturnValueOnce(generatedStringDef) |
| 159 | + optionalDefMock.mockReturnValueOnce(generatedARNDef) |
| 160 | + |
| 161 | + expect(webHookUrlDef(false, initialValue)).toBe(generatedARNDef) |
| 162 | + |
| 163 | + expect(optionalDefMock).toHaveBeenCalledExactlyOnceWith(generatedStringDef, expect.any(Function), { initiallyActive }) |
| 164 | + }) |
| 165 | + |
| 166 | + it('predicate function tests for hostingType of "webhook"', async () => { |
| 167 | + optionalStringDefMock.mockReturnValueOnce(generatedStringDef) |
| 168 | + optionalDefMock.mockReturnValueOnce(generatedARNDef) |
| 169 | + |
| 170 | + expect(webHookUrlDef(false, webhookInitialValue)).toBe(generatedARNDef) |
| 171 | + |
| 172 | + const checkIfActiveFunction = optionalDefMock.mock.calls[0][1] |
| 173 | + |
| 174 | + expect(checkIfActiveFunction([{ hostingType: 'lambda' }])).toBe(false) |
| 175 | + expect(checkIfActiveFunction([{ hostingType: 'webhook' }])).toBe(true) |
| 176 | + expect(checkIfActiveFunction()).toBe(false) |
| 177 | + }) |
| 178 | +}) |
0 commit comments