generated from Real-Dev-Squad/website-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* integrate url shortnening api * refactor code * refactor code amd add types * add toast for no logged in user * add test for generate url * revert package json changes * fix import's * fix: copy input box area and validate url entered by user * refactor code and write tests * remove comments * test: fix failing tests * test: fix failing tests * Rename ShortenUrl.ts to shortenUrl.ts * Update shortenUrl.test.ts * add base short url * remove random color from profile icon * make type to capital case * remove only from test --------- Co-authored-by: Sunny Sahsi <[email protected]>
- Loading branch information
1 parent
bf716a9
commit 1be31df
Showing
8 changed files
with
263 additions
and
113 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
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,53 @@ | ||
import { UserTypes } from '@/types/user.types'; | ||
import { userData } from '../../fixtures/users'; | ||
import { TINY_API_URL } from '@/constants/url'; | ||
import shortenUrl from '@/utils/shortenUrl'; | ||
|
||
describe('shortenUrl', () => { | ||
beforeEach(() => { | ||
global.fetch = jest.fn(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return the shortened URL when the API call is successful', async () => { | ||
const originalUrl = 'https://example.com/original'; | ||
|
||
const mockResponseData = { short_url: 'https://example.com/shortened' }; | ||
|
||
global.fetch.mockResolvedValue({ | ||
ok: true, | ||
json: async () => mockResponseData, | ||
headers: { | ||
get: () => 'application/json', | ||
}, | ||
}); | ||
|
||
const shortenedUrl = await shortenUrl(originalUrl, { ...userData.data } as UserTypes); | ||
|
||
expect(shortenedUrl).toBe('https://example.com/shortened'); | ||
|
||
expect(global.fetch).toHaveBeenCalledWith( | ||
expect.stringContaining(TINY_API_URL), | ||
expect.objectContaining({ | ||
method: 'POST', | ||
headers: expect.objectContaining({ | ||
'Content-Type': 'application/json', | ||
}), | ||
}) | ||
); | ||
}); | ||
|
||
it('should return null when the API call fails', async () => { | ||
const originalUrl = 'https://example.com/original'; | ||
|
||
global.fetch.mockResolvedValue({ | ||
ok: false, | ||
status: 500, | ||
statusText: 'Internal Server Error', | ||
}); | ||
|
||
const shortenedUrl = await shortenUrl(originalUrl, { ...userData.data } as UserTypes); | ||
|
||
expect(shortenedUrl).toBeNull(); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const TINY_API_URL = 'https://staging-tinysite-api.realdevsquad.com/v1'; | ||
export const TINY_API_GOOGLE_LOGIN = `${TINY_API_URL}/auth/google/login`; | ||
export const TINY_API_LOGOUT = `${TINY_API_URL}/auth/logout`; | ||
export const BASE_SHORT_URL = 'https://staging-tinysite.realdevsquad.com'; |
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
Oops, something went wrong.