Skip to content

Commit af8b7b7

Browse files
authored
fix: enhance active route comparison to allow if/else restrictions (#87)
1 parent d917b05 commit af8b7b7

File tree

4 files changed

+63
-13
lines changed

4 files changed

+63
-13
lines changed
Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,56 @@
1-
import { getToolbar, getText } from '../support/app.po';
1+
import {
2+
getToolbar,
3+
getText,
4+
getNavigationLinks,
5+
getToggleButton,
6+
getRestrictionLabel,
7+
} from '../support/app.po';
28

39
describe('route-restrictions', () => {
4-
beforeEach(() => cy.visit('/'));
5-
610
it('should display home page text', () => {
11+
cy.visit('/');
712
getToolbar().contains('Route restrictions');
813
getText().contains('This route is OPEN');
14+
getToggleButton().should('be.visible');
15+
});
16+
17+
it('should display toggle button and restriction', () => {
18+
cy.visit('/');
19+
getToggleButton().should('be.visible');
20+
getRestrictionLabel().should('contain.text', 'OFF');
21+
});
22+
23+
it('should toggle restriction on toggle click', () => {
24+
cy.visit('/');
25+
getRestrictionLabel().should('contain.text', 'OFF');
26+
getToggleButton().click();
27+
getRestrictionLabel().should('contain.text', 'ON');
28+
});
29+
30+
it('should display restrictions page text', () => {
31+
cy.visit('/restricted');
32+
getText().contains('This route is restricted');
33+
});
34+
35+
it('should switch to restrictions on nav click', () => {
36+
cy.visit('/');
37+
getNavigationLinks().eq(1).click();
38+
cy.url().should('eq', 'http://localhost:4200/restricted');
39+
getText().contains('This route is restricted');
40+
});
41+
42+
it('should redirect to home page when restriction is set', () => {
43+
cy.visit('/restricted');
44+
getToggleButton().click();
45+
cy.url().should('eq', 'http://localhost:4200/');
46+
getText().contains('This route is OPEN');
47+
});
48+
49+
it('should redirect to home page when restriction is set on nav click', () => {
50+
cy.visit('/');
51+
getToggleButton().click();
52+
getNavigationLinks().eq(1).click();
53+
cy.url().should('eq', 'http://localhost:4200/');
54+
getText().contains('This route is OPEN');
955
});
1056
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
export const getToolbar = () => cy.get('mat-toolbar');
22
export const getText = () => cy.get('p');
3+
export const getNavigationLinks = () => cy.get('a.mat-button');
4+
export const getToggleButton = () => cy.get('button:contains("Toggle")');
5+
export const getRestrictionLabel = () => cy.get('label');

apps/route-restrictions/src/app/app.component.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@
1111
<button mat-raised-button type="button" (click)="toggle()">
1212
Toggle restriction
1313
</button>
14-
{{ restricted ? 'ON' : 'OFF' }}
14+
<label>{{ restricted ? 'ON' : 'OFF' }}</label>
1515

1616
<router>
17-
<route path="/restricted" *ngIf="!restricted">
17+
<route path="/restricted" *ngIf="!restricted; else restrictedRoute">
1818
<rr-restricted *routeComponent></rr-restricted>
1919
</route>
20+
<ng-template #restrictedRoute>
21+
<route path="/restricted" redirectTo="/"></route>
22+
</ng-template>
2023
<route path="" [exact]="false">
2124
<rr-unknown *routeComponent></rr-unknown>
2225
</route>

libs/router/src/lib/router.component.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ interface State {
2424
routes: Route[];
2525
}
2626

27-
type UnregisterableRoute = Route & { unregister?: boolean };
28-
2927
@Component({
3028
// tslint:disable-next-line:component-selector
3129
selector: 'router',
@@ -110,7 +108,7 @@ export class RouterComponent implements OnInit, OnDestroy {
110108
}
111109

112110
unregisterRoute(route: Route) {
113-
this.updateRoutes({ ...route, unregister: true });
111+
this.updateRoutes(route);
114112
}
115113

116114
normalizePath(path: string) {
@@ -138,8 +136,7 @@ export class RouterComponent implements OnInit, OnDestroy {
138136
return (
139137
previous.path === current.path &&
140138
compareParams(previous.params, current.params) &&
141-
previous.route.path === current.route.path &&
142-
previous.route.options.exact === current.route.options.exact
139+
previous.route === current.route
143140
);
144141
}
145142

@@ -160,11 +157,12 @@ export class RouterComponent implements OnInit, OnDestroy {
160157
this.state$.next({ ...this.state$.value, ...newState });
161158
}
162159

163-
private updateRoutes(route: UnregisterableRoute) {
160+
private updateRoutes(route: Route) {
164161
const routes = this.state$.value.routes;
165-
if (route.unregister) {
162+
const index = routes.indexOf(route);
163+
if (index > -1) {
166164
this.updateState({
167-
routes: routes.filter((r) => r.matcher !== route.matcher),
165+
routes: [...routes.slice(0, index), ...routes.slice(index + 1)],
168166
});
169167
} else {
170168
this.updateState({ routes: routes.concat(route).sort(compareRoutes) });

0 commit comments

Comments
 (0)