Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
minottic committed Jan 25, 2024
1 parent f37a2c0 commit 1c3cf5c
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 4 deletions.
127 changes: 125 additions & 2 deletions scilog/src/app/overview/add-logbook/add-logbook.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { UserPreferencesService } from '@shared/user-preferences.service';
import { LogbookDataService, LogbookItemDataService } from '@shared/remote-data.service';
import { of } from 'rxjs';
import { MatChipInputEvent } from '@angular/material/chips';
import { SnackbarService } from 'src/app/core/snackbar.service';

class UserPreferencesMock {
userInfo = {
Expand All @@ -16,7 +17,6 @@ class UserPreferencesMock {
}



describe('AddLogbookComponent', () => {
const mockDialogRef = {
close: jasmine.createSpy('close')
Expand All @@ -26,12 +26,14 @@ describe('AddLogbookComponent', () => {
let fixture: ComponentFixture<AddLogbookComponent>;
let logbookItemDataServiceSpy:any;
let logbookDataSpy:any;
let snackBarSpy: SnackbarService;

logbookItemDataServiceSpy = jasmine.createSpyObj("LogbookItemDataService", ["getFile"]);
logbookItemDataServiceSpy.getFile.and.returnValue([]);

logbookDataSpy = jasmine.createSpyObj("LogbookDataService", ["getLocations", "patchLogbook", "postLogbook", "uploadLogbookThumbnail"]);
logbookDataSpy.getLocations.and.returnValue(of([{}]));
snackBarSpy = jasmine.createSpyObj("SnackbarService", ["_showMessage"]);

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -43,7 +45,8 @@ describe('AddLogbookComponent', () => {
{provide: MAT_DIALOG_DATA, useValue: null},
{provide: LogbookItemDataService, useValue: logbookItemDataServiceSpy},
{provide: UserPreferencesService, useClass: UserPreferencesMock},
{provide: LogbookDataService, useValue: logbookDataSpy}
{provide: LogbookDataService, useValue: logbookDataSpy},
{provide: SnackbarService, useValue: snackBarSpy}
],
})
.compileComponents();
Expand Down Expand Up @@ -94,4 +97,124 @@ describe('AddLogbookComponent', () => {
expect(spyUpdate).toHaveBeenCalledTimes(2);
});

it('should test getForm', () => {
expect(component['getForm']('accessGroups').value).toEqual(['startGroup']);
expect(component['getForm']('floatLabel').value).toEqual('auto');
expect(component['getForm']('title').value).toEqual('');
});

it('should test setForm', () => {
component['logbook'] = {ownerGroup: 'testOwner', name: 'a'};
component['setForm']('ownerGroup');
component['setForm']('title', 'name');
expect(component['getForm']('ownerGroup').value).toEqual('testOwner');
expect(component['getForm']('title').value).toEqual('a');
});

[
{input: ['roles'], output: true},
{input: [], output: false}
].forEach((t, i) => {
it(`should test isAdmin ${i}`, () => {
component['logbook'] = {adminACL: t.input};
expect(component['isAdmin']()).toEqual(t.output);
});
});

[
{input: '2200-12-12T00:00:00Z', output: undefined},
{input: '2023-12-12T00:00:00Z', output: jasmine.anything()}
].forEach((t, i) => {
it(`should test hasExpired ${i}`, () => {
component['logbook'] = {expiresAt: t.input};
component['setExpiredTooltip']();
expect(component.tooltips.expired).toEqual(t.output);
});
});

[
{input: ['roles'], output: [undefined, false]},
{input: ['admin'], output: [`Only members of 'admin' can change the ownerGroup`, true]}
].forEach((t, i) => {
it(`should test setOwnerGroupWithEditability ${i}`, () => {
component['logbook'] = {adminACL: t.input, ownerGroup: 'ownerGroup'};
component['setOwnerGroupWithEditability']();
expect(component.tooltips.ownerGroup).toEqual(t.output[0] as string);
expect(component['getForm']('ownerGroup').disabled).toEqual(t.output[1] as boolean);
});
});

[
{input: undefined, output: false},
{input: 'Expired', output: true}
].forEach((t, i) => {
it(`should test setWithEditability ${i}`, () => {
component.tooltips.expired = t.input;
component['logbook'] = {name: 'a', location: 'b', expiresAt: t.input};
component['setWithEditability']('title');
component['setWithEditability']('location');
expect(component['getForm']('title').disabled).toEqual(t.output);
expect(component['getForm']('location').disabled).toEqual(t.output);
});
});

it('should test showSnackbarMessage', () => {
component['showSnackbarMessage']('aMessage', 'warning');
expect(snackBarSpy._showMessage).toHaveBeenCalledOnceWith({
message: 'aMessage',
panelClass: ['warning-snackbar'],
action: 'Dismiss',
show: true,
duration: 4000,
type: 'serverMessage',
horizontalPosition: 'center',
verticalPosition: 'top',
})
});

it('should test setDefaults on creation', () => {
component['userPreferences'].userInfo.roles.push('any-authenticated-user');
const accessGroupsValidatorsSpy = spyOn(component.accessGroups, 'addValidators');
const ownerGroupValidatorsSpy = spyOn(component['getForm']('ownerGroup'), 'addValidators');
component['setDefaults']();
expect(component.accessGroupsAvail).toEqual(['roles']);
expect(accessGroupsValidatorsSpy).toHaveBeenCalledTimes(1);
expect(ownerGroupValidatorsSpy).toHaveBeenCalledTimes(2);
});

[
{input: {adminACL: ['roles']}, output: 0},
{input: {adminACL: []}, output: 1},
{input: {expiresAt: '2023-12-12T00:00:00Z'}, output: true},
{input: {expiresAt: '2200-12-12T00:00:00Z'}, output: false},
].forEach((t, i) => {
it(`should test setDefaults on update ${i}`, () => {
component.logbook = {
...t.input,
ownerGroup: 'ownerGroup',
accessGroups: ['accessGroups'],
name: 'name',
location: 'location',
description: 'description',
isPrivate: true,
}
const ownerGroupValidatorsSpy = spyOn(component['getForm']('ownerGroup'), 'addValidators');
component['setDefaults']();
expect(component['getForm']('ownerGroup').value).toEqual('ownerGroup');
expect(component['getForm']('title').value).toEqual('name');
expect(component['getForm']('description').value).toEqual('description');
expect(component['getForm']('location').value).toEqual('location');
expect(component['getForm']('isPrivate').value).toEqual(true);
expect(component.accessGroups.value).toEqual(['accessGroups']);
if (t.input.adminACL)
expect(ownerGroupValidatorsSpy).toHaveBeenCalledTimes(t.output as number);
if (t.input.expiresAt) {
expect(component['getForm']('description').disabled).toEqual(t.output as boolean);
expect(component['getForm']('title').disabled).toEqual(t.output as boolean);
expect(component['getForm']('location').disabled).toEqual(t.output as boolean);
expect(component['getForm']('isPrivate').disabled).toEqual(t.output as boolean);
}
});
});

});
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { LogbookWidgetComponent } from './logbook-cover.component';
import { LogbookItemDataService } from '@shared/remote-data.service';
import { UserPreferencesService } from '@shared/user-preferences.service';
Expand Down Expand Up @@ -38,7 +37,8 @@ describe('LogbookWidgetComponent', () => {
"tags" : [ ],
"versionable" : true,
"deleted" : false,

"updateACL": ["updateRole"],
"deleteACL": ["deleteRole"],
}

logbookSpy = jasmine.createSpyObj("LogbookInfoService", ["logbookInfo"]);
Expand Down Expand Up @@ -71,4 +71,26 @@ describe('LogbookWidgetComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});


[
{input: ['roles'], output: ['updateRole']},
{input: ['updateRole'], output: []},
{input: ['admin'], output: []}
].forEach((t, i) => {
it(`should test enable members ${i}`, () => {
component['userPreferences'].userInfo.roles = t.input;
const groups = component['enabledMembers']('update');
expect(groups).toEqual(t.output);
if (t.output.length > 0)
expect(component.tooltips.update).toEqual(`Enabled for members of '${t.output.concat('admin')}'`);
});
});

it('should test enableActions', () => {
component['enableActions']();
expect(component.tooltips.update).toEqual(`Enabled for members of 'updateRole,admin'`);
expect(component.tooltips.delete).toEqual(`Enabled for members of 'deleteRole,admin'`);
expect(component.tooltips.edit).toEqual(`Enabled for members of 'updateRole,deleteRole,admin'`);
});
});

0 comments on commit 1c3cf5c

Please sign in to comment.