import { matchPath } from 'react-router' ; import { noop } from '@proton/shared/lib/helpers/function' ; import { FeatureCode } from '@proton/components' ; type HttpMethod = 'get' | 'post' | 'put' | 'delete' ; type ApiMockHandler = ( ... arg : any [ ] ) => any ; type ApiMockEntry = { method ? : HttpMethod ; handler : ( ... arg : any [ ] ) => any ; } ; type ApiMock = { [ url ] : ApiMockEntry [ ] | undefined ; } ; export const apiMocks = { } ; export const api = jest . fn < Promise < any > , any > ( async ( args ) => { let matchData = { } as any ; const entryKey = Object . keys ( apiMocks ) . find ( ( path ) => { matchData = matchPath ( args . url , { path , exact : true } ) ; return matchData !== null ; } ) ; const entry = apiMocks [ entryKey || '' ] ?. find ( ( entry ) => entry . method === undefined || entry . method === args . method ) ; if ( entry ) { return entry . handler ( { ... matchData , ... args } ) ; } console . log ( 'api' , args , apiMocks ) ; return { } ; } ) ; export const addApiMock = ( url , handler , method ? ) => { const newEntry = { method , handler } ; if ( ! apiMocks [ url ] ) { apiMocks [ url ] = [ newEntry ] ; } else { apiMocks [ url ] = apiMocks [ url ] ?. filter ( ( entry ) => entry . method !== newEntry . method ) . concat ( [ newEntry ] ) ; } } ; export const addApiResolver = ( url , method ? ) => { let resolveLastPromise = noop ; const resolve = ( value ) => resolveLastPromise ( value ) ; const promise = new Promise ( ( resolve ) => { resolveLastPromise = resolve ; } ) ; addApiMock ( url , ( ) => promise , method ) ; return resolve ; } ; export const clearApiMocks = ( ) => { Object . keys ( apiMocks ) . forEach ( ( key ) => delete apiMocks [ key ] ) ; } ; export const featureFlags = { } ; export const defaultFeatureFlagValue = { Code : '' , Type : 'boolean' , Global : false , DefaultValue : false , Value : false , UpdateTime : 1616511553 , Writable : true , } ; export const setFeatureFlags = ( featureCode , value ) => { featureFlags [ featureCode ] = { ... defaultFeatureFlagValue , Code : featureCode , Value : value , } ; } ; export const clearFeatureFlags = ( ) => { Object . keys ( featureFlags ) . forEach ( ( key ) => delete apiMocks [ key ] ) ; } ; export const registerFeatureFlagsApiMock = ( ) => { addApiMock ( 'core/v4/features' , ( args ) => { const { Code } = args . params ; const features = Code . split ( ',' ) ; return { Features : features . map ( ( code ) => featureFlags [ code ] ? featureFlags [ code ] : { ... defaultFeatureFlagValue , Code : code } ) , } ; } , 'get' ) ; } ; export const registerMinimalFlags = ( ) => { setFeatureFlags ( FeatureCode . EnabledEncryptedSearch , true ) ; setFeatureFlags ( FeatureCode . SpotlightEncryptedSearch , false ) ; } ; export const parseFormData = ( data ) => { const result = { } ; const createStructure = ( resultContext , name , left , value ) => { if ( ! resultContext [ name ] ) { resultContext [ name ] = { } ; } if ( left === '' ) { resultContext [ name ] = value ; } else { const [ , newName , newLeft ] = /^\[([^[]+)\](.*)/ . exec ( left ) || [ ] ; createStructure ( resultContext [ name ] , newName , newLeft , value ) ; } } ; Object . entries ( data ) . forEach ( ( [ key , value ] ) => { const [ , name , left ] = /^([^[]+)(.*)/ . exec ( key ) || [ ] ; createStructure ( result , name , left , value ) ; } ) ; return result ; } ;