Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a JSON object into a response object

I am trying to pass mock JSON data into my jasmine unit test.

The format of my JSON is like the one below:

{   
   "CompanyResponse":{    
      "CompanyCreatedDate":"1990-10-02",    
      "RunDate":"2",   
      "LastChangedDate":"2015-10-02",   
      "CompanySummary": {   
         "Id":"Apple",   
         "MaximumCredit":"10000000",   
         "Margin":"60000000",    
         "Limit":"-1500000",    
         "HistoricData":{    
            "CompanyHistoricData": [   
               {  
                  "LaunchDate":"2008-08-31",  
                  "Product":"Iphone2",  
                  "TotalProductsCreated":"1000000",  
                  "TotalProductsSold":"800000",  
                  "TotalReturns":"200000",  
                  "TotalMargin":"600000"  
               },  
               {  
                  "LaunchDate":"2010-08-31",  
                  "Product":"Iphone4",  
                  "TotalProductsCreated":"2000000",  
                  "TotalProductsSold":"1500000",  
                  "TotalReturns":"350000",  
                  "TotalMargin":"800000"  
               }  
            ]  
         },  
         "RefurbishedData": {  
            "CompanyRefurbished": [  
               {  
                  "Id":"Apple.201221.12",  
                  "ProductId":"iph-213454",  
                  "StartDate":"2015-09-07",  
                  "FinishDate":"2015-09-10",  
                  "CostOfRefurbishing":"50"  
               },  
               {  
                  "Id":"Apple.201228.12",  
                  "ProductId":"iph-4155655",  
                  "StartDate":"2015-09-10",    
                  "FinishDate":"2015-09-12",  
                  "CostOfRefurbishing":"85"  
               }  
            ]  
         }  
      }  
   }  
}  

I am using the above JSON to pass on to a function similar to the one below for unit testing:

public getTotal(response: CompanyResponse): void {    
  var companySummary = response.CompanySummary;    
  
  //gets total 'CostOfRefurbishing' for all phones    
  var totalRefurbishmentAmount :number = 0;

  for (let companyRefurbishments of companySummary.RefurbishedData) {
    totalRefurbishmentAmount += Number.parseInt(companyRefurbishments.CostOfRefurbishing.toString());
  }
}

The problem I am facing is that I am not able to pass CompanyResponse as a whole to the getTotal() function. It doesn't work even if I use JSON.stringify() because it just converts it to a string and it doesn't work if I use JSON.parse() either as it converts it back into the Object format.

Here's how we call the getTotal() method in a normal scenario:

export class MyService{    
 async CompanySummary(): Promise<CompanySummaryResponse>    
{    
    const response = await this.http.fetch('CompanySummary');    
    return await response.json();    
  }    
}    

var myService = new MyService();    
CompanySummary: CompanySummaryResponse;    
CompanySummary = await myService.CompanySummary();    
this.calculator.getTotal(CompanySummary);    

Cheers, Guru

like image 508
Gurudutt Shenoy Avatar asked Dec 18 '22 07:12

Gurudutt Shenoy


1 Answers

Can you use the standard Response Interface from the JavaScript Fetch API to mock a response object?

If you look at the documentation for the constructor method - it accepts a body parameter and an init options object. The body parameter can be a Blob, so you could;

var data = {foo: "bar"};
var blob = new Blob([JSON.stringify(data, null, 2)], {type : 'application/json'});

var init = { "status" : 200 , "statusText" : "SuperSmashingGreat!" };
var myResponse = new Response(blob, init);

This would create a Response object that you should be able to pass to your test.

like image 172
Tom Avatar answered Dec 24 '22 00:12

Tom