Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stardustmeg committed Aug 31, 2024
1 parent fc897d9 commit fd745b3
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 9 deletions.
34 changes: 33 additions & 1 deletion src/app/auth/services/auth-service/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
import { provideHttpClient } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';

import { MessageService } from 'primeng/api';
import { of, Subject } from 'rxjs';

import { AuthService } from './auth.service';

describe('AuthService', () => {
let service: AuthService;
let routerEventsSubject: Subject<NavigationEnd>;

beforeEach(() => {
routerEventsSubject = new Subject();

TestBed.configureTestingModule({
providers: [provideHttpClient(), MessageService],
providers: [
provideHttpClient(),
MessageService,
{
provide: ActivatedRoute,
useValue: {
snapshot: { queryParams: {} },
queryParams: of({}),
},
},
{
provide: Router,
useValue: {
events: routerEventsSubject.asObservable(),
navigate: jest.fn(),
},
},
{
provide: Location,
useValue: {
back: jest.fn(),
},
},
],
});
service = TestBed.inject(AuthService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});

it('should handle router events', () => {
routerEventsSubject.next(new NavigationEnd(1, 'http://example.com', 'http://example.com'));
});
});
10 changes: 9 additions & 1 deletion src/app/auth/services/auth-service/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import { User } from '@/app/api/models/user';
import { SignInService } from '@/app/api/signInService/sign-in.service';
import STORE_KEYS from '@/app/core/constants/store';
import { LocalStorageService } from '@/app/core/services/local-storage/local-storage.service';
import { RoutingService } from '@/app/core/services/routing/routing.service';
import { PersonalInfoService } from '@/app/profile/services/personalInfo/personal-info.service';
import { APP_PATH, APP_ROUTE } from '@/app/shared/constants/routes';
import { ModalService } from '@/app/shared/services/modal/modal.service';
import { USER_MESSAGE } from '@/app/shared/services/userMessage/constants/user-messages';
import { UserMessageService } from '@/app/shared/services/userMessage/user-message.service';

Expand All @@ -27,8 +29,10 @@ export class AuthService implements OnDestroy {
private signInService = inject(SignInService);
private personalInfoService = inject(PersonalInfoService);
private router = inject(Router);
private routingService = inject(RoutingService);
private userMessageService = inject(UserMessageService);
private localStorageService = inject(LocalStorageService);
private modalService = inject(ModalService);

public isRegistrationSuccess$$ = signal(false);
public errorMessage$$ = signal<string>('');
Expand Down Expand Up @@ -60,7 +64,11 @@ export class AuthService implements OnDestroy {
this.setLoginSignals(userData);
this.personalInfoService.setUserInfo(email);
this.localStorageService.saveCurrentUser(email, data.token);
this.router.navigate([APP_PATH.DEFAULT]);
if (!this.routingService.isDetailedPage$$()) {
this.router.navigate([APP_PATH.DEFAULT]);
} else {
this.modalService.closeModal();
}
this.userMessageService.showSuccessMessage(USER_MESSAGE.LOGIN_SUCCESSFUL);
} catch (err: unknown) {
if (isOverriddenHttpErrorResponse(err)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { HttpClient } from '@angular/common/http';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActivatedRoute } from '@angular/router';

import { of } from 'rxjs';

import { UserMessageService } from '@/app/shared/services/userMessage/user-message.service';

import LocalStorageData from '../../models/store.model';
Expand All @@ -16,7 +18,12 @@ describe('NavigationComponent', () => {
await TestBed.configureTestingModule({
imports: [NavigationComponent],
providers: [
{ provide: ActivatedRoute, useValue: {} },
{
provide: ActivatedRoute,
useValue: {
queryParams: of({}),
},
},
{ provide: HttpClient, useValue: {} },
{
provide: UserMessageService,
Expand Down
2 changes: 2 additions & 0 deletions src/app/core/services/routing/routing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class RoutingService {

public queryParams$$ = signal<Record<string, string>>({});
public isAdminPage$$ = signal<boolean>(false);
public isDetailedPage$$ = signal<boolean>(false);
public isAdminCarriagesPage$$ = signal<boolean>(false);
public currentRideId$$ = signal<string>('');

Expand All @@ -27,6 +28,7 @@ export class RoutingService {
this.router.events.pipe(filter((event) => event instanceof NavigationEnd)).subscribe(() => {
const { url } = this.router;
this.isAdminPage$$.set(url.startsWith(APP_ROUTE.ADMIN));
this.isDetailedPage$$.set(url.startsWith(APP_ROUTE.TRIP));
this.isAdminCarriagesPage$$.set(url.startsWith(`${APP_ROUTE.ADMIN}/carriages`));

const rideId = this.extractRideIdFromUrl(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ <h2 class="ride-id">Ride {{ tripItem.rideId }}</h2>
}
</div>

<ng-template #modalContent>
<ng-template #tripModalContent>
<app-trip-details [trip]="tripItem"></app-trip-details>
</ng-template>

<ng-template #loginModalContent>
<app-login-form></app-login-form>
</ng-template>
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ describe('TripDetailedComponent', () => {

it('should open modal when tripItem is present', () => {
component.tripItem = mockCurrentRide;
component.modalContent = {} as TemplateRef<unknown>;
component.tripModalContent = {} as TemplateRef<unknown>;
component.openModal();

expect(modalService.openModal).toHaveBeenCalledWith(component.modalContent, 'Route 1');
expect(modalService.openModal).toHaveBeenCalledWith(component.tripModalContent, 'Route 1');
});

it('should not open modal when tripItem is null', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { RippleModule } from 'primeng/ripple';
import { TabViewChangeEvent, TabViewModule } from 'primeng/tabview';

import { SeatService } from '@/app/admin/services/seat/seat.service';
import { AuthService } from '@/app/auth/services/auth-service/auth.service';
import STORE_KEYS from '@/app/core/constants/store';
import { LocalStorageService } from '@/app/core/services/local-storage/local-storage.service';
import { RoutingService } from '@/app/core/services/routing/routing.service';
Expand All @@ -17,6 +18,7 @@ import { template } from '@/app/shared/constants/string-templates';
import { ModalService } from '@/app/shared/services/modal/modal.service';
import { stringTemplate } from '@/app/shared/utils/string-template';

import { LoginFormComponent } from '../../../auth/components/login-form/login-form.component';
import { TripDetailsComponent } from '../../../home/components/trip-details/trip-details.component';
import { TripTimelineComponent } from '../../../home/components/trip-timeline/trip-timeline.component';
import { TrainCarriagesListComponent } from '../../components/train-carriages-list/train-carriages-list.component';
Expand All @@ -36,6 +38,7 @@ import { isCurrentRide } from './helpers/helper';
TripTimelineComponent,
TripDetailsComponent,
TrainCarriagesListComponent,
LoginFormComponent,
],
templateUrl: './trip-detailed.component.html',
styleUrl: './trip-detailed.component.scss',
Expand All @@ -47,12 +50,14 @@ export class TripDetailedComponent implements OnInit, OnDestroy {
private modalService = inject(ModalService);
private localStorageService = inject(LocalStorageService);
private trainCarriagesListService = inject(TrainCarriagesListService);
private authService = inject(AuthService);

public seatService = inject(SeatService);

public tripItem!: CurrentRide | null;

@ViewChild('modalContent') public modalContent!: TemplateRef<unknown>;
@ViewChild('tripModalContent') public tripModalContent!: TemplateRef<unknown>;
@ViewChild('loginModalContent') public loginModalContent!: TemplateRef<unknown>;

public ngOnInit(): void {
this.tripItem = this.findRideById() ?? this.getCurrentRideFromLocalStorage();
Expand All @@ -70,7 +75,7 @@ export class TripDetailedComponent implements OnInit, OnDestroy {
public openModal(): void {
if (this.tripItem) {
this.modalService.openModal(
this.modalContent,
this.tripModalContent,
stringTemplate(template.ROUTE_TITLE, { id: this.tripItem.routeId }),
);
}
Expand Down Expand Up @@ -136,6 +141,10 @@ export class TripDetailedComponent implements OnInit, OnDestroy {
}

public bookSeat(): void {
this.seatService.bookSelectedSeat(this.tripItem!);
if (this.authService.isLoggedIn$$()) {
this.seatService.bookSelectedSeat(this.tripItem!);
} else {
this.modalService.openModal(this.loginModalContent);
}
}
}

0 comments on commit fd745b3

Please sign in to comment.