Skip to content

Commit

Permalink
ACS-5949 Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksanderSklorz committed Oct 4, 2023
1 parent a0866b5 commit 2860da5
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,28 @@
import { LibraryMetadataFormComponent } from './library-metadata-form.component';
import { TestBed, ComponentFixture, fakeAsync, tick } from '@angular/core/testing';
import { Store } from '@ngrx/store';
import { UpdateLibraryAction } from '@alfresco/aca-shared/store';
import { SnackbarAction, SnackbarErrorAction, SnackbarInfoAction, UpdateLibraryAction } from '@alfresco/aca-shared/store';
import { AppTestingModule } from '../../../testing/app-testing.module';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { Site, SitePaging } from '@alfresco/js-api';
import { Actions } from '@ngrx/effects';
import { Subject } from 'rxjs';

describe('LibraryMetadataFormComponent', () => {
let fixture: ComponentFixture<LibraryMetadataFormComponent>;
let component: LibraryMetadataFormComponent;
let store: Store<any>;
let actions$: Subject<SnackbarAction>;

beforeEach(() => {
actions$ = new Subject<SnackbarAction>();
TestBed.configureTestingModule({
imports: [AppTestingModule, LibraryMetadataFormComponent],
providers: [
{
provide: Actions,
useValue: actions$
},
{
provide: Store,
useValue: {
Expand Down Expand Up @@ -108,6 +116,86 @@ describe('LibraryMetadataFormComponent', () => {
expect(component.form.value).toEqual(newSiteEntryModel);
});

it('should assign form value to node entry if updating of form is finished with success', () => {
component.node = {
entry: {
id: 'libraryId',
role: 'SiteManager',
title: 'libraryTitle',
description: 'description',
visibility: Site.VisibilityEnum.PRIVATE
} as Site
};
const entry = {
id: 'libraryId',
title: 'some different title',
description: 'some different description',
visibility: Site.VisibilityEnum.PUBLIC
} as Site;
component.ngOnInit();
component.form.setValue(entry);

actions$.next(new SnackbarInfoAction('LIBRARY.SUCCESS.LIBRARY_UPDATED'));
expect(component.node.entry).toEqual(jasmine.objectContaining(entry));
});

it('should not assign form value to node entry if info snackbar was displayed for different action than updating library', () => {
component.node = {
entry: {
id: 'libraryId',
role: 'SiteManager',
title: 'libraryTitle',
description: 'description',
visibility: Site.VisibilityEnum.PRIVATE
} as Site
};
const entry = {
id: 'libraryId',
title: 'some different title',
description: 'some different description',
visibility: Site.VisibilityEnum.PUBLIC
} as Site;
component.ngOnInit();
component.form.setValue(entry);

actions$.next(new SnackbarInfoAction('Some different action'));
expect(component.node.entry).not.toEqual(jasmine.objectContaining(entry));
});

it('should not call markAsDirty on form if updating of form is finished with error', () => {
component.node = {
entry: {
id: 'libraryId',
role: 'SiteManager',
title: 'libraryTitle',
description: 'description',
visibility: Site.VisibilityEnum.PRIVATE
} as Site
};
component.ngOnInit();
spyOn(component.form, 'markAsDirty');

actions$.next(new SnackbarErrorAction('LIBRARY.ERRORS.LIBRARY_UPDATE_ERROR'));
expect(component.form.markAsDirty).toHaveBeenCalled();
});

it('should call markAsDirty on form if error snackbar was displayed for different action than updating library', () => {
component.node = {
entry: {
id: 'libraryId',
role: 'SiteManager',
title: 'libraryTitle',
description: 'description',
visibility: Site.VisibilityEnum.PRIVATE
} as Site
};
component.ngOnInit();
spyOn(component.form, 'markAsDirty');

actions$.next(new SnackbarErrorAction('Some different action'));
expect(component.form.markAsDirty).not.toHaveBeenCalled();
});

it('should update library node if form is valid', () => {
const siteEntryModel = {
title: 'libraryTitle',
Expand All @@ -129,6 +217,23 @@ describe('LibraryMetadataFormComponent', () => {
expect(store.dispatch).toHaveBeenCalledWith(new UpdateLibraryAction(siteEntryModel));
});

it('should call markAsPristine on form when updating valid form and has permission to update', () => {
component.node = {
entry: {
id: 'libraryId',
role: 'SiteManager',
title: 'libraryTitle',
description: 'description',
visibility: Site.VisibilityEnum.PRIVATE
} as Site
};
spyOn(component.form, 'markAsPristine');
component.ngOnInit();

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

it('should not update library node if it has no permission', () => {
const siteEntryModel = {
title: 'libraryTitle',
Expand All @@ -150,6 +255,23 @@ describe('LibraryMetadataFormComponent', () => {
expect(store.dispatch).not.toHaveBeenCalledWith(new UpdateLibraryAction(siteEntryModel));
});

it('should not call markAsPristine on form when updating valid form but has not permission to update', () => {
component.node = {
entry: {
id: 'libraryId',
role: 'Consumer',
title: 'libraryTitle',
description: 'description',
visibility: Site.VisibilityEnum.PRIVATE
} as Site
};
spyOn(component.form, 'markAsPristine');
component.ngOnInit();

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

it('should not update library node if form is invalid', () => {
const siteEntryModel = {
title: 'libraryTitle',
Expand All @@ -173,6 +295,24 @@ describe('LibraryMetadataFormComponent', () => {
expect(store.dispatch).not.toHaveBeenCalledWith(new UpdateLibraryAction(siteEntryModel));
});

it('should not call markAsPristine on form when updating invalid form and has permission to update', () => {
component.node = {
entry: {
id: 'libraryId',
role: 'SiteManager',
title: 'libraryTitle',
description: 'description',
visibility: Site.VisibilityEnum.PRIVATE
} as Site
};
spyOn(component.form, 'markAsPristine');
spyOnProperty(component.form, 'valid').and.returnValue(false);
component.ngOnInit();

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

it('should toggle edit mode', () => {
component.edit = false;

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

it('should call markAsPristine on form when cancelled', () => {
component.node = {
entry: {
id: 'some id',
title: 'some title',
visibility: Site.VisibilityEnum.PUBLIC
} as Site
};
spyOn(component.form, 'markAsPristine');

component.cancel();
expect(component.form.markAsPristine).toHaveBeenCalled();
});

it('should warn if library name input is used by another library', fakeAsync(() => {
const title = 'some-title';
spyOn(component['queriesApi'], 'findSites').and.returnValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export class LibraryMetadataFormComponent implements OnInit, OnChanges, OnDestro
this.handleUpdatingEvents<SnackbarInfoAction>(SnackbarActionTypes.Info, 'LIBRARY.SUCCESS.LIBRARY_UPDATED', () =>
Object.assign(this.node.entry, this.form.value)
);
this.handleUpdatingEvents<SnackbarErrorAction>(SnackbarActionTypes.Error, 'LIBRARY.ERRORS.LIBRARY_UPDATE_ERROR', this.form.markAsDirty);
this.handleUpdatingEvents<SnackbarErrorAction>(SnackbarActionTypes.Error, 'LIBRARY.ERRORS.LIBRARY_UPDATE_ERROR', () => this.form.markAsDirty());
}

ngOnDestroy() {
Expand Down

0 comments on commit 2860da5

Please sign in to comment.