<commit_before>import {routeRe, route, createRouter} from './jouter'

describe('createRouter', () => {
  let currentPath = '/foo'
  let listeners = []

  beforeEach(() => {
    currentPath = '/foo'
    listeners = []
  })

  const fakePathHandler = {
    get: jest.fn(() => currentPath),
    set: jest.fn((path, title) => {
      currentPath = path
      listeners.forEach(f => f())
    }),
    listen: jest.fn(f => listeners.push(f))
  }

  test('will return a router object', () => {
    const router = createRouter()
    expect(router.add).toBeInstanceOf(Function)
    expect(router.start).toBeInstanceOf(Function)
    expect(router.handleEvent).toBeInstanceOf(Function)
    expect(router.go).toBeInstanceOf(Function)
  })

  test('switching paths', () => {
    const router = createRouter(fakePathHandler)
    router.go('/test')
    expect(currentPath).toBe('/test')
  })

  test('adding and handling routes', () => {
    const router = createRouter(fakePathHandler)
    const fn = jest.fn()
    router.add(fn, '/:x')
    router.go('/bar')
    expect(fn).toHaveBeenCalledWith('bar')
  })

  test('start routing', () => {
    const router = createRouter(fakePathHandler)
    const fn = jest.fn()
    router.add(fn, '/:x')
    router.start()
    expect(listeners.length).toBe(1)
    listeners[0]()
    expect(fn).toHaveBeenCalledWith('foo')
  })

  test('handing events', () => {
    const router = createRouter(fakePathHandler)
    const fn = jest.fn()
    const fakeEvent = {
      preventDefault: jest.fn(),
      target: {
        href: '/baz',
        title: 'Baz'
      }
    }
    router.add(fn, '/:x')
    router.handleEvent(fakeEvent)
    expect(fakeEvent.preventDefault).toHaveBeenCalled()
    expect(fn).toHaveBeenCalledWith('baz')
    expect(currentPath).toBe('/baz')
  })

  test('router object is a function', () => {
    const router = createRouter(fakePathHandler)
    expect(router).toBeInstanceOf(Function)
  })

  test('router objects can be used as route handlers', () => {
    const router = createRouter(fakePathHandler)
    const subrouter = createRouter(fakePathHandler)
    const fn = jest.fn()
    router.add(subrouter, '/sub/...')
    subrouter.add(fn, '/foo/:x')
    router.go('/sub/foo/2')
    expect(fn).toHaveBeenCalledWith('2')<commit_msg>Use read DOM event for tests<commit_after>import {routeRe, route, createRouter} from './jouter'

describe('createRouter', () => {
  let currentPath = '/foo'
  let listeners = []

  beforeEach(() => {
    currentPath = '/foo'
    listeners = []
  })

  const fakePathHandler = {
    get: jest.fn(() => currentPath),
    set: jest.fn((path, title) => {
      currentPath = path
      listeners.forEach(f => f())
    }),
    listen: jest.fn(f => listeners.push(f))
  }

  test('will return a router object', () => {
    const router = createRouter()
    expect(router.add).toBeInstanceOf(Function)
    expect(router.start).toBeInstanceOf(Function)
    expect(router.handleEvent).toBeInstanceOf(Function)
    expect(router.go).toBeInstanceOf(Function)
  })

  test('switching paths', () => {
    const router = createRouter(fakePathHandler)
    router.go('/test')
    expect(currentPath).toBe('/test')
  })

  test('adding and handling routes', () => {
    const router = createRouter(fakePathHandler)
    const fn = jest.fn()
    router.add(fn, '/:x')
    router.go('/bar')
    expect(fn).toHaveBeenCalledWith('bar')
  })

  test('start routing', () => {
    const router = createRouter(fakePathHandler)
    const fn = jest.fn()
    router.add(fn, '/:x')
    router.start()
    expect(listeners.length).toBe(1)
    listeners[0]()
    expect(fn).toHaveBeenCalledWith('foo')
  })

  test('handing events', () => {
    const router = createRouter(fakePathHandler)
    const fn = jest.fn()
    const elem = document.createElement('a')
    elem.href = '/baz'
    router.add(fn, '/:x')
    elem.addEventListener('click', router.handleEvent)
    elem.dispatchEvent(new Event('click'));
    expect(fn).toHaveBeenCalledWith('baz')
    expect(currentPath).toBe('/baz')
  })

  test('router object is a function', () => {
    const router = createRouter(fakePathHandler)
    expect(router).toBeInstanceOf(Function)
  })

  test('router objects can be used as route handlers', () => {
    const router = createRouter(fakePathHandler)
    const subrouter = createRouter(fakePathHandler)
    const fn = jest.fn()
    router.add(subrouter, '/sub/...')
    subrouter.add(fn, '/foo/:x')
    router.go('/sub/foo/2')
    expect(fn).toHaveBeenCalledWith('2')
{'fraction_non_alphanumeric': 0.16637089618456077, 'fraction_numerical': 0.001774622892635315, 'mean_word_length': 3.3947368421052633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7007243', 'n_tokens_mistral': 1429, 'n_tokens_neox': 1416, 'n_words': 268}