Skip to content

Commit

Permalink
✅ Better tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
elbywan committed Jul 3, 2022
1 parent 3a1f7b8 commit 77e85dc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
15 changes: 2 additions & 13 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ export interface Wretch<Self = unknown, Chain = unknown> {
*/
addon: <W, R>(addon: WretchAddon<W, R>) => W & Self & Wretch<Self & W, Chain & R>

/**
* Sets the default fetch options used for every subsequent fetch call.
* @param options - New default options
* @param replace - If true, replaces the existing options instead of mixing in
*/
defaults(this: Self & Wretch<Self, Chain>, options: WretchOptions, replace?: boolean): this

/**
* Sets the method (text, json ...) used to parse the data contained in the response body in case of an HTTP error.
*
Expand Down Expand Up @@ -206,19 +199,15 @@ export const core: Wretch = {
}
},

defaults(options, replace = false) {
errorType(errorType: string) {
return this.clone({
config: {
...this._config,
defaults: replace ? options : mix(this._config.defaults, options)
errorType
}
})
},

errorType(errorType: string) {
return this.clone({ errorType })
},

polyfills(polyfills, replace = false) {
return this.clone({
config: {
Expand Down
21 changes: 17 additions & 4 deletions test/node/wretch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ const duckImage = fs.readFileSync(duckImagePath)

describe("Wretch", function () {

beforeEach(() => {
wretch.defaults({}, true)
wretch.errorType("text")
})

it("should set and use non global polyfills", async function () {
global["FormData"] = null
global["URLSearchParams"] = null
Expand All @@ -73,7 +78,7 @@ describe("Wretch", function () {
performance,
PerformanceObserver,
AbortController
})
}, true)
})

it("should perform crud requests and parse a text response", async function () {
Expand Down Expand Up @@ -384,7 +389,7 @@ describe("Wretch", function () {
expect(check).toBe(3)
})

it("should set default fetch options", async function () {
it("should set global default fetch options", async function () {
let rejected = await new Promise(res => wretch(`${_URL}/customHeaders`).get().badRequest(_ => {
res(true)
}).res(result => res(!result)))
Expand Down Expand Up @@ -458,19 +463,27 @@ describe("Wretch", function () {
})

it("should change the parsing used in the default error handler", async function () {
// Local
await wretch(`${_URL}/json500`)
.errorType("json")
.get()
.internalError(error => { expect(error.json).toEqual({ error: 500, message: "ok" }) })
.res(_ => fail("I should never be called because an error was thrown"))
.then(_ => expect(_).toBe(undefined))
// Default (text)
await wretch(`${_URL}/json500`)
.get()
.internalError(error => { expect(error.text).toEqual(`{"error":500,"message":"ok"}`) })
.res(_ => fail("I should never be called because an error was thrown"))
.then(_ => expect(_).toBe(undefined))
// Global
wretch.errorType("json")
await wretch(`${_URL}/json500`)
.get()
.internalError(error => { expect(error.json).toEqual({ error: 500, message: "ok" }) })
.res(_ => fail("I should never be called because an error was thrown"))
.then(_ => expect(_).toBe(undefined))
// Change back
wretch.errorType("text")

})

it("should retrieve performance timings associated with a fetch request", function (done) {
Expand Down

0 comments on commit 77e85dc

Please sign in to comment.