|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | +import { APP_BASE_HREF, HashLocationStrategy, LocationStrategy, PathLocationStrategy } from '@angular/common'; |
| 4 | +import { UIRouter } from '@uirouter/core'; |
| 5 | +import { Ng2LocationServices } from '../../src/location/locationService'; |
| 6 | + |
| 7 | +@Component({ selector: 'test', template: '' }) |
| 8 | +class TestComponent { |
| 9 | + constructor(public locationStrategy: LocationStrategy) {} |
| 10 | +} |
| 11 | + |
| 12 | +type Strategy = typeof HashLocationStrategy | typeof PathLocationStrategy; |
| 13 | +describe('locationService', () => { |
| 14 | + const setup = (LocationStrategyClass: Strategy) => { |
| 15 | + const router = new UIRouter(); |
| 16 | + const fixture = TestBed.configureTestingModule({ |
| 17 | + declarations: [TestComponent], |
| 18 | + imports: [], |
| 19 | + providers: [ |
| 20 | + { provide: LocationStrategy, useClass: LocationStrategyClass }, |
| 21 | + { provide: APP_BASE_HREF, useValue: '/' }, |
| 22 | + ], |
| 23 | + }).createComponent(TestComponent); |
| 24 | + fixture.detectChanges(); |
| 25 | + const locationStrategy = fixture.componentInstance.locationStrategy; |
| 26 | + |
| 27 | + return { router, fixture, locationStrategy }; |
| 28 | + }; |
| 29 | + |
| 30 | + const expectUrlReadAfterWrite = (strategy: Strategy, url: string) => { |
| 31 | + const { router, locationStrategy } = setup(HashLocationStrategy); |
| 32 | + const locationServices = new Ng2LocationServices(router, locationStrategy, false); |
| 33 | + |
| 34 | + // Set the URL |
| 35 | + locationServices._set(null, null, url, false); |
| 36 | + // Read it back |
| 37 | + expect(locationServices._get()).toBe(url); |
| 38 | + // Read it directly from Angular LocationStrategy |
| 39 | + expect(locationStrategy.path()).toBe(url); |
| 40 | + }; |
| 41 | + |
| 42 | + describe('+ HashLocationStrategy', () => { |
| 43 | + it('should read/write the url path', () => { |
| 44 | + expectUrlReadAfterWrite(HashLocationStrategy, '/foo'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should read/write query params', () => { |
| 48 | + expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should read/write multiple query params', () => { |
| 52 | + expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1&query2=value2'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should read/write multiple query params of the same name', () => { |
| 56 | + expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1&query1=value2'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should read/write multiple path + query params + hash', () => { |
| 60 | + expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1&query1=value2#hashvalue'); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('+ HashLocationStrategy', () => { |
| 65 | + it('should read/write the url path', () => { |
| 66 | + expectUrlReadAfterWrite(PathLocationStrategy, '/foo'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should read/write query params', () => { |
| 70 | + expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should read/write multiple query params', () => { |
| 74 | + expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1&query2=value2'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should read/write multiple query params of the same name', () => { |
| 78 | + expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1&query1=value2'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should read/write multiple path + query params + hash', () => { |
| 82 | + expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1&query1=value2#hashvalue'); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments