11import { $TSAny , $TSContext , AmplifyCategories , AmplifyFault , AmplifySupportedService , IContextPrint } from '@aws-amplify/amplify-cli-core' ;
22import { prompter } from '@aws-amplify/amplify-prompts' ;
3+ import { mockClient } from 'aws-sdk-client-mock' ;
4+ import 'aws-sdk-client-mock-jest' ;
5+ import { PinpointClient , UpdateApnsChannelCommand , UpdateApnsSandboxChannelCommand } from '@aws-sdk/client-pinpoint' ;
36import * as configureKey from '../apns-key-config' ;
47import * as configureCertificate from '../apns-cert-config' ;
58
@@ -14,6 +17,8 @@ jest.mock('../apns-cert-config');
1417jest . mock ( '@aws-amplify/amplify-prompts' ) ;
1518const prompterMock = prompter as jest . Mocked < typeof prompter > ;
1619
20+ const mockPinpointClient = mockClient ( PinpointClient as any ) ;
21+
1722class NoErrorThrownError extends Error { }
1823// wrapper to avoid conditional error checks
1924const getError = async < TError > ( call : ( ) => unknown ) : Promise < TError > => {
@@ -44,7 +49,7 @@ describe('channel-APNS', () => {
4449
4550 const mockPinpointResponseErr = new Error ( 'channel-APNS.test.js error' ) ;
4651 const mockPinpointResponseData = {
47- APNSChannelResponse : { } ,
52+ APNSChannelResponse : { Enabled : true , ApplicationId : 'test-app-id' } ,
4853 } ;
4954
5055 const mockAPNSChannelResponseData = ( status : boolean , action : ChannelAction , output : $TSAny ) : IChannelAPIResponse => ( {
@@ -64,49 +69,22 @@ describe('channel-APNS', () => {
6469 const mockKeyConfig = { } ;
6570 const mockCertificateConfig = { } ;
6671
67- const mockPinpointClient = {
68- updateApnsChannel : jest . fn ( ) . mockReturnValue ( {
69- promise : jest . fn ( ) . mockResolvedValue ( mockPinpointResponseData ) ,
70- } ) ,
71- updateApnsSandboxChannel : jest . fn ( ) . mockReturnValue ( {
72- promise : jest . fn ( ) . mockResolvedValue ( mockPinpointResponseData ) ,
73- } ) ,
74- } ;
75-
76- const mockPinpointClientReject = {
77- updateApnsChannel : jest . fn ( ) . mockReturnValue ( {
78- promise : jest . fn ( ) . mockRejectedValue ( mockPinpointResponseErr ) ,
79- } ) ,
80- updateApnsSandboxChannel : jest . fn ( ) . mockReturnValue ( {
81- promise : jest . fn ( ) . mockRejectedValue ( mockPinpointResponseErr ) ,
82- } ) ,
83- } ;
84-
8572 const mockContext : $TSContext = {
8673 exeInfo : {
8774 serviceMeta : {
8875 output : mockServiceOutput ,
8976 } ,
90- pinpointClient : mockPinpointClient ,
77+ pinpointClient : mockPinpointClient as any ,
9178 } ,
9279 print : {
9380 info : jest . fn ( ) ,
9481 error : jest . fn ( ) ,
9582 } as unknown as IContextPrint ,
9683 } as unknown as $TSContext ;
9784
98- const mockContextReject = {
99- exeInfo : {
100- serviceMeta : {
101- output : mockServiceOutput ,
102- } ,
103- pinpointClient : mockPinpointClientReject ,
104- } ,
105- print : {
106- info : jest . fn ( ) ,
107- error : jest . fn ( ) ,
108- } ,
109- } ;
85+ beforeEach ( ( ) => {
86+ mockPinpointClient . reset ( ) ;
87+ } ) ;
11088
11189 beforeAll ( ( ) => {
11290 global . console = { ...global . console , log : jest . fn ( ) } ;
@@ -118,69 +96,73 @@ describe('channel-APNS', () => {
11896 } ) ;
11997
12098 test ( 'configure' , async ( ) => {
99+ mockPinpointClient . on ( UpdateApnsChannelCommand as any ) . resolves ( mockPinpointResponseData as any ) ;
100+ mockPinpointClient . on ( UpdateApnsSandboxChannelCommand as any ) . resolves ( mockPinpointResponseData as any ) ;
101+
121102 mockChannelOutput . Enabled = true ;
122103 prompterMock . yesOrNo . mockResolvedValueOnce ( true ) ;
123- await channelAPNS . configure ( mockContext ) . then ( ( ) => {
124- expect ( mockPinpointClient . updateApnsChannel ) . toBeCalled ( ) ;
125- } ) ;
104+ await channelAPNS . configure ( mockContext ) ;
105+ expect ( mockPinpointClient ) . toHaveReceivedCommand ( UpdateApnsChannelCommand as any ) ;
126106
127107 mockChannelOutput . Enabled = true ;
128108 prompterMock . yesOrNo . mockResolvedValueOnce ( false ) ;
129- await channelAPNS . configure ( mockContext ) . then ( ( ) => {
130- expect ( mockPinpointClient . updateApnsChannel ) . toBeCalled ( ) ;
131- } ) ;
109+ prompterMock . pick . mockResolvedValueOnce ( 'Certificate' ) ;
110+ await channelAPNS . configure ( mockContext ) ;
111+ expect ( mockPinpointClient ) . toHaveReceivedCommand ( UpdateApnsChannelCommand as any ) ;
132112
133113 mockChannelOutput . Enabled = false ;
134114 prompterMock . yesOrNo . mockResolvedValueOnce ( true ) ;
135115 prompterMock . pick . mockResolvedValueOnce ( 'Certificate' ) ;
136- await channelAPNS . configure ( mockContext ) . then ( ( ) => {
137- expect ( mockPinpointClient . updateApnsChannel ) . toBeCalled ( ) ;
138- } ) ;
116+ await channelAPNS . configure ( mockContext ) ;
117+ expect ( mockPinpointClient ) . toHaveReceivedCommand ( UpdateApnsChannelCommand as any ) ;
139118 } ) ;
140119
141120 test ( 'enable' , async ( ) => {
142- prompterMock . pick . mockResolvedValueOnce ( 'Certificate' ) ;
121+ mockPinpointClient . on ( UpdateApnsChannelCommand as any ) . resolves ( mockPinpointResponseData as any ) ;
122+ mockPinpointClient . on ( UpdateApnsSandboxChannelCommand as any ) . resolves ( mockPinpointResponseData as any ) ;
143123
124+ prompterMock . pick . mockResolvedValueOnce ( 'Certificate' ) ;
144125 const disableData = await channelAPNS . enable ( mockContext , 'successMessage' ) ;
145- expect ( mockPinpointClient . updateApnsChannel ) . toBeCalled ( ) ;
146- expect ( mockPinpointClient . updateApnsSandboxChannel ) . toBeCalled ( ) ;
126+ expect ( mockPinpointClient ) . toHaveReceivedCommand ( UpdateApnsChannelCommand as any ) ;
127+ expect ( mockPinpointClient ) . toHaveReceivedCommand ( UpdateApnsSandboxChannelCommand as any ) ;
147128 expect ( disableData ) . toEqual ( mockAPNSChannelResponseData ( true , ChannelAction . ENABLE , mockPinpointResponseData . APNSChannelResponse ) ) ;
148129
149130 prompterMock . pick . mockResolvedValueOnce ( 'Key' ) ;
150131 const enableData = await channelAPNS . enable ( mockContext , 'successMessage' ) ;
151- expect ( mockPinpointClient . updateApnsChannel ) . toBeCalled ( ) ;
152- expect ( mockPinpointClient . updateApnsSandboxChannel ) . toBeCalled ( ) ;
132+ expect ( mockPinpointClient ) . toHaveReceivedCommand ( UpdateApnsChannelCommand as any ) ;
133+ expect ( mockPinpointClient ) . toHaveReceivedCommand ( UpdateApnsSandboxChannelCommand as any ) ;
153134 expect ( enableData ) . toEqual ( mockAPNSChannelResponseData ( true , ChannelAction . ENABLE , mockPinpointResponseData . APNSChannelResponse ) ) ;
154135 } ) ;
155136
156137 test ( 'enable unsuccessful' , async ( ) => {
157- prompterMock . pick . mockResolvedValueOnce ( 'Certificate' ) ;
138+ mockPinpointClient . on ( UpdateApnsChannelCommand as any ) . rejects ( mockPinpointResponseErr ) ;
158139
159- const errCert : AmplifyFault = await getError ( async ( ) =>
160- channelAPNS . enable ( mockContextReject as unknown as $TSContext , 'successMessage' ) ,
161- ) ;
162- expect ( mockContextReject . exeInfo . pinpointClient . updateApnsChannel ) . toBeCalled ( ) ;
140+ prompterMock . pick . mockResolvedValueOnce ( 'Certificate' ) ;
141+ const errCert : AmplifyFault = await getError ( async ( ) => channelAPNS . enable ( mockContext , 'successMessage' ) ) ;
142+ expect ( mockPinpointClient ) . toHaveReceivedCommand ( UpdateApnsChannelCommand as any ) ;
163143 expect ( errCert ?. downstreamException ?. message ) . toContain ( mockPinpointResponseErr . message ) ;
164144
165145 prompterMock . pick . mockResolvedValueOnce ( 'Key' ) ;
166- const errKey : AmplifyFault = await getError ( async ( ) =>
167- channelAPNS . enable ( mockContextReject as unknown as $TSContext , 'successMessage' ) ,
168- ) ;
169- expect ( mockPinpointClient . updateApnsChannel ) . toBeCalled ( ) ;
146+ const errKey : AmplifyFault = await getError ( async ( ) => channelAPNS . enable ( mockContext , 'successMessage' ) ) ;
147+ expect ( mockPinpointClient ) . toHaveReceivedCommand ( UpdateApnsChannelCommand as any ) ;
170148 expect ( errKey ?. downstreamException ?. message ) . toContain ( mockPinpointResponseErr . message ) ;
171149 } ) ;
172150
173151 test ( 'disable' , async ( ) => {
174- await channelAPNS . disable ( mockContext ) . then ( ( data ) => {
175- expect ( mockPinpointClient . updateApnsChannel ) . toBeCalled ( ) ;
176- expect ( mockPinpointClient . updateApnsSandboxChannel ) . toBeCalled ( ) ;
177- expect ( data ) . toEqual ( mockAPNSChannelResponseData ( true , ChannelAction . DISABLE , mockPinpointResponseData . APNSChannelResponse ) ) ;
178- } ) ;
152+ mockPinpointClient . on ( UpdateApnsChannelCommand as any ) . resolves ( mockPinpointResponseData as any ) ;
153+ mockPinpointClient . on ( UpdateApnsSandboxChannelCommand as any ) . resolves ( mockPinpointResponseData as any ) ;
154+
155+ const data = await channelAPNS . disable ( mockContext ) ;
156+ expect ( mockPinpointClient ) . toHaveReceivedCommand ( UpdateApnsChannelCommand as any ) ;
157+ expect ( mockPinpointClient ) . toHaveReceivedCommand ( UpdateApnsSandboxChannelCommand as any ) ;
158+ expect ( data ) . toEqual ( mockAPNSChannelResponseData ( true , ChannelAction . DISABLE , mockPinpointResponseData . APNSChannelResponse ) ) ;
179159 } ) ;
180160
181161 test ( 'disable unsuccessful' , async ( ) => {
182- const errKey : AmplifyFault = await getError ( async ( ) => channelAPNS . disable ( mockContextReject as unknown as $TSContext ) ) ;
183- expect ( mockPinpointClient . updateApnsChannel ) . toBeCalled ( ) ;
162+ mockPinpointClient . on ( UpdateApnsChannelCommand as any ) . rejects ( mockPinpointResponseErr ) ;
163+
164+ const errKey : AmplifyFault = await getError ( async ( ) => channelAPNS . disable ( mockContext ) ) ;
165+ expect ( mockPinpointClient ) . toHaveReceivedCommand ( UpdateApnsChannelCommand as any ) ;
184166 expect ( errKey ?. downstreamException ?. message ) . toContain ( mockPinpointResponseErr . message ) ;
185167 } ) ;
186168} ) ;
0 commit comments