-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added test to show how old queryParse is not necessary
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |