Skip to content

Commit

Permalink
Fix tests and make translate pipe standalone
Browse files Browse the repository at this point in the history
  • Loading branch information
PaRangger committed Nov 27, 2024
1 parent 860ede4 commit 701bc14
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 33 deletions.
8 changes: 4 additions & 4 deletions src/main/webapp/app/shared/pipes/artemis-translate.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { TranslatePipe, TranslateService } from '@ngx-translate/core';
@Pipe({
name: 'artemisTranslate',
pure: false,
standalone: true,
})
/**
* a simple wrapper to prevent compile errors in IntelliJ
*/
export class ArtemisTranslatePipe implements PipeTransform, OnDestroy {
private translatePipe: TranslatePipe;
constructor(
private translateService: TranslateService,
private changeDetectorRef: ChangeDetectorRef,
) {

// We cannot inject() ChangeDetectorRef here
constructor(translateService: TranslateService, changeDetectorRef: ChangeDetectorRef) {
this.translatePipe = new TranslatePipe(translateService, changeDetectorRef);
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/webapp/app/shared/shared-common.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { CloseCircleComponent } from 'app/shared/close-circle/close-circle.compo
import { ArtemisDateRangePipe } from 'app/shared/pipes/artemis-date-range.pipe';

@NgModule({
imports: [ArtemisSharedLibsModule, TranslateDirective],
imports: [ArtemisSharedLibsModule, TranslateDirective, ArtemisTranslatePipe],
declarations: [
ArtemisDatePipe,
ArtemisDateRangePipe,
Expand All @@ -27,7 +27,6 @@ import { ArtemisDateRangePipe } from 'app/shared/pipes/artemis-date-range.pipe';
ArtemisDurationFromSecondsPipe,
DurationPipe,
CloseCircleComponent,
ArtemisTranslatePipe,
],
exports: [
ArtemisSharedLibsModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ArtemisTestModule } from '../../test.module';
import { Result } from 'app/entities/result.model';
import { AlertOverlayComponent } from 'app/shared/alert/alert-overlay.component';
import { AssessmentWarningComponent } from 'app/assessment/assessment-warning/assessment-warning.component';
import { MockProvider } from 'ng-mocks';
import { MockPipe, MockProvider } from 'ng-mocks';
import { Exercise, ExerciseType } from 'app/entities/exercise.model';
import { MockSyncStorage } from '../../helpers/mocks/service/mock-sync-storage.service';
import { LocalStorageService, SessionStorageService } from 'ngx-webstorage';
Expand Down Expand Up @@ -49,8 +49,8 @@ describe('AssessmentHeaderComponent', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ArtemisTestModule, NgbTooltipMocksModule, NgbAlertsMocksModule, RouterModule.forRoot([])],
declarations: [AssessmentHeaderComponent, AssessmentWarningComponent, AlertOverlayComponent, ArtemisTranslatePipe, MockTranslateValuesDirective],
imports: [ArtemisTestModule, NgbTooltipMocksModule, NgbAlertsMocksModule, RouterModule.forRoot([]), MockPipe(ArtemisTranslatePipe)],
declarations: [AssessmentHeaderComponent, AssessmentWarningComponent, AlertOverlayComponent, MockTranslateValuesDirective],
providers: [
{
provide: AlertService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,39 @@ describe('PostingContentPartComponent', () => {
let contentBeforeReference: string;
let contentAfterReference: string;

beforeEach(() => {
return TestBed.configureTestingModule({
imports: [ArtemisTestModule, MatDialogModule, MatMenuModule],
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
ArtemisTestModule,
MatDialogModule,
MatMenuModule,
HtmlForPostingMarkdownPipe, // we want to test against the rendered string, therefore we cannot mock the pipe
],
declarations: [
PostingContentPartComponent,
HtmlForPostingMarkdownPipe, // we want to test against the rendered string, therefore we cannot mock the pipe
// FaIconComponent, // we want to test the type of rendered icons, therefore we cannot mock the component
MockRouterLinkDirective,
MockQueryParamsDirective,
],
providers: [{ provide: FileService, useClass: MockFileService }, { provide: Router, useClass: MockRouter }, MockProvider(AccountService)],
})
.compileComponents()
.then(() => {
fixture = TestBed.createComponent(PostingContentPartComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
router = TestBed.inject(Router);
fileService = TestBed.inject(FileService);

navigateByUrlSpy = jest.spyOn(router, 'navigateByUrl');
openAttachmentSpy = jest.spyOn(fileService, 'downloadFile');
enlargeImageSpy = jest.spyOn(component, 'enlargeImage');

contentBeforeReference = '**Be aware**\n\n I want to reference the following Post ';
contentAfterReference = 'in my content,\n\n does it *actually* work?';
});
providers: [
{ provide: FileService, useClass: MockFileService },
{
provide: Router,
useClass: MockRouter,
},
MockProvider(AccountService),
],
}).compileComponents();
fixture = TestBed.createComponent(PostingContentPartComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement;
router = TestBed.inject(Router);
fileService = TestBed.inject(FileService);
navigateByUrlSpy = jest.spyOn(router, 'navigateByUrl');
openAttachmentSpy = jest.spyOn(fileService, 'downloadFile');
enlargeImageSpy = jest.spyOn(component, 'enlargeImage');
contentBeforeReference = '**Be aware**\n\n I want to reference the following Post ';
contentAfterReference = 'in my content,\n\n does it *actually* work?';
});

describe('For posting without reference', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import { MockTranslateService } from '../helpers/mocks/service/mock-translate.se
import { PLACEHOLDER_USER_REACTED, ReactingUsersOnPostingPipe } from 'app/shared/pipes/reacting-users-on-posting.pipe';
import { TranslateService } from '@ngx-translate/core';
import { metisTutor, metisUser1, metisUser2 } from '../helpers/sample/metis-sample-data';
import { MockPipe } from 'ng-mocks';

describe('ReactingUsersOnPostingsPipe', () => {
let reactingUsersPipe: ReactingUsersOnPostingPipe;
let translateService: TranslateService;
let updateReactingUsersStringSpy: jest.SpyInstance;
let transformedStringWithReactingUsers: string;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [HtmlForPostingMarkdownPipe],
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [MockPipe(HtmlForPostingMarkdownPipe)],
providers: [{ provide: TranslateService, useClass: MockTranslateService }],
})
.compileComponents()
Expand Down

0 comments on commit 701bc14

Please sign in to comment.