1
+ import { wait } from '@vlocode/util' ;
2
+ import 'jest' ;
3
+ import { HttpRequestInfo , MetadataApi , SalesforceConnection } from '../connection' ;
4
+ import { SoapClient } from '../soapClient' ;
5
+
6
+ function mockConnection ( ) {
7
+ const baseUrl = 'https://test.salesforce.com' ;
8
+ const apiRequests = new Array < HttpRequestInfo > ( ) ;
9
+ const apiResponses : Record < string , any > = { }
10
+ async function request < T > ( info : HttpRequestInfo ) : Promise < T > {
11
+ await wait ( 5 ) ;
12
+ apiRequests . push ( { ...info } ) ;
13
+ return apiResponses [ info . url ] ;
14
+ }
15
+ const mock = {
16
+ apiResponses,
17
+ apiRequests,
18
+ request,
19
+ _baseUrl ( ) { return baseUrl } ,
20
+ instanceUrl : baseUrl ,
21
+ } ;
22
+ return mock as any as ( SalesforceConnection & typeof mock ) ;
23
+ }
24
+
25
+ function mockSoapClient ( ) {
26
+ const soapRequests = new Array < { method : string , request : any } > ( ) ;
27
+ const soapResponses : Record < string , { body : object } > = { }
28
+ async function request < T > ( method : string , request : object , options ?: any ) : Promise < T > {
29
+ await wait ( 5 ) ;
30
+ soapRequests . push ( { method, request } ) ;
31
+ return ( soapResponses [ method ] ?? ( { body : { } } ) ) as any as T ;
32
+ }
33
+ const mock = {
34
+ soapRequests,
35
+ soapResponses,
36
+ request
37
+ } ;
38
+ return mock as any as ( SoapClient & typeof mock ) ;
39
+ }
40
+
41
+ describe ( 'MetadataApi' , ( ) => {
42
+ describe ( '#createMetadata' , ( ) => {
43
+ it ( 'should include type attribute in create calls' , async ( ) => {
44
+ // Arrange
45
+ const connection = mockConnection ( ) ;
46
+ const soap = mockSoapClient ( ) ;
47
+ const sut = new MetadataApi ( connection ) ;
48
+ sut [ 'soap' ] = soap ;
49
+
50
+ // Act
51
+ await sut . create ( 'CustomMetadata' , {
52
+ label : 'test' ,
53
+ fullName : 'test' ,
54
+ values : [
55
+ { field : 'Test' , value : 1 }
56
+ ]
57
+ } ) ;
58
+
59
+ // Assert
60
+ expect ( soap . soapRequests [ 0 ] . method ) . toBe ( 'createMetadata' ) ;
61
+ expect ( soap . soapRequests [ 0 ] . request . type ) . toBe ( 'CustomMetadata' ) ;
62
+ expect ( soap . soapRequests [ 0 ] . request . metadata . length ) . toBe ( 1 ) ;
63
+ expect ( soap . soapRequests [ 0 ] . request . metadata [ 0 ] . $ [ 'xsi:type' ] ) . toBe ( 'CustomMetadata' ) ;
64
+ expect ( soap . soapRequests [ 0 ] . request . metadata [ 0 ] . label ) . toBe ( 'test' ) ;
65
+ expect ( soap . soapRequests [ 0 ] . request . metadata [ 0 ] . fullName ) . toBe ( 'test' ) ;
66
+ } ) ;
67
+ } ) ;
68
+ } ) ;
0 commit comments