[ADF-5422] remove deprecated "async()" from unit tests (#7109)

* remove angualar async from content services

* upgrade more tests

* upgrade core tests

* upgrade tests

* fix deprecated constant

* fix tests

* fix after rebase
This commit is contained in:
Denys Vuika
2021-06-15 16:16:15 +01:00
committed by GitHub
parent ba03c60adb
commit 3079aa48c3
121 changed files with 5316 additions and 4780 deletions

View File

@@ -16,7 +16,7 @@
*/
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
import { CommentModel, UserProcessModel } from '../models';
import { CommentListComponent } from './comment-list.component';
import { By } from '@angular/platform-browser';
@@ -26,28 +26,28 @@ import { setupTestBed } from '../testing/setup-test-bed';
import { CoreTestingModule } from '../testing/core.testing.module';
import { TranslateModule } from '@ngx-translate/core';
const testUser: UserProcessModel = new UserProcessModel({
const testUser = new UserProcessModel({
id: '1',
firstName: 'Test',
lastName: 'User',
email: 'tu@domain.com'
});
const processCommentOne: CommentModel = new CommentModel({
const processCommentOne = new CommentModel({
id: 1,
message: 'Test Comment',
created: new Date(),
createdBy: testUser
});
const processCommentTwo: CommentModel = new CommentModel({
const processCommentTwo = new CommentModel({
id: 2,
message: '2nd Test Comment',
created: new Date(),
createdBy: testUser
});
const contentCommentUserPictureDefined: CommentModel = new CommentModel({
const contentCommentUserPictureDefined = new CommentModel({
id: 2,
message: '2nd Test Comment',
created: new Date(),
@@ -63,7 +63,7 @@ const contentCommentUserPictureDefined: CommentModel = new CommentModel({
}
});
const processCommentUserPictureDefined: CommentModel = new CommentModel({
const processCommentUserPictureDefined = new CommentModel({
id: 2,
message: '2nd Test Comment',
created: new Date(),
@@ -76,7 +76,7 @@ const processCommentUserPictureDefined: CommentModel = new CommentModel({
}
});
const contentCommentUserNoPictureDefined: CommentModel = new CommentModel({
const contentCommentUserNoPictureDefined = new CommentModel({
id: 2,
message: '2nd Test Comment',
created: new Date(),
@@ -91,7 +91,7 @@ const contentCommentUserNoPictureDefined: CommentModel = new CommentModel({
}
});
const processCommentUserNoPictureDefined: CommentModel = new CommentModel({
const processCommentUserNoPictureDefined = new CommentModel({
id: 2,
message: '2nd Test Comment',
created: new Date(),
@@ -119,7 +119,7 @@ describe('CommentListComponent', () => {
schemas: [CUSTOM_ELEMENTS_SCHEMA]
});
beforeEach(async(() => {
beforeEach(() => {
ecmUserService = TestBed.inject(EcmUserService);
spyOn(ecmUserService, 'getUserProfileImage').and.returnValue('alfresco-logo.svg');
@@ -130,16 +130,16 @@ describe('CommentListComponent', () => {
commentList = fixture.componentInstance;
element = fixture.nativeElement;
fixture.detectChanges();
}));
});
afterEach(() => {
fixture.destroy();
});
it('should emit row click event', async(() => {
it('should emit row click event', fakeAsync(() => {
commentList.comments = [Object.assign({}, processCommentOne)];
commentList.clickRow.subscribe((selectedComment) => {
commentList.clickRow.subscribe((selectedComment: CommentModel) => {
expect(selectedComment.id).toEqual(1);
expect(selectedComment.message).toEqual('Test Comment');
expect(selectedComment.createdBy).toEqual(testUser);
@@ -153,7 +153,7 @@ describe('CommentListComponent', () => {
});
}));
it('should deselect the previous selected comment when a new one is clicked', async(() => {
it('should deselect the previous selected comment when a new one is clicked', fakeAsync(() => {
processCommentOne.isSelected = true;
const commentOne = Object.assign({}, processCommentOne);
const commentTwo = Object.assign({}, processCommentTwo);
@@ -174,127 +174,126 @@ describe('CommentListComponent', () => {
});
}));
it('should not show comment list if no input is given', async(() => {
it('should not show comment list if no input is given', async () => {
fixture.detectChanges();
await fixture.whenStable();
fixture.whenStable().then(() => {
expect(fixture.nativeElement.querySelector('adf-datatable')).toBeNull();
});
}));
expect(fixture.nativeElement.querySelector('adf-datatable')).toBeNull();
});
it('should show comment message when input is given', async(() => {
it('should show comment message when input is given', async () => {
commentList.comments = [Object.assign({}, processCommentOne)];
fixture.detectChanges();
await fixture.whenStable();
fixture.whenStable().then(() => {
const elements = fixture.nativeElement.querySelectorAll('#comment-message');
expect(elements.length).toBe(1);
expect(elements[0].innerText).toBe(processCommentOne.message);
expect(fixture.nativeElement.querySelector('#comment-message:empty')).toBeNull();
});
}));
const elements = fixture.nativeElement.querySelectorAll('#comment-message');
expect(elements.length).toBe(1);
expect(elements[0].innerText).toBe(processCommentOne.message);
expect(fixture.nativeElement.querySelector('#comment-message:empty')).toBeNull();
});
it('should show comment user when input is given', async(() => {
it('should show comment user when input is given', async () => {
commentList.comments = [Object.assign({}, processCommentOne)];
fixture.detectChanges();
await fixture.whenStable();
fixture.whenStable().then(() => {
const elements = fixture.nativeElement.querySelectorAll('#comment-user');
expect(elements.length).toBe(1);
expect(elements[0].innerText).toBe(processCommentOne.createdBy.firstName + ' ' + processCommentOne.createdBy.lastName);
expect(fixture.nativeElement.querySelector('#comment-user:empty')).toBeNull();
});
}));
const elements = fixture.nativeElement.querySelectorAll('#comment-user');
expect(elements.length).toBe(1);
expect(elements[0].innerText).toBe(processCommentOne.createdBy.firstName + ' ' + processCommentOne.createdBy.lastName);
expect(fixture.nativeElement.querySelector('#comment-user:empty')).toBeNull();
});
it('comment date time should start with few seconds ago when comment date is few seconds ago', async(() => {
it('comment date time should start with few seconds ago when comment date is few seconds ago', async () => {
const commentFewSecond = Object.assign({}, processCommentOne);
commentFewSecond.created = new Date();
commentList.comments = [commentFewSecond];
fixture.detectChanges();
await fixture.whenStable();
fixture.whenStable().then(() => {
element = fixture.nativeElement.querySelector('#comment-time');
expect(element.innerText).toContain('a few seconds ago');
});
}));
element = fixture.nativeElement.querySelector('#comment-time');
expect(element.innerText).toContain('a few seconds ago');
});
it('comment date time should start with Yesterday when comment date is yesterday', async(() => {
it('comment date time should start with Yesterday when comment date is yesterday', async () => {
const commentOld = Object.assign({}, processCommentOne);
commentOld.created = new Date((Date.now() - 24 * 3600 * 1000));
commentList.comments = [commentOld];
fixture.detectChanges();
await fixture.whenStable();
fixture.whenStable().then(() => {
element = fixture.nativeElement.querySelector('#comment-time');
expect(element.innerText).toContain('a day ago');
});
}));
element = fixture.nativeElement.querySelector('#comment-time');
expect(element.innerText).toContain('a day ago');
});
it('comment date time should not start with Today/Yesterday when comment date is before yesterday', async(() => {
it('comment date time should not start with Today/Yesterday when comment date is before yesterday', async () => {
const commentOld = Object.assign({}, processCommentOne);
commentOld.created = new Date((Date.now() - 24 * 3600 * 1000 * 2));
commentList.comments = [commentOld];
fixture.detectChanges();
await fixture.whenStable();
fixture.whenStable().then(() => {
element = fixture.nativeElement.querySelector('#comment-time');
expect(element.innerText).not.toContain('Today');
expect(element.innerText).not.toContain('Yesterday');
});
}));
element = fixture.nativeElement.querySelector('#comment-time');
expect(element.innerText).not.toContain('Today');
expect(element.innerText).not.toContain('Yesterday');
});
it('should show user icon when input is given', async(() => {
it('should show user icon when input is given', async () => {
commentList.comments = [Object.assign({}, processCommentOne)];
fixture.detectChanges();
await fixture.whenStable();
fixture.whenStable().then(() => {
const elements = fixture.nativeElement.querySelectorAll('#comment-user-icon');
expect(elements.length).toBe(1);
expect(elements[0].innerText).toContain(commentList.getUserShortName(processCommentOne.createdBy));
expect(fixture.nativeElement.querySelector('#comment-user-icon:empty')).toBeNull();
});
}));
const elements = fixture.nativeElement.querySelectorAll('#comment-user-icon');
expect(elements.length).toBe(1);
expect(elements[0].innerText).toContain(commentList.getUserShortName(processCommentOne.createdBy));
expect(fixture.nativeElement.querySelector('#comment-user-icon:empty')).toBeNull();
});
it('should return content picture when is a content user with a picture', async(() => {
it('should return content picture when is a content user with a picture', async () => {
commentList.comments = [contentCommentUserPictureDefined];
fixture.detectChanges();
await fixture.whenStable();
fixture.whenStable().then(() => {
const elements = fixture.nativeElement.querySelectorAll('.adf-people-img');
expect(elements.length).toBe(1);
expect(fixture.nativeElement.getElementsByClassName('adf-people-img')[0].src).toContain('alfresco-logo.svg');
});
}));
const elements = fixture.nativeElement.querySelectorAll('.adf-people-img');
expect(elements.length).toBe(1);
expect(fixture.nativeElement.getElementsByClassName('adf-people-img')[0].src).toContain('alfresco-logo.svg');
});
it('should return process picture when is a process user with a picture', async(() => {
it('should return process picture when is a process user with a picture', async () => {
commentList.comments = [processCommentUserPictureDefined];
fixture.detectChanges();
await fixture.whenStable();
fixture.whenStable().then(() => {
const elements = fixture.nativeElement.querySelectorAll('.adf-people-img');
expect(elements.length).toBe(1);
expect(fixture.nativeElement.getElementsByClassName('adf-people-img')[0].src).toContain('alfresco-logo.svg');
});
}));
const elements = fixture.nativeElement.querySelectorAll('.adf-people-img');
expect(elements.length).toBe(1);
expect(fixture.nativeElement.getElementsByClassName('adf-people-img')[0].src).toContain('alfresco-logo.svg');
});
it('should return content short name when is a content user without a picture', async(() => {
it('should return content short name when is a content user without a picture', async () => {
commentList.comments = [contentCommentUserNoPictureDefined];
fixture.detectChanges();
await fixture.whenStable();
fixture.whenStable().then(() => {
const elements = fixture.nativeElement.querySelectorAll('.adf-comment-user-icon');
expect(elements.length).toBe(1);
});
}));
const elements = fixture.nativeElement.querySelectorAll('.adf-comment-user-icon');
expect(elements.length).toBe(1);
});
it('should return process short name when is a process user without a picture', async(() => {
it('should return process short name when is a process user without a picture', async () => {
commentList.comments = [processCommentUserNoPictureDefined];
fixture.detectChanges();
fixture.whenStable().then(() => {
const elements = fixture.nativeElement.querySelectorAll('.adf-comment-user-icon');
expect(elements.length).toBe(1);
});
}));
fixture.detectChanges();
await fixture.whenStable();
const elements = fixture.nativeElement.querySelectorAll('.adf-comment-user-icon');
expect(elements.length).toBe(1);
});
});

View File

@@ -16,7 +16,7 @@
*/
import { CUSTOM_ELEMENTS_SCHEMA, SimpleChange } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { of, throwError } from 'rxjs';
import { CommentProcessService } from '../services/comment-process.service';
import { CommentsComponent } from './comments.component';
@@ -27,7 +27,6 @@ import { TranslateModule } from '@ngx-translate/core';
import { CommentModel } from '../models/comment.model';
describe('CommentsComponent', () => {
let component: CommentsComponent;
let fixture: ComponentFixture<CommentsComponent>;
let getProcessCommentsSpy: jasmine.Spy;
@@ -109,66 +108,65 @@ describe('CommentsComponent', () => {
expect(getProcessCommentsSpy).not.toHaveBeenCalled();
});
it('should display comments when the task has comments', async(() => {
it('should display comments when the task has comments', async () => {
const change = new SimpleChange(null, '123', true);
component.ngOnChanges({'taskId': change});
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(fixture.nativeElement.querySelectorAll('#comment-message').length).toBe(3);
expect(fixture.nativeElement.querySelector('#comment-message:empty')).toBeNull();
});
}));
fixture.detectChanges();
await fixture.whenStable();
it('should display comments count when the task has comments', async(() => {
expect(fixture.nativeElement.querySelectorAll('#comment-message').length).toBe(3);
expect(fixture.nativeElement.querySelector('#comment-message:empty')).toBeNull();
});
it('should display comments count when the task has comments', async () => {
const change = new SimpleChange(null, '123', true);
component.ngOnChanges({'taskId': change});
fixture.whenStable().then(() => {
fixture.detectChanges();
const element = fixture.nativeElement.querySelector('#comment-header');
expect(element.innerText).toBe('COMMENTS.HEADER');
});
}));
it('should not display comments when the task has no comments', async(() => {
fixture.detectChanges();
await fixture.whenStable();
const element = fixture.nativeElement.querySelector('#comment-header');
expect(element.innerText).toBe('COMMENTS.HEADER');
});
it('should not display comments when the task has no comments', async () => {
component.taskId = '123';
getProcessCommentsSpy.and.returnValue(of([]));
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('#comment-container')).toBeNull();
});
}));
it('should display comments input by default', async(() => {
fixture.detectChanges();
await fixture.whenStable()
expect(fixture.nativeElement.querySelector('#comment-container')).toBeNull();
});
it('should display comments input by default', async () => {
const change = new SimpleChange(null, '123', true);
component.ngOnChanges({'taskId': change});
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('#comment-input')).not.toBeNull();
});
}));
it('should not display comments input when the task is readonly', async(() => {
component.readOnly = true;
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('#comment-input')).toBeNull();
});
}));
await fixture.whenStable()
expect(fixture.nativeElement.querySelector('#comment-input')).not.toBeNull();
});
it('should not display comments input when the task is readonly', async () => {
component.readOnly = true;
fixture.detectChanges();
await fixture.whenStable()
expect(fixture.nativeElement.querySelector('#comment-input')).toBeNull();
});
describe('change detection taskId', () => {
const change = new SimpleChange('123', '456', true);
const nullChange = new SimpleChange('123', null, true);
beforeEach(async(() => {
beforeEach(() => {
component.taskId = '123';
fixture.detectChanges();
fixture.whenStable().then(() => {
getProcessCommentsSpy.calls.reset();
});
}));
});
it('should fetch new comments when taskId changed', () => {
component.ngOnChanges({'taskId': change});
@@ -187,17 +185,13 @@ describe('CommentsComponent', () => {
});
describe('change detection node', () => {
const change = new SimpleChange('123', '456', true);
const nullChange = new SimpleChange('123', null, true);
beforeEach(async(() => {
beforeEach(() => {
component.nodeId = '123';
fixture.detectChanges();
fixture.whenStable().then(() => {
getContentCommentsSpy.calls.reset();
});
}));
});
it('should fetch new comments when nodeId changed', () => {
component.ngOnChanges({'nodeId': change});
@@ -217,81 +211,81 @@ describe('CommentsComponent', () => {
describe('Add comment task', () => {
beforeEach(async(() => {
beforeEach(() => {
component.taskId = '123';
fixture.detectChanges();
fixture.whenStable();
}));
});
it('should sanitize comment when user input contains html elements', async(() => {
it('should sanitize comment when user input contains html elements', async () => {
const element = fixture.nativeElement.querySelector('.adf-comments-input-add');
component.message = '<div class="text-class"><button onclick=""><h1>action</h1></button></div>';
element.dispatchEvent(new Event('click'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(addProcessCommentSpy).toHaveBeenCalledWith('123', 'action');
});
}));
it('should normalize comment when user input contains spaces sequence', async(() => {
fixture.detectChanges();
await fixture.whenStable();
expect(addProcessCommentSpy).toHaveBeenCalledWith('123', 'action');
});
it('should normalize comment when user input contains spaces sequence', async () => {
const element = fixture.nativeElement.querySelector('.adf-comments-input-add');
component.message = 'test comment';
element.dispatchEvent(new Event('click'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(addProcessCommentSpy).toHaveBeenCalledWith('123', 'test comment');
});
}));
it('should add break lines to comment when user input contains new line characters', async(() => {
fixture.detectChanges();
await fixture.whenStable();
expect(addProcessCommentSpy).toHaveBeenCalledWith('123', 'test comment');
});
it('should add break lines to comment when user input contains new line characters', async () => {
const element = fixture.nativeElement.querySelector('.adf-comments-input-add');
component.message = 'these\nare\nparagraphs\n';
element.dispatchEvent(new Event('click'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(addProcessCommentSpy).toHaveBeenCalledWith('123', 'these<br/>are<br/>paragraphs');
});
}));
it('should call service to add a comment when add button is pressed', async(() => {
fixture.detectChanges();
await fixture.whenStable();
expect(addProcessCommentSpy).toHaveBeenCalledWith('123', 'these<br/>are<br/>paragraphs');
});
it('should call service to add a comment when add button is pressed', async () => {
const element = fixture.nativeElement.querySelector('.adf-comments-input-add');
component.message = 'Test Comment';
element.dispatchEvent(new Event('click'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(addProcessCommentSpy).toHaveBeenCalled();
const elements = fixture.nativeElement.querySelectorAll('#comment-message');
expect(elements.length).toBe(1);
expect(elements[0].innerText).toBe('Test Comment');
});
}));
it('should not call service to add a comment when comment is empty', async(() => {
fixture.detectChanges();
await fixture.whenStable();
expect(addProcessCommentSpy).toHaveBeenCalled();
const elements = fixture.nativeElement.querySelectorAll('#comment-message');
expect(elements.length).toBe(1);
expect(elements[0].innerText).toBe('Test Comment');
});
it('should not call service to add a comment when comment is empty', async () => {
const element = fixture.nativeElement.querySelector('.adf-comments-input-add');
component.message = '';
element.dispatchEvent(new Event('click'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(addProcessCommentSpy).not.toHaveBeenCalled();
});
}));
it('should clear comment when escape key is pressed', async(() => {
fixture.detectChanges();
await fixture.whenStable();
expect(addProcessCommentSpy).not.toHaveBeenCalled();
});
it('should clear comment when escape key is pressed', async () => {
const event = new KeyboardEvent('keydown', {'key': 'Escape'});
let element = fixture.nativeElement.querySelector('#comment-input');
element.dispatchEvent(event);
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
element = fixture.nativeElement.querySelector('#comment-input');
expect(element.value).toBe('');
});
}));
await fixture.whenStable();
element = fixture.nativeElement.querySelector('#comment-input');
expect(element.value).toBe('');
});
it('should emit an error when an error occurs adding the comment', () => {
const emitSpy = spyOn(component.error, 'emit');
@@ -304,81 +298,81 @@ describe('CommentsComponent', () => {
describe('Add comment node', () => {
beforeEach(async(() => {
beforeEach(() => {
component.nodeId = '123';
fixture.detectChanges();
fixture.whenStable();
}));
});
it('should call service to add a comment when add button is pressed', async(() => {
it('should call service to add a comment when add button is pressed', async () => {
const element = fixture.nativeElement.querySelector('.adf-comments-input-add');
component.message = 'Test Comment';
element.dispatchEvent(new Event('click'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(addContentCommentSpy).toHaveBeenCalled();
const elements = fixture.nativeElement.querySelectorAll('#comment-message');
expect(elements.length).toBe(1);
expect(elements[0].innerText).toBe('Test Comment');
});
}));
it('should sanitize comment when user input contains html elements', async(() => {
fixture.detectChanges();
await fixture.whenStable();
expect(addContentCommentSpy).toHaveBeenCalled();
const elements = fixture.nativeElement.querySelectorAll('#comment-message');
expect(elements.length).toBe(1);
expect(elements[0].innerText).toBe('Test Comment');
});
it('should sanitize comment when user input contains html elements', async () => {
const element = fixture.nativeElement.querySelector('.adf-comments-input-add');
component.message = '<div class="text-class"><button onclick=""><h1>action</h1></button></div>';
element.dispatchEvent(new Event('click'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(addContentCommentSpy).toHaveBeenCalledWith('123', 'action');
});
}));
it('should normalize comment when user input contains spaces sequence', async(() => {
fixture.detectChanges();
await fixture.whenStable();
expect(addContentCommentSpy).toHaveBeenCalledWith('123', 'action');
});
it('should normalize comment when user input contains spaces sequence', async () => {
const element = fixture.nativeElement.querySelector('.adf-comments-input-add');
component.message = 'test comment';
element.dispatchEvent(new Event('click'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(addContentCommentSpy).toHaveBeenCalledWith('123', 'test comment');
});
}));
it('should add break lines to comment when user input contains new line characters', async(() => {
fixture.detectChanges();
await fixture.whenStable();
expect(addContentCommentSpy).toHaveBeenCalledWith('123', 'test comment');
});
it('should add break lines to comment when user input contains new line characters', async () => {
const element = fixture.nativeElement.querySelector('.adf-comments-input-add');
component.message = 'these\nare\nparagraphs\n';
element.dispatchEvent(new Event('click'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(addContentCommentSpy).toHaveBeenCalledWith('123', 'these<br/>are<br/>paragraphs');
});
}));
it('should not call service to add a comment when comment is empty', async(() => {
fixture.detectChanges();
await fixture.whenStable();
expect(addContentCommentSpy).toHaveBeenCalledWith('123', 'these<br/>are<br/>paragraphs');
});
it('should not call service to add a comment when comment is empty', async () => {
const element = fixture.nativeElement.querySelector('.adf-comments-input-add');
component.message = '';
element.dispatchEvent(new Event('click'));
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(addContentCommentSpy).not.toHaveBeenCalled();
});
}));
it('should clear comment when escape key is pressed', async(() => {
fixture.detectChanges();
await fixture.whenStable();
expect(addContentCommentSpy).not.toHaveBeenCalled();
});
it('should clear comment when escape key is pressed', async () => {
const event = new KeyboardEvent('keydown', {'key': 'Escape'});
let element = fixture.nativeElement.querySelector('#comment-input');
element.dispatchEvent(event);
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
element = fixture.nativeElement.querySelector('#comment-input');
expect(element.value).toBe('');
});
}));
await fixture.whenStable();
element = fixture.nativeElement.querySelector('#comment-input');
expect(element.value).toBe('');
});
it('should emit an error when an error occurs adding the comment', () => {
const emitSpy = spyOn(component.error, 'emit');