I have a little function in Angular 7 that I am testing with Jest. The function looks like this:
private checkFreeProduct(allowance: SubscriberConnectivityAllowanceInterface): Observable<SubscriberConnectivityAllowanceInterface> {
// TODO: This is currently just a temp function to be extended when required
return of(allowance);
}
As you can see, for the moment all it is doing is creating an observable from its input, but it is under development and will be extended.
I am testing it with Jest like this:
it('should return an observable of the allowance', () => {
const allowance: SubscriberConnectivityAllowanceInterface = {
hotspotAuthenticated: HotspotAuthenticationEnum.TRUE,
remainingOctets: 100,
remainingSeconds: 200,
activeProductCost: ConnectivityProductCostEnum.PAID,
activeProductDuration: ConnectivityProductDurationEnum.FLIGHT,
activeProductType: ConnectivityProductTypeEnum.PREMIUM,
connectivityProducts: []
};
const expected = hot('a|', {
a: allowance
});
expect(hotspotService['checkFreeProduct'](allowance)).toBeObservable(expected);
});
However, the test is failing because of some timing issue. The expected
observable result looks like this:
[
{
"frame": 0,
"notification": {
"error": undefined,
"hasValue": true,
"kind": "N",
"value": {
"activeProductCost": "paid",
"activeProductDuration": "flight",
"activeProductType": "premium",
"connectivityProducts": [],
"hotspotAuthenticated": 1,
"remainingOctets": 100,
"remainingSeconds": 200
}
}
},
{
"frame": 10,
"notification": {
"error": undefined,
"hasValue": false,
"kind": "C",
"value": undefined
}
}
]
and the observable created from the function call hotspotService['checkFreeProduct'](allowance)
looks like this:
[
{
"frame": 0,
"notification": {
"error": undefined,
"hasValue": true,
"kind": "N",
"value": {
"activeProductCost": "paid",
"activeProductDuration": "flight",
"activeProductType": "premium",
"connectivityProducts": [],
"hotspotAuthenticated": 1,
"remainingOctets": 100,
"remainingSeconds": 200
}
}
},
{
"frame": 0, // <------- this is the only difference
"notification": {
"error": undefined,
"hasValue": false,
"kind": "C",
"value": undefined
}
}
]
Now I'm not exactly sure why there are two emissions from these observables, but I'll go with it. What I don't understand is why the observable from the function call emits two events, both on frame 0. I have tried both hot()
and cold()
, and have experimented with various marbles in these calls but no joy. Can someone please explain?