Skip to content

Commit aa323ed

Browse files
Remove decodeURIComponent call in installRouter (#560)
* fix: don't spyOn pwaHelpers.installRouter I found that after duplicating this test, `spy1.mockRestore` was not restoring enough state, somehow. This caused the second test's `const cb = ...` to fail. * test: router does not escape parameteers * test: do not mock `decodeURIComponent` * test: URL double-decode Tests fail on this commit, but will be fixed on the next. * fix: don't decode `window.location` --------- Co-authored-by: Fernando Pasik <fernando@pasik.com.ar>
1 parent 5d63d96 commit aa323ed

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/lib/route.test.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('route element', () => {
5959
define: jest.fn(),
6060
get: customElementsGet,
6161
},
62-
decodeURIComponent: jest.fn((val) => val),
62+
decodeURIComponent,
6363
scrollTo: jest.fn(),
6464
},
6565
});
@@ -127,20 +127,39 @@ describe('route element', () => {
127127
it('can set active route', async () => {
128128
connectRouter(mockStore({}) as unknown as TestStore);
129129
const route = new Route();
130-
const spy1 = jest.spyOn(pwaHelpers, 'installRouter');
131-
const spy2 = jest.spyOn(actions, 'setActiveRoute');
130+
const installRouter = jest.mocked(pwaHelpers.installRouter);
131+
const spy = jest.spyOn(actions, 'setActiveRoute');
132+
132133
const pathname = '/example';
133134
const search = '?test=testing';
134135
const hash = '#example';
135136

136137
await route.firstUpdated();
137-
const cb = spy1.mock.results[0].value as typeof pwaHelpers.installRouter.arguments;
138+
const cb = installRouter.mock.results[0].value as typeof pwaHelpers.installRouter.arguments;
138139
cb({ hash, pathname, search });
139140

140-
expect(spy2).toHaveBeenCalledWith(pathname + search + hash);
141+
expect(spy).toHaveBeenCalledWith(pathname + search + hash);
141142

142-
spy1.mockRestore();
143-
spy2.mockRestore();
143+
spy.mockRestore();
144+
});
145+
146+
it('does not decode URLs again', async () => {
147+
connectRouter(mockStore({}) as unknown as TestStore);
148+
const route = new Route();
149+
const installRouter = jest.mocked(pwaHelpers.installRouter);
150+
const spy = jest.spyOn(actions, 'setActiveRoute');
151+
const pathname = '/component%2Fone/component_two';
152+
const search = '?hash=%23';
153+
const hash = '#example';
154+
155+
await route.firstUpdated();
156+
157+
const cb = installRouter.mock.results[0].value as typeof pwaHelpers.installRouter.arguments;
158+
cb({ hash, pathname, search });
159+
160+
expect(spy).toHaveBeenCalledWith(pathname + search + hash);
161+
162+
spy.mockRestore();
144163
});
145164
});
146165

src/lib/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default (store: LazyStore & Store): void => {
5555
await this.updateComplete;
5656
if (!Route.routerInstalled) {
5757
installRouter(({ pathname, search, hash }: Location): void => {
58-
const path = window.decodeURIComponent(pathname + search + hash);
58+
const path = pathname + search + hash;
5959
store.dispatch(setActiveRoute(path));
6060
});
6161
Route.routerInstalled = true;

src/lib/service.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,14 @@ describe('router service', () => {
139139
expect(SUT.active).toBe(true);
140140
expect(SUT.params).toStrictEqual({ id: '1' });
141141
});
142+
143+
it('does not escape parameters', () => {
144+
const route = '/product/:id';
145+
const activeRoute = '/product/before%2Fafter';
146+
147+
const SUT = refreshRoute(route, activeRoute);
148+
149+
expect(SUT.active).toBe(true);
150+
expect(SUT.params).toStrictEqual({ id: 'before%2Fafter' });
151+
});
142152
});

0 commit comments

Comments
 (0)