'use strict' ; import { expect } from 'chai' ; import { capitalize } from 'lodash' ; import { forEach , SDK , SDKFetch } from './index' ; import { MockFetch } from './mock/MockFetch' ; export function notInclude ( collection , ele ) { let result = true ; const unionFlag = ele [ '_id' ] ; forEach ( collection , val => { if ( val [ '_id' ] === unionFlag ) { result = false ; } } ) ; return result ; } export function clone < T > ( a ) { return JSON . parse ( JSON . stringify ( a ) ) ; } /**
 * deep equal between a and b
 * loose property compare
 * a: {
 *   foo: 1,
 *   bar: 2
 * }
 * b: {
 *   foo: 1,
 *   bar: 2,
 *   baz: 3
 * }
 * equals(a, b) // pass
 */ export function equals ( a , b ) { const _a = clone ( a ) ; const _b = clone ( b ) ; forEach ( _b , ( _ , key ) => { if ( typeof a [ key ] === 'undefined' ) { delete _b [ key ] ; } } ) ; function deleteUndefined ( obj ) { forEach ( obj , ( val , key ) => { if ( typeof val === 'undefined' ) { delete _a [ key ] ; } else if ( val && typeof val === 'object' ) { deleteUndefined ( val ) ; } } ) ; } deleteUndefined ( _a ) ; expect ( _a ) . to . deep . equal ( _b ) ; } export function looseDeepEqual ( a , b ) { forEach ( a , ( val , key ) => { if ( val && typeof val === 'object' ) { looseDeepEqual ( val , b [ key ] ) ; } else if ( val ) { expect ( val ) . to . deep . equal ( b [ key ] ) ; } } ) ; } export function expectToDeepEqualForFieldsOfTheExpected ( actual , expected , ... fieldsToBeOmitted ) { expect ( typeof actual ) . to . equal ( 'object' ) ; const omitted = new Set ( fieldsToBeOmitted ) ; Object . keys ( expected ) . forEach ( ( field ) => { if ( omitted . has ( field ) ) { return ; } expect ( actual [ field ] ) . to . deep . equal ( expected [ field ] ) ; } ) ; } export function mock < T > ( sdk ) { const mockFetch = new MockFetch ; const methods = [ 'get' , 'put' , 'post' , 'delete' ] ; return ( m , schedule ? ) => { methods . forEach ( method => { sdk . fetch [ method ] = function ( url , arg2 ? ) { const mockResult = mockFetch [ `mock${ capitalize ( method ) }` ] ( url , arg2 ) ; mockResult . mockResponse . respond ( m , schedule ) ; return mockResult . request ; } ; } ) ; } ; } export function restore ( sdk ) { sdk . fetch = new SDKFetch ; } export function forEachFalsyValueOfProperty ( prop , f ) { [ { } , { [ prop ] : 0 } , { [ prop ] : '' } , { [ prop ] : null } , { [ prop ] : undefined } , { [ prop ] : false } , { [ prop ] : NaN } ] . forEach ( f ) ; }