Skip to content

Commit

Permalink
Add and adjusts tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RaymondLuong3 committed Dec 4, 2024
1 parent dbd75c2 commit d58cf38
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<ng-container *transloco="let t; read: 'book_select'">
@if (availableBooks.length > 0 && !readonly && !basicMode) {
<div>
<div class="scope-selection">
<mat-checkbox
class="ot-checkbox"
value="OT"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,15 @@ describe('BookMultiSelectComponent', () => {
expect(component.partialOT).toBe(false);
expect(component.partialDC).toBe(true);
});

it('can hide checkboxes and progress in basic mode', async () => {
await component.ngOnChanges();
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('.book-multi-select .border-fill')).not.toBeNull();
expect(fixture.nativeElement.querySelector('.scope-selection')).not.toBeNull();
component.basicMode = true;
fixture.detectChanges();
expect(fixture.nativeElement.querySelector('.book-multi-select .border-fill')).toBeNull();
expect(fixture.nativeElement.querySelector('.scope-selection')).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ <h4 class="explanation">
}
<h2>{{ t("reference_books") }}</h2>
<p>{{ trainingSourceProjectName }}</p>
@if (selectableTrainingBooks.length === 0) {
@if (selectableSourceTrainingBooks.length === 0) {
<app-notice mode="basic" type="light">{{ t("training_books_will_appear") }}</app-notice>
} @else {
<app-book-multi-select
[availableBooks]="selectableTrainingBooks"
[availableBooks]="selectableSourceTrainingBooks"
[selectedBooks]="userSelectedSourceTrainingBooks"
[basicMode]="true"
(bookSelect)="onSourceTrainingBookSelect($event)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ describe('DraftGenerationStepsComponent', () => {
component.userSelectedTrainingBooks = trainingBooks;
component.userSelectedTranslateBooks = translationBooks;
component.selectedTrainingDataIds = trainingDataFiles;
component.userSelectedSourceTrainingBooks = trainingBooks;
component['draftSourceProjectIds'] = { draftingSourceId: 'sourceProject', trainingSourceId: 'sourceProject' };

spyOn(component.done, 'emit');
expect(component.isStepsCompleted).toBe(false);
Expand All @@ -238,11 +240,9 @@ describe('DraftGenerationStepsComponent', () => {
fixture.detectChanges();

expect(component.done.emit).toHaveBeenCalledWith({
trainingBooks: trainingBooks.filter(book => !translationBooks.includes(book)),
trainingDataFiles,
trainingScriptureRanges: [],
translationBooks,
translationScriptureRanges: [],
trainingScriptureRanges: [{ projectId: 'sourceProject', scriptureRange: 'LEV' }],
translationScriptureRanges: [{ projectId: 'sourceProject', scriptureRange: 'GEN;EXO' }],
fastTraining: false
} as DraftGenerationStepsResult);
expect(component.isStepsCompleted).toBe(true);
Expand Down Expand Up @@ -294,6 +294,111 @@ describe('DraftGenerationStepsComponent', () => {
}));
});

describe('additional training source project', () => {
beforeEach(fakeAsync(() => {
const mockTargetProjectDoc = {
data: createTestProjectProfile({
texts: [{ bookNum: 1 }, { bookNum: 2 }, { bookNum: 3 }, { bookNum: 6 }, { bookNum: 7 }],
translateConfig: {
source: { projectRef: 'sourceProject' },
draftConfig: {
additionalTrainingSourceEnabled: true,
additionalTrainingSource: { projectRef: 'sourceProject2' }
}
}
})
} as SFProjectProfileDoc;
when(mockActivatedProjectService.projectDoc).thenReturn(mockTargetProjectDoc);
when(mockActivatedProjectService.projectDoc$).thenReturn(targetProjectDoc$);
when(mockFeatureFlagService.allowFastTraining).thenReturn(createTestFeatureFlag(false));
when(mockProjectService.getProfile(anything())).thenResolve(mockSourceNonNllbProjectDoc);
when(mockNllbLanguageService.isNllbLanguageAsync(anything())).thenResolve(true);
when(mockNllbLanguageService.isNllbLanguageAsync('xyz')).thenResolve(false);
when(mockTrainingDataService.queryTrainingDataAsync(anything())).thenResolve(instance(mockTrainingDataQuery));
when(mockTrainingDataQuery.docs).thenReturn([]);

fixture = TestBed.createComponent(DraftGenerationStepsComponent);
component = fixture.componentInstance;
tick();
fixture.detectChanges();
}));

it('should show and hide selectable training source books when training books selected', () => {
const trainingBooks = [3];
const trainingDataFiles: string[] = [];
const translationBooks = [1, 2];
const availableBooks = [1, 2, 3];

component.userSelectedTrainingBooks = [];
component.userSelectedTranslateBooks = translationBooks;
component.selectedTrainingDataIds = trainingDataFiles;
component.userSelectedSourceTrainingBooks = [];
component.userSelectedAdditionalSourceTrainingBooks = [];
component['availableAdditionalTrainingBooks'] = availableBooks;
component['draftSourceProjectIds'] = {
draftingSourceId: 'sourceProject',
trainingSourceId: 'sourceProject',
trainingAdditionalSourceId: 'sourceProject2'
};
expect(component.availableTrainingBooks).toEqual(availableBooks);
expect(component.selectableSourceTrainingBooks).toEqual([]);
expect(component.selectableAdditionalTrainingBooks).toEqual([]);

// select a training book
component.onTrainingBookSelect(trainingBooks);
fixture.detectChanges();
expect(component.selectableSourceTrainingBooks).toEqual(trainingBooks);
expect(component.selectableAdditionalTrainingBooks).toEqual(trainingBooks);
expect(component.userSelectedSourceTrainingBooks).toEqual(trainingBooks);
expect(component.userSelectedAdditionalSourceTrainingBooks).toEqual(trainingBooks);

// deselect all training books
component.onTrainingBookSelect([]);
fixture.detectChanges();
expect(component.selectableSourceTrainingBooks).toEqual([]);
expect(component.selectableAdditionalTrainingBooks).toEqual([]);
expect(component.userSelectedSourceTrainingBooks).toEqual([]);
expect(component.userSelectedAdditionalSourceTrainingBooks).toEqual([]);
});

it('should correctly emit the selected books when done', () => {
const trainingBooks = [3];
const trainingDataFiles: string[] = [];
const translationBooks = [1, 2];

component.userSelectedTrainingBooks = trainingBooks;
component.userSelectedTranslateBooks = translationBooks;
component.selectedTrainingDataIds = trainingDataFiles;
component.userSelectedSourceTrainingBooks = trainingBooks;
component.userSelectedAdditionalSourceTrainingBooks = trainingBooks;
component['draftSourceProjectIds'] = {
draftingSourceId: 'sourceProject',
trainingSourceId: 'sourceProject',
trainingAdditionalSourceId: 'sourceProject2'
};

spyOn(component.done, 'emit');
expect(component.isStepsCompleted).toBe(false);
// Advance to the next step when at last step should emit books result
fixture.detectChanges();
component.tryAdvanceStep();
fixture.detectChanges();
component.tryAdvanceStep();
fixture.detectChanges();

expect(component.done.emit).toHaveBeenCalledWith({
trainingDataFiles,
trainingScriptureRanges: [
{ projectId: 'sourceProject', scriptureRange: 'LEV' },
{ projectId: 'sourceProject2', scriptureRange: 'LEV' }
],
translationScriptureRanges: [{ projectId: 'sourceProject', scriptureRange: 'GEN;EXO' }],
fastTraining: false
} as DraftGenerationStepsResult);
expect(component.isStepsCompleted).toBe(true);
});
});

describe('allow fast training feature flag is enabled', () => {
beforeEach(fakeAsync(() => {
when(mockActivatedProjectService.projectDoc).thenReturn(mockTargetProjectDoc);
Expand All @@ -317,6 +422,8 @@ describe('DraftGenerationStepsComponent', () => {
component.userSelectedTrainingBooks = trainingBooks;
component.userSelectedTranslateBooks = translationBooks;
component.selectedTrainingDataIds = trainingDataFiles;
component.userSelectedSourceTrainingBooks = trainingBooks;
component['draftSourceProjectIds'] = { draftingSourceId: 'sourceProject', trainingSourceId: 'sourceProject' };

spyOn(component.done, 'emit');

Expand All @@ -338,11 +445,9 @@ describe('DraftGenerationStepsComponent', () => {
fixture.detectChanges();

expect(component.done.emit).toHaveBeenCalledWith({
trainingBooks,
trainingDataFiles,
trainingScriptureRanges: [],
translationBooks,
translationScriptureRanges: [],
trainingScriptureRanges: [{ projectId: 'sourceProject', scriptureRange: 'GEN;EXO' }],
translationScriptureRanges: [{ projectId: 'sourceProject', scriptureRange: 'LEV;NUM' }],
fastTraining: true
} as DraftGenerationStepsResult);
expect(generateDraftButton['disabled']).toBe(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ import { TrainingDataUploadDialogComponent } from '../training-data/training-dat
import { TrainingDataService } from '../training-data/training-data.service';

export interface DraftGenerationStepsResult {
trainingBooks: number[];
trainingDataFiles: string[];
trainingScriptureRange?: string;
trainingScriptureRanges: ProjectScriptureRange[];
translationBooks: number[];
translationScriptureRange?: string;
translationScriptureRanges: ProjectScriptureRange[];
fastTraining: boolean;
Expand Down Expand Up @@ -57,7 +55,7 @@ export class DraftGenerationStepsComponent extends SubscriptionDisposable implem

availableTranslateBooks?: number[] = undefined;
availableTrainingBooks: number[] = [];
selectableTrainingBooks: number[] = [];
selectableSourceTrainingBooks: number[] = [];
selectableAdditionalTrainingBooks: number[] = [];
availableTrainingData: Readonly<TrainingData>[] = [];

Expand Down Expand Up @@ -245,10 +243,18 @@ export class DraftGenerationStepsComponent extends SubscriptionDisposable implem
onTrainingBookSelect(selectedBooks: number[]): void {
const newBookSelections: number[] = selectedBooks.filter(b => !this.userSelectedTrainingBooks.includes(b));
this.userSelectedTrainingBooks = selectedBooks;
this.selectableTrainingBooks = selectedBooks;
this.selectableSourceTrainingBooks = selectedBooks;
this.selectableAdditionalTrainingBooks = this.availableAdditionalTrainingBooks.filter(b =>
selectedBooks.includes(b)
);

// remove selected books that are no longer selectable
this.userSelectedSourceTrainingBooks = this.userSelectedSourceTrainingBooks.filter(b => selectedBooks.includes(b));
this.userSelectedAdditionalSourceTrainingBooks = this.userSelectedAdditionalSourceTrainingBooks.filter(b =>
selectedBooks.includes(b)
);

// automatically select books that are newly selected as training books
for (const bookNum of newBookSelections) {
this.userSelectedSourceTrainingBooks.push(bookNum);
if (this.selectableAdditionalTrainingBooks.includes(bookNum)) {
Expand Down Expand Up @@ -314,11 +320,8 @@ export class DraftGenerationStepsComponent extends SubscriptionDisposable implem
this.userSelectedTranslateBooks
);
this.done.emit({
// TODO: Can trainingBooks and translationBooks be removed?
trainingBooks: this.userSelectedTrainingBooks,
trainingScriptureRanges,
trainingDataFiles: this.selectedTrainingDataIds,
translationBooks: this.userSelectedTranslateBooks,
translationScriptureRanges: [translationScriptureRange],
fastTraining: this.fastTraining
});
Expand All @@ -343,7 +346,8 @@ export class DraftGenerationStepsComponent extends SubscriptionDisposable implem

this.initialSelectedTrainingBooks = newSelectedTrainingBooks;
this.userSelectedTrainingBooks = [...newSelectedTrainingBooks];
this.selectableTrainingBooks = [...newSelectedTrainingBooks];
this.userSelectedSourceTrainingBooks = [...newSelectedTrainingBooks];
this.selectableSourceTrainingBooks = [...newSelectedTrainingBooks];
this.selectableAdditionalTrainingBooks = this.availableAdditionalTrainingBooks.filter(b =>
newSelectedTrainingBooks.includes(b)

Check warning on line 352 in src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/draft-generation-steps/draft-generation-steps.component.ts

View check run for this annotation

Codecov / codecov/patch

src/SIL.XForge.Scripture/ClientApp/src/app/translate/draft-generation/draft-generation-steps/draft-generation-steps.component.ts#L352

Added line #L352 was not covered by tests
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1966,10 +1966,8 @@ describe('DraftGenerationComponent', () => {

env.component.currentPage = 'steps';
env.component.startBuild({
trainingBooks: [],
trainingDataFiles: [],
trainingScriptureRanges: [],
translationBooks: [],
translationScriptureRanges: [],
fastTraining: false,
projectId: projectId
Expand All @@ -1978,10 +1976,8 @@ describe('DraftGenerationComponent', () => {
expect(env.component.currentPage).toBe('steps');
expect(mockDraftGenerationService.startBuildOrGetActiveBuild).toHaveBeenCalledWith({
projectId: projectId,
trainingBooks: [],
trainingDataFiles: [],
trainingScriptureRanges: [],
translationBooks: [],
translationScriptureRanges: [],
fastTraining: false
});
Expand All @@ -1997,21 +1993,17 @@ describe('DraftGenerationComponent', () => {
env.component.cancelDialogRef = instance(mockDialogRef);

env.component.startBuild({
trainingBooks: [],
trainingDataFiles: [],
trainingScriptureRanges: [],
translationBooks: [],
translationScriptureRanges: [],
fastTraining: false,
projectId: projectId
});
env.startedOrActiveBuild$.next({ ...buildDto, state: BuildStates.Queued });
expect(mockDraftGenerationService.startBuildOrGetActiveBuild).toHaveBeenCalledWith({
projectId: projectId,
trainingBooks: [],
trainingDataFiles: [],
trainingScriptureRanges: [],
translationBooks: [],
translationScriptureRanges: [],
fastTraining: false
});
Expand All @@ -2026,21 +2018,17 @@ describe('DraftGenerationComponent', () => {
env.component.cancelDialogRef = instance(mockDialogRef);

env.component.startBuild({
trainingBooks: [],
trainingDataFiles: [],
trainingScriptureRanges: [],
translationBooks: [],
translationScriptureRanges: [],
fastTraining: false,
projectId: projectId
});
env.startedOrActiveBuild$.next({ ...buildDto, state: BuildStates.Pending });
expect(mockDraftGenerationService.startBuildOrGetActiveBuild).toHaveBeenCalledWith({
projectId: projectId,
trainingBooks: [],
trainingDataFiles: [],
trainingScriptureRanges: [],
translationBooks: [],
translationScriptureRanges: [],
fastTraining: false
});
Expand All @@ -2055,21 +2043,17 @@ describe('DraftGenerationComponent', () => {
env.component.cancelDialogRef = instance(mockDialogRef);

env.component.startBuild({
trainingBooks: [],
trainingDataFiles: [],
trainingScriptureRanges: [],
translationBooks: [],
translationScriptureRanges: [],
fastTraining: false,
projectId: projectId
});
env.startedOrActiveBuild$.next({ ...buildDto, state: BuildStates.Active });
expect(mockDraftGenerationService.startBuildOrGetActiveBuild).toHaveBeenCalledWith({
projectId: projectId,
trainingBooks: [],
trainingDataFiles: [],
trainingScriptureRanges: [],
translationBooks: [],
translationScriptureRanges: [],
fastTraining: false
});
Expand All @@ -2085,21 +2069,17 @@ describe('DraftGenerationComponent', () => {
env.component.cancelDialogRef = instance(mockDialogRef);

env.component.startBuild({
trainingBooks: [],
trainingDataFiles: [],
trainingScriptureRanges: [],
translationBooks: [],
translationScriptureRanges: [],
fastTraining: false,
projectId: projectId
});
env.startedOrActiveBuild$.next({ ...buildDto, state: BuildStates.Canceled });
expect(mockDraftGenerationService.startBuildOrGetActiveBuild).toHaveBeenCalledWith({
projectId: projectId,
trainingBooks: [],
trainingDataFiles: [],
trainingScriptureRanges: [],
translationBooks: [],
translationScriptureRanges: [],
fastTraining: false
});
Expand All @@ -2114,10 +2094,8 @@ describe('DraftGenerationComponent', () => {
});

env.component.startBuild({
trainingBooks: [],
trainingDataFiles: [],
trainingScriptureRanges: [],
translationBooks: [],
translationScriptureRanges: [],
fastTraining: false,
projectId: projectId
Expand All @@ -2126,10 +2104,8 @@ describe('DraftGenerationComponent', () => {

expect(mockDraftGenerationService.startBuildOrGetActiveBuild).toHaveBeenCalledWith({
projectId: projectId,
trainingBooks: [],
trainingDataFiles: [],
trainingScriptureRanges: [],
translationBooks: [],
translationScriptureRanges: [],
fastTraining: false
});
Expand Down
Loading

0 comments on commit d58cf38

Please sign in to comment.