From 99c293b7d081d6ed6d5cf1a2e6abe970bf1af2ca Mon Sep 17 00:00:00 2001 From: Micah Thomas Date: Mon, 9 Dec 2024 19:19:17 -0800 Subject: [PATCH] test: added test to show how old queryParse is not necessary --- apps/storefront/tests/shared/request.test.ts | 54 ++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 apps/storefront/tests/shared/request.test.ts diff --git a/apps/storefront/tests/shared/request.test.ts b/apps/storefront/tests/shared/request.test.ts new file mode 100644 index 00000000..ba3e7d60 --- /dev/null +++ b/apps/storefront/tests/shared/request.test.ts @@ -0,0 +1,54 @@ +import { RequestType } from '@/shared/service/request/base'; + +// TODO: Old logic - remove +const queryParse = (query: T): string => { + let queryText = ''; + + Object.keys(query || {}).forEach((key: string) => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + queryText += `${key}=${(query as any)[key]}&`; + }); + return queryText.slice(0, -1); +}; + +describe('queryParse', () => { + it('should stringify objects', () => { + const params = { + foo: 'bar', + hello: 'world', + }; + const output = queryParse(params); + + expect(output).toMatchInlineSnapshot(`"foo=bar&hello=world"`); + + const newLogic = new URLSearchParams(params); + + expect(newLogic.toString()).toMatchInlineSnapshot(`"foo=bar&hello=world"`); + + expect(output).toMatch(newLogic.toString()); + }); +}); + +describe('enum should use typeof keyof for function argument type', () => { + it('should accept enum so that types are checked properly', () => { + const validFunction = (type: RequestType) => { + if (type === RequestType.B2BGraphql) { + return true; + } + return false; + }; + + const alsoValidFunction = (type: keyof typeof RequestType) => { + if (type === RequestType.B2BGraphql) { + return true; + } + return false; + }; + + expect(validFunction(RequestType.B2BGraphql)).toBe(true); + expect(alsoValidFunction(RequestType.B2BGraphql)).toBe(true); + // @ts-expect-error typescript should complain but javascript output should still work + expect(validFunction('B2BGraphql')).toBe(true); + expect(alsoValidFunction('B2BGraphql')).toBe(true); + }); +});