Skip to content

Commit

Permalink
test: added test to show how old queryParse is not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-micah committed Dec 10, 2024
1 parent a198f6d commit 99c293b
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions apps/storefront/tests/shared/request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { RequestType } from '@/shared/service/request/base';

// TODO: Old logic - remove
const queryParse = <T>(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);
});
});

0 comments on commit 99c293b

Please sign in to comment.