-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from labcodes/feature/add-tests
Feature/add tests
- Loading branch information
Showing
13 changed files
with
411 additions
and
217 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 @@ | ||
repo_token: Bn0yjmlwMyda9F4v9czf1TI4nqoRdKSsa |
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 |
---|---|---|
|
@@ -53,6 +53,7 @@ typings/ | |
|
||
# Yarn Integrity file | ||
.yarn-integrity | ||
yarn.lock | ||
|
||
# dotenv environment variables file | ||
.env | ||
|
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,5 @@ | ||
language: node_js | ||
node_js: | ||
- "current" | ||
script: | ||
- npm run coveralls |
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,82 @@ | ||
import { fetchFromApi } from '../lib/api'; | ||
|
||
describe('fetchFromApi', () => { | ||
beforeEach(() => { | ||
fetch.resetMocks(); | ||
}); | ||
|
||
it('should resolve fetch and return data with error', async () => { | ||
fetch.mockResolvedValue({ | ||
status: 400, | ||
message: 'The name is empty' | ||
}); | ||
|
||
const requestData = { | ||
method: 'POST', | ||
body: { name: '' } | ||
}; | ||
try { | ||
await fetchFromApi('http://localhost:3000/products', requestData); | ||
} catch (e) { | ||
expect(e).toEqual({ | ||
status: 400, | ||
message: 'The name is empty' | ||
}); | ||
} | ||
}); | ||
|
||
it('should resolve fetch and return data', async () => { | ||
fetch.mockResolvedValue({ | ||
data: [{ name: 'Xbox' }] | ||
}); | ||
|
||
const response = await fetchFromApi('http://localhost:3000/products'); | ||
|
||
expect(response).toEqual({ | ||
data: [{ name: 'Xbox' }] | ||
}); | ||
}); | ||
|
||
it('should define application/json as default headers', async () => { | ||
fetch.mockResolvedValue({ | ||
data: [{ name: 'Xbox' }] | ||
}); | ||
let requestData = {}; | ||
|
||
const response = await fetchFromApi('http://localhost:3000/products', requestData); | ||
|
||
expect(requestData).toEqual({ | ||
headers: { | ||
'content-type': 'application/json' | ||
} | ||
}); | ||
}); | ||
|
||
it('should not overwrite content type if specified in requestData', async () => { | ||
fetch.mockResolvedValue({ | ||
data: [{ name: 'Xbox' }] | ||
}); | ||
let requestData = { | ||
headers: { | ||
'content-type': 'application/x-www-form-urlencoded' | ||
} | ||
}; | ||
|
||
const response = await fetchFromApi('http://localhost:3000/products', requestData); | ||
|
||
expect(requestData).toEqual({ | ||
headers: { | ||
'content-type': 'application/x-www-form-urlencoded' | ||
} | ||
}); | ||
}); | ||
|
||
it('should reject fetch and catch by error', async () => { | ||
fetch.mockRejectedValue(new Error('404 Not found')); | ||
try { | ||
await fetchFromApi('http://localhost:3000/products/999'); | ||
} catch (e) { | ||
expect(e).toEqual(new Error('404 Not found')); | ||
} | ||
}); | ||
}); |
Oops, something went wrong.