|
1 |
| -import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 1 | +import { TestBed, ComponentFixture } from '@angular/core/testing'; |
2 | 2 | import { CreateVehicleFormComponent } from './create-vehicle-form.component';
|
| 3 | +import { CommonModule } from '@angular/common'; |
| 4 | +import { ReactiveFormsModule } from '@angular/forms'; |
| 5 | +import { MatFormFieldModule } from '@angular/material/form-field'; |
| 6 | +import { MatInputModule } from '@angular/material/input'; |
| 7 | +import { MatSelectModule } from '@angular/material/select'; |
| 8 | +import { MatButtonModule } from '@angular/material/button'; |
| 9 | +import { MatDialogModule, MatDialogRef } from '@angular/material/dialog'; |
| 10 | +import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; |
| 11 | +import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
| 12 | +import { VehicleBrandsService } from '../../../../src/services/vehicle-brands.service'; |
| 13 | +import { VehiclesService } from '../../../../src/services/vehicles.service'; |
| 14 | +import { NotificationService } from '../../../../src/services/notification.service'; |
| 15 | +import { of, throwError } from 'rxjs'; |
| 16 | +import { VehicleBrandDto } from '../../../dtos/vehicle-brand'; |
| 17 | +import { VehicleDto } from '../../../dtos/vehicle'; |
| 18 | +import { Page } from '../../../dtos/page'; |
| 19 | +import { MatSelect } from '@angular/material/select'; |
3 | 20 |
|
4 | 21 | describe('CreateVehicleFormComponent', () => {
|
5 | 22 | let component: CreateVehicleFormComponent;
|
6 | 23 | let fixture: ComponentFixture<CreateVehicleFormComponent>;
|
| 24 | + let vehicleBrandsService: jest.Mocked<VehicleBrandsService>; |
| 25 | + let vehiclesService: jest.Mocked<VehiclesService>; |
| 26 | + let notificationService: jest.Mocked<NotificationService>; |
| 27 | + let dialogRef: jest.Mocked<MatDialogRef<CreateVehicleFormComponent>>; |
7 | 28 |
|
8 | 29 | beforeEach(async () => {
|
| 30 | + const vehicleBrandsServiceMock = { |
| 31 | + getVehicleBrands: jest.fn().mockReturnValue( |
| 32 | + of({ |
| 33 | + data: [], |
| 34 | + meta: { itemCount: 0 }, |
| 35 | + } as unknown as Page<VehicleBrandDto>) |
| 36 | + ), |
| 37 | + }; |
| 38 | + const vehiclesServiceMock = { |
| 39 | + createVehicle: jest.fn().mockReturnValue(of({} as VehicleDto)), |
| 40 | + }; |
| 41 | + const notificationServiceMock = { |
| 42 | + showSuccess: jest.fn(), |
| 43 | + showError: jest.fn(), |
| 44 | + }; |
| 45 | + const dialogRefMock = { |
| 46 | + close: jest.fn(), |
| 47 | + }; |
| 48 | + |
9 | 49 | await TestBed.configureTestingModule({
|
10 |
| - imports: [CreateVehicleFormComponent], |
| 50 | + imports: [ |
| 51 | + CommonModule, |
| 52 | + ReactiveFormsModule, |
| 53 | + MatFormFieldModule, |
| 54 | + MatInputModule, |
| 55 | + MatSelectModule, |
| 56 | + MatButtonModule, |
| 57 | + MatDialogModule, |
| 58 | + MatProgressSpinnerModule, |
| 59 | + BrowserAnimationsModule, |
| 60 | + CreateVehicleFormComponent, // Import the standalone component |
| 61 | + ], |
| 62 | + providers: [ |
| 63 | + { provide: VehicleBrandsService, useValue: vehicleBrandsServiceMock }, |
| 64 | + { provide: VehiclesService, useValue: vehiclesServiceMock }, |
| 65 | + { provide: NotificationService, useValue: notificationServiceMock }, |
| 66 | + { provide: MatDialogRef, useValue: dialogRefMock }, |
| 67 | + ], |
11 | 68 | }).compileComponents();
|
12 | 69 |
|
13 | 70 | fixture = TestBed.createComponent(CreateVehicleFormComponent);
|
14 | 71 | component = fixture.componentInstance;
|
| 72 | + vehicleBrandsService = TestBed.inject( |
| 73 | + VehicleBrandsService |
| 74 | + ) as jest.Mocked<VehicleBrandsService>; |
| 75 | + vehiclesService = TestBed.inject( |
| 76 | + VehiclesService |
| 77 | + ) as jest.Mocked<VehiclesService>; |
| 78 | + notificationService = TestBed.inject( |
| 79 | + NotificationService |
| 80 | + ) as jest.Mocked<NotificationService>; |
| 81 | + dialogRef = TestBed.inject(MatDialogRef) as jest.Mocked< |
| 82 | + MatDialogRef<CreateVehicleFormComponent> |
| 83 | + >; |
15 | 84 | fixture.detectChanges();
|
| 85 | + |
| 86 | + // Initialize the brandDropdown element |
| 87 | + component.brandDropdown = { |
| 88 | + panel: { |
| 89 | + nativeElement: document.createElement('div'), |
| 90 | + }, |
| 91 | + } as MatSelect; |
16 | 92 | });
|
17 | 93 |
|
18 |
| - it('should create', () => { |
| 94 | + it('should create the component', () => { |
19 | 95 | expect(component).toBeTruthy();
|
20 | 96 | });
|
| 97 | + |
| 98 | + it('should render the correct content', () => { |
| 99 | + fixture.detectChanges(); |
| 100 | + const compiled = fixture.nativeElement as HTMLElement; |
| 101 | + expect(compiled.querySelector('h2')?.textContent).toContain('Dodaj pojazd'); |
| 102 | + expect( |
| 103 | + compiled.querySelector('button[type="submit"]')?.textContent |
| 104 | + ).toContain('DODAJ'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should call onSubmit when the form is submitted', () => { |
| 108 | + jest.spyOn(component, 'onSubmit'); |
| 109 | + vehiclesService.createVehicle.mockReturnValue(of({} as VehicleDto)); |
| 110 | + fixture.detectChanges(); |
| 111 | + const form = fixture.nativeElement.querySelector('form'); |
| 112 | + form.dispatchEvent(new Event('submit')); |
| 113 | + expect(component.onSubmit).toHaveBeenCalled(); |
| 114 | + expect(vehiclesService.createVehicle).toHaveBeenCalledWith( |
| 115 | + component.vehicleForm.value |
| 116 | + ); |
| 117 | + expect(notificationService.showSuccess).toHaveBeenCalledWith( |
| 118 | + 'Dodano pojazd.🥳' |
| 119 | + ); |
| 120 | + expect(dialogRef.close).toHaveBeenCalledWith('vehicleCreated'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should handle error when vehicle creation fails', () => { |
| 124 | + jest.spyOn(component, 'onSubmit'); |
| 125 | + vehiclesService.createVehicle.mockReturnValue( |
| 126 | + throwError(() => ({ error: { status: 409 } })) |
| 127 | + ); |
| 128 | + fixture.detectChanges(); |
| 129 | + const form = fixture.nativeElement.querySelector('form'); |
| 130 | + form.dispatchEvent(new Event('submit')); |
| 131 | + expect(component.onSubmit).toHaveBeenCalled(); |
| 132 | + expect(vehiclesService.createVehicle).toHaveBeenCalledWith( |
| 133 | + component.vehicleForm.value |
| 134 | + ); |
| 135 | + expect(notificationService.showError).toHaveBeenCalledWith( |
| 136 | + 'Pojazd o podanym numerze rejestracyjnym lub numerze VIN już istnieje.😥' |
| 137 | + ); |
| 138 | + expect(dialogRef.close).not.toHaveBeenCalledWith('vehicleCreated'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should load vehicle brands on init', () => { |
| 142 | + const vehicleBrands: VehicleBrandDto[] = [ |
| 143 | + { name: 'Toyota' } as VehicleBrandDto, |
| 144 | + { name: 'Honda' } as VehicleBrandDto, |
| 145 | + ]; |
| 146 | + vehicleBrandsService.getVehicleBrands.mockReturnValue( |
| 147 | + of({ |
| 148 | + data: vehicleBrands, |
| 149 | + meta: { itemCount: 2 }, |
| 150 | + } as Page<VehicleBrandDto>) |
| 151 | + ); |
| 152 | + component.ngOnInit(); |
| 153 | + expect(vehicleBrandsService.getVehicleBrands).toHaveBeenCalled(); |
| 154 | + expect(component.vehicleBrands).toEqual(vehicleBrands); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should call onDropdownOpened when the dropdown is opened', () => { |
| 158 | + jest.spyOn(component, 'onDropdownOpened'); |
| 159 | + fixture.detectChanges(); |
| 160 | + const dropdown = fixture.nativeElement.querySelector('mat-select'); |
| 161 | + dropdown.dispatchEvent(new Event('openedChange')); |
| 162 | + expect(component.onDropdownOpened).toHaveBeenCalled(); |
| 163 | + }); |
| 164 | + |
| 165 | + it('should call loadVehicleBrands on scroll', () => { |
| 166 | + jest.spyOn(component, 'loadVehicleBrands'); |
| 167 | + component.vehicleBrandsCurrentPage = 1; |
| 168 | + component.vehicleBrands = [{ name: 'Toyota' } as VehicleBrandDto]; |
| 169 | + fixture.detectChanges(); |
| 170 | + component.onScroll(); |
| 171 | + expect(component.loadVehicleBrands).toHaveBeenCalled(); |
| 172 | + }); |
21 | 173 | });
|
0 commit comments