|
| 1 | +import { |
| 2 | + CUSTOM_ELEMENTS_SCHEMA, |
| 3 | + NO_ERRORS_SCHEMA, |
| 4 | + signal |
| 5 | +} from '@angular/core'; |
| 6 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 7 | +import { FormsModule } from '@angular/forms'; |
| 8 | +import { MutationResult } from '@apollo/client'; |
| 9 | +import { LuigiContextService } from '@luigi-project/client-support-angular'; |
| 10 | +import { |
| 11 | + ClientEnvironment, EnvConfigService, |
| 12 | + I18nService, |
| 13 | + LuigiCoreService, LuigiGlobalContext, NodeContext, ResourceService |
| 14 | +} from '@openmfp/portal-ui-lib'; |
| 15 | +import { of, throwError } from 'rxjs'; |
| 16 | +import { OrganizationManagementComponent } from './organization-management.component'; |
| 17 | + |
| 18 | +describe('OrganizationManagementComponent', () => { |
| 19 | + let component: OrganizationManagementComponent; |
| 20 | + let fixture: ComponentFixture<OrganizationManagementComponent>; |
| 21 | + let resourceServiceMock: jest.Mocked<ResourceService>; |
| 22 | + let i18nServiceMock: jest.Mocked<I18nService>; |
| 23 | + let luigiCoreServiceMock: jest.Mocked<LuigiCoreService>; |
| 24 | + let envConfigServiceMock: jest.Mocked<EnvConfigService>; |
| 25 | + |
| 26 | + beforeEach(async () => { |
| 27 | + resourceServiceMock = { |
| 28 | + readOrganizations: jest.fn(), |
| 29 | + create: jest.fn(), |
| 30 | + } as any; |
| 31 | + |
| 32 | + i18nServiceMock = { |
| 33 | + translationTable: {}, |
| 34 | + getTranslation: jest.fn(), |
| 35 | + } as any; |
| 36 | + |
| 37 | + luigiCoreServiceMock = { |
| 38 | + getGlobalContext: jest.fn(), |
| 39 | + showAlert: jest.fn(), |
| 40 | + } as any; |
| 41 | + |
| 42 | + envConfigServiceMock = { |
| 43 | + getEnvConfig: jest.fn(), |
| 44 | + } as any; |
| 45 | + |
| 46 | + await TestBed.configureTestingModule({ |
| 47 | + imports: [OrganizationManagementComponent, FormsModule], |
| 48 | + providers: [ |
| 49 | + { provide: ResourceService, useValue: resourceServiceMock }, |
| 50 | + { provide: I18nService, useValue: i18nServiceMock }, |
| 51 | + { provide: LuigiCoreService, useValue: luigiCoreServiceMock }, |
| 52 | + { provide: EnvConfigService, useValue: envConfigServiceMock }, |
| 53 | + LuigiContextService, |
| 54 | + ], |
| 55 | + schemas: [CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA], |
| 56 | + }) |
| 57 | + .overrideComponent(OrganizationManagementComponent, { |
| 58 | + set: { template: '', imports: [] }, |
| 59 | + }) |
| 60 | + .compileComponents(); |
| 61 | + |
| 62 | + fixture = TestBed.createComponent(OrganizationManagementComponent); |
| 63 | + component = fixture.componentInstance; |
| 64 | + }); |
| 65 | + |
| 66 | + it('should create', () => { |
| 67 | + expect(component).toBeTruthy(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should react to context input change', () => { |
| 71 | + const mockContext = { |
| 72 | + translationTable: { hello: 'world' }, |
| 73 | + } as any as NodeContext; |
| 74 | + |
| 75 | + resourceServiceMock.readOrganizations.mockReturnValue(of({} as any)); |
| 76 | + |
| 77 | + const contextSignal = signal<NodeContext | null>(mockContext); |
| 78 | + component.context = contextSignal as any; |
| 79 | + |
| 80 | + fixture.detectChanges(); |
| 81 | + |
| 82 | + expect(component['i18nService'].translationTable).toEqual( |
| 83 | + mockContext.translationTable, |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should initialize with empty organizations', () => { |
| 88 | + expect(component.organizations()).toEqual([]); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should read organizations on init', () => { |
| 92 | + const mockOrganizations = { |
| 93 | + Accounts: [ |
| 94 | + { metadata: { name: 'org1' } }, |
| 95 | + { metadata: { name: 'org2' } }, |
| 96 | + ], |
| 97 | + }; |
| 98 | + const mockGlobalContext: LuigiGlobalContext = { |
| 99 | + portalContext: {}, |
| 100 | + userId: 'user1', |
| 101 | + |
| 102 | + token: 'token', |
| 103 | + organization: 'org1', |
| 104 | + portalBaseUrl: 'https://test.com', |
| 105 | + }; |
| 106 | + luigiCoreServiceMock.getGlobalContext.mockReturnValue(mockGlobalContext); |
| 107 | + resourceServiceMock.readOrganizations.mockReturnValue( |
| 108 | + of(mockOrganizations as any), |
| 109 | + ); |
| 110 | + |
| 111 | + component.ngOnInit(); |
| 112 | + |
| 113 | + expect(resourceServiceMock.readOrganizations).toHaveBeenCalled(); |
| 114 | + expect(component.organizations()).toEqual(['org2']); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should set organization to switch', () => { |
| 118 | + const event = { target: { value: 'testOrg' } }; |
| 119 | + component.setOrganizationToSwitch(event); |
| 120 | + expect(component.organizationToSwitch).toBe('testOrg'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should onboard new organization successfully', () => { |
| 124 | + const mockResponse: MutationResult<void> = { |
| 125 | + data: undefined, |
| 126 | + loading: false, |
| 127 | + error: undefined, |
| 128 | + called: true, |
| 129 | + client: {} as any, |
| 130 | + reset: jest.fn(), |
| 131 | + }; |
| 132 | + resourceServiceMock.create.mockReturnValue(of(mockResponse)); |
| 133 | + component.newOrganization = 'newOrg'; |
| 134 | + component.organizations.set(['existingOrg']); |
| 135 | + |
| 136 | + component.onboardOrganization(); |
| 137 | + |
| 138 | + expect(resourceServiceMock.create).toHaveBeenCalled(); |
| 139 | + expect(component.organizations()).toEqual(['newOrg', 'existingOrg']); |
| 140 | + expect(component.organizationToSwitch).toBe('newOrg'); |
| 141 | + expect(component.newOrganization).toBe(''); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should handle organization creation error', () => { |
| 145 | + resourceServiceMock.create.mockReturnValue( |
| 146 | + throwError(() => new Error('Creation failed')), |
| 147 | + ); |
| 148 | + component.newOrganization = 'newOrg'; |
| 149 | + |
| 150 | + component.onboardOrganization(); |
| 151 | + |
| 152 | + expect(luigiCoreServiceMock.showAlert).toHaveBeenCalledWith({ |
| 153 | + text: 'Failure! Could not create organization: newOrg.', |
| 154 | + type: 'error', |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should switch organization', async () => { |
| 159 | + const mockEnvConfig: ClientEnvironment = { |
| 160 | + idpName: 'test', |
| 161 | + organization: 'test', |
| 162 | + oauthServerUrl: 'https://test.com', |
| 163 | + clientId: 'test', |
| 164 | + baseDomain: 'test.com', |
| 165 | + isLocal: false, |
| 166 | + developmentInstance: false, |
| 167 | + authData: { |
| 168 | + expires_in: '3600', |
| 169 | + access_token: 'test-access-token', |
| 170 | + id_token: 'test-id-token', |
| 171 | + }, |
| 172 | + }; |
| 173 | + envConfigServiceMock.getEnvConfig.mockResolvedValue(mockEnvConfig); |
| 174 | + component.organizationToSwitch = 'newOrg'; |
| 175 | + Object.defineProperty(window, 'location', { |
| 176 | + value: { protocol: 'https:', port: '8080' }, |
| 177 | + writable: true, |
| 178 | + }); |
| 179 | + |
| 180 | + await component.switchOrganization(); |
| 181 | + |
| 182 | + expect(window.location.href).toBe('https://newOrg.test.com:8080'); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should not switch and show alert for invalid organization name', async () => { |
| 186 | + const mockEnvConfig: ClientEnvironment = { |
| 187 | + idpName: 'test', |
| 188 | + organization: 'test', |
| 189 | + oauthServerUrl: 'https://test.com', |
| 190 | + clientId: 'test', |
| 191 | + baseDomain: 'test.com', |
| 192 | + isLocal: false, |
| 193 | + developmentInstance: false, |
| 194 | + authData: { |
| 195 | + expires_in: '3600', |
| 196 | + access_token: 'test-access-token', |
| 197 | + id_token: 'test-id-token', |
| 198 | + }, |
| 199 | + }; |
| 200 | + envConfigServiceMock.getEnvConfig.mockResolvedValue(mockEnvConfig); |
| 201 | + |
| 202 | + const invalidNames = ['-abc', 'abc-', 'a.b', 'a b', '']; |
| 203 | + |
| 204 | + for (const name of invalidNames) { |
| 205 | + component.organizationToSwitch = name as any; |
| 206 | + Object.defineProperty(window, 'location', { |
| 207 | + value: { protocol: 'https:', port: '' }, |
| 208 | + writable: true, |
| 209 | + }); |
| 210 | + |
| 211 | + await component.switchOrganization(); |
| 212 | + |
| 213 | + expect(luigiCoreServiceMock.showAlert).toHaveBeenCalledWith({ |
| 214 | + text: |
| 215 | + 'Organization name is not valid for subdomain usage, accrording to RFC 1034/1123.', |
| 216 | + type: 'error', |
| 217 | + }); |
| 218 | + |
| 219 | + expect((window.location as any).href).toBeUndefined(); |
| 220 | + (luigiCoreServiceMock.showAlert as jest.Mock).mockClear(); |
| 221 | + } |
| 222 | + }); |
| 223 | +}); |
0 commit comments