Skip to content

Commit

Permalink
ACS-5647 Corrected unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksanderSklorz committed Oct 24, 2023
1 parent 249de2a commit 7812689
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ describe('LibraryMetadataFormComponent', () => {

it('should initialize form with node data', () => {
fixture.detectChanges();
component.toggleEdit();

expect(component.form.value).toEqual(siteEntryModel);
});
Expand All @@ -89,6 +90,7 @@ describe('LibraryMetadataFormComponent', () => {
};

fixture.detectChanges();
component.toggleEdit();

expect(component.form.value).toEqual(siteEntryModel);

Expand Down Expand Up @@ -161,6 +163,7 @@ describe('LibraryMetadataFormComponent', () => {
component.node.entry.role = Site.RoleEnum.SiteManager;

fixture.detectChanges();
component.toggleEdit();

component.update();

Expand All @@ -172,6 +175,7 @@ describe('LibraryMetadataFormComponent', () => {
siteEntryModel.title = ' some title ';
component.node.entry.title = siteEntryModel.title;
component.ngOnInit();
component.toggleEdit();

component.update();
expect(store.dispatch).toHaveBeenCalledWith(
Expand All @@ -186,6 +190,7 @@ describe('LibraryMetadataFormComponent', () => {
component.node.entry.role = Site.RoleEnum.SiteManager;
spyOn(component.form, 'markAsPristine');
component.ngOnInit();
component.toggleEdit();

component.update();
expect(component.form.markAsPristine).toHaveBeenCalled();
Expand All @@ -195,6 +200,7 @@ describe('LibraryMetadataFormComponent', () => {
component.node.entry.role = Site.RoleEnum.SiteConsumer;

fixture.detectChanges();
component.toggleEdit();

component.update();

Expand All @@ -205,6 +211,7 @@ describe('LibraryMetadataFormComponent', () => {
component.node.entry.role = Site.RoleEnum.SiteConsumer;
spyOn(component.form, 'markAsPristine');
component.ngOnInit();
component.toggleEdit();

component.update();
expect(component.form.markAsPristine).not.toHaveBeenCalled();
Expand All @@ -214,6 +221,7 @@ describe('LibraryMetadataFormComponent', () => {
component.node.entry.role = Site.RoleEnum.SiteManager;

fixture.detectChanges();
component.toggleEdit();

component.form.controls['title'].setErrors({ maxlength: true });

Expand All @@ -227,23 +235,36 @@ describe('LibraryMetadataFormComponent', () => {
spyOn(component.form, 'markAsPristine');
spyOnProperty(component.form, 'valid').and.returnValue(false);
component.ngOnInit();
component.toggleEdit();

component.update();
expect(component.form.markAsPristine).not.toHaveBeenCalled();
});

it('should toggle edit mode', () => {
component.edit = false;
it('should disable form after calling toggleEdit if form was enabled', () => {
spyOn(component.form, 'disable');

component.toggleEdit();
expect(component.edit).toBe(true);
expect(component.form.disable).toHaveBeenCalledWith({
emitEvent: false
});
});

it('should enable form without id field after calling toggleEdit if form was disabled', () => {
component.toggleEdit();
spyOn(component.form, 'enable');
spyOn(component.form.controls.id, 'disable');

component.toggleEdit();
expect(component.edit).toBe(false);
expect(component.form.enable).toHaveBeenCalledWith({
emitEvent: false
});
expect(component.form.controls.id.disable).toHaveBeenCalled();
});

it('should cancel from changes', () => {
fixture.detectChanges();
component.toggleEdit();

expect(component.form.value).toEqual(siteEntryModel);

Expand All @@ -252,6 +273,7 @@ describe('LibraryMetadataFormComponent', () => {
expect(component.form.value.title).toBe('libraryTitle-edit');

component.cancel();
component.toggleEdit();

expect(component.form.value).toEqual(siteEntryModel);
});
Expand Down Expand Up @@ -328,6 +350,7 @@ describe('LibraryMetadataFormComponent', () => {

it('should set proper titleErrorTranslationKey when there is error for empty title', () => {
component.ngOnInit();
component.toggleEdit();

component.form.controls.title.setValue(' ');
expect(component.titleErrorTranslationKey).toBe('LIBRARY.ERRORS.ONLY_SPACES');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,16 @@ export class LibraryMetadataFormComponent implements OnInit, OnChanges, OnDestro

matcher = new InstantErrorStateMatcher();
canUpdateLibrary = false;
visibilityLabel = '';

onDestroy$: Subject<boolean> = new Subject<boolean>();

constructor(private alfrescoApiService: AlfrescoApiService, protected store: Store<AppStore>, private actions$: Actions) {}
getVisibilityLabel(value: string) {
return this.libraryType.find((type) => type.value === value).label;
}

toggleEdit() {
if (this.form.enabled) {
this.form.disable();
this.form.disable({
emitEvent: false
});
} else {
this.form.enable({
emitEvent: false
Expand Down Expand Up @@ -175,7 +173,6 @@ export class LibraryMetadataFormComponent implements OnInit, OnChanges, OnDestro
}
});
this.canUpdateLibrary = this.node?.entry?.role === 'SiteManager';
this.visibilityLabel = this.libraryType.find((type) => type.value === this.form.controls['visibility'].value).label;
this.handleUpdatingEvent<SnackbarInfoAction>(SnackbarActionTypes.Info, 'LIBRARY.SUCCESS.LIBRARY_UPDATED', () =>
Object.assign(this.node.entry, this.form.value)
);
Expand Down

0 comments on commit 7812689

Please sign in to comment.