-
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.
fix: rejected promise on Klarna error
- Loading branch information
Showing
9 changed files
with
360 additions
and
196 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
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,16 @@ | ||
import { parseJSON } from '../json'; | ||
|
||
describe('json utils', () => { | ||
describe('parseJSON', () => { | ||
it('should return object for valid JSON string', () => { | ||
expect(parseJSON('{"test": [1, true, "hello", null]}')).toEqual({ | ||
test: [1, true, 'hello', null], | ||
}); | ||
}); | ||
|
||
it('should return null for invalid JSON string', () => { | ||
expect(parseJSON('')).toEqual(null); | ||
expect(parseJSON('test')).toEqual(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 @@ | ||
export { parseJSON } from './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,7 @@ | ||
export function parseJSON(jsonString: string): any | null { | ||
try { | ||
return JSON.parse(jsonString); | ||
} catch { | ||
return 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,57 @@ | ||
import { HttpRequest } from '../src/http-request'; | ||
import nock from 'nock'; | ||
|
||
describe('HttpRequest', () => { | ||
const httpRequest = new HttpRequest({ | ||
authorization: 'none', | ||
apiEndpoint: 'example.com', | ||
}); | ||
|
||
describe('invoke', () => { | ||
it('should not reject promise when there is an error about wrong hostname', async () => { | ||
const originalError = new Error('Test'); | ||
nock('https://example.com') | ||
.get('/test') | ||
.replyWithError(originalError); | ||
|
||
// Test should not throw error | ||
const { statusCode, error, response } = await httpRequest.invoke( | ||
'GET', | ||
'/test' | ||
); | ||
expect(statusCode).toEqual(500); | ||
expect(error).toEqual(originalError); | ||
expect(response).toEqual(undefined); | ||
}); | ||
|
||
it('should not parse non JSON response', async () => { | ||
nock('https://example.com') | ||
.get('/test') | ||
.reply(200, 'hello'); | ||
|
||
// Test should not throw error | ||
const { statusCode, error, response } = await httpRequest.invoke( | ||
'GET', | ||
'/test' | ||
); | ||
expect(statusCode).toEqual(200); | ||
expect(error).toEqual(undefined); | ||
expect(response).toEqual('hello'); | ||
}); | ||
|
||
it('should return not successful response as error', async () => { | ||
nock('https://example.com') | ||
.get('/test') | ||
.reply(404, { notFound: true }); | ||
|
||
// Test should not throw error | ||
const { statusCode, error, response } = await httpRequest.invoke( | ||
'GET', | ||
'/test' | ||
); | ||
expect(statusCode).toEqual(404); | ||
expect(error).toEqual({ notFound: true }); | ||
expect(response).toEqual(undefined); | ||
}); | ||
}); | ||
}); |
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.