Skip to content

Commit

Permalink
[Tests] adding a stringify test for a nested array of mixed objects a…
Browse files Browse the repository at this point in the history
…nd primitives.

Closes #153
  • Loading branch information
ljharb committed Feb 19, 2016
1 parent fb1c92e commit 0f8650b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions test/stringify.js
Expand Up @@ -21,7 +21,7 @@ test('stringify()', function (t) {
st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }), 'a%5Bb%5D%5Bc%5D%5Bd%5D=e');
st.end();
});

t.test('stringifies a nested object with dots notation', function (st) {
st.equal(qs.stringify({ a: { b: 'c' } }, { allowDots: true }), 'a.b=c');
st.equal(qs.stringify({ a: { b: { c: { d: 'e' } } } }, { allowDots: true }), 'a.b.c.d=e');
Expand Down Expand Up @@ -53,7 +53,7 @@ test('stringify()', function (t) {
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }), 'a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d');
st.end();
});

t.test('stringifies a nested array value with dots notation', function (st) {
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { allowDots: true, encode: false }), 'a.b[0]=c&a.b[1]=d');
st.end();
Expand All @@ -64,7 +64,12 @@ test('stringify()', function (t) {
st.equal(qs.stringify({ a: [{ b: { c: [1] } }] }), 'a%5B0%5D%5Bb%5D%5Bc%5D%5B0%5D=1');
st.end();
});


t.test('stringifies an array with mixed objects and primitives', function (st) {
st.equal(qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encode: false }), 'a[0][b]=1&a[1]=2&a[2]=3');
st.end();
});

t.test('stringifies an object inside an array with dots notation', function (st) {
st.equal(qs.stringify({ a: [{ b: 'c' }] }, { allowDots: true, encode: false }), 'a[0].b=c');
st.equal(qs.stringify({ a: [{ b: { c: [1] } }] }, { allowDots: true, encode: false }), 'a[0].b.c[0]=1');
Expand Down Expand Up @@ -249,7 +254,7 @@ test('stringify()', function (t) {
st.equal(qs.stringify({ a: 'c', z: { j: 'a', i: 'b' }, b: 'f' }, { sort: sort }), 'a=c&b=f&z%5Bi%5D=b&z%5Bj%5D=a');
st.end();
});

t.test('can sort the keys at depth 3 or more too', function (st) {
var sort = function (a, b) { return a.localeCompare(b); };
st.equal(qs.stringify({ a: 'a', z: { zj: {zjb: 'zjb', zja: 'zja'}, zi: {zib: 'zib', zia: 'zia'} }, b: 'b' }, { sort: sort, encode: false }), 'a=a&b=b&z[zi][zia]=zia&z[zi][zib]=zib&z[zj][zja]=zja&z[zj][zjb]=zjb');
Expand Down

0 comments on commit 0f8650b

Please sign in to comment.