pattern.test.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. import { URLPattern } from '../src/pattern'
  2. describe('patterns', () => {
  3. test('build static path', () => {
  4. const path = '/hello/world'
  5. expect(URLPattern.build(path)).toBe(path)
  6. })
  7. test('build dynamic path', () => {
  8. expect(URLPattern.build('/hello/:name'))
  9. .toStrictEqual(/^\/hello\/(?<name>[^/]+)$/)
  10. })
  11. test('build dynamic path with regex', () => {
  12. expect(URLPattern.build('/users/:id(\\d+)'))
  13. .toStrictEqual(/^\/users\/(?<id>\d+)$/)
  14. })
  15. describe('matches', () => {
  16. test('match', () => {
  17. expect(URLPattern.match('/hello/world', URLPattern.build('/hello/world'))).toBe(true)
  18. expect(URLPattern.match('/hello/world', URLPattern.build('/hello/:name'))).toStrictEqual({name: 'world'})
  19. expect(URLPattern.match('/users/123', URLPattern.build('/users/:id(\\d+)'))).toStrictEqual({id: '123'})
  20. expect(URLPattern.match('/users/someone', URLPattern.build('/users/:id(\\d+)'))).toBe(false)
  21. })
  22. test('is', () => {
  23. expect(URLPattern.is('/hello/world', URLPattern.build('/hello/world'))).toBe(true)
  24. expect(URLPattern.is('/hello/world', URLPattern.build('/hello/:name'))).toBe(true)
  25. expect(URLPattern.is('/users/123', URLPattern.build('/users/:id(\\d+)'))).toBe(true)
  26. expect(URLPattern.is('/users/someone', URLPattern.build('/users/:id(\\d+)'))).toBe(false)
  27. })
  28. })
  29. })