Skip to content

Commit

Permalink
try to override provider mid test
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel7373 committed Oct 22, 2024
1 parent f8e1e44 commit 9cae752
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ let objectiveService = {
deleteObjective: jest.fn(),
};

interface MatDialogDataInterface {
objective: { objectiveId: number | undefined; teamId: number | undefined };
}

const quarterService = {
getAllQuarters(): Observable<Quarter[]> {
return of([
Expand All @@ -43,6 +47,9 @@ const quarterService = {
new Quarter(999, 'Backlog', null, null),
]);
},
getCurrentQuarter(): Observable<Quarter> {
return of(new Quarter(2, quarter.label, quarter.startDate, quarter.endDate));
},
};

const teamService = {
Expand All @@ -58,7 +65,7 @@ const dialogMock = {
close: jest.fn(),
};

let matDataMock: { objective: { objectiveId: number | undefined; teamId: number | undefined } } = {
let matDataMock: MatDialogDataInterface = {
objective: {
objectiveId: undefined,
teamId: 1,
Expand Down Expand Up @@ -118,6 +125,16 @@ describe('ObjectiveDialogComponent', () => {
it.each([['DRAFT'], ['ONGOING']])(
'onSubmit create',
fakeAsync((state: string) => {
matDataMock = {
objectiveId: 99,
teamId: 1,
} as any as MatDialogDataInterface;

TestBed.overrideProvider(MAT_DIALOG_DATA, { useValue: matDataMock });
TestBed.compileComponents();
component = fixture.componentInstance;
fixture.detectChanges();

//Prepare data
let title: string = 'title';
let description: string = 'description';
Expand All @@ -142,7 +159,6 @@ describe('ObjectiveDialogComponent', () => {
tick(200);
const quarterSelect: HTMLSelectElement = fixture.debugElement.query(By.css('#quarter')).nativeElement;
quarterSelect.value = quarter.toString();

// Trigger update of form
fixture.detectChanges();
titleInput.dispatchEvent(new Event('input'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class ObjectiveFormComponent implements OnInit, OnDestroy {
createKeyResults: new FormControl<boolean>(false),
});
quarters$: Observable<Quarter[]> = of([]);
currentQuarter$: Observable<Quarter> = of();
quarters: Quarter[] = [];
teams$: Observable<Team[]> = of([]);
currentTeam: Subject<Team> = new Subject<Team>();
Expand Down Expand Up @@ -82,16 +83,16 @@ export class ObjectiveFormComponent implements OnInit, OnDestroy {
const isCreating: boolean = !!this.data.objective.objectiveId;
this.teams$ = this.teamService.getAllTeams().pipe(takeUntil(this.unsubscribe$));
this.quarters$ = this.quarterService.getAllQuarters();
this.currentQuarter$ = this.quarterService.getCurrentQuarter();
const objective$ = isCreating
? this.objectiveService.getFullObjective(this.data.objective.objectiveId!)
: of(this.getDefaultObjective());

forkJoin([objective$, this.quarters$]).subscribe(([objective, quarters]) => {
forkJoin([objective$, this.quarters$, this.currentQuarter$]).subscribe(([objective, quarters, currentQuarter]) => {
this.quarters = quarters;
const teamId = isCreating ? objective.teamId : this.data.objective.teamId;
let quarterId = getValueFromQuery(this.route.snapshot.queryParams['quarter'], quarters[1].id)[0];
const newEditQuarter = isCreating ? currentQuarter.id : objective.quarterId;
let quarterId = getValueFromQuery(this.route.snapshot.queryParams['quarter'], newEditQuarter)[0];

let currentQuarter: Quarter | undefined = this.quarters.find((quarter) => quarter.id == quarterId);
if (currentQuarter && !this.isBacklogQuarter(currentQuarter.label) && this.data.action == 'releaseBacklog') {
quarterId = quarters[1].id;
}
Expand Down

0 comments on commit 9cae752

Please sign in to comment.