Last active
May 31, 2020 03:40
-
-
Save night-fury-rider/8761ec59a92c7be28870565318105214 to your computer and use it in GitHub Desktop.
Unit testing Angular Application using Jasmine - globalmocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Use shollow copy of your app data. | |
export const appData = { | |
defaultOrderIndex: 0, | |
sortOrders: [{ | |
type: 'string', | |
name: 'Title', | |
sortAttribute: 'title' | |
}] | |
// Remaining test data should be added here. | |
} | |
import { Component, Input } from '@angular/core'; | |
// Write a stub component as needded. | |
@Component({selector: 'app-home', template: ''}) | |
export class HomeStubComponent{ @Input() appData; } // here appData is @Input for HomeComponent | |
// Create a spy object on HomeService. | |
export const HomeService = jasmine.createSpyObj('HomeService', ['get', 'set', 'searchSubscriber$']); | |
// Mock for method of HomeService. | |
HomeService.get.and.callFake(() => { | |
return { | |
name: 'Yuvraj' | |
}; | |
}); | |
// Callback mock for method of HomeService. | |
HomeService.set.and.callFake((callback) => { | |
callback(); | |
}); | |
// Nested callback mock for method of HomeService. | |
HomeService.searchSubscriber$ = { | |
subscribe: (callback) => { | |
callback('Yuvraj Patil'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment