|
| 1 | +import { TestBed, waitForAsync } from '@angular/core/testing'; |
| 2 | +import { InternalLinkService } from './internal-link.service'; |
| 3 | +import { NativeWindowService } from './window.service'; |
| 4 | + |
| 5 | +describe('InternalLinkService', () => { |
| 6 | + let service: InternalLinkService; |
| 7 | + |
| 8 | + beforeEach(waitForAsync(() => { |
| 9 | + return TestBed.configureTestingModule({ |
| 10 | + providers: [ |
| 11 | + InternalLinkService, |
| 12 | + { provide: NativeWindowService, useValue: { nativeWindow: { location: { origin: 'https://currentdomain' } } } }, |
| 13 | + ], |
| 14 | + }).compileComponents(); |
| 15 | + })); |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + service = TestBed.inject(InternalLinkService); |
| 19 | + }); |
| 20 | + |
| 21 | + describe('isLinkInternal', () => { |
| 22 | + it('should return true for internal link starting with "/"', () => { |
| 23 | + const result = service.isLinkInternal('/my-link'); |
| 24 | + expect(result).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should return true for internal link starting with currentURL', () => { |
| 28 | + const result = service.isLinkInternal('https://currentdomain/my-link'); |
| 29 | + expect(result).toBe(true); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return true for internal link starting with "currentdomain"', () => { |
| 33 | + const result = service.isLinkInternal('currentdomain/my-link'); |
| 34 | + expect(result).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return false for external link', () => { |
| 38 | + const result = service.isLinkInternal('https://externaldomain/my-link'); |
| 39 | + expect(result).toBe(false); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return true for internal link without leading "/"', () => { |
| 43 | + const result = service.isLinkInternal('my-link'); |
| 44 | + expect(result).toBe(true); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('transformInternalLink', () => { |
| 49 | + it('should transform internal link by removing currentURL', () => { |
| 50 | + const result = service.getRelativePath('https://currentdomain/my-link'); |
| 51 | + expect(result).toBe('/my-link'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should transform internal link by adding leading "/" if missing', () => { |
| 55 | + const result = service.getRelativePath('currentdomain/my-link'); |
| 56 | + expect(result).toBe('/my-link'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should return unchanged link for external link', () => { |
| 60 | + const result = service.getRelativePath('https://externalDomain/my-link'); |
| 61 | + expect(result).toBe('https://externalDomain/my-link'); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should return unchanged link for internal link with leading "/"', () => { |
| 65 | + const result = service.getRelativePath('/my-link'); |
| 66 | + expect(result).toBe('/my-link'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | +}); |
0 commit comments