| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import { serializeStringArray } from '@datahub/utils/array/serialize-string'; | 
					
						
							| 
									
										
										
										
											2020-11-09 12:17:51 -08:00
										 |  |  | import { difference } from 'lodash-es'; | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Validates that an array of json object string values match | 
					
						
							|  |  |  |  * @param {Array<string>} values the received list of strings | 
					
						
							|  |  |  |  * @param {Array<string>} expectedValues the expected list of strings | 
					
						
							|  |  |  |  * @returns {true} | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export const jsonValuesMatch = (values: Array<string>, expectedValues: Array<string>): true => { | 
					
						
							|  |  |  |   const sValues = serializeStringArray(values); | 
					
						
							|  |  |  |   const sExpectedValues = serializeStringArray(expectedValues); | 
					
						
							|  |  |  |   const match = sValues === sExpectedValues; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (!match) { | 
					
						
							|  |  |  |     throw new Error( | 
					
						
							|  |  |  |       ` Found ${difference(values, expectedValues).join(', ')}. Expected only ${expectedValues.join(', ')}` | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return match; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Validates that an array of json object string values only contains what is expected. :) | 
					
						
							|  |  |  |  * @param values - received list of strings | 
					
						
							|  |  |  |  * @param expectedValues - expected list of strings | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export const jsonValuesAreIncluded = (values: Array<string>, expectedValues: Array<string>): true => { | 
					
						
							|  |  |  |   const serializedExpectedValues = serializeStringArray(expectedValues); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   values.forEach(value => { | 
					
						
							|  |  |  |     if (!serializedExpectedValues.includes(value)) { | 
					
						
							|  |  |  |       throw new Error(`Found unexpected values ${difference(values, expectedValues).join(', ')}`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | }; |