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
This commit is contained in:
Alexandre Vryghem
2023-07-12 14:31:09 +02:00
parent b7dfe0f23d
commit 4b1a8a90a6

View File

@@ -1,5 +1,6 @@
import { TestBed, waitForAsync } from '@angular/core/testing'; import { TestBed, waitForAsync } from '@angular/core/testing';
import { Observable, Subject } from 'rxjs'; import { Observable, Subject } from 'rxjs';
import { take } from 'rxjs/operators';
import { provideMockActions } from '@ngrx/effects/testing'; import { provideMockActions } from '@ngrx/effects/testing';
import { cold, hot } from 'jasmine-marbles'; import { cold, hot } from 'jasmine-marbles';
import { NotificationsService } from '../../../shared/notifications/notifications.service'; import { NotificationsService } from '../../../shared/notifications/notifications.service';
@@ -11,13 +12,10 @@ import {
RemoveFieldUpdateAction, RemoveFieldUpdateAction,
RemoveObjectUpdatesAction RemoveObjectUpdatesAction
} from './object-updates.actions'; } from './object-updates.actions';
import { import { INotification, Notification } from '../../../shared/notifications/models/notification.model';
INotification, import { NotificationsServiceStub } from '../../../shared/testing/notifications-service.stub';
Notification import { Action } from '@ngrx/store';
} from '../../../shared/notifications/models/notification.model';
import { NotificationType } from '../../../shared/notifications/models/notification-type'; import { NotificationType } from '../../../shared/notifications/models/notification-type';
import { filter } from 'rxjs/operators';
import { hasValue } from '../../../shared/empty.util';
import { NoOpAction } from '../../../shared/ngrx/no-op.action'; import { NoOpAction } from '../../../shared/ngrx/no-op.action';
describe('ObjectUpdatesEffects', () => { describe('ObjectUpdatesEffects', () => {
@@ -31,13 +29,7 @@ describe('ObjectUpdatesEffects', () => {
providers: [ providers: [
ObjectUpdatesEffects, ObjectUpdatesEffects,
provideMockActions(() => actions), provideMockActions(() => actions),
{ { provide: NotificationsService, useClass: NotificationsServiceStub },
provide: NotificationsService,
useValue: {
remove: (notification) => { /* empty */
}
}
},
], ],
}); });
})); }));
@@ -59,7 +51,6 @@ describe('ObjectUpdatesEffects', () => {
action = new RemoveObjectUpdatesAction(testURL); action = new RemoveObjectUpdatesAction(testURL);
}); });
it('should emit the action from the actionMap\'s value which key matches the action\'s URL', () => { it('should emit the action from the actionMap\'s value which key matches the action\'s URL', () => {
action = new RemoveObjectUpdatesAction(testURL);
actions = hot('--a-', { a: action }); actions = hot('--a-', { a: action });
(updatesEffects as any).actionMap$[testURL].subscribe((act) => emittedAction = act); (updatesEffects as any).actionMap$[testURL].subscribe((act) => emittedAction = act);
const expected = cold('--b-', { b: undefined }); const expected = cold('--b-', { b: undefined });
@@ -81,14 +72,19 @@ describe('ObjectUpdatesEffects', () => {
removeAction = new RemoveObjectUpdatesAction(testURL); removeAction = new RemoveObjectUpdatesAction(testURL);
}); });
it('should return a RemoveObjectUpdatesAction', () => { it('should return a RemoveObjectUpdatesAction', () => {
actions = hot('a|', { a: new DiscardObjectUpdatesAction(testURL, infoNotification) }); actions = hot('a', { a: new DiscardObjectUpdatesAction(testURL, infoNotification) });
updatesEffects.removeAfterDiscardOrReinstateOnUndo$.pipe(
filter(((action) => hasValue(action)))) // Because we use Subject and not BehaviourSubject we need to subscribe to it beforehand because it does not
.subscribe((t) => { // keep track of the current state
expect(t).toEqual(removeAction); let emittedAction: Action | undefined;
} updatesEffects.removeAfterDiscardOrReinstateOnUndo$.subscribe((action: Action | NoOpAction) => {
) emittedAction = action;
; });
// This expect ensures that the mapLastActions$ was processed
expect(updatesEffects.mapLastActions$).toBeObservable(cold('a', { a: undefined }));
expect(emittedAction).toEqual(removeAction);
}); });
}); });
@@ -98,12 +94,24 @@ describe('ObjectUpdatesEffects', () => {
infoNotification.options.timeOut = 10; infoNotification.options.timeOut = 10;
}); });
it('should return an action with type NO_ACTION', () => { it('should return an action with type NO_ACTION', () => {
actions = hot('a', { a: new DiscardObjectUpdatesAction(testURL, infoNotification) }); actions = hot('--(ab)', {
actions = hot('b', { b: new ReinstateObjectUpdatesAction(testURL) }); a: new DiscardObjectUpdatesAction(testURL, infoNotification),
updatesEffects.removeAfterDiscardOrReinstateOnUndo$.subscribe((t) => { b: new ReinstateObjectUpdatesAction(testURL),
expect(t).toEqual(new NoOpAction()); });
}
); // Because we use Subject and not BehaviourSubject we need to subscribe to it beforehand because it does not
// keep track of the current state
let emittedAction: Action | undefined;
updatesEffects.removeAfterDiscardOrReinstateOnUndo$.pipe(
take(2)
).subscribe((action: Action | NoOpAction) => {
emittedAction = action;
});
// This expect ensures that the mapLastActions$ was processed
expect(updatesEffects.mapLastActions$).toBeObservable(cold('--(ab)', { a: undefined, b: undefined }));
expect(emittedAction).toEqual(new RemoveObjectUpdatesAction(testURL));
}); });
}); });
@@ -113,12 +121,22 @@ describe('ObjectUpdatesEffects', () => {
infoNotification.options.timeOut = 10; infoNotification.options.timeOut = 10;
}); });
it('should return a RemoveObjectUpdatesAction', () => { it('should return a RemoveObjectUpdatesAction', () => {
actions = hot('a', { a: new DiscardObjectUpdatesAction(testURL, infoNotification) }); actions = hot('--(ab)', {
actions = hot('b', { b: new RemoveFieldUpdateAction(testURL, testUUID) }); a: new DiscardObjectUpdatesAction(testURL, infoNotification),
b: new RemoveFieldUpdateAction(testURL, testUUID),
});
updatesEffects.removeAfterDiscardOrReinstateOnUndo$.subscribe((t) => // Because we use Subject and not BehaviourSubject we need to subscribe to it beforehand because it does not
expect(t).toEqual(new RemoveObjectUpdatesAction(testURL)) // keep track of the current state
); let emittedAction: Action | undefined;
updatesEffects.removeAfterDiscardOrReinstateOnUndo$.subscribe((action: Action | NoOpAction) => {
emittedAction = action;
});
// This expect ensures that the mapLastActions$ was processed
expect(updatesEffects.mapLastActions$).toBeObservable(cold('--(ab)', { a: undefined, b: undefined }));
expect(emittedAction).toEqual(new RemoveObjectUpdatesAction(testURL));
}); });
}); });
}); });