This Question seems to be same but definitely not duplicate.
In one of my function i am calling this.cordovaFile.readAsArrayBuffer(this.cordovaFile.dataDirectory, key)
this cordovafile.readAsArrayBuffer()
reads data from ipad File storage, but in Unit test i cant do that since it runs in Browsers, so i am using SpyOn to get fake value, i am getting difficulties to do that.
can some one help me below is the Function
private getTimesheetByKey(key: TIMESHEET_KEYS): Observable<TimesheetModel> {
let timesheet: TimesheetModel;
return from(
this.cordovaFile
.readAsArrayBuffer(this.cordovaFile.dataDirectory, key)
.then(compressedConfirmation => {
const start = moment();
const uint8Array = new Uint8Array(compressedConfirmation);
const jsonTimeSheet = this.LZString.decompressFromUint8Array(uint8Array);
timesheet = new TimesheetModelFromJson(<JsonTimesheetModel>(
JSON.parse(jsonTimeSheet)
));
this.saveTimesheetByKey(key, timesheet);
return timesheet;
})
.catch((error: Error) => {})
);
}
and This is how the Unit Test what i am trying to write, I dont know exactly how to SpyOn this.cordovaFile.readAsArrayBuffer(this.cordovaFile.dataDirectory, key)
and returns value
it('should save the Timesheet to file storage', () => {
spyOn(LocalStorageTimesheetService, 'getTimesheetByKey')
.and.callThrough(cordovaFile.readAsArrayBuffer())
.and.returnValue(timesheet);
expect(timesheet).toEqual();
});
this.cordovaFile.readAsArrayBuffer()
should return fake value something like below
timesheet = {
startOfWork: '2019-07-02T02:00:00.000Z',
notifications: [],
additionalExpenses: [],
manualTimesheetEntries: [],
endOfWork: undefined,
isSubmitted: false,
attendanceType: 'FREE',
};