|
1 | | -import { describe, expect, it } from 'vitest'; |
2 | | -import { isHttpError, isRedirect } from '../../src/common/utils'; |
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { getRouteId, isHttpError, isRedirect } from '../../src/common/utils'; |
3 | 3 |
|
4 | 4 | describe('isRedirect', () => { |
5 | 5 | it.each([ |
@@ -40,3 +40,65 @@ describe('isHttpError', () => { |
40 | 40 | }, |
41 | 41 | ); |
42 | 42 | }); |
| 43 | + |
| 44 | +describe('getRouteId', () => { |
| 45 | + it('returns route.id when event has untrack (SvelteKit 2+)', () => { |
| 46 | + const untrack = vi.fn(<T>(fn: () => T) => fn()); |
| 47 | + const event = { |
| 48 | + route: { id: '/blog/[slug]' }, |
| 49 | + untrack, |
| 50 | + }; |
| 51 | + |
| 52 | + // @ts-expect-error - only passing a partial load event here |
| 53 | + expect(getRouteId(event)).toBe('/blog/[slug]'); |
| 54 | + |
| 55 | + expect(untrack).toHaveBeenCalledTimes(1); |
| 56 | + expect(untrack).toHaveBeenCalledWith(expect.any(Function)); |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns undefined when event has untrack but route.id is null', () => { |
| 60 | + const untrack = vi.fn(<T>(fn: () => T) => fn()); |
| 61 | + const event = { |
| 62 | + route: { id: null }, |
| 63 | + untrack, |
| 64 | + }; |
| 65 | + |
| 66 | + // @ts-expect-error - only passing a partial load event here |
| 67 | + expect(getRouteId(event)).toBeUndefined(); |
| 68 | + |
| 69 | + expect(untrack).toHaveBeenCalledTimes(1); |
| 70 | + expect(untrack).toHaveBeenCalledWith(expect.any(Function)); |
| 71 | + }); |
| 72 | + |
| 73 | + it('falls back to getOwnPropertyDescriptor and avoids triggering the proxy', () => { |
| 74 | + const routeId = '/users/[id]'; |
| 75 | + |
| 76 | + let routeIdAccessed = false; |
| 77 | + const route = { id: routeId }; |
| 78 | + |
| 79 | + // taken from https://github.com/sveltejs/kit/blob/159aece0654db020f95bc414f6a21f25fbc5f22f/packages/kit/src/runtime/client/client.js#L783-L790 |
| 80 | + const proxiedRoute = new Proxy(route, { |
| 81 | + get: (target, key) => { |
| 82 | + routeIdAccessed = true; |
| 83 | + // @ts-expect-error - this is fine for the test |
| 84 | + return target[key]; |
| 85 | + }, |
| 86 | + }); |
| 87 | + |
| 88 | + const event = { route: proxiedRoute }; |
| 89 | + // @ts-expect-error - only passing a partial load event here |
| 90 | + expect(getRouteId(event)).toBe(routeId); |
| 91 | + expect(routeIdAccessed).toBe(false); |
| 92 | + |
| 93 | + // sanity check that the proxying mechanism works |
| 94 | + expect(event.route.id).toBe(routeId); |
| 95 | + expect(routeIdAccessed).toBe(true); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns undefined when event has no route', () => { |
| 99 | + // @ts-expect-error - only passing a partial load event here |
| 100 | + expect(getRouteId({})).toBeUndefined(); |
| 101 | + // @ts-expect-error - only passing a partial load event here |
| 102 | + expect(getRouteId({ route: null })).toBeUndefined(); |
| 103 | + }); |
| 104 | +}); |
0 commit comments