-
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.
Added tests, added STOP_WHEN_FOUND as environment variable to either …
…stop ar continue the script
- Loading branch information
Showing
10 changed files
with
3,255 additions
and
111 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,6 @@ | ||
module.exports = { | ||
presets: [ | ||
'@babel/preset-env', | ||
'@babel/preset-typescript', | ||
], | ||
}; |
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,100 @@ | ||
import axios from 'axios'; | ||
import moment from 'moment'; | ||
import {fetchAvailabilities, availableAppointment, sendSlackNotification} from './index'; | ||
|
||
jest.mock('axios'); | ||
|
||
// Mock the environment variables | ||
let originalEnv: NodeJS.ProcessEnv; | ||
|
||
beforeEach(() => { | ||
// Store the original environment variables | ||
originalEnv = { ...process.env }; | ||
|
||
// Override environment variables for test | ||
process.env.APPOINTMENT_URL = 'http://test.com/availabilities.json?start_date=2020-01-01'; | ||
process.env.TIMESPAN_DAYS = '7'; | ||
process.env.SCHEDULE = '* * * * *'; | ||
process.env.SLACK_WEBHOOK_URL = 'http://test.com/slack-webhook-url'; | ||
process.env.DOCTOR_BOOKING_URL = 'http://test.com/doctor-booking-url'; | ||
}); | ||
|
||
afterEach(() => { | ||
// Restore the original environment variables | ||
process.env = originalEnv; | ||
}); | ||
|
||
describe("fetchAvailabilities", () => { | ||
test("Fetches availabilities from API and returns available dates", async () => { | ||
// setup | ||
const expectedDate = moment().add(1, 'days').format('YYYY-MM-DD'); | ||
const url = 'http://test.com/availabilities.json?start_date=2020-01-01'; | ||
|
||
(axios.get as jest.MockedFunction<typeof axios.get>).mockResolvedValueOnce({ | ||
data: { next_slot: expectedDate } | ||
}); | ||
|
||
const today = moment().format('YYYY-MM-DD'); | ||
const replacedUrl = url.replace(/start_date=\d{4}-\d{2}-\d{2}/, `start_date=${today}`); | ||
|
||
// work | ||
const dates = await fetchAvailabilities(); | ||
|
||
// expect | ||
expect(dates).toEqual([expectedDate]); | ||
expect(axios.get).toHaveBeenCalledWith(replacedUrl); | ||
expect(axios.get).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe("availableAppointment", () => { | ||
test("Checks available dates and returns an appointment within timespan", async () => { | ||
// setup | ||
const now = moment().format('YYYY-MM-DD'); | ||
const futureDate = moment().add(3, 'days').format('YYYY-MM-DD'); | ||
const dates = [now, futureDate]; | ||
|
||
// work | ||
const date = await availableAppointment(dates, 3); | ||
|
||
// expect | ||
expect(date).toEqual(now); | ||
}); | ||
|
||
test("Returns empty string when no appointments are available", async () => { | ||
// setup | ||
const futureDate = moment().add(5, 'days').format('YYYY-MM-DD'); | ||
const dates = [futureDate]; | ||
|
||
// work | ||
const date = await availableAppointment(dates, 3); | ||
|
||
// expect | ||
expect(date).toEqual(''); | ||
}); | ||
}); | ||
|
||
describe("sendSlackNotification", () => { | ||
test("Sends a notification to Slack", async () => { | ||
// setup | ||
const date = moment().format('YYYY-MM-DD'); | ||
const message = { | ||
blocks: [ | ||
{ | ||
type: "section", | ||
text: { | ||
type: "mrkdwn", | ||
text: `:pill: An appointment is available on *${date}* :calendar:. You can book it here: http://test.com/doctor-booking-url` | ||
} | ||
}, | ||
] | ||
}; | ||
|
||
// work | ||
await sendSlackNotification(date); | ||
|
||
// expect | ||
expect(axios.post).toHaveBeenCalledTimes(1); | ||
expect(axios.post).toHaveBeenCalledWith('http://test.com/slack-webhook-url', message); | ||
}); | ||
}); |
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,15 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
transform: { | ||
"^.+\\.ts?$": "ts-jest" | ||
}, | ||
moduleFileExtensions: [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"jsx", | ||
"json", | ||
"node" | ||
], | ||
}; |
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,4 @@ | ||
export default { | ||
get: jest.fn(() => Promise.resolve({ data: {} })), | ||
post: jest.fn(() => Promise.resolve({ data: {} })), | ||
}; |
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,6 +1,6 @@ | ||
{ | ||
"name": "doctolib-appointment-finder", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"dependencies": { | ||
"@types/node": "^20.2.5", | ||
"@types/node-cron": "^3.0.7", | ||
|
@@ -12,9 +12,18 @@ | |
"typescript": "^5.0.4" | ||
}, | ||
"scripts": { | ||
"start": "ts-node index.ts" | ||
"start": "ts-node index.ts", | ||
"test": "jest" | ||
}, | ||
"main": "index.js", | ||
"author": "René Kann <[email protected]>", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@babel/preset-env": "^7.22.4", | ||
"@babel/preset-typescript": "^7.21.5", | ||
"@types/jest": "^29.5.2", | ||
"jest": "^29.5.0", | ||
"jest-mock-axios": "^4.7.2", | ||
"ts-jest": "^29.1.0" | ||
} | ||
} |
Oops, something went wrong.