@@ -115,13 +115,52 @@ expect.Assertion.prototype.beServedByCloudinary = function (done) {
115115 return this ;
116116} ;
117117
118+ /**
119+ * Asserts that a given object is a datasource.
120+ *
121+ * @returns {expect.Assertion }
122+ */
123+ expect . Assertion . prototype . beADatasource = function ( ) {
124+ let datasource ;
125+ datasource = this . obj ;
126+ this . assert ( 'values' in datasource , function ( ) {
127+ return `expected datasource to contain mandatory field: 'values'` ;
128+ } , function ( ) {
129+ return `expected datasource not to contain a 'values' field` ;
130+ } ) ;
131+ if ( ! isEmpty ( datasource . values ) ) {
132+ datasource . values . forEach ( ( value ) => {
133+ this . assert ( typeof value . value === 'string' , function ( ) {
134+ return `expected datasource to contain item with mandatory field 'value' type string` ;
135+ } , function ( ) {
136+ return `expected datasource not to contain item with mandatory field 'value' type string` ;
137+ } ) ;
138+ this . assert ( typeof value . external_id === 'string' , function ( ) {
139+ return `expected datasource field to contain item with mandatory field: 'value' type string` ;
140+ } , function ( ) {
141+ return `expected datasource not to contain item with mandatory field 'external_id' type string` ;
142+ } ) ;
143+ if ( ! isEmpty ( value . state ) ) {
144+ const states = [ 'active' , 'inactive' ] ;
145+ this . assert ( includes ( states , value . state ) , function ( ) {
146+ return `expected datasource field state to be one of ${ states . join ( ', ' ) } . Unknown state ${ value . state } received` ;
147+ } , function ( ) {
148+ return `expected datasource field state not to be of a certain state` ;
149+ } ) ;
150+ }
151+ } ) ;
152+ }
153+ return this ;
154+ } ;
155+
118156/**
119157 * Asserts that a given object is a metadata field.
120158 * Optionally tests the values in the metadata field for equality
121159 *
160+ * @param {string } type The type of metadata field we expect
122161 * @returns {expect.Assertion }
123162 */
124- expect . Assertion . prototype . beAMetadataField = function ( ) {
163+ expect . Assertion . prototype . beAMetadataField = function ( type = '' ) {
125164 let metadataField , expectedValues ;
126165 if ( Array . isArray ( this . obj ) ) {
127166 [ metadataField , expectedValues ] = this . obj ;
@@ -145,16 +184,24 @@ expect.Assertion.prototype.beAMetadataField = function () {
145184 } , function ( ) {
146185 return `expected metadata field of type ${ metadataField . type } not to contain a datasource field` ;
147186 } ) ;
187+ expect ( metadataField . datasource ) . to . beADatasource ( ) ;
148188 }
149189
150190 // Make sure type is acceptable
151- const acceptableTypes = [ 'string' , 'integer' , 'date' , 'enum' , 'set' ] ;
152- this . assert ( includes ( acceptableTypes , metadataField . type ) , function ( ) {
153- return `expected metadata field type to be one of ${ acceptableTypes . join ( ', ' ) } . Unknown field type ${ metadataField . type } received` ;
154- } , function ( ) {
155- return `expected metadata field not to be of a certain type` ;
156- } ) ;
157-
191+ if ( type ) {
192+ this . assert ( type === metadataField . type , function ( ) {
193+ return `expected metadata field type to equal ${ type } ` ;
194+ } , function ( ) {
195+ return `expected metadata field type ${ metadataField . type } not to equal ${ type } ` ;
196+ } ) ;
197+ } else {
198+ const acceptableTypes = [ 'string' , 'integer' , 'date' , 'enum' , 'set' ] ;
199+ this . assert ( includes ( acceptableTypes , metadataField . type ) , function ( ) {
200+ return `expected metadata field type to be one of ${ acceptableTypes . join ( ', ' ) } . Unknown field type ${ metadataField . type } received` ;
201+ } , function ( ) {
202+ return `expected metadata field not to be of a certain type` ;
203+ } ) ;
204+ }
158205 // Verify object values
159206 if ( expectedValues ) {
160207 Object . entries ( expectedValues ) . forEach ( ( [ key , value ] ) => {
@@ -233,6 +280,23 @@ exports.apiParamMatcher = function (name, value) {
233280 } ;
234281} ;
235282
283+ /**
284+ Create a matcher method for api JSON parameters
285+ @private
286+ @function helper.apiJsonParamMatcher
287+ @param {string } name the parameter name
288+ @param {* } value the parameter value
289+ @return {function } the matcher function as (arg)->Boolean
290+ */
291+ exports . apiJsonParamMatcher = function ( name , value ) {
292+ return function ( arg ) {
293+ var expected , jsonArg ;
294+ jsonArg = JSON . parse ( arg ) ;
295+ expected = JSON . stringify ( value ) ;
296+ return jsonArg [ name ] && JSON . stringify ( jsonArg [ name ] ) === expected ;
297+ } ;
298+ } ;
299+
236300/**
237301 Escape RegExp characters
238302 @private
@@ -328,3 +392,14 @@ exports.setupCache = function () {
328392exports . uploadImage = function ( options ) {
329393 return cloudinary . v2 . uploader . upload ( exports . IMAGE_FILE , options ) ;
330394} ;
395+
396+ /**
397+ * Convert a timestamp to the date part of an ISO8601 string
398+ *
399+ * @param {string } timestamp The timestamp to convert
400+ * @returns {string } The date part of a ISO8601 date time
401+ */
402+ exports . toISO8601DateOnly = function ( timestamp ) {
403+ const date = new Date ( timestamp ) ;
404+ return date . toISOString ( ) . split ( 'T' ) [ 0 ] ;
405+ } ;
0 commit comments