Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pshrmn committed Feb 11, 2017
1 parent ef74e92 commit 1b5dc78
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"presets": [
"./config/buildPreset"
],
"plugins": [
"transform-export-extensions",
"transform-object-rest-spread"
]
}
15 changes: 15 additions & 0 deletions config/buildPreset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const buildPreset = require('babel-preset-es2015').buildPreset

const BABEL_ENV = process.env.BABEL_ENV

module.exports = {
presets: [
[
buildPreset,
{
loose: true,
modules: BABEL_ENV === 'es' ? false : 'commonjs'
}
]
]
}
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@
"invariant": "^2.2.2"
},
"devDependencies": {
"babel-cli": "^6.22.2",
"babel-core": "^6.22.1",
"babel-jest": "^18.0.0",
"babel-plugin-transform-export-extensions": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.22.0",
"babel-preset-es2015": "^6.22.0",
"history": "^4.5.1",
"jest": "^18.1.0"
"jest": "^18.1.0",
"qs": "^6.3.0"
}
}
125 changes: 125 additions & 0 deletions tests/qhistory.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import qhistory from '../src/index'
import { createMemoryHistory } from 'history'
import { stringify, parse } from 'qs'

describe('qhistory', () => {
describe('query', () => {
it('will be present on initial location', () => {
const history = createMemoryHistory({
initialEntries: [{ pathname: '/first', search: '?second=third'}]
})
const q = qhistory(history, stringify, parse)
expect(q.location.query).toBeDefined()
})

it('will be present after navigating', () => {
const history = createMemoryHistory()
const q = qhistory(history, stringify, parse)

q.listen((location) => {
expect(location.query).toEqual({ tooGoodToBe: 'true' })
expect(location.search).toBe('?tooGoodToBe=true')
})

q.push('/test?tooGoodToBe=true')
})

it('will be an empty query object when there is no query/search', () => {
const history = createMemoryHistory()
const q = qhistory(history, stringify, parse)

q.listen((location) => {
expect(location.query).toBeInstanceOf(Object)
expect(Object.keys(location.query).length).toBe(0)
})

q.push('/test')
})
})

describe('search', () => {
it('will prefer query object over search string', () => {
const history = createMemoryHistory()
const q = qhistory(history, stringify, parse)

q.push({ pathname: '/somewhere',
search: '?overRainbow=false',
query: { overRainbow: true }
})
const mostRecentLocation = q.entries[q.entries.length-1]
expect(mostRecentLocation.search).toBe('?overRainbow=true')
})

it('uses search if no query provided', () => {
const history = createMemoryHistory()
const q = qhistory(history, stringify, parse)
q.push({ pathname: '/somewhere', search: '?overRainbow=false' })
const mostRecentLocation = q.entries[q.entries.length-1]
expect(mostRecentLocation.search).toBe('?overRainbow=false')
})

it('sets search to empty string if no query/search', () => {
const history = createMemoryHistory()
const q = qhistory(history, stringify, parse)
q.push({ pathname: '/somewhere' })
const mostRecentLocation = q.entries[q.entries.length-1]
expect(mostRecentLocation.search).toBe('')
})
})

describe('push', () => {
it('will set search string for location passed to actual history', () => {
const history = createMemoryHistory()
const q = qhistory(history, stringify, parse)

q.push({ pathname: '/somewhere', query: { overRainbow: true }})
const mostRecentLocation = q.entries[q.entries.length-1]
expect(mostRecentLocation.search).toBe('?overRainbow=true')
})

it('will call the history instance\'s push method', () => {
const history = createMemoryHistory()
const q = qhistory(history, stringify, parse)

q.push({ pathname: '/somewhere', query: { overRainbow: true }})
expect(q.action).toBe('PUSH')
})
})

describe('replace', () => {
it('will set search string for location passed to actual history', () => {
const history = createMemoryHistory()
const q = qhistory(history, stringify, parse)

expect(q.length).toBe(1)
q.replace({ pathname: '/somewhere', query: { overRainbow: true }})
const mostRecentLocation = q.entries[q.entries.length-1]
expect(mostRecentLocation.search).toBe('?overRainbow=true')
})

it('will call the history instance\'s replace method', () => {
const history = createMemoryHistory()
const q = qhistory(history, stringify, parse)

expect(q.length).toBe(1)
q.replace({ pathname: '/somewhere', query: { overRainbow: true }})
// test length to ensure replace was called
expect(q.length).toBe(1)
expect(q.action).toBe('REPLACE')
})
})

describe('createHref', () => {
it('uses the query object when creating a path from location', () => {
const history = createMemoryHistory()
const q = qhistory(history, stringify, parse)
const location = {
pathname: '/neighborhood',
query: {
beautifulDay: true
}
}
expect(q.createHref(location)).toBe('/neighborhood?beautifulDay=true')
})
})
})

0 comments on commit 1b5dc78

Please sign in to comment.