Skip to content

Commit

Permalink
Refactor sorting encoder decoder test
Browse files Browse the repository at this point in the history
  • Loading branch information
taro-28 committed Aug 27, 2024
1 parent 09e43bb commit 9aa6395
Showing 1 changed file with 130 additions and 184 deletions.
314 changes: 130 additions & 184 deletions packages/tanstack-table-search-params/src/encoder-decoder/sorting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,215 +12,161 @@ const customDefaultValue = [

describe("sorting", () => {
describe("encode", () =>
test.each<{
describe.each<{
name: string;
stateValue: Parameters<typeof encodeSorting>[0];
defaultValue: Parameters<typeof encodeSorting>[1];
want: ReturnType<typeof encodeSorting>;
}>([
{
name: "empty array(default value)",
stateValue: [],
name: "default default value",
defaultValue: defaultDefaultSorting,
want: undefined,
},
{
name: "non-empty array",
stateValue: [{ id: "foo", desc: true }],
defaultValue: defaultDefaultSorting,
want: "foo.desc",
},
{
name: "multiple items",
stateValue: [
{ id: "foo", desc: true },
{ id: "bar", desc: false },
],
defaultValue: defaultDefaultSorting,
want: "foo.desc,bar.asc",
},
{
name: "with custom default value: empty array",
stateValue: [],
defaultValue: customDefaultValue,
want: encodedEmptyStringForCustomDefaultValue,
},
{
name: "with custom default value: default value",
stateValue: customDefaultValue,
defaultValue: customDefaultValue,
want: undefined,
},
{
name: "with custom default value: non-empty array",
stateValue: [{ id: "foo", desc: true }],
defaultValue: customDefaultValue,
want: "foo.desc",
},
{
name: "with custom default value: multiple items",
stateValue: [
{ id: "foo", desc: true },
{ id: "bar", desc: false },
],
defaultValue: customDefaultValue,
want: "foo.desc,bar.asc",
},
])("$name", ({ stateValue, defaultValue, want }) =>
expect(encodeSorting(stateValue, defaultValue)).toEqual(want),
name: "with custom default value",
defaultValue: customDefaultValue,
},
])("default value: $name", ({ defaultValue }) =>
test.each<{
name: string;
stateValue: Parameters<typeof encodeSorting>[0];
want: ReturnType<typeof encodeSorting>;
}>([
{
name: "default value",
stateValue: defaultValue,
want: undefined,
},
{
name: "empty array",
stateValue: [],
want:
JSON.stringify(defaultValue) ===
JSON.stringify(defaultDefaultSorting)
? undefined
: encodedEmptyStringForCustomDefaultValue,
},
{
name: "non-empty array",
stateValue: [{ id: "foo", desc: true }],
want: "foo.desc",
},
{
name: "multiple items",
stateValue: [
{ id: "foo", desc: true },
{ id: "bar", desc: false },
],
want: "foo.desc,bar.asc",
},
])("$name", ({ stateValue, want }) =>
expect(encodeSorting(stateValue, defaultValue)).toEqual(want),
),
));

describe("decode", () =>
test.each<{
describe.each<{
name: string;
queryValue: Parameters<typeof decodeSorting>[0];
defaultValue: Parameters<typeof decodeSorting>[1];
want: ReturnType<typeof decodeSorting>;
}>([
{
name: "string",
queryValue: "foo.desc",
defaultValue: defaultDefaultSorting,
want: [{ id: "foo", desc: true }],
},
{
name: "string array",
queryValue: ["foo.desc"],
defaultValue: defaultDefaultSorting,
want: [],
},
{
name: "undefined",
queryValue: undefined,
defaultValue: defaultDefaultSorting,
want: [],
},
{
name: "empty string",
queryValue: "",
defaultValue: defaultDefaultSorting,
want: [],
},
{
name: "encodedEmptyStringForCustomDefaultValue",
queryValue: encodedEmptyStringForCustomDefaultValue,
name: "default default value",
defaultValue: defaultDefaultSorting,
want: [],
},
{
name: "invalid string",
queryValue: "foo",
defaultValue: defaultDefaultSorting,
want: [],
},
{
name: "invalid order",
queryValue: "foo.bar",
defaultValue: defaultDefaultSorting,
want: [],
},
{
name: "with custom default value: string",
queryValue: "foo.desc",
defaultValue: customDefaultValue,
want: [{ id: "foo", desc: true }],
},
{
name: "with custom default value: string array",
queryValue: ["foo.desc"],
defaultValue: customDefaultValue,
want: customDefaultValue,
},
{
name: "with custom default value: undefined",
queryValue: undefined,
defaultValue: customDefaultValue,
want: customDefaultValue,
},
{
name: "with custom default value: empty string",
queryValue: "",
defaultValue: customDefaultValue,
want: customDefaultValue,
},
{
name: "with custom default value: default value",
queryValue: `${customDefaultValue[0]?.id}.${customDefaultValue[0]?.desc ? "desc" : "asc"}`,
defaultValue: customDefaultValue,
want: customDefaultValue,
},
{
name: "with custom default value: encodedEmptyStringForCustomDefaultValue",
queryValue: encodedEmptyStringForCustomDefaultValue,
defaultValue: customDefaultValue,
want: [],
},
{
name: "with custom default value: invalid string",
queryValue: "foo",
defaultValue: customDefaultValue,
want: customDefaultValue,
},
{
name: "with custom default value: invalid order",
queryValue: "foo.bar",
defaultValue: customDefaultValue,
want: customDefaultValue,
},
])("$name", ({ queryValue, want, defaultValue }) =>
expect(decodeSorting(queryValue, defaultValue)).toEqual(want),
name: "with custom default value",
defaultValue: customDefaultValue,
},
])("default value: $name", ({ defaultValue }) =>
test.each<{
name: string;
queryValue: Parameters<typeof decodeSorting>[0];
want: ReturnType<typeof decodeSorting>;
}>([
{
name: "default value",
queryValue: encodeSorting(defaultValue, defaultValue),
want: defaultValue,
},
{
name: "empty string",
queryValue: "",
want: defaultValue,
},
{
name: "encodedEmptyStringForCustomDefaultValue",
queryValue: encodedEmptyStringForCustomDefaultValue,
want: [],
},
{
name: "string",
queryValue: "foo.desc",
want: [{ id: "foo", desc: true }],
},
{
name: "string array",
queryValue: ["foo.desc"],
want: defaultValue,
},
{
name: "undefined",
queryValue: undefined,
want: defaultValue,
},
{
name: "invalid string",
queryValue: "foo",
want: defaultValue,
},
{
name: "invalid order",
queryValue: "foo.bar",
want: defaultValue,
},
])("$name", ({ queryValue, want }) =>
expect(decodeSorting(queryValue, defaultValue)).toEqual(want),
),
));

describe("encode and decode", () =>
test.each<{
describe.each<{
name: string;
stateValue: Parameters<typeof encodeSorting>[0];
defaultValue: Parameters<typeof encodeSorting>[1];
}>([
{
name: "empty array(default value)",
stateValue: [],
defaultValue: defaultDefaultSorting,
},
{
name: "non-empty array",
stateValue: [{ id: "foo", desc: true }],
defaultValue: defaultDefaultSorting,
},
{
name: "multiple items",
stateValue: [
{ id: "foo", desc: true },
{ id: "bar", desc: false },
],
name: "default default value",
defaultValue: defaultDefaultSorting,
},
{
name: "with custom default value: empty array",
stateValue: [],
defaultValue: customDefaultValue,
},
{
name: "with custom default value: default value",
stateValue: customDefaultValue,
defaultValue: customDefaultValue,
},
{
name: "with custom default value: non-empty array",
stateValue: [{ id: "foo", desc: true }],
defaultValue: customDefaultValue,
},
{
name: "with custom default value: multiple items",
stateValue: [
{ id: "foo", desc: true },
{ id: "bar", desc: false },
],
defaultValue: customDefaultValue,
},
])("$name", ({ stateValue, defaultValue }) => {
expect(
decodeSorting(encodeSorting(stateValue, defaultValue), defaultValue),
).toEqual(stateValue);
}));
name: "with custom default value",
defaultValue: customDefaultValue,
},
])("default value: $name", ({ defaultValue }) =>
test.each<{
name: string;
stateValue: Parameters<typeof encodeSorting>[0];
}>([
{
name: "default value",
stateValue: defaultValue,
},
{
name: "empty array",
stateValue: [],
},
{
name: "non-empty array",
stateValue: [{ id: "foo", desc: true }],
},
{
name: "multiple items",
stateValue: [
{ id: "foo", desc: true },
{ id: "bar", desc: false },
],
},
])("$name", ({ stateValue }) => {
expect(
decodeSorting(encodeSorting(stateValue, defaultValue), defaultValue),
).toEqual(stateValue);
}),
));
});

0 comments on commit 9aa6395

Please sign in to comment.