DataFormat unit tests

This commit is contained in:
Ingo Schommer 2017-01-04 09:09:25 +13:00 committed by Damian Mooyman
parent b06e10c4a8
commit 8981861c76
2 changed files with 46 additions and 2 deletions

View File

@ -2,7 +2,11 @@ import i18n from 'i18n';
import qs from 'qs';
/**
* Merge existing querystring with new querystring object
* Merge existing querystring with new querystring object.
* Does not deep merge.
*
* Example:
* `urlQuery({ foo: 1 }, { bar: 2 })` returns `'?foo=1&bar=2'`
*
* @param {Object} locationQuery - Current browser querystring object (window.location.query)
* @param {Object} newQuery - New object to update. Set to null to clear instead of merge.
@ -26,7 +30,9 @@ export function urlQuery(locationQuery, newQuery) {
/**
* Turn flatterned querystring object into recursive nested objects,
* similarly to how PHP handles nested querystring objects.
* E.g. turn `{'query[val]': bob}` into `{query: { val: bob }}`
*
* Example:
* `decodeQuery('query[val]=bob')` returns `{query: { val: bob }}`
*
* @param {String} query - Querystring string
* @return {Object} - Unflattened query object

View File

@ -0,0 +1,38 @@
/* global jest, jasmine, describe, beforeEach, it, pit, expect, process */
jest.unmock('isomorphic-fetch');
jest.unmock('../DataFormat');
jest.unmock('qs');
jest.unmock('merge');
import { urlQuery, decodeQuery } from '../DataFormat';
describe('DataFormat', () => {
describe('urlQuery()', () => {
it('should return empty string when no newQuery is given', () => {
expect(urlQuery({}, null)).toBe('');
});
it('should add new keys', () => {
expect(urlQuery({ foo: 1 }, { bar: 2 })).toBe('?foo=1&bar=2');
});
it('should overwrite existing keys', () => {
expect(urlQuery({ foo: 1 }, { foo: 2 })).toBe('?foo=2');
});
it('should not deep merge nested keys', () => {
expect(urlQuery({ foo: { bar: 1 } }, { foo: { baz: 2 } })).toBe('?foo%5Bbaz%5D=2');
});
});
describe('decodeQuery', () => {
it('should decode flat keys', () => {
expect(decodeQuery('?foo=1')).toEqual({ foo: '1' });
});
it('should decode nested keys (PHP style)', () => {
expect(decodeQuery('?foo[bar]=1')).toEqual({ foo: { bar: '1' } });
});
});
});