Skip to content

Commit

Permalink
fix #7876
Browse files Browse the repository at this point in the history
  • Loading branch information
kryzanivska-nastya committed Dec 22, 2024
1 parent 2a47053 commit d6f5a57
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { FRIENDS, FIRSTFRIEND, SECONDFRIEND } from '@global-user/mocks/friends-m
import { MatDialogModule } from '@angular/material/dialog';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { HabitService } from '../../../../../../../service/habit/habit.service';

describe('HabitInviteFriendsPopUpComponent', () => {
let component: HabitInviteFriendsPopUpComponent;
Expand All @@ -26,11 +27,16 @@ describe('HabitInviteFriendsPopUpComponent', () => {
userFriendsServiceMock.inviteFriendsToHabit = jasmine.createSpy('inviteFriendsToHabit').and.returnValue(of({}));
userFriendsServiceMock.addedFriends = [];

const mockHabitService = {
getFriendsWithInvitations: jasmine.createSpy('getFriendsWithInvitations').and.returnValue(of({ page: [] }))
};

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [HabitInviteFriendsPopUpComponent],
imports: [HttpClientTestingModule, TranslateModule.forRoot(), MatDialogModule, MatCheckboxModule],
providers: [
{ provide: HabitService, useValue: mockHabitService },
{ provide: LocalStorageService, useValue: localStorageServiceMock },
{ provide: Router, useValue: routerSpy },
{ provide: MatSnackBarComponent, useValue: MatSnackBarMock },
Expand Down Expand Up @@ -58,24 +64,6 @@ describe('HabitInviteFriendsPopUpComponent', () => {
expect(spy2).toHaveBeenCalled();
});

describe('setFriendDisable', () => {
it('should return true if the friend is in the addedFriends list and invitationSent is false', () => {
const friendId = 1;
userFriendsServiceMock.addedFriends = [FIRSTFRIEND, SECONDFRIEND];
component.invitationSent = false;
const result = component.setFriendDisable(friendId);
expect(result).toBe(false);
});

it('should return true if invitationSent is true, regardless of addedFriends', () => {
const friendId = 1;
userFriendsServiceMock.addedFriends = [FIRSTFRIEND, SECONDFRIEND];
component.invitationSent = true;
const result = component.setFriendDisable(friendId);
expect(result).toBe(true);
});
});

xit('should update allAdd status', () => {
component.friends = [FIRSTFRIEND, SECONDFRIEND];
component.updateAllAdd();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class HabitInviteFriendsPopUpComponent implements OnInit, OnDestroy {
allAdd = false;
searchIcon = searchIcon;
habitId: number;

constructor(
private readonly userFriendsService: UserFriendsService,
private readonly localStorageService: LocalStorageService,
Expand Down Expand Up @@ -60,7 +60,7 @@ export class HabitInviteFriendsPopUpComponent implements OnInit, OnDestroy {

onFriendCheckboxChange(friendId: number, isChecked: boolean) {
const friend = this.friends.find((f) => f.id === friendId);
if (friend && !friend.hasInvitation) {
if (friend && !friend.hasInvitation) {
friend.added = isChecked;
this.toggleFriendSelection(friendId, isChecked);
this.updateAllAdd();
Expand Down Expand Up @@ -93,9 +93,9 @@ export class HabitInviteFriendsPopUpComponent implements OnInit, OnDestroy {

setFriendDisable(friendId: number): boolean {
const friend = this.friends.find((f) => f.id === friendId);
return friend ? friend.hasInvitation : false;
return friend ? friend.hasInvitation : false;
}

setAllFriendsDisable(): boolean {
return this.friends.every((friend) => friend.hasInvitation);
}
Expand All @@ -112,6 +112,7 @@ export class HabitInviteFriendsPopUpComponent implements OnInit, OnDestroy {
this.allAdd = added;
this.friends.forEach((friend) => {
if (!this.isFriendAddedAlready(friend.id) && !friend.hasInvitation) {
friend.added = added;
this.toggleFriendSelection(friend.id, added);
}
});
Expand All @@ -135,5 +136,5 @@ export class HabitInviteFriendsPopUpComponent implements OnInit, OnDestroy {
ngOnDestroy() {
this.destroyed$.next(true);
this.destroyed$.complete();
}
}
}

0 comments on commit d6f5a57

Please sign in to comment.