Skip to content

Commit 1eb8895

Browse files
committed
fix: remove console.log & add tests
1 parent 314da2c commit 1eb8895

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

projects/lib/organization/components/organization-management/organization-management.component.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,43 @@ describe('OrganizationManagementComponent', () => {
181181

182182
expect(window.location.href).toBe('https://newOrg.test.com:8080');
183183
});
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+
});
184223
});

projects/lib/organization/components/organization-management/organization-management.component.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,6 @@ export class OrganizationManagementComponent implements OnInit {
195195

196196
const fullSubdomain = `${sanitizedOrg}.${baseDomain}`;
197197
const port = window.location.port ? `:${window.location.port}` : '';
198-
199-
console.log(`Switching to organization ${this.organizationToSwitch}`);
200-
console.log(`Redirecting to ${protocol}//${fullSubdomain}${port}`);
201198
window.location.href = `${protocol}//${fullSubdomain}${port}`;
202199
}
203200
}

projects/lib/portal-options/services/header-bar-renderers/namespace-selection-renderer.service.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,50 @@ describe('NamespaceSelectionRendererService', () => {
172172
value: { pathname: origPathname },
173173
});
174174
});
175+
176+
it('should skip items without name and avoid duplicates', async () => {
177+
const origPathname = window.location.pathname;
178+
Object.defineProperty(window, 'location', {
179+
value: { pathname: '/ns1/workloads' },
180+
writable: true,
181+
});
182+
183+
const portalConfig: any = {
184+
portalContext: { crdGatewayApiUrl: 'https://api.example.com/graphql' },
185+
};
186+
187+
mockAuthService.getToken.mockReturnValue('token');
188+
189+
mockResourceService.list.mockReturnValue(
190+
of([
191+
{ metadata: {} } as any,
192+
{ metadata: { name: 'ns1' } } as any,
193+
{ metadata: { name: 'ns1' } } as any,
194+
]),
195+
);
196+
197+
const renderer = service.create(portalConfig);
198+
const container = document.createElement('div');
199+
const nodeItems = [
200+
{
201+
label: 'Workloads',
202+
node: {
203+
navigationContext: 'workloads',
204+
context: { resourceDefinition: { scope: 'Namespaced' } },
205+
},
206+
},
207+
] as any;
208+
209+
renderer(container, nodeItems, () => {});
210+
211+
const cb = getChildrenByTag(container, 'ui5-combobox')[0];
212+
const items = getChildrenByTag(cb, 'ui5-cb-item');
213+
const texts = items.map((i) => i.getAttribute('text'));
214+
215+
expect(texts).toEqual(['-all-', 'ns1']);
216+
217+
Object.defineProperty(window, 'location', {
218+
value: { pathname: origPathname },
219+
});
220+
});
175221
});

0 commit comments

Comments
 (0)