Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
minottic committed Jan 30, 2024
1 parent 20fb699 commit 97034df
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
60 changes: 46 additions & 14 deletions scilog/src/app/logbook/core/snippet/snippet.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { of } from 'rxjs';
import { UserPreferencesService } from '@shared/user-preferences.service';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { WidgetItemConfig } from '@model/config';
import { IsAllowedService } from 'src/app/overview/is-allowed.service';


describe('SnippetComponent', () => {
let component: SnippetComponent;
let fixture: ComponentFixture<SnippetComponent>;
let logbookItemDataServiceSpy: any;
let userPreferencesServiceSpy: any;
let isActionAllowedService: IsAllowedService;

logbookItemDataServiceSpy = jasmine.createSpyObj(
"LogbookItemDataService",
Expand Down Expand Up @@ -58,6 +60,7 @@ describe('SnippetComponent', () => {
{ provide: MatDialogRef, useValue: {} },
{ provide: LogbookItemDataService, useValue: logbookItemDataServiceSpy },
{ provide: UserPreferencesService, useValue: userPreferencesServiceSpy },
{ provide: IsAllowedService, useClass: isActionAllowedService },
],
declarations: [SnippetComponent]
})
Expand Down Expand Up @@ -215,35 +218,37 @@ describe('SnippetComponent', () => {
}));

it('should release the lock', async () => {
spyOn(component, 'allowEdit').and.returnValue(true);
component._enableEdit = false;
component._enableEdit = {update: false, delete: false};
component.snippetIsAccessedByAnotherUser = true;
spyOn(component['isActionAllowed'], 'canUpdate').and.returnValue(true);
spyOn(component['isActionAllowed'], 'canDelete').and.returnValue(true);
spyOn(component['isActionAllowed'], 'isNotExpired').and.returnValue(true);
await component.releaseLock();
expect(component.enableEdit).toEqual(true);
expect(component.enableEdit).toEqual({update: true, delete: true});
expect(component.snippetIsAccessedByAnotherUser).toEqual(false);
expect(component['logbookItemDataService'].deleteAllInProgressEditing)
.toHaveBeenCalledOnceWith(snippetMock.id);
})
});

it('should add the lock', () => {
spyOn(component, 'releaseLock');
spyOn(window, 'clearTimeout');
component.snippetIsAccessedByAnotherUser = false;
component.timerId = 123;
component._enableEdit = true;
component._enableEdit = {update: true, delete: true};
component.lockEdit();
expect(component.enableEdit).toEqual(false);
expect(component.enableEdit).toEqual({update: false, delete: false});
expect(component.snippetIsAccessedByAnotherUser).toEqual(true);
expect(window.clearTimeout).toHaveBeenCalledOnceWith(123);
})
});

it('should set the editing timeout', () => {
spyOn(window, "setTimeout");
component.timerId = null;
component.setEditTimeout(123);
expect(component.timerId).not.toEqual(null);
expect(window.setTimeout).toHaveBeenCalledOnceWith(jasmine.any(Function), 123);
})
});

it('should lock the edit timeout and set by', () => {
const avatarForUser = "aTestUserForlockEditUntilTimeout";
Expand All @@ -253,49 +258,76 @@ describe('SnippetComponent', () => {
expect(component.avatarHash).toEqual(avatarForUser);
expect(component.lockEdit).toHaveBeenCalledTimes(1);
expect(component.setEditTimeout).toHaveBeenCalledTimes(1);
})
});

it('should set locked', () => {
spyOn(component, "getLastEditedSnippet").and.returnValue({createdAt: "2023-01-01", updatedBy: "aUser"});
spyOn(Date.prototype, "getTime").and.returnValue(1672531200001);
spyOn(component, "lockEditUntilTimeout");
component.setLocked();
expect(component.lockEditUntilTimeout).toHaveBeenCalledOnceWith("aUser", component._timeoutMilliseconds - 1);
})
});

it('should release old lock', () => {
spyOn(component, "getLastEditedSnippet").and.returnValue({createdAt: "2023-01-01", updatedBy: "aUser"});
spyOn(Date.prototype, "getTime").and.returnValue(16725312000000);
spyOn(component, "releaseLock");
component.setLocked();
expect(component.releaseLock).toHaveBeenCalledTimes(1);
})
});

it('should get undefined from last edited snippet', () => {
const subs = [{snippetType: 'paragraph'}, {snippetType: 'paragraph'}];
expect(component.getLastEditedSnippet(subs)).toEqual(undefined);
})
});

it('should get first toDelete from last edited snippet', () => {
const subs = [
{ snippetType: "edit", toDelete: true, id: "1" },
{ snippetType: "edit", toDelete: true, id: "2" }
];
expect(component.getLastEditedSnippet(subs)).toEqual(subs[0]);
})
});

it('should get last snippet from last edited snippet', () => {
const subs = [
{snippetType: "edit", createdAt: "2023-01-02"},
{snippetType: "edit", createdAt: "2023-01-01"},
];
expect(component.getLastEditedSnippet(subs)).toEqual(subs[0]);
})
});

it('should call setLocked after subsnippets change', () => {
spyOn(component, "setLocked");
component.subsnippets = [{parentId: "123"}];
expect(component.setLocked).toHaveBeenCalledTimes(1);
});

[
{input: {v: true, isNotExpired: true}, output: true},
{input: {v: true, isNotExpired: false}, output: false},
{input: {v: false, isNotExpired: true}, output: false},
{input: {v: false, isNotExpired: false}, output: false},
].forEach((t, i) => {
it(`should test commonCondition ${i}`, () => {
spyOn(component['isActionAllowed'], "isNotExpired").and.returnValue(t.input.isNotExpired);
expect(component['commonCondition'](t.input.v)).toEqual(t.output);
})
});

[
{input: {v: true, canUpdate: true}, output: true},
{input: {v: true, canUpdate: false}, output: false},
{input: {v: false, canUpdate: false}, output: false},
{input: {v: false, canUpdate: true}, output: false},
].forEach((t, i) => {
it(`should test enableEdit ${i}`, () => {
spyOn(component['isActionAllowed'], "canUpdate").and.returnValue(t.input.canUpdate);
spyOn(component['isActionAllowed'], "canDelete").and.returnValue(true);
spyOn<any>(component, "commonCondition").and.returnValue(t.input.v);
component.enableEdit = t.input.v;
expect(component._enableEdit.update).toEqual(t.output);
})
})

});
17 changes: 15 additions & 2 deletions scilog/src/app/logbook/widgets/todos/todos.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ChangeStreamService } from '@shared/change-stream.service';
import { AppConfigService } from 'src/app/app-config.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { ViewsService } from '@shared/views.service';
import { IsAllowedService } from 'src/app/overview/is-allowed.service';

class ChangeStreamServiceMock {
getNotification(id:string){
Expand Down Expand Up @@ -38,16 +39,19 @@ const getConfig = () => ({});
describe('TodosComponent', () => {
let component: TodosComponent;
let fixture: ComponentFixture<TodosComponent>;


let isActionAllowedServiceSpy
beforeEach(waitForAsync(() => {
isActionAllowedServiceSpy = jasmine.createSpyObj("IsAllowedService", ["canUpdate", "canDelete"]);
isActionAllowedServiceSpy.tooltips = {update: '', delete: ''};
TestBed.configureTestingModule({
providers:[
{provide: LogbookInfoService, useClass: LogbookInfoMock},
{provide: TasksService, useClass: TasksServiceMock},
{ provide: ChangeStreamService, useClass: ChangeStreamServiceMock },
{ provide: AppConfigService, useValue: { getConfig } },
{ provide: ViewsService, useClass: ViewsServiceMock },
{ provide: IsAllowedService, useValue: isActionAllowedServiceSpy },
],
imports: [HttpClientTestingModule],
declarations: [ TodosComponent ]
Expand All @@ -65,4 +69,13 @@ describe('TodosComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

it('should test isAllowed', () => {
isActionAllowedServiceSpy.canUpdate.calls.reset();
isActionAllowedServiceSpy.canDelete.calls.reset();
component.isAllowed(0);
expect(isActionAllowedServiceSpy.canUpdate).toHaveBeenCalledTimes(1);
expect(isActionAllowedServiceSpy.canDelete).toHaveBeenCalledTimes(1);
});

});

0 comments on commit 97034df

Please sign in to comment.