Skip to content

Commit

Permalink
feat(fetchpaths): add tests for various paths (#575)
Browse files Browse the repository at this point in the history
* add tests for various paths

* chore: self mutation

Signed-off-by: github-actions <[email protected]>

---------

Signed-off-by: github-actions <[email protected]>
Co-authored-by: github-actions <[email protected]>
  • Loading branch information
charliehart-mondo and github-actions authored Mar 25, 2024
1 parent b2de20a commit 142b092
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
3 changes: 1 addition & 2 deletions docs/packages/mondo-fetch/classes/FetchClient.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 7 additions & 30 deletions packages/mondo-fetch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ export class FetchClient {
* @param baseUrl optional parameter that can be set when the client is created
* @returns A url
*/
private getUrl(providedUrl: string, baseUrl?: string): string {
return new URL(providedUrl, baseUrl).href
private getUrl(providedUrl: string): string {
return new URL(providedUrl, this.baseUrl).href

}

/**
Expand All @@ -30,13 +31,7 @@ export class FetchClient {
* @returns Promise<Result<T, FetchErrorTypes>>
*/
async get<T>(url: string, requestOptions?: RequestOptionsWithoutBody): Promise<Result<T, FetchErrorTypes>> {
const result = await this.createTimedFetchRequest<T>(this.getUrl(url, this.baseUrl), HttpMethods.GET, requestOptions)
if (result.isErrored) {
console.error(result.error.message)
return raiseFailure(result.error)
}

return raiseSuccess(result.data)
return this.createTimedFetchRequest<T>(this.getUrl(url), HttpMethods.GET, requestOptions)
}

/**
Expand All @@ -46,13 +41,7 @@ export class FetchClient {
* @returns Promise<Result<T, FetchErrorTypes>>
*/
public async post<T>(url: string, requestOptions?: RequestOptions): Promise<Result<T, FetchErrorTypes>> {
const result = await this.createTimedFetchRequest<T>(this.getUrl(url, this.baseUrl), HttpMethods.POST, requestOptions)
if (result.isErrored) {
console.error(result.error.message)
return raiseFailure(result.error)
}

return raiseSuccess(result.data)
return this.createTimedFetchRequest<T>(this.getUrl(url), HttpMethods.POST, requestOptions)
}

/**
Expand All @@ -62,13 +51,7 @@ export class FetchClient {
* @returns Promise<Result<T, FetchErrorTypes>>
*/
public async put<T>(url: string, requestOptions?: RequestOptions): Promise<Result<T, FetchErrorTypes>> {
const result = await this.createTimedFetchRequest<T>(this.getUrl(url, this.baseUrl), HttpMethods.PUT, requestOptions)
if (result.isErrored) {
console.error(result.error.message)
return raiseFailure(result.error)
}

return raiseSuccess(result.data)
return this.createTimedFetchRequest<T>(this.getUrl(url), HttpMethods.PUT, requestOptions)
}

/**
Expand All @@ -78,13 +61,7 @@ export class FetchClient {
* @returns Promise<Result<T, FetchErrorTypes>>
*/
public async delete<T>(url: string, requestOptions?: RequestOptionsWithoutBody): Promise<Result<T, FetchErrorTypes>> {
const result = await this.createTimedFetchRequest<T>(this.getUrl(url, this.baseUrl), HttpMethods.DELETE, requestOptions)
if (result.isErrored) {
console.error(result.error.message)
return raiseFailure(result.error)
}

return raiseSuccess(result.data)
return this.createTimedFetchRequest<T>(this.getUrl(url), HttpMethods.DELETE, requestOptions)
}

/**
Expand Down
36 changes: 35 additions & 1 deletion packages/mondo-fetch/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('#FetchClient', () => {
describe('getUrl', () => {
it('Returns a valid url when there is an invalid url due to extra slashes(/)', async () => {
// arrange
const client = new FetchClient({baseUrl: `${url}`})
const client = new FetchClient({baseUrl: url})
const providedUrl = '/stub/test'

const mock = generateFetchMock(stubData)
Expand Down Expand Up @@ -96,6 +96,40 @@ describe('#FetchClient', () => {
// assert
expect(fetchSpy).toHaveBeenCalledWith(expectedUrl, expect.anything())
})

it('Returns a url if there is a path in the base url provided', async () => {
// arrange
const client = new FetchClient({baseUrl: `${url}path/`})
const providedUrl = 'stub/test'

const mock = generateFetchMock(stubData)
fetchSpy.mockImplementation(mock)

const expectedUrl = `${url}path/stub/test`

// act
await client.get(providedUrl)

// assert
expect(fetchSpy).toHaveBeenCalledWith(expectedUrl, expect.anything())
})

it('Returns a url if there is a base url provided and a full url', async () => {
// arrange
const client = new FetchClient({baseUrl: `${url}/path`})
const providedUrl = 'https://stub.com/stub/test'

const mock = generateFetchMock(stubData)
fetchSpy.mockImplementation(mock)

const expectedUrl = providedUrl

// act
await client.get(providedUrl)

// assert
expect(fetchSpy).toHaveBeenCalledWith(expectedUrl, expect.anything())
})
})

describe('Requests', () => {
Expand Down

0 comments on commit 142b092

Please sign in to comment.