|
| 1 | +import test from "ava" |
| 2 | + |
| 3 | +import { |
| 4 | + paramsSerializer, |
| 5 | + UnserializableParamError, |
| 6 | +} from "../src/lib/params-serializer" |
| 7 | + |
| 8 | +test("serializes empty object", (t) => { |
| 9 | + t.is(paramsSerializer({}), "") |
| 10 | +}) |
| 11 | + |
| 12 | +test("serializes string", (t) => { |
| 13 | + t.is(paramsSerializer({ foo: "d" }), "foo=d") |
| 14 | + t.is(paramsSerializer({ foo: "null" }), "foo=null") |
| 15 | + t.is(paramsSerializer({ foo: "undefined" }), "foo=undefined") |
| 16 | + t.is(paramsSerializer({ foo: "0" }), "foo=0") |
| 17 | +}) |
| 18 | + |
| 19 | +test("serializes number", (t) => { |
| 20 | + t.is(paramsSerializer({ foo: 1 }), "foo=1") |
| 21 | + t.is(paramsSerializer({ foo: 23.8 }), "foo=23.8") |
| 22 | +}) |
| 23 | + |
| 24 | +test("serializes boolean", (t) => { |
| 25 | + t.is(paramsSerializer({ foo: true }), "foo=true") |
| 26 | + t.is(paramsSerializer({ foo: false }), "foo=false") |
| 27 | +}) |
| 28 | + |
| 29 | +test("removes undefined params", (t) => { |
| 30 | + t.is(paramsSerializer({ bar: undefined }), "") |
| 31 | + t.is(paramsSerializer({ foo: 1, bar: undefined }), "foo=1") |
| 32 | +}) |
| 33 | + |
| 34 | +test("removes null params", (t) => { |
| 35 | + t.is(paramsSerializer({ bar: null }), "") |
| 36 | + t.is(paramsSerializer({ foo: 1, bar: null }), "foo=1") |
| 37 | +}) |
| 38 | + |
| 39 | +test("serializes empty array params", (t) => { |
| 40 | + t.is(paramsSerializer({ bar: [] }), "bar=") |
| 41 | + t.is(paramsSerializer({ foo: 1, bar: [] }), "bar=&foo=1") |
| 42 | +}) |
| 43 | + |
| 44 | +test("serializes array params with one value", (t) => { |
| 45 | + t.is(paramsSerializer({ bar: ["a"] }), "bar=a") |
| 46 | + t.is(paramsSerializer({ foo: 1, bar: ["a"] }), "bar=a&foo=1") |
| 47 | +}) |
| 48 | + |
| 49 | +test("serializes array params with many values", (t) => { |
| 50 | + t.is(paramsSerializer({ foo: 1, bar: ["a", "2"] }), "bar=a&bar=2&foo=1") |
| 51 | + t.is( |
| 52 | + paramsSerializer({ foo: 1, bar: ["null", "2", "undefined"] }), |
| 53 | + "bar=null&bar=2&bar=undefined&foo=1" |
| 54 | + ) |
| 55 | + t.is(paramsSerializer({ foo: 1, bar: ["", "", ""] }), "bar=&bar=&bar=&foo=1") |
| 56 | + t.is( |
| 57 | + paramsSerializer({ foo: 1, bar: ["", "a", "2"] }), |
| 58 | + "bar=&bar=a&bar=2&foo=1" |
| 59 | + ) |
| 60 | + t.is( |
| 61 | + paramsSerializer({ foo: 1, bar: ["", "a", ""] }), |
| 62 | + "bar=&bar=a&bar=&foo=1" |
| 63 | + ) |
| 64 | +}) |
| 65 | + |
| 66 | +test("cannot serialize single element array params with empty string", (t) => { |
| 67 | + t.throws(() => paramsSerializer({ foo: [""] }), { |
| 68 | + instanceOf: UnserializableParamError, |
| 69 | + }) |
| 70 | +}) |
| 71 | + |
| 72 | +test("cannot serialize unserializable values", (t) => { |
| 73 | + t.throws(() => paramsSerializer({ foo: {} }), { |
| 74 | + instanceOf: UnserializableParamError, |
| 75 | + }) |
| 76 | + t.throws(() => paramsSerializer({ foo: { x: 2 } }), { |
| 77 | + instanceOf: UnserializableParamError, |
| 78 | + }) |
| 79 | + t.throws(() => paramsSerializer({ foo: () => {} }), { |
| 80 | + instanceOf: UnserializableParamError, |
| 81 | + }) |
| 82 | +}) |
| 83 | + |
| 84 | +test("cannot serialize array params with unserializable values", (t) => { |
| 85 | + t.throws(() => paramsSerializer({ bar: ["a", null] }), { |
| 86 | + instanceOf: UnserializableParamError, |
| 87 | + }) |
| 88 | + t.throws(() => paramsSerializer({ bar: ["a", undefined] }), { |
| 89 | + instanceOf: UnserializableParamError, |
| 90 | + }) |
| 91 | + t.throws(() => paramsSerializer({ bar: ["a", ["s"]] }), { |
| 92 | + instanceOf: UnserializableParamError, |
| 93 | + }) |
| 94 | + t.throws(() => paramsSerializer({ bar: ["a", []] }), { |
| 95 | + instanceOf: UnserializableParamError, |
| 96 | + }) |
| 97 | + t.throws(() => paramsSerializer({ bar: ["a", {}] }), { |
| 98 | + instanceOf: UnserializableParamError, |
| 99 | + }) |
| 100 | + t.throws(() => paramsSerializer({ bar: ["a", { x: 2 }] }), { |
| 101 | + instanceOf: UnserializableParamError, |
| 102 | + }) |
| 103 | + t.throws(() => paramsSerializer({ bar: ["a", () => {}] }), { |
| 104 | + instanceOf: UnserializableParamError, |
| 105 | + }) |
| 106 | +}) |
0 commit comments