-
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.
Merge pull request #22 from austin1237/local-jobApi
local script now uses job api instead of writing to a file.
- Loading branch information
Showing
7 changed files
with
222 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,27 @@ | ||
import axios from 'axios'; | ||
|
||
export interface Job { | ||
title: string; | ||
company: string; | ||
keyword: string; | ||
link: string; | ||
} | ||
|
||
export interface Response { | ||
total: number; | ||
uncached: number; | ||
duplicates: number; | ||
} | ||
|
||
export class JobService { | ||
private endpoint: string; | ||
|
||
constructor(endpoint: string) { | ||
this.endpoint = endpoint; | ||
} | ||
|
||
async sendJobs(jobs: Job[]): Promise<Response> { | ||
const response = await axios.post(this.endpoint, { jobs }); | ||
return response.data as Response; | ||
} | ||
} |
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,43 @@ | ||
import axios from 'axios'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import { JobService, Job, Response } from '../src/jobService'; | ||
|
||
describe('JobService', () => { | ||
let mockAxios: MockAdapter; | ||
let jobService: JobService; | ||
|
||
beforeEach(() => { | ||
mockAxios = new MockAdapter(axios); | ||
jobService = new JobService('http://mock-endpoint.com'); | ||
}); | ||
|
||
afterEach(() => { | ||
mockAxios.reset(); | ||
}); | ||
|
||
it('should send jobs and return the response', async () => { | ||
const jobs: Job[] = [ | ||
{ title: 'Software Engineer', company: 'Company1', keyword: 'Go', link: 'http://example.com/job1' }, | ||
{ title: 'Data Analyst', company: 'Company2', keyword: 'Python', link: 'http://example.com/job2' }, | ||
{ title: 'Financial Advisor', company: 'Company3', keyword: 'Finance', link: 'http://example.com/job3' }, | ||
]; | ||
|
||
const expectedResponse: Response = { total: 3, uncached: 1, duplicates: 2 }; | ||
|
||
mockAxios.onPost('http://mock-endpoint.com').reply(200, expectedResponse); | ||
|
||
const response = await jobService.sendJobs(jobs); | ||
|
||
expect(response).toEqual(expectedResponse); | ||
}); | ||
|
||
it('should throw an error if the request fails', async () => { | ||
const jobs: Job[] = [ | ||
{ title: 'Software Engineer', company: 'Company1', keyword: 'Go', link: 'http://example.com/job1' }, | ||
]; | ||
|
||
mockAxios.onPost('http://mock-endpoint.com').networkError(); | ||
|
||
await expect(jobService.sendJobs(jobs)).rejects.toThrow('Network Error'); | ||
}); | ||
}); |