Skip to content

Commit 4b1a8a9

Browse files
Fix ObjectUpdatesEffects no expectation tests
Because of the switch from BehaviourSubject to Subject the tests always failed because of a timout because the previous state is not kept with Subject and should be manually triggered during the tests
1 parent b7dfe0f commit 4b1a8a9

1 file changed

Lines changed: 51 additions & 33 deletions

File tree

src/app/core/data/object-updates/object-updates.effects.spec.ts

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TestBed, waitForAsync } from '@angular/core/testing';
22
import { Observable, Subject } from 'rxjs';
3+
import { take } from 'rxjs/operators';
34
import { provideMockActions } from '@ngrx/effects/testing';
45
import { cold, hot } from 'jasmine-marbles';
56
import { NotificationsService } from '../../../shared/notifications/notifications.service';
@@ -11,13 +12,10 @@ import {
1112
RemoveFieldUpdateAction,
1213
RemoveObjectUpdatesAction
1314
} from './object-updates.actions';
14-
import {
15-
INotification,
16-
Notification
17-
} from '../../../shared/notifications/models/notification.model';
15+
import { INotification, Notification } from '../../../shared/notifications/models/notification.model';
16+
import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub';
17+
import { Action } from '@ngrx/store';
1818
import { NotificationType } from '../../../shared/notifications/models/notification-type';
19-
import { filter } from 'rxjs/operators';
20-
import { hasValue } from '../../../shared/empty.util';
2119
import { NoOpAction } from '../../../shared/ngrx/no-op.action';
2220

2321
describe('ObjectUpdatesEffects', () => {
@@ -31,13 +29,7 @@ describe('ObjectUpdatesEffects', () => {
3129
providers: [
3230
ObjectUpdatesEffects,
3331
provideMockActions(() => actions),
34-
{
35-
provide: NotificationsService,
36-
useValue: {
37-
remove: (notification) => { /* empty */
38-
}
39-
}
40-
},
32+
{ provide: NotificationsService, useClass: NotificationsServiceStub },
4133
],
4234
});
4335
}));
@@ -59,7 +51,6 @@ describe('ObjectUpdatesEffects', () => {
5951
action = new RemoveObjectUpdatesAction(testURL);
6052
});
6153
it('should emit the action from the actionMap\'s value which key matches the action\'s URL', () => {
62-
action = new RemoveObjectUpdatesAction(testURL);
6354
actions = hot('--a-', { a: action });
6455
(updatesEffects as any).actionMap$[testURL].subscribe((act) => emittedAction = act);
6556
const expected = cold('--b-', { b: undefined });
@@ -81,14 +72,19 @@ describe('ObjectUpdatesEffects', () => {
8172
removeAction = new RemoveObjectUpdatesAction(testURL);
8273
});
8374
it('should return a RemoveObjectUpdatesAction', () => {
84-
actions = hot('a|', { a: new DiscardObjectUpdatesAction(testURL, infoNotification) });
85-
updatesEffects.removeAfterDiscardOrReinstateOnUndo$.pipe(
86-
filter(((action) => hasValue(action))))
87-
.subscribe((t) => {
88-
expect(t).toEqual(removeAction);
89-
}
90-
)
91-
;
75+
actions = hot('a', { a: new DiscardObjectUpdatesAction(testURL, infoNotification) });
76+
77+
// Because we use Subject and not BehaviourSubject we need to subscribe to it beforehand because it does not
78+
// keep track of the current state
79+
let emittedAction: Action | undefined;
80+
updatesEffects.removeAfterDiscardOrReinstateOnUndo$.subscribe((action: Action | NoOpAction) => {
81+
emittedAction = action;
82+
});
83+
84+
// This expect ensures that the mapLastActions$ was processed
85+
expect(updatesEffects.mapLastActions$).toBeObservable(cold('a', { a: undefined }));
86+
87+
expect(emittedAction).toEqual(removeAction);
9288
});
9389
});
9490

@@ -98,12 +94,24 @@ describe('ObjectUpdatesEffects', () => {
9894
infoNotification.options.timeOut = 10;
9995
});
10096
it('should return an action with type NO_ACTION', () => {
101-
actions = hot('a', { a: new DiscardObjectUpdatesAction(testURL, infoNotification) });
102-
actions = hot('b', { b: new ReinstateObjectUpdatesAction(testURL) });
103-
updatesEffects.removeAfterDiscardOrReinstateOnUndo$.subscribe((t) => {
104-
expect(t).toEqual(new NoOpAction());
105-
}
106-
);
97+
actions = hot('--(ab)', {
98+
a: new DiscardObjectUpdatesAction(testURL, infoNotification),
99+
b: new ReinstateObjectUpdatesAction(testURL),
100+
});
101+
102+
// Because we use Subject and not BehaviourSubject we need to subscribe to it beforehand because it does not
103+
// keep track of the current state
104+
let emittedAction: Action | undefined;
105+
updatesEffects.removeAfterDiscardOrReinstateOnUndo$.pipe(
106+
take(2)
107+
).subscribe((action: Action | NoOpAction) => {
108+
emittedAction = action;
109+
});
110+
111+
// This expect ensures that the mapLastActions$ was processed
112+
expect(updatesEffects.mapLastActions$).toBeObservable(cold('--(ab)', { a: undefined, b: undefined }));
113+
114+
expect(emittedAction).toEqual(new RemoveObjectUpdatesAction(testURL));
107115
});
108116
});
109117

@@ -113,12 +121,22 @@ describe('ObjectUpdatesEffects', () => {
113121
infoNotification.options.timeOut = 10;
114122
});
115123
it('should return a RemoveObjectUpdatesAction', () => {
116-
actions = hot('a', { a: new DiscardObjectUpdatesAction(testURL, infoNotification) });
117-
actions = hot('b', { b: new RemoveFieldUpdateAction(testURL, testUUID) });
124+
actions = hot('--(ab)', {
125+
a: new DiscardObjectUpdatesAction(testURL, infoNotification),
126+
b: new RemoveFieldUpdateAction(testURL, testUUID),
127+
});
128+
129+
// Because we use Subject and not BehaviourSubject we need to subscribe to it beforehand because it does not
130+
// keep track of the current state
131+
let emittedAction: Action | undefined;
132+
updatesEffects.removeAfterDiscardOrReinstateOnUndo$.subscribe((action: Action | NoOpAction) => {
133+
emittedAction = action;
134+
});
135+
136+
// This expect ensures that the mapLastActions$ was processed
137+
expect(updatesEffects.mapLastActions$).toBeObservable(cold('--(ab)', { a: undefined, b: undefined }));
118138

119-
updatesEffects.removeAfterDiscardOrReinstateOnUndo$.subscribe((t) =>
120-
expect(t).toEqual(new RemoveObjectUpdatesAction(testURL))
121-
);
139+
expect(emittedAction).toEqual(new RemoveObjectUpdatesAction(testURL));
122140
});
123141
});
124142
});

0 commit comments

Comments
 (0)