|
| 1 | +import { APP_BASE_HREF } from '@angular/common'; |
1 | 2 | import { Component, DebugElement, ViewChildren, QueryList } from '@angular/core'; |
2 | | -import { async, ComponentFixture, TestBed } from '@angular/core/testing'; |
| 3 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
3 | 4 | import { By } from '@angular/platform-browser'; |
4 | 5 |
|
5 | 6 | import { UIRouterModule } from '../../src/uiRouterNgModule'; |
6 | 7 | import { UISref } from '../../src/directives/uiSref'; |
7 | | -import { UIRouter, TargetState, TransitionOptions, StateService } from '@uirouter/core'; |
8 | | -import { Subject, Subscription } from 'rxjs'; |
| 8 | +import { UIRouter, TargetState, TransitionOptions } from '@uirouter/core'; |
| 9 | +import { Subscription } from 'rxjs'; |
9 | 10 | import { clickOnElement } from '../testUtils'; |
10 | 11 |
|
11 | 12 | describe('uiSref', () => { |
@@ -41,138 +42,115 @@ describe('uiSref', () => { |
41 | 42 | } |
42 | 43 | } |
43 | 44 |
|
44 | | - describe('when applied to a link tag', () => { |
45 | | - describe('when the uiSref is empty', () => { |
46 | | - let des: DebugElement[]; |
47 | | - let fixture: ComponentFixture<TestComponent>; |
48 | | - let gospy: jasmine.Spy; |
49 | | - |
50 | | - beforeEach(() => { |
51 | | - fixture = TestBed.configureTestingModule({ |
52 | | - declarations: [TestComponent], |
53 | | - imports: [UIRouterModule.forRoot({ useHash: true })], |
54 | | - }).createComponent(TestComponent); |
55 | | - fixture.detectChanges(); |
56 | | - des = fixture.debugElement.queryAll(By.directive(UISref)); |
57 | | - const stateService = fixture.debugElement.injector.get(StateService); |
58 | | - gospy = spyOn(stateService, 'go'); |
59 | | - }); |
60 | | - |
61 | | - it('should not bind "null" string to `href`', () => { |
62 | | - expect(des[0].nativeElement.hasAttribute('href')).toBeFalsy(); |
63 | | - expect(des[1].nativeElement.hasAttribute('href')).toBeFalsy(); |
64 | | - }); |
65 | | - |
66 | | - it('should ignore the click event', () => { |
67 | | - clickOnElement(des[0]); |
68 | | - expect(gospy).not.toHaveBeenCalled(); |
69 | | - |
70 | | - clickOnElement(des[1]); |
71 | | - expect(gospy).not.toHaveBeenCalled(); |
72 | | - }); |
73 | | - }); |
74 | | - |
75 | | - describe('when the uiSref is not empty', () => { |
76 | | - let des: DebugElement[]; |
77 | | - let comp: TestComponent; |
78 | | - let uiRouterMock: UIRouter; |
79 | | - let fixture: ComponentFixture<TestComponent>; |
80 | | - |
81 | | - beforeEach(async(() => { |
82 | | - uiRouterMock = { |
83 | | - globals: { |
84 | | - states$: new Subject(), |
85 | | - }, |
86 | | - stateService: { |
87 | | - go: jest.fn(), |
88 | | - target: jest.fn(), |
89 | | - href: jest.fn(), |
90 | | - }, |
91 | | - } as any; |
92 | | - TestBed.configureTestingModule({ |
93 | | - declarations: [TestComponent], |
94 | | - imports: [UIRouterModule.forRoot({ useHash: true })], |
95 | | - }) |
96 | | - .overrideComponent(TestComponent, { |
97 | | - set: { |
98 | | - providers: [{ provide: UIRouter, useValue: uiRouterMock }], |
99 | | - }, |
100 | | - }) |
101 | | - .compileComponents(); |
102 | | - })); |
103 | | - |
104 | | - beforeEach(() => { |
105 | | - fixture = TestBed.createComponent(TestComponent); |
| 45 | + const setup = () => { |
| 46 | + const fixture = TestBed.configureTestingModule({ |
| 47 | + declarations: [TestComponent], |
| 48 | + imports: [UIRouterModule.forRoot({ useHash: true })], |
| 49 | + providers: [{ provide: APP_BASE_HREF, useValue: '/root' }], |
| 50 | + }).createComponent(TestComponent); |
| 51 | + fixture.detectChanges(); |
| 52 | + const srefElements = fixture.debugElement.queryAll(By.directive(UISref)); |
| 53 | + const router = fixture.debugElement.injector.get(UIRouter); |
| 54 | + |
| 55 | + return { fixture, srefElements, router }; |
| 56 | + }; |
| 57 | + |
| 58 | + // Extract the logical portion of the URL after the hash |
| 59 | + const urlOfElement = (srefElement: DebugElement) => srefElement.nativeElement.href.replace(/^[^#]*#/, ''); |
| 60 | + |
| 61 | + it('renders an href for a state with an url', () => { |
| 62 | + const { fixture, srefElements, router } = setup(); |
| 63 | + router.stateRegistry.register({ name: 'urlstate', url: '/myurl' }); |
| 64 | + fixture.componentInstance.linkA = 'urlstate'; |
| 65 | + fixture.detectChanges(); |
| 66 | + expect(srefElements[0].nativeElement.hasAttribute('href')).toBeTruthy(); |
| 67 | + expect(urlOfElement(srefElements[0])).toBe('/myurl'); |
| 68 | + }); |
106 | 69 |
|
107 | | - comp = fixture.componentInstance; |
108 | | - comp.linkA = ''; |
109 | | - fixture.detectChanges(); |
110 | | - des = fixture.debugElement.queryAll(By.directive(UISref)); |
111 | | - }); |
| 70 | + // it('renders an empty href for a url-less state', () => { |
| 71 | + // const { fixture, srefElements, router } = setup(); |
| 72 | + // router.stateRegistry.register({ name: 'urlless' }); |
| 73 | + // fixture.componentInstance.linkA = 'urlless'; |
| 74 | + // fixture.detectChanges(); |
| 75 | + // expect(srefElements[0].nativeElement.hasAttribute('href')).toBeTruthy(); |
| 76 | + // expect(urlOfElement(srefElements[0])).toBe(''); |
| 77 | + // }); |
| 78 | + |
| 79 | + it('renders no href when the sref state is empty', () => { |
| 80 | + const { fixture, srefElements } = setup(); |
| 81 | + fixture.componentInstance.linkA = null; |
| 82 | + expect(srefElements[0].nativeElement.hasAttribute('href')).toBeFalsy(); |
| 83 | + }); |
112 | 84 |
|
113 | | - describe('when target is _blank', () => { |
114 | | - beforeEach(() => { |
115 | | - comp.targetA = '_blank'; |
116 | | - fixture.detectChanges(); |
117 | | - }); |
| 85 | + it('should ignore the click event when the sref state is empty', () => { |
| 86 | + const { fixture, srefElements, router } = setup(); |
| 87 | + const gospy = jest.spyOn(router.stateService, 'go'); |
| 88 | + fixture.componentInstance.linkA = null; |
| 89 | + fixture.componentInstance.linkB = null; |
| 90 | + fixture.detectChanges(); |
118 | 91 |
|
119 | | - describe('when clicked', () => { |
120 | | - beforeEach(() => { |
121 | | - clickOnElement(des[0]); |
122 | | - }); |
| 92 | + clickOnElement(srefElements[0]); |
| 93 | + expect(gospy).not.toHaveBeenCalled(); |
123 | 94 |
|
124 | | - it('should ignore the click event', () => { |
125 | | - expect(uiRouterMock.stateService.go).not.toHaveBeenCalled(); |
126 | | - }); |
127 | | - }); |
128 | | - }); |
| 95 | + clickOnElement(srefElements[0]); |
| 96 | + expect(gospy).not.toHaveBeenCalled(); |
| 97 | + }); |
129 | 98 |
|
130 | | - describe('when target is not _blank', () => { |
131 | | - beforeEach(() => { |
132 | | - comp.targetA = ''; |
133 | | - comp.linkA = 'state'; |
134 | | - fixture.detectChanges(); |
135 | | - }); |
| 99 | + it('should call stateService.go with the linked state when clicked', () => { |
| 100 | + const { fixture, srefElements, router } = setup(); |
| 101 | + const gospy = jest.spyOn(router.stateService, 'go'); |
| 102 | + fixture.componentInstance.linkA = 'stateref'; |
| 103 | + fixture.detectChanges(); |
| 104 | + clickOnElement(srefElements[0]); |
| 105 | + expect(gospy).toHaveBeenCalled(); |
| 106 | + expect(gospy.mock.calls[0][0]).toBe('stateref'); |
| 107 | + }); |
136 | 108 |
|
137 | | - describe('when clicked', () => { |
138 | | - beforeEach(() => { |
139 | | - clickOnElement(des[0]); |
140 | | - }); |
| 109 | + it('should ignore the click event when target is _blank', () => { |
| 110 | + const { fixture, srefElements, router } = setup(); |
| 111 | + const gospy = jest.spyOn(router.stateService, 'go'); |
| 112 | + router.stateRegistry.register({ name: 'statea', url: '/statea' }); |
| 113 | + fixture.componentInstance.linkA = 'statea'; |
| 114 | + fixture.componentInstance.targetA = '_blank'; |
| 115 | + fixture.detectChanges(); |
| 116 | + clickOnElement(srefElements[0]); |
| 117 | + expect(gospy).not.toHaveBeenCalled(); |
| 118 | + }); |
141 | 119 |
|
142 | | - it('should navigate to the state', () => { |
143 | | - expect(uiRouterMock.stateService.go).toHaveBeenCalled(); |
144 | | - }); |
145 | | - }); |
146 | | - }); |
| 120 | + describe('opening in a new tab', () => { |
| 121 | + let srefElement: DebugElement; |
| 122 | + let gospy: jest.SpyInstance; |
147 | 123 |
|
148 | | - describe('opening in a new tab', () => { |
149 | | - beforeEach(() => { |
150 | | - comp.targetA = ''; |
151 | | - fixture.detectChanges(); |
152 | | - }); |
| 124 | + beforeEach(() => { |
| 125 | + const { fixture, srefElements, router } = setup(); |
| 126 | + srefElement = srefElements[0]; |
| 127 | + gospy = jest.spyOn(router.stateService, 'go'); |
| 128 | + fixture.componentInstance.targetA = 'somestate'; |
| 129 | + fixture.detectChanges(); |
| 130 | + }); |
153 | 131 |
|
154 | | - it('should fallback to the browser default when the button is not the left', () => { |
155 | | - clickOnElement(des[0], 1); |
156 | | - expect(uiRouterMock.stateService.go).not.toHaveBeenCalled(); |
157 | | - }); |
| 132 | + it('should fallback to the browser default when the button is not the left', () => { |
| 133 | + clickOnElement(srefElement, 1); |
| 134 | + expect(gospy).not.toHaveBeenCalled(); |
| 135 | + }); |
158 | 136 |
|
159 | | - it('should fallback to the browser default when the button is not set', () => { |
160 | | - clickOnElement(des[0], null); |
161 | | - expect(uiRouterMock.stateService.go).not.toHaveBeenCalled(); |
162 | | - }); |
| 137 | + it('should fallback to the browser default when the button is not set', () => { |
| 138 | + clickOnElement(srefElement, null); |
| 139 | + expect(gospy).not.toHaveBeenCalled(); |
| 140 | + }); |
163 | 141 |
|
164 | | - it('should fallback to the browser default when the meta key is pressed', () => { |
165 | | - clickOnElement(des[0], 0, true); |
166 | | - expect(uiRouterMock.stateService.go).not.toHaveBeenCalled(); |
167 | | - }); |
| 142 | + it('should fallback to the browser default when the meta key is pressed', () => { |
| 143 | + clickOnElement(srefElement, 0, true); |
| 144 | + expect(gospy).not.toHaveBeenCalled(); |
| 145 | + }); |
168 | 146 |
|
169 | | - it('should fallback to the browser default when the ctrl key is pressed', () => { |
170 | | - clickOnElement(des[0], 0, false, true); |
171 | | - expect(uiRouterMock.stateService.go).not.toHaveBeenCalled(); |
172 | | - }); |
173 | | - }); |
| 147 | + it('should fallback to the browser default when the ctrl key is pressed', () => { |
| 148 | + clickOnElement(srefElement, 0, false, true); |
| 149 | + expect(gospy).not.toHaveBeenCalled(); |
174 | 150 | }); |
| 151 | + }); |
175 | 152 |
|
| 153 | + describe('when applied to a link tag', () => { |
176 | 154 | describe('when the bound values change', () => { |
177 | 155 | let fixture: ComponentFixture<TestComponent>; |
178 | 156 | let comp: TestComponent; |
|
0 commit comments