Repository: nuxt/nuxt
File: test/nuxt/composables.test.ts (TypeScript)

Code before:
    expect(() => navigateTo('https://test.com')).toThrowErrorMatchingInlineSnapshot('[Error: Navigating to an external URL is not allowed by default. Use `navigateTo(url, { external: true })`.]')
    expect(() => navigateTo('https://test.com', { external: true })).not.toThrow()
  })
  it('navigateTo should disallow navigation to data/script URLs', () => {
    const urls = [
      ['data:alert("hi")', 'data'],
      ['\0data:alert("hi")', 'data'],
    ]
    for (const [url, protocol] of urls) {
      expect(() => navigateTo(url, { external: true })).toThrowError(`Cannot navigate to a URL with '${protocol}:' protocol.`)
    }
  })
  it('navigateTo should replace current navigation state if called within middleware', () => {
    const nuxtApp = useNuxtApp()
    nuxtApp._processingMiddleware = true
    expect(navigateTo('/')).toMatchInlineSnapshot(`"/"`)
    expect(navigateTo('/', { replace: true })).toMatchInlineSnapshot(`
      {
        "path": "/",
        "replace": true,
      }
    `)
    nuxtApp._processingMiddleware = false
  })

  it.todo('#28425', async () => {
    router.addRoute({
      name: 'slug',
      path: '/28425/:slug',
      component: defineComponent({
        template: '<div> slug page </div>',
        async setup () {
          await new Promise(res => setTimeout(res, 200))
        },
      }),
    })
    const el = await mountSuspended({ setup: () => () => h(NuxtPage) })
    const route = useRoute()
    await navigateTo('/28425/p1') // remove this line to prevent the issue.
    await navigateTo('/28425/p1')
    await navigateTo('/28425/p2')
    await navigateTo('/28425/p3')
    await waitForPageChange()
    expect(el.html()).toContain('<div> slug page </div>')
    expect(route.fullPath).toMatchInlineSnapshot(`"/28425/p3"`)
    el.unmount()
    router.removeRoute('slug')
  })
})

describe('routing utilities: `resolveRouteObject`', () => {

Review comment (bug):
We cant use the `.fails` here because the test results in an infinite loop that doesnt time out.

Code after:
    const urls = [
      ['data:alert("hi")', 'data'],
      ['\0data:alert("hi")', 'data'],
    ]
    for (const [url, protocol] of urls) {
      expect(() => navigateTo(url, { external: true })).toThrowError(`Cannot navigate to a URL with '${protocol}:' protocol.`)
    }
  })
  it('navigateTo should replace current navigation state if called within middleware', () => {
    const nuxtApp = useNuxtApp()
    nuxtApp._processingMiddleware = true
    expect(navigateTo('/')).toMatchInlineSnapshot(`"/"`)
    expect(navigateTo('/', { replace: true })).toMatchInlineSnapshot(`
      {
        "path": "/",
        "replace": true,
      }
    `)
    nuxtApp._processingMiddleware = false
  })

  it('#28425', async () => {
    router.addRoute({
      name: 'slug',
      path: '/28425/:slug',
      component: defineComponent({
        template: '<div> slug page </div>',
        async setup () {
          await new Promise(res => setTimeout(res, 200))
        },
      }),
    })
    const el = await mountSuspended({ setup: () => () => h(NuxtPage) })
    const route = useRoute()
    await navigateTo('/28425/p1') // remove this line to prevent the issue.
    await navigateTo('/28425/p1')
    await navigateTo('/28425/p2')
    await navigateTo('/28425/p3')
    await waitForPageChange()
    expect(el.html()).toContain('<div> slug page </div>')
    expect(route.fullPath).toMatchInlineSnapshot(`"/28425/p3"`)
    el.unmount()
    router.removeRoute('slug')
  })
})

describe('routing utilities: `resolveRouteObject`', () => {
  it('resolveRouteObject should correctly resolve a route object', () => {
    expect(resolveRouteObject({ path: '/test' })).toMatchInlineSnapshot(`"/test"`)
    expect(resolveRouteObject({ path: '/test', hash: '#thing', query: { foo: 'bar' } })).toMatchInlineSnapshot(`"/test?foo=bar#thing"`)
  })