Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/router / __tests__ / OneRouter.test.ts
Size: Mime:
// for the types...
// @see https://blog.kevinchisholm.com/jasmine/jasmine-cannot-find-name-describe-beforeeach-expect/
import 'jest'

import { observe } from 'xmobx/mobx'
import { isArray } from 'exotic'
import { OneRouterToRuleThemAll, oneRouter, setConfig } from '../src'
import { RouterType } from '../src/typings'

describe('oneRouter', () => {
  // update ----
  test.skip('can .update hash', () => {
    const twoRouter = new OneRouterToRuleThemAll()
    // @note this is the one that breaks for us here
    // main problem  of course is observer
    // so we want to  update  when router updates
    twoRouter.update('#hash=1')
    expect(twoRouter.pathname).toContain('some-url')
  })

  test('loads for window', () => {
    const twoRouter = new OneRouterToRuleThemAll()

    global.window = {
      location: {
        href: 'http://localhost/@@mock',
        pathname: '@@mock',
        search: '',
        reload() {
          // use this to mock reload
        },
        pushState() {
          // use this to mock pushstate
        },
      },
    }
    expect(twoRouter.pathname).toContain('@@mock')
    delete global.window
  })
  test('loads for express', () => {
    const twoRouter = new OneRouterToRuleThemAll()

    global.oneUrl = {
      protocol: 'http',
      host: 'localhost',
      pathname: '/@@mock',
      search: {},
      hash: '',
      full: `http://localhost/@@mock`,
      userAgent: '',
    }
    console.log(twoRouter.pathname)
    expect(twoRouter.pathname).toContain('@@mock')
    delete global.oneUrl
  })
  test('loads via setting .oneUrl', () => {
    const twoRouter = new OneRouterToRuleThemAll()

    twoRouter.oneUrl = {
      protocol: 'http',
      host: 'localhost',
      pathname: '/@@mock',
      search: {},
      hash: '',
      full: `http://localhost/@@mock`,
      userAgent: '',
    }
    expect(twoRouter.pathname).toContain('@@mock')
    twoRouter.oneUrl = undefined
  })

  test('works for setting your own .router', () => {
    const twoRouter = new OneRouterToRuleThemAll()

    // @todo - test this one in the browser version
    // with the setting the merge thing
    const match = {
      isExact: true,
      params: {
        productId: '420004',
      },
      path: '/product/:productId',
      url: '/product/420004',
    }
    const location = {
      href: 'http://localhost/product/420005',
      pathname: '/product/420005',
      search: '',
      reload() {
        // use this to mock reload
      },
      pushState() {
        // use this to mock pushstate
      },
      toString() {
        return location.href
      },
    }
    const history = {
      length: 0,
      //  | 'POP' | 'REPLACE',
      action: 'PUSH',
      location,
      push(path, state?: any) {
        console.log('push')
      },
    }
    const route = {
      match,
      location,
    }
    const router: RouterType = {
      history,
      route,
    }

    setConfig({
      pathParams: {
        fallback: '/product/:productId',
      },
    })

    twoRouter.router = router
    const actualParam = '' + twoRouter.get('productId')
    expect(actualParam).toEqual('420005')
  })

  test('can .update query using an object', () => {
    // @todo
    // const history = {
    //   push(path, state?: any) {
    //     expect(path).toContain('some-url')
    //   },
    // }

    const twoRouter = new OneRouterToRuleThemAll()
    twoRouter.update({
      to: 'some-url',
    })
    expect(twoRouter.toString()).toContain('some-url')
  })

  test('can .update query using an string', () => {
    const twoRouter = new OneRouterToRuleThemAll()
    twoRouter.update('some-url')
    expect(twoRouter.pathname).toContain('some-url')
  })

  test('can .update router using .set - but not the url?', () => {
    const twoRouter = new OneRouterToRuleThemAll()
    twoRouter.set('one', 1)
    expect(twoRouter.get('one')).toEqual(1)
  })

  // del ----
  test('can .delete query using string', () => {
    const twoRouter = new OneRouterToRuleThemAll()
    twoRouter.set('one', 1).delete('one')
    expect(twoRouter.get('one')).toEqual(undefined)
  })

  // entries / read ------

  test('can match route params', () => {
    const twoRouter = new OneRouterToRuleThemAll()

    setConfig({
      pathParams: {
        fallback: '/fallback/:eh',
      },
      routePathsList: [
        {
          path: '/fallback/:eh',
          component: undefined,
          exact: true,
        },
      ],
    })

    twoRouter.update('/fallback/igloo')

    const eh = twoRouter.get('eh')
    expect(eh).toEqual('igloo')
    expect(twoRouter).toMatchSnapshot()
  })

  test('can use entries object', () => {
    const twoRouter = new OneRouterToRuleThemAll()
    const isEntriesObj = typeof twoRouter.entries() === 'object'
    expect(isEntriesObj).toEqual(true)
  })

  // would solve your problems of numbers not being numbers,
  // they are strings right
  test('@todo can use with autofixing data types', () => {
    const twoRouter = new OneRouterToRuleThemAll()
    twoRouter.update({
      list: [
        {
          label: 'one',
          value: 1,
        },
        {
          label: 'two',
          value: 2,
        },
      ],
    })

    console.debug('autofix')

    const list = twoRouter.entries().list
    const listIsArray = isArray(list)
    expect(listIsArray).toBe(true)
    expect(list).toMatchSnapshot()
  })

  test('.replace', () => {
    const twoRouter = new OneRouterToRuleThemAll()

    const url = 'https://canada.me/igloo'
    twoRouter.clear()
    twoRouter.replace(url)
    expect(twoRouter.pathname).toEqual(url)
  })

  test('can tap entries', () => {
    const twoRouter = new OneRouterToRuleThemAll()

    // set
    twoRouter.setTapEntries(() => true)
    const entries = twoRouter.entries()
    expect(entries).toEqual(true)

    // clear
    twoRouter.setTapEntries()
    const entriesUntapped = twoRouter.entries()
    expect(entriesUntapped).not.toEqual(true)
  })
  test('can observe changes', () => {
    const twoRouter = new OneRouterToRuleThemAll()

    expect.assertions(3)

    // console.log('1. initial')
    // initially
    expect(twoRouter.observable.url).toEqual(twoRouter.url)

    // @note - added `, 'url'` - was triggering 2x...
    // console.log('2. observe')
    // subscribe
    const disposer = observe(twoRouter.observable, 'url', () => {
      // updates
      expect(twoRouter.observable.url).toEqual('eh')
    })

    // console.log('3. setUrl')
    // update, will trigger ^
    twoRouter.setUrl('eh')

    // console.log('4. expect')
    expect(twoRouter.url).toEqual('eh')

    // console.log('5. cleanup')
    // cleanup
    disposer()
  })

  // is observable

  // test('observer triggers when we change route/.update', () => {
  //   expect.assertions(1)
  //   twoRouter.onChange(function(location, action) {
  //     console.log('location', location)
  //     expect(arguments.length).toEqual(2)
  //   })
  //   twoRouter.update('/eh')
  //   expect(twoRouter.pathname).toEqual('/eh')
  // })

  test.skip('matching routes with react-router-config', () => {
    //
  })
  test.skip('history instantiation', () => {
    //
  })
  test.skip('render as component', () => {
    //
  })
  test.skip('setting the `router`', () => {
    //
  })
  test.skip('reload', () => {
    //
  })
  test.skip('multiple transitions checking the diff', () => {
    //
  })

  // issues here when you ssr right
  describe('cross realm (server, browser)', () => {
    test('has the url from the request on the server', () => {
      // expect()
    })
    test('has the url from the request on the browser', () => {
      //
    })
    test('has the __SAME__ url on the browser AND the server!!!', () => {
      //
    })
  })
})