Skip to content

Commit

Permalink
[CST-12109] rewrite unit test case
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaismailati committed Feb 23, 2024
1 parent 37dc8cc commit 49a3310
Showing 1 changed file with 48 additions and 18 deletions.
66 changes: 48 additions & 18 deletions src/app/item-page/alerts/item-alerts.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { ComponentFixture, TestBed, fakeAsync, tick, waitForAsync } from '@angular/core/testing';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { ItemAlertsComponent } from './item-alerts.component';
import { TranslateModule } from '@ngx-translate/core';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { Item } from '../../core/shared/item.model';
import { By } from '@angular/platform-browser';
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
import { of } from 'rxjs';
import { DsoWithdrawnReinstateModalService } from '../../shared/dso-page/dso-withdrawn-reinstate-service/dso-withdrawn-reinstate-modal.service';
import { DsoWithdrawnReinstateModalService, REQUEST_REINSTATE } from '../../shared/dso-page/dso-withdrawn-reinstate-service/dso-withdrawn-reinstate-modal.service';
import { CorrectionTypeDataService } from '../../core/submission/correctiontype-data.service';
import { TestScheduler } from 'rxjs/testing';
import { CorrectionType } from '../../core/submission/models/correctiontype.model';
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../../shared/remote-data.utils';
import { createPaginatedList } from '../../shared/testing/utils.test';
import { of } from 'rxjs';

describe('ItemAlertsComponent', () => {
let component: ItemAlertsComponent;
Expand All @@ -16,6 +20,7 @@ describe('ItemAlertsComponent', () => {
let authorizationService;
let dsoWithdrawnReinstateModalService;
let correctionTypeDataService;
let testScheduler: TestScheduler;

const itemMock = Object.assign(new Item(), {
uuid: 'item-uuid',
Expand All @@ -25,9 +30,7 @@ describe('ItemAlertsComponent', () => {
beforeEach(waitForAsync(() => {
authorizationService = jasmine.createSpyObj('authorizationService', ['isAuthorized']);
dsoWithdrawnReinstateModalService = jasmine.createSpyObj('dsoWithdrawnReinstateModalService', ['openCreateWithdrawnReinstateModal']);
correctionTypeDataService = jasmine.createSpyObj('correctionTypeDataService', {
findByItem: of({})
});
correctionTypeDataService = jasmine.createSpyObj('correctionTypeDataService', ['findByItem']);
TestBed.configureTestingModule({
declarations: [ItemAlertsComponent],
imports: [TranslateModule.forRoot()],
Expand Down Expand Up @@ -85,6 +88,7 @@ describe('ItemAlertsComponent', () => {
isWithdrawn: true
});
component.item = item;
(correctionTypeDataService.findByItem).and.returnValue(createSuccessfulRemoteDataObject$([]));
fixture.detectChanges();
});

Expand All @@ -100,6 +104,7 @@ describe('ItemAlertsComponent', () => {
isWithdrawn: false
});
component.item = item;
(correctionTypeDataService.findByItem).and.returnValue(createSuccessfulRemoteDataObject$([]));
fixture.detectChanges();
});

Expand All @@ -109,17 +114,42 @@ describe('ItemAlertsComponent', () => {
});
});

it('should return true when user is not an admin and there is at least one correction with topic REQUEST_REINSTATE', fakeAsync((done) => {
const isAdmin = false;
const correction = [{ topic: 'REQUEST_REINSTATE' }];
authorizationService.isAuthorized.and.returnValue(of(isAdmin));
correctionTypeDataService.findByItem.and.returnValue(of(correction));

const result$ = component.showReinstateButton$();
tick();
result$.subscribe((result) => {
expect(result).toBeTrue();
done();
describe('when the item is reinstated', () => {
const correctionType = Object.assign(new CorrectionType(), {
topic: REQUEST_REINSTATE
});
}));
const correctionRD = createSuccessfulRemoteDataObject(createPaginatedList([correctionType]));

beforeEach(() => {
item = itemMock;
component.item = item;
(correctionTypeDataService.findByItem).and.returnValue(of(correctionRD));

testScheduler = new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});
fixture.detectChanges();
});

it('should return true when user is not an admin and there is at least one correction with topic REQUEST_REINSTATE', () => {
testScheduler.run(({ cold, expectObservable }) => {
const isAdminMarble = 'a';
const correctionMarble = 'b';
const expectedMarble = 'c';

const isAdminValues = { a: false };
const correctionValues = { b: correctionRD };
const expectedValues = { c: true };

const isAdmin$ = cold(isAdminMarble, isAdminValues);
const correction$ = cold(correctionMarble, correctionValues);

(authorizationService.isAuthorized).and.returnValue(isAdmin$);
(correctionTypeDataService.findByItem).and.returnValue(correction$);

expectObservable(component.showReinstateButton$()).toBe(expectedMarble, expectedValues);
});
});

});
});

0 comments on commit 49a3310

Please sign in to comment.