From 9aa6395b6da78fbb4399c1d023de2dc6acb2d6ce Mon Sep 17 00:00:00 2001 From: taro-28 Date: Tue, 27 Aug 2024 23:18:17 +0900 Subject: [PATCH] Refactor sorting encoder decoder test --- .../src/encoder-decoder/sorting.test.ts | 314 ++++++++---------- 1 file changed, 130 insertions(+), 184 deletions(-) diff --git a/packages/tanstack-table-search-params/src/encoder-decoder/sorting.test.ts b/packages/tanstack-table-search-params/src/encoder-decoder/sorting.test.ts index f5c3ffd..45da67d 100644 --- a/packages/tanstack-table-search-params/src/encoder-decoder/sorting.test.ts +++ b/packages/tanstack-table-search-params/src/encoder-decoder/sorting.test.ts @@ -12,215 +12,161 @@ const customDefaultValue = [ describe("sorting", () => { describe("encode", () => - test.each<{ + describe.each<{ name: string; - stateValue: Parameters[0]; defaultValue: Parameters[1]; - want: ReturnType; }>([ { - 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[0]; + want: ReturnType; + }>([ + { + 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[0]; defaultValue: Parameters[1]; - want: ReturnType; }>([ { - 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[0]; + want: ReturnType; + }>([ + { + 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[0]; defaultValue: Parameters[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[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); + }), + )); });