-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
} | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) | ||
}) |