import { ContentType } from '../../+internal/contentType.util' ; import { factorizeBody } from '../http.responseBody.factory' ; describe ( '#bodyFactory' , ( ) => { test ( 'factorizes body for urlencoded' , ( ) => { const headers = { 'Content-Type' : ContentType . APPLICATION_X_WWW_FORM_URLENCODED } ; const body = { test : 'test' } ; const factorizedBody = factorizeBody ( { headers , body } ) ; expect ( factorizedBody ) . toEqual ( 'test=test' ) ; } ) ; test ( 'factorizes body for JSON' , ( ) => { const headers = { 'Content-Type' : ContentType . APPLICATION_JSON } ; const body = { test : 'test' } ; const factorizedBody = factorizeBody ( { headers , body } ) ; expect ( factorizedBody ) . toEqual ( JSON . stringify ( body ) ) ; } ) ; test ( 'factorizes body for plain text' , ( ) => { const headers = { 'Content-Type' : ContentType . TEXT_PLAIN } ; const bodies = [ 33 , 'test' , false ] ; const factorizedBodies = bodies . map ( body => factorizeBody ( { headers , body } ) ) ; expect ( factorizedBodies ) . toEqual ( bodies . map ( String ) ) ; } ) ; test ( 'doesn\'t factorize body for urlencoded if body is stringified' , ( ) => { const headers = { 'Content-Type' : ContentType . APPLICATION_X_WWW_FORM_URLENCODED } ; const body = 'test=test' ; const factorizedBody = factorizeBody ( { body , headers } ) ; expect ( factorizedBody ) . toEqual ( 'test=test' ) ; } ) ; test ( `doesn't factorize body if "Content-Type" is unknown` , ( ) => { const headers = { 'Content-Type' : ContentType . AUDIO } ; const body = '1234' ; const factorizedBody = factorizeBody ( { headers , body } ) ; expect ( factorizedBody ) . toEqual ( '1234' ) ; } ) ; } ) ;