Reputation: 632
I am trying to Mock the model value.I am importing Userdata model in component.ts
import {Userdata} from '../../../../model/userdata';
Following is the function:
getUserName(element:Userdata):string{
let retVal:string = '';
if(element.userdetail.length > 0){
retVal = element.userdetail[0].name;
}
return retVal;
}
in Userdata model
constructor(
public id:string,
public name:string,
public description:string,
public userdetail:Array<AppUser>,
){}
}
export class AppUser {
constructor(
public id:string,
public name:string,
){}
}
model is referred to the parameter.How can I mock the model in spec.ts? Thanks in Advance.
Upvotes: 0
Views: 613
Reputation: 18809
You can just create a new UserData
mocked how you would like in your test file.
Something like this should work:
it('returns the first name in array if the array has length', () => {
const mockUserData = new UserData('1', 'My name', 'description', [{ id: '1', name: 'name' }]);
const result = component.getUserName(mockUserData);
expect(result).toBe('name');
});
it('returns an empty string if the array does not have length', () => {
const mockUserData = new UserData('1', 'My name', 'description', []);
const result = component.getUserName(mockUserData);
expect(result).toBe('');
});
Upvotes: 1