|
| 1 | +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; |
| 2 | +import { UnitCreateComponent } from './unit-create.component'; |
| 3 | +import { UnitsService } from 'src/app/common/services'; |
| 4 | +import { DeviceUserService } from 'src/app/common/services/device-users'; |
| 5 | +import { MatDialogRef } from '@angular/material/dialog'; |
| 6 | +import { of } from 'rxjs'; |
| 7 | +import { UnitModel, SiteDto, DeviceUserRequestModel, OperationResult, OperationDataResult } from 'src/app/common/models'; |
| 8 | + |
| 9 | +describe('UnitCreateComponent', () => { |
| 10 | + let component: UnitCreateComponent; |
| 11 | + let fixture: ComponentFixture<UnitCreateComponent>; |
| 12 | + let mockUnitsService: jasmine.SpyObj<UnitsService>; |
| 13 | + let mockDeviceUserService: jasmine.SpyObj<DeviceUserService>; |
| 14 | + let mockDialogRef: jasmine.SpyObj<MatDialogRef<UnitCreateComponent>>; |
| 15 | + |
| 16 | + beforeEach(waitForAsync(() => { |
| 17 | + mockUnitsService = jasmine.createSpyObj('UnitsService', ['createUnit']); |
| 18 | + mockDeviceUserService = jasmine.createSpyObj('DeviceUserService', ['getDeviceUsersFiltered']); |
| 19 | + mockDialogRef = jasmine.createSpyObj('MatDialogRef', ['close']); |
| 20 | + |
| 21 | + TestBed.configureTestingModule({ |
| 22 | + declarations: [UnitCreateComponent], |
| 23 | + providers: [ |
| 24 | + { provide: UnitsService, useValue: mockUnitsService }, |
| 25 | + { provide: DeviceUserService, useValue: mockDeviceUserService }, |
| 26 | + { provide: MatDialogRef, useValue: mockDialogRef } |
| 27 | + ] |
| 28 | + }).compileComponents(); |
| 29 | + })); |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + fixture = TestBed.createComponent(UnitCreateComponent); |
| 33 | + component = fixture.componentInstance; |
| 34 | + }); |
| 35 | + |
| 36 | + it('should create', () => { |
| 37 | + expect(component).toBeTruthy(); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('ngOnInit', () => { |
| 41 | + it('should call loadAllSimpleSites on initialization', () => { |
| 42 | + const mockSites: SiteDto[] = [ |
| 43 | + { siteId: 1, siteName: 'Site 1' } as SiteDto, |
| 44 | + { siteId: 2, siteName: 'Site 2' } as SiteDto |
| 45 | + ]; |
| 46 | + const mockResult: OperationDataResult<Array<SiteDto>> = { |
| 47 | + success: true, message: '', |
| 48 | + model: mockSites |
| 49 | + }; |
| 50 | + mockDeviceUserService.getDeviceUsersFiltered.and.returnValue(of(mockResult)); |
| 51 | + |
| 52 | + component.ngOnInit(); |
| 53 | + |
| 54 | + expect(mockDeviceUserService.getDeviceUsersFiltered).toHaveBeenCalled(); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('loadAllSimpleSites', () => { |
| 59 | + it('should load and transform sites correctly', () => { |
| 60 | + const mockSites: SiteDto[] = [ |
| 61 | + { siteId: 1, siteName: 'Site 1', fullName: '' } as SiteDto, |
| 62 | + { siteId: 2, siteName: 'Site 2', fullName: '' } as SiteDto |
| 63 | + ]; |
| 64 | + const mockResult: OperationDataResult<Array<SiteDto>> = { |
| 65 | + success: true, message: '', |
| 66 | + model: mockSites |
| 67 | + }; |
| 68 | + mockDeviceUserService.getDeviceUsersFiltered.and.returnValue(of(mockResult)); |
| 69 | + |
| 70 | + component.loadAllSimpleSites(); |
| 71 | + |
| 72 | + expect(mockDeviceUserService.getDeviceUsersFiltered).toHaveBeenCalledWith(jasmine.any(DeviceUserRequestModel)); |
| 73 | + expect(component.simpleSites.length).toBe(2); |
| 74 | + expect(component.simpleSites[0].fullName).toBe('Site 1'); |
| 75 | + expect(component.simpleSites[1].fullName).toBe('Site 2'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should handle empty sites response', () => { |
| 79 | + const mockResult: OperationDataResult<Array<SiteDto>> = { |
| 80 | + success: true, message: '', |
| 81 | + model: [] |
| 82 | + }; |
| 83 | + mockDeviceUserService.getDeviceUsersFiltered.and.returnValue(of(mockResult)); |
| 84 | + |
| 85 | + component.loadAllSimpleSites(); |
| 86 | + |
| 87 | + expect(component.simpleSites).toEqual([]); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('hide', () => { |
| 92 | + it('should close dialog with true when result is true', () => { |
| 93 | + component.hide(true); |
| 94 | + |
| 95 | + expect(mockDialogRef.close).toHaveBeenCalledWith(true); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should close dialog with false when result is false', () => { |
| 99 | + component.hide(false); |
| 100 | + |
| 101 | + expect(mockDialogRef.close).toHaveBeenCalledWith(false); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should close dialog with false by default', () => { |
| 105 | + component.hide(); |
| 106 | + |
| 107 | + expect(mockDialogRef.close).toHaveBeenCalledWith(false); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('createUnit', () => { |
| 112 | + it('should create unit successfully and close dialog', () => { |
| 113 | + const mockResult: OperationResult = { |
| 114 | + success: true, |
| 115 | + message: '' |
| 116 | + }; |
| 117 | + mockUnitsService.createUnit.and.returnValue(of(mockResult)); |
| 118 | + component.unitModel = new UnitModel(); |
| 119 | + component.unitModel.siteId = 1; |
| 120 | + |
| 121 | + component.createUnit(); |
| 122 | + |
| 123 | + expect(mockUnitsService.createUnit).toHaveBeenCalledWith(component.unitModel); |
| 124 | + expect(mockDialogRef.close).toHaveBeenCalledWith(true); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should not close dialog when creation fails', () => { |
| 128 | + const mockResult: OperationResult = { |
| 129 | + success: false, |
| 130 | + message: '' |
| 131 | + }; |
| 132 | + mockUnitsService.createUnit.and.returnValue(of(mockResult)); |
| 133 | + component.unitModel = new UnitModel(); |
| 134 | + |
| 135 | + component.createUnit(); |
| 136 | + |
| 137 | + expect(mockUnitsService.createUnit).toHaveBeenCalled(); |
| 138 | + expect(mockDialogRef.close).not.toHaveBeenCalled(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should handle null response', () => { |
| 142 | + mockUnitsService.createUnit.and.returnValue(of(null)); |
| 143 | + component.unitModel = new UnitModel(); |
| 144 | + |
| 145 | + component.createUnit(); |
| 146 | + |
| 147 | + expect(mockUnitsService.createUnit).toHaveBeenCalled(); |
| 148 | + expect(mockDialogRef.close).not.toHaveBeenCalled(); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments