next.js/packages/next/src/client/components/router-reducer/should-hard-navigate.test.tsx
should-hard-navigate.test.tsx179 lines4.1 KB
import React from 'react'
import {
  PrefetchHint,
  type FlightData,
  type FlightRouterState,
} from '../../../shared/lib/app-router-types'
import { shouldHardNavigate } from './should-hard-navigate'

describe('shouldHardNavigate', () => {
  it('should return false if the segments match', () => {
    const getInitialRouterStateTree = (): FlightRouterState => [
      '',
      {
        children: [
          'linking',
          {
            children: ['', {}],
          },
        ],
      },
      undefined,
      undefined,
      PrefetchHint.IsRootLayout,
    ]
    const initialRouterStateTree = getInitialRouterStateTree()
    const getFlightData = (): FlightData => {
      return [
        [
          'children',
          'linking',
          'children',
          'about',
          [
            'about',
            {
              children: ['', {}],
            },
          ],
          ['about', {}, <h1>About Page!</h1>],
          <>
            <title>About page!</title>
          </>,
        ],
      ]
    }
    const flightData = getFlightData()

    if (typeof flightData === 'string') {
      throw new Error('invalid flight data')
    }

    // Mirrors the way router-reducer values are passed in.
    const flightDataPath = flightData[0]
    const flightSegmentPath = flightDataPath.slice(0, -4)

    const result = shouldHardNavigate(
      ['', ...flightSegmentPath],
      initialRouterStateTree
    )

    expect(result).toBe(false)
  })

  it('should return false if segments are dynamic and match', () => {
    const getInitialRouterStateTree = (): FlightRouterState => [
      '',
      {
        children: [
          'link-hard-push',
          {
            children: [
              ['id', '123', 'd', null],
              {
                children: ['', {}],
              },
            ],
          },
        ],
      },
      null,
      null,
      PrefetchHint.IsRootLayout,
    ]
    const initialRouterStateTree = getInitialRouterStateTree()
    const getFlightData = (): FlightData => {
      return [
        [
          'children',
          'link-hard-push',
          'children',
          ['id', '123', 'd', null],
          [
            ['id', '123', 'd', null],
            {
              children: ['', {}],
            },
          ],
          [['id', '123', 'd', null], {}, null],
          null,
        ],
      ]
    }
    const flightData = getFlightData()

    if (typeof flightData === 'string') {
      throw new Error('invalid flight data')
    }

    // Mirrors the way router-reducer values are passed in.
    const flightDataPath = flightData[0]
    const flightSegmentPath = flightDataPath.slice(0, -4)

    const result = shouldHardNavigate(
      ['', ...flightSegmentPath],
      initialRouterStateTree
    )

    expect(result).toBe(false)
  })

  it('should return true if segments are dynamic and mismatch', () => {
    const getInitialRouterStateTree = (): FlightRouterState => [
      '',
      {
        children: [
          'link-hard-push',
          {
            children: [
              ['id', '456', 'd', null],
              {
                children: ['', {}],
              },
            ],
          },
        ],
      },
      null,
      null,
      PrefetchHint.IsRootLayout,
    ]
    const initialRouterStateTree = getInitialRouterStateTree()
    const getFlightData = (): FlightData => {
      return [
        [
          'children',
          'link-hard-push',
          'children',
          ['id', '123', 'd', null],
          [
            ['id', '123', 'd', null],
            {
              children: ['', {}],
            },
          ],
          [['id', '123', 'd', null], {}, null],
          null,
          false,
        ],
      ]
    }
    const flightData = getFlightData()

    if (typeof flightData === 'string') {
      throw new Error('invalid flight data')
    }

    // Mirrors the way router-reducer values are passed in.
    const flightDataPath = flightData[0]
    const flightSegmentPath = flightDataPath.slice(0, -4)

    const result = shouldHardNavigate(
      ['', ...flightSegmentPath],
      initialRouterStateTree
    )

    expect(result).toBe(true)
  })
})
Quest for Codev2.0.0
/
SIGN IN