|
| 1 | +/* eslint-disable max-lines */ |
| 2 | +/* eslint-disable dot-notation */ |
1 | 3 | import { ComponentFixture, TestBed } from '@angular/core/testing';
|
2 |
| - |
| 4 | +import { ActivatedRoute } from '@angular/router'; |
| 5 | +import { of, throwError } from 'rxjs'; |
3 | 6 | import { QboBaseMappingComponent } from './qbo-base-mapping.component';
|
| 7 | +import { MappingService } from 'src/app/core/services/common/mapping.service'; |
| 8 | +import { IntegrationsToastService } from 'src/app/core/services/common/integrations-toast.service'; |
| 9 | +import { WorkspaceService } from 'src/app/core/services/common/workspace.service'; |
| 10 | +import { QboImportSettingsService } from 'src/app/core/services/qbo/qbo-configuration/qbo-import-settings.service'; |
| 11 | +import { AccountingDisplayName, AccountingField, FyleField, ToastSeverity } from 'src/app/core/models/enum/enum.model'; |
| 12 | +import { mockCreditCardAccounts, mockGeneralSettings, mockImportSettings, mockMappingSetting } from '../../../qbo.fixture'; |
| 13 | +import { QBOWorkspaceGeneralSetting } from 'src/app/core/models/qbo/db/workspace-general-setting.model'; |
| 14 | +import { MappingSetting } from 'src/app/core/models/db/mapping-setting.model'; |
| 15 | +import { QBOImportSettingGet } from 'src/app/core/models/qbo/qbo-configuration/qbo-import-setting.model'; |
4 | 16 |
|
5 |
| -xdescribe('QboBaseMappingComponent', () => { |
| 17 | +describe('QboBaseMappingComponent', () => { |
6 | 18 | let component: QboBaseMappingComponent;
|
7 | 19 | let fixture: ComponentFixture<QboBaseMappingComponent>;
|
| 20 | + let mockActivatedRoute: any; |
| 21 | + let mockMappingService: jasmine.SpyObj<MappingService>; |
| 22 | + let mockToastService: jasmine.SpyObj<IntegrationsToastService>; |
| 23 | + let mockWorkspaceService: jasmine.SpyObj<WorkspaceService>; |
| 24 | + let mockImportSettingsService: jasmine.SpyObj<QboImportSettingsService>; |
8 | 25 |
|
9 | 26 | beforeEach(async () => {
|
| 27 | + mockActivatedRoute = { |
| 28 | + params: of({ source_field: 'EMPLOYEE' }), |
| 29 | + snapshot: { |
| 30 | + params: { source_field: 'EMPLOYEE' } |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + mockMappingService = jasmine.createSpyObj('MappingService', ['triggerAutoMapEmployees', 'getMappingSettings', 'getPaginatedDestinationAttributes']); |
| 35 | + mockToastService = jasmine.createSpyObj('IntegrationsToastService', ['displayToastMessage']); |
| 36 | + mockWorkspaceService = jasmine.createSpyObj('WorkspaceService', ['getWorkspaceGeneralSettings']); |
| 37 | + mockImportSettingsService = jasmine.createSpyObj('QboImportSettingsService', ['getImportSettings']); |
| 38 | + |
10 | 39 | await TestBed.configureTestingModule({
|
11 |
| - declarations: [ QboBaseMappingComponent ] |
12 |
| - }) |
13 |
| - .compileComponents(); |
| 40 | + declarations: [ QboBaseMappingComponent ], |
| 41 | + providers: [ |
| 42 | + { provide: ActivatedRoute, useValue: mockActivatedRoute }, |
| 43 | + { provide: MappingService, useValue: mockMappingService }, |
| 44 | + { provide: IntegrationsToastService, useValue: mockToastService }, |
| 45 | + { provide: WorkspaceService, useValue: mockWorkspaceService }, |
| 46 | + { provide: QboImportSettingsService, useValue: mockImportSettingsService } |
| 47 | + ] |
| 48 | + }).compileComponents(); |
14 | 49 |
|
15 | 50 | fixture = TestBed.createComponent(QboBaseMappingComponent);
|
16 | 51 | component = fixture.componentInstance;
|
17 |
| - fixture.detectChanges(); |
18 | 52 | });
|
19 | 53 |
|
20 | 54 | it('should create', () => {
|
21 | 55 | expect(component).toBeTruthy();
|
22 | 56 | });
|
| 57 | + |
| 58 | + it('should trigger auto map employees successfully', () => { |
| 59 | + mockMappingService.triggerAutoMapEmployees.and.returnValue(of(null)); |
| 60 | + |
| 61 | + component.triggerAutoMapEmployees(); |
| 62 | + |
| 63 | + expect(component.isLoading).toBeFalse(); |
| 64 | + expect(mockToastService.displayToastMessage).toHaveBeenCalledWith(ToastSeverity.INFO, 'Auto mapping of employees may take few minutes'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should handle error when triggering auto map employees', () => { |
| 68 | + mockMappingService.triggerAutoMapEmployees.and.returnValue(throwError('Error')); |
| 69 | + |
| 70 | + component.triggerAutoMapEmployees(); |
| 71 | + |
| 72 | + expect(component.isLoading).toBeFalse(); |
| 73 | + expect(mockToastService.displayToastMessage).toHaveBeenCalledWith(ToastSeverity.ERROR, 'Something went wrong, please try again'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should handle route parameter changes', () => { |
| 77 | + mockWorkspaceService.getWorkspaceGeneralSettings.and.returnValue(of(mockGeneralSettings)); |
| 78 | + mockMappingService.getMappingSettings.and.returnValue(of({ count: 1, next: null, previous: null, results: mockImportSettings.mapping_settings })); |
| 79 | + mockImportSettingsService.getImportSettings.and.returnValue(of(mockImportSettings)); |
| 80 | + mockMappingService.getPaginatedDestinationAttributes.and.returnValue(of(mockCreditCardAccounts)); |
| 81 | + |
| 82 | + mockActivatedRoute.params = of({ source_field: 'Vendor' }); |
| 83 | + fixture.detectChanges(); |
| 84 | + |
| 85 | + expect(component.sourceField).toBe(FyleField.EMPLOYEE); |
| 86 | + expect(component.isLoading).toBeFalse(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should return employee_field_mapping when sourceField is EMPLOYEE', () => { |
| 90 | + component.sourceField = FyleField.EMPLOYEE; |
| 91 | + const workspaceGeneralSetting = { employee_field_mapping: 'VENDOR' } as QBOWorkspaceGeneralSetting; |
| 92 | + const mappingSettings: MappingSetting[] = []; |
| 93 | + |
| 94 | + const result = (component as any).getDestinationField(workspaceGeneralSetting, mappingSettings); |
| 95 | + expect(result).toBe('VENDOR'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should return ACCOUNT when sourceField is CATEGORY', () => { |
| 99 | + component.sourceField = FyleField.CATEGORY; |
| 100 | + const workspaceGeneralSetting = {} as QBOWorkspaceGeneralSetting; |
| 101 | + const mappingSettings: MappingSetting[] = []; |
| 102 | + |
| 103 | + const result = (component as any).getDestinationField(workspaceGeneralSetting, mappingSettings); |
| 104 | + expect(result).toBe(AccountingField.ACCOUNT); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should return destination_field from mappingSettings for other sourceFields', () => { |
| 108 | + component.sourceField = FyleField.CATEGORY; |
| 109 | + const workspaceGeneralSetting = {} as QBOWorkspaceGeneralSetting; |
| 110 | + const mappingSettings: MappingSetting[] = [ |
| 111 | + { |
| 112 | + source_field: FyleField.CATEGORY, |
| 113 | + destination_field: AccountingField.ACCOUNT, |
| 114 | + id: 0, |
| 115 | + created_at: new Date(), |
| 116 | + updated_at: new Date(), |
| 117 | + workspace: 0, |
| 118 | + import_to_fyle: false, |
| 119 | + is_custom: false, |
| 120 | + source_placeholder: null |
| 121 | + } |
| 122 | + ]; |
| 123 | + |
| 124 | + const result = (component as any).getDestinationField(workspaceGeneralSetting, mappingSettings); |
| 125 | + expect(result).toBe('ACCOUNT'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should return empty string if no matching mapping setting is found', () => { |
| 129 | + component.sourceField = FyleField.VENDOR; |
| 130 | + const workspaceGeneralSetting = {} as QBOWorkspaceGeneralSetting; |
| 131 | + const mappingSettings: MappingSetting[] = []; |
| 132 | + |
| 133 | + const result = (component as any).getDestinationField(workspaceGeneralSetting, mappingSettings); |
| 134 | + expect(result).toBe(''); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should return employee_field_mapping when sourceField is EMPLOYEE', () => { |
| 138 | + component.sourceField = FyleField.EMPLOYEE; |
| 139 | + const workspaceGeneralSetting = { employee_field_mapping: 'VENDOR' } as QBOWorkspaceGeneralSetting; |
| 140 | + const mappingSettings: MappingSetting[] = []; |
| 141 | + |
| 142 | + const result = (component as any).getDestinationField(workspaceGeneralSetting, mappingSettings); |
| 143 | + expect(result).toBe('VENDOR'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should return ACCOUNT when sourceField is CATEGORY', () => { |
| 147 | + component.sourceField = FyleField.CATEGORY; |
| 148 | + const workspaceGeneralSetting = {} as QBOWorkspaceGeneralSetting; |
| 149 | + const mappingSettings: MappingSetting[] = []; |
| 150 | + |
| 151 | + const result = (component as any).getDestinationField(workspaceGeneralSetting, mappingSettings); |
| 152 | + expect(result).toBe(AccountingField.ACCOUNT); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should return destination_field from mappingSettings for VENDOR sourceField', () => { |
| 156 | + component.sourceField = FyleField.VENDOR; |
| 157 | + const workspaceGeneralSetting = {} as QBOWorkspaceGeneralSetting; |
| 158 | + |
| 159 | + const result = (component as any).getDestinationField(workspaceGeneralSetting, mockMappingSetting); |
| 160 | + expect(result).toBe(AccountingField.ACCOUNT); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should return empty string if no matching mapping setting is found', () => { |
| 164 | + component.sourceField = FyleField.VENDOR; |
| 165 | + const workspaceGeneralSetting = {} as QBOWorkspaceGeneralSetting; |
| 166 | + const mappingSettings: MappingSetting[] = []; |
| 167 | + |
| 168 | + const result = (component as any).getDestinationField(workspaceGeneralSetting, mappingSettings); |
| 169 | + expect(result).toBe(''); |
| 170 | + }); |
23 | 171 | });
|
0 commit comments