-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refractor: improve general code quality (#13)
* refractor: remove duplicate type definition * chore(types): add foreign key fields to types * refractor: remove unused dependency * refractor: remove unused module declaration * feat: add mock for request utility * chore(tests): add test for utilities and main class * chore(publish): bump package version to 1.0.4
- Loading branch information
1 parent
bc815c6
commit c1d3f90
Showing
10 changed files
with
142 additions
and
17 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,21 @@ | ||
import { mock } from "bun:test"; | ||
|
||
import buildQuery from "utils/query-generator"; | ||
import type { APIRequestInit, Entity } from "types/api-entities"; | ||
|
||
mock.module("utils/request", () => ({ | ||
request: async <T extends Entity = any>( | ||
_url: any, | ||
_options: APIRequestInit<T> = {}, | ||
) => { | ||
return { | ||
ok: true, | ||
url: `${_url}${ | ||
_options.query && Object.keys(_options.query).length | ||
? `?${buildQuery(_options.query)}` | ||
: "" | ||
}`, | ||
json: async () => ({ data: null, error: null, pagination: null }), | ||
}; | ||
}, | ||
})); |
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,9 @@ | ||
import "__mocks__/utils/request"; | ||
|
||
import HellHub from "index"; | ||
import { it } from "bun:test"; | ||
|
||
it("Main class correctly infers types", async () => { | ||
const response = await HellHub.planets(1); | ||
const { data: _ } = await response.json(); | ||
}); |
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,13 @@ | ||
import { it, expect } from "bun:test"; | ||
|
||
import buildQuery from "utils/query-generator"; | ||
import type { Planet } from "types/api-entities"; | ||
|
||
it("Query generator works as expected", () => { | ||
const query = buildQuery<Planet[]>({ | ||
limit: 10, | ||
filters: { name: { $contains: "earth" } }, | ||
}); | ||
|
||
expect(query).toBe("limit=10&filters[name][contains]=earth"); | ||
}); |
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,52 @@ | ||
import "__mocks__/utils/request"; | ||
|
||
import { | ||
generateDynamicRequestFn, | ||
generateSingleRequestFn, | ||
} from "utils/request-generator"; | ||
|
||
import { it, expect } from "bun:test"; | ||
import type { Planet } from "types/api-entities"; | ||
|
||
it("SingleRequestFn works as expected", async () => { | ||
const singleRequest = generateSingleRequestFn<Planet>("https://example.com"); | ||
|
||
const response = await singleRequest({ | ||
query: { | ||
limit: 10, | ||
filters: { name: { $contains: "earth" } }, | ||
}, | ||
}); | ||
|
||
expect(response.ok).toBe(true); | ||
|
||
expect(response.url).toBe( | ||
"https://example.com?limit=10&filters[name][contains]=earth", | ||
); | ||
|
||
const json = await response.json(); | ||
expect(json).toHaveProperty("data", null); | ||
expect(json).toHaveProperty("error", null); | ||
}); | ||
|
||
it("DynamicRequestFn works as expected", async () => { | ||
const dynamicRequest = generateDynamicRequestFn<Planet>( | ||
"https://example.com", | ||
); | ||
|
||
const response = await dynamicRequest({ | ||
limit: 10, | ||
filters: { name: { $contains: "earth" } }, | ||
}); | ||
|
||
expect(response.ok).toBe(true); | ||
|
||
expect(response.url).toBe( | ||
"https://example.com?limit=10&filters[name][contains]=earth", | ||
); | ||
|
||
const json = await response.json(); | ||
expect(json).toHaveProperty("data", null); | ||
expect(json).toHaveProperty("error", null); | ||
expect(json).toHaveProperty("pagination", null); | ||
}); |
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,26 @@ | ||
import "__mocks__/utils/request"; | ||
|
||
import { it, expect } from "bun:test"; | ||
import { request } from "utils/request"; | ||
import type { Planet } from "types/api-entities"; | ||
|
||
it("Request util works as expected", async () => { | ||
const response = await request<Planet[]>("https://example.com", { | ||
query: { | ||
limit: 10, | ||
filters: { name: { $contains: "earth" } }, | ||
}, | ||
}); | ||
|
||
expect(response.ok).toBe(true); | ||
|
||
expect(response.url).toBe( | ||
"https://example.com?limit=10&filters[name][contains]=earth", | ||
); | ||
|
||
expect(await response.json()).toEqual({ | ||
data: null, | ||
error: null, | ||
pagination: null as any, | ||
}); | ||
}); |
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
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
This file was deleted.
Oops, something went wrong.
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