Skip to content
This repository has been archived by the owner on Feb 20, 2022. It is now read-only.

Commit

Permalink
✅ Setup jest (& mocks) and test for getStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
thibmaek committed Nov 24, 2016
1 parent cce345c commit f15398e
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 114 deletions.
6 changes: 3 additions & 3 deletions __mocks__/composedFetch.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as res from './res_pp.json';
import * as res from './res_postal_point.json';

const composedFetch = id => {
return new Promise((resolve, reject) => {
process.nextTick(() =>
res[status] === `active`
id === `323267009691651472`
? resolve(res)
: reject({
error: `no active step found for ${id}`,
error: `Incorrect id with of value ${id} passed`,
})
);
});
Expand Down
93 changes: 0 additions & 93 deletions __mocks__/res_direct.json

This file was deleted.

1 change: 1 addition & 0 deletions __mocks__/res_direct_absent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"items":[{"key":{"id":776158581,"version":10,"primaryKey":"776158581-1479649399532","created":1479649399532},"services":["9000002000750"],"events":[{"key":"undelivered.advised-addressee_absent_message_left","location":{"fr":"ANTWERPEN DAM MAIL","nl":"ANTWERPEN DAM MAIL","en":"ANTWERPEN DAM MAIL"},"date":"2016-11-22","time":"12:28","irregularity":false},{"key":"taken-on-round-out_for_distribution","location":{"fr":"ANTWERPEN DAM MAIL","nl":"ANTWERPEN DAM MAIL","en":"ANTWERPEN DAM MAIL"},"date":"2016-11-22","time":"07:23","irregularity":false},{"key":"sorted-sorted_out","location":{"fr":"Antwerpen X Parcel Sorter","nl":"Antwerpen X Parcel Sorter","en":"Antwerpen X Parcel Sorter"},"date":"2016-11-21","time":"18:21","irregularity":false},{"key":"N-SENT-OTHER","date":"2016-11-21","time":"10:30","irregularity":false},{"key":"received.domestic-corner","location":{"fr":"PP OCTA+ SINT KRUIS","nl":"PP OCTA+ SINT KRUIS","en":"PP OCTA+ SINT KRUIS"},"date":"2016-11-21","time":"10:18","irregularity":false},{"key":"announcement.normal-announced_parcel_by_customer","location":{"fr":"","nl":"","en":""},"date":"2016-11-20","time":"14:43","irregularity":false}],"itemCode":"323267009999171004","sender":{"countryCode":"BE","postcode":"8310","municipality":"SINT-KRUIS"},"receiver":{"name":"DE CALUWE DURVASA","countryCode":"BE","postcode":"2060","municipality":"ANTWERPEN"},"customerReference":"1493998-12","deliveryPoint":{"name":{"fr":"POSTPUNT Q8 ANTWERPEN","nl":"POSTPUNT Q8 ANTWERPEN","en":"POSTPUNT Q8 ANTWERPEN"},"id":"806480","type":"POSTAL_POINT","street":{"fr":"Schijnpoortweg","nl":"Schijnpoortweg","en":"Schijnpoortweg"},"streetNumber":{"fr":"18","nl":"18","en":"18"},"postcode":{"fr":"2060","nl":"2060","en":"2060"},"openingSchedules":[{"earliestOpeningHour":"06:00","latestClosingHour":"22:00","openingHours":[{"open":"06:00","close":"22:00"}],"dayOfTheWeek":"MONDAY"},{"openingHours":[],"dayOfTheWeek":"TUESDAY"},{"earliestOpeningHour":"06:00","latestClosingHour":"22:00","openingHours":[{"open":"06:00","close":"22:00"}],"dayOfTheWeek":"WEDNESDAY"},{"earliestOpeningHour":"06:00","latestClosingHour":"22:00","openingHours":[{"open":"06:00","close":"22:00"}],"dayOfTheWeek":"THURSDAY"},{"earliestOpeningHour":"06:00","latestClosingHour":"22:00","openingHours":[{"open":"06:00","close":"22:00"}],"dayOfTheWeek":"FRIDAY"},{"earliestOpeningHour":"06:00","latestClosingHour":"22:00","openingHours":[{"open":"06:00","close":"22:00"}],"dayOfTheWeek":"SATURDAY"},{"earliestOpeningHour":"06:00","latestClosingHour":"22:00","openingHours":[{"open":"06:00","close":"22:00"}],"dayOfTheWeek":"SUNDAY"}],"longitude":4.43789,"latitude":51.22692,"municipality":{"fr":"Antwerpen","nl":"Antwerpen","en":"Antwerpen"},"showMap":true},"weightInGrams":480,"product":"2000000191034","productCategory":"parcel","processOverview":{"processSteps":[{"name":"prepare","status":"completed","label":{"main":"preparation","detail":"bySender"},"knownProcessStep":"IN_PREPARATION"},{"name":"processing","status":"completed","label":{"main":"processing","detail":"byBpost"},"knownProcessStep":"PROCESSING"},{"name":"out_for_delivery","status":"active","label":{"main":"onTheWay","detail":"toPostPoint"},"knownProcessStep":"ON_THE_WAY_TO_POST_POINT"},{"name":"post_point","status":"upcoming","label":{"main":"available","detail":"inPostPoint"},"knownProcessStep":"AVAILABLE_IN_POST_POINT_INTERNATIONAL"},{"name":"delivered","status":"upcoming","label":{"main":"pickedUp","detail":"inPostPoint"},"knownProcessStep":"PICKED_UP_IN_POST_POINT_INTERNATIONAL"}],"activeStepTextKey":"OUT_FOR_DELIVERY_POST_POINT","textKey":"step.text.out_for_delivery.post_point","alert":false},"contactForMoreInformation":"BPOST","signatureViewType":"NONE","requestedDeliveryMethod":"HOME","inNetworkDate":"2016-11-21","earliestAvailableTime":{"time":"11:00","day":"2016-11-23"},"latestAvailableTime":{"time":"22:00","day":"2016-12-07"},"attemptedDeliveryTime":{"time":"12:28","day":"2016-11-22"},"faqs":[{"id":"faqid_2","title":{"fr":"Quid si je ne suis pas chez moi lorsque le facteur vient livrer mon paquet ?","nl":"Wat als ik niet thuis ben als de postbode mijn pakje komt afleveren?","en":"What if I am not home when the parcel is delivered?"}},{"id":"faqid_4","title":{"fr":"Comment puis-je demander une deuxième présentation ?","nl":"Hoe kan ik een tweede aanbieding aanvragen?","en":"How do I request a second delivery attempt?"}},{"id":"faqid_33","title":{"fr":"Où est mon colis ?","nl":"Waar is mijn pakje?","en":"Where is my parcel?"}}],"retourOrBackToSender":false}]}
1 change: 1 addition & 0 deletions __mocks__/res_direct_delivered.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"items":[{"key":{"id":776158581,"version":12,"primaryKey":"776158581-1479649399532","created":1479649399532},"services":["9000002000750"],"events":[{"key":"distribution.normal-regular","location":{"fr":"PP Q8 ANTWERPEN","nl":"PP Q8 ANTWERPEN","en":"PP Q8 ANTWERPEN"},"date":"2016-11-23","time":"13:37","irregularity":false},{"key":"arrival.pickup-point-drop_off_postman","location":{"fr":"PP Q8 ANTWERPEN","nl":"PP Q8 ANTWERPEN","en":"PP Q8 ANTWERPEN"},"date":"2016-11-23","time":"09:10","irregularity":false},{"key":"undelivered.advised-addressee_absent_message_left","location":{"fr":"ANTWERPEN DAM MAIL","nl":"ANTWERPEN DAM MAIL","en":"ANTWERPEN DAM MAIL"},"date":"2016-11-22","time":"12:28","irregularity":false},{"key":"taken-on-round-out_for_distribution","location":{"fr":"ANTWERPEN DAM MAIL","nl":"ANTWERPEN DAM MAIL","en":"ANTWERPEN DAM MAIL"},"date":"2016-11-22","time":"07:23","irregularity":false},{"key":"sorted-sorted_out","location":{"fr":"Antwerpen X Parcel Sorter","nl":"Antwerpen X Parcel Sorter","en":"Antwerpen X Parcel Sorter"},"date":"2016-11-21","time":"18:21","irregularity":false},{"key":"N-SENT-OTHER","date":"2016-11-21","time":"10:30","irregularity":false},{"key":"received.domestic-corner","location":{"fr":"PP OCTA+ SINT KRUIS","nl":"PP OCTA+ SINT KRUIS","en":"PP OCTA+ SINT KRUIS"},"date":"2016-11-21","time":"10:18","irregularity":false},{"key":"announcement.normal-announced_parcel_by_customer","location":{"fr":"","nl":"","en":""},"date":"2016-11-20","time":"14:43","irregularity":false}],"itemCode":"323267009999171004","sender":{"countryCode":"BE","postcode":"8310","municipality":"SINT-KRUIS"},"receiver":{"name":"DE CALUWE DURVASA","countryCode":"BE","postcode":"2060","municipality":"ANTWERPEN"},"customerReference":"1493998-12","deliveryPoint":{"name":{"fr":"POSTPUNT Q8 ANTWERPEN","nl":"POSTPUNT Q8 ANTWERPEN","en":"POSTPUNT Q8 ANTWERPEN"},"id":"806480","type":"POSTAL_POINT","street":{"fr":"Schijnpoortweg","nl":"Schijnpoortweg","en":"Schijnpoortweg"},"streetNumber":{"fr":"18","nl":"18","en":"18"},"postcode":{"fr":"2060","nl":"2060","en":"2060"},"openingSchedules":[{"earliestOpeningHour":"06:00","latestClosingHour":"22:00","openingHours":[{"open":"06:00","close":"22:00"}],"dayOfTheWeek":"MONDAY"},{"openingHours":[],"dayOfTheWeek":"TUESDAY"},{"earliestOpeningHour":"06:00","latestClosingHour":"22:00","openingHours":[{"open":"06:00","close":"22:00"}],"dayOfTheWeek":"WEDNESDAY"},{"earliestOpeningHour":"06:00","latestClosingHour":"22:00","openingHours":[{"open":"06:00","close":"22:00"}],"dayOfTheWeek":"THURSDAY"},{"earliestOpeningHour":"06:00","latestClosingHour":"22:00","openingHours":[{"open":"06:00","close":"22:00"}],"dayOfTheWeek":"FRIDAY"},{"earliestOpeningHour":"06:00","latestClosingHour":"22:00","openingHours":[{"open":"06:00","close":"22:00"}],"dayOfTheWeek":"SATURDAY"},{"earliestOpeningHour":"06:00","latestClosingHour":"22:00","openingHours":[{"open":"06:00","close":"22:00"}],"dayOfTheWeek":"SUNDAY"}],"longitude":4.43789,"latitude":51.22692,"municipality":{"fr":"Antwerpen","nl":"Antwerpen","en":"Antwerpen"},"showMap":false},"weightInGrams":480,"product":"2000000191034","productCategory":"parcel","processOverview":{"processSteps":[{"name":"prepare","status":"completed","label":{"main":"preparation","detail":"bySender"},"knownProcessStep":"IN_PREPARATION"},{"name":"processing","status":"completed","label":{"main":"processing","detail":"byBpost"},"knownProcessStep":"PROCESSING"},{"name":"out_for_delivery","status":"completed","label":{"main":"onTheWay","detail":"toPostPoint"},"knownProcessStep":"ON_THE_WAY_TO_POST_POINT"},{"name":"post_point","status":"completed","label":{"main":"available","detail":"inPostPoint"},"knownProcessStep":"AVAILABLE_IN_POST_POINT_INTERNATIONAL"},{"name":"delivered","status":"active","label":{"main":"pickedUp","detail":"inPostPoint"},"knownProcessStep":"PICKED_UP_IN_POST_POINT_INTERNATIONAL"}],"activeStepTextKey":"DELIVERED","textKey":"step.text.delivered","alert":false},"contactForMoreInformation":"BPOST","signatureViewType":"NONE","requestedDeliveryMethod":"HOME","inNetworkDate":"2016-11-21","attemptedDeliveryTime":{"time":"12:28","day":"2016-11-22"},"actualDeliveryTime":{"time":"13:37","day":"2016-11-23"},"faqs":[{"id":"faqid_2","title":{"fr":"Quid si je ne suis pas chez moi lorsque le facteur vient livrer mon paquet ?","nl":"Wat als ik niet thuis ben als de postbode mijn pakje komt afleveren?","en":"What if I am not home when the parcel is delivered?"}},{"id":"faqid_4","title":{"fr":"Comment puis-je demander une deuxième présentation ?","nl":"Hoe kan ik een tweede aanbieding aanvragen?","en":"How do I request a second delivery attempt?"}},{"id":"faqid_33","title":{"fr":"Où est mon colis ?","nl":"Waar is mijn pakje?","en":"Where is my parcel?"}}],"retourOrBackToSender":false}]}
5 changes: 3 additions & 2 deletions __mocks__/res_pp.json → __mocks__/res_postal_point.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
{
{"items":
[{
"key":{
"id":759682715,
"version":5,
Expand Down Expand Up @@ -173,3 +173,4 @@
"requestedDeliveryMethod":"POSTAL_POINT",
"inNetworkDate":"2016-11-09",
"faqs":[{"id":"faqid_9","title":{"fr":"Puis-je donner procuration à quelqu'un pour aller chercher mon paquet au bureau de poste ou au Point Poste ?","nl":"Kan ik iemand machtigen om mijn pakje op te halen in het postkantoor of postpunt?","en":"Can I authorise someone to pick up my parcel at the post office or PostPoint?"}},{"id":"faqid_12","title":{"fr":"Combien de temps mon paquet reste-t-il disponible dans un point d'enlèvement ?","nl":"Hoe lang blijft mijn pakje ter beschikking in een afhaalpunt?","en":"How long will my parcel remain available for me at the pick-up point?"}},{"id":"faqid_13","title":{"fr":"Puis-je faire transférer mon paquet vers un autre point d'enlèvement ?","nl":"Kan ik mijn pakje laten transfereren naar een ander afhaalpunt?","en":"Can I ask for my parcel to be transferred to another pick-up point?"}},{"id":"faqid_14","title":{"fr":"Quand puis-je aller chercher mon paquet dans un point d'enlèvement? Quelles sont les heures d'ouverture ?","nl":"Wanneer kan ik mijn pakje gaan afhalen in een afhaalpunt? Wat zijn de openingsuren?","en":"When can I pick up my parcel at a pick-up point? What are the opening times?"}}],"retourOrBackToSender":false}]
}
11 changes: 11 additions & 0 deletions __tests__/methods/__snapshots__/getStatus.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exports[`Method: getStatus should return the object for currently active step 1`] = `
Object {
"knownProcessStep": "PICKED_UP_IN_POST_POINT_INTERNATIONAL",
"label": Object {
"detail": "inPostPoint",
"main": "pickedUp",
},
"name": "delivered",
"status": "active",
}
`;
25 changes: 10 additions & 15 deletions __tests__/methods/getStatus.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import * as bpost from '/src/lib/methods/getStatus.js';
jest.mock(`/__mocks__/composedFetch`);

const result = {
name: `delivered`,
status: `active`,
label: {
main: `pickedUp`,
detail: `inPostPoint`,
},
knownProcessStep: `PICKED_UP_IN_POST_POINT_INTERNATIONAL`,
};
import * as bpost from '../../src/lib/methods/getStatus.js';
jest.mock(`../../__mocks__/composedFetch`);

describe(`Method: getStatus`, () => {
it(`should return the object for currently active step`, () => {
return bpost.default(`323267009691651472`)
.then(res => expect(res).toEqual(result));
it(`should return the object for currently active step`, async () => {
const request = await bpost.default(`323267009691651472`);
expect(request).toMatchSnapshot();
});

it(`should fail if an incorrect id param is passed`, async () => {
const req = await bpost.default(`3D3267OO9691651472`);
expect(req).toBeFalsy();
});
});
8 changes: 8 additions & 0 deletions __tests__/networkRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import composedFetch from '../src/lib/composedFetch';

describe(`Method: composedFetch`, () => {
it(`returns invalid response`, async () => {
const request = await composedFetch(`3232D7OO9691651472`);
expect(request).toBeFalsy();
});
});
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"pre-push": "npm run lint:build:test"
}
},
"jest": {
"testEnvironment": "node"
},
"scripts": {
"lint": "eslint src/*.js",
"test": "jest --coverage",
Expand Down
3 changes: 2 additions & 1 deletion src/lib/composedFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export default itemIdentifier => axios.get(`items`, {
baseURL: `http://track.bpost.be/btr/api/`,
responseType: `json`,
params: { itemIdentifier },
}).then(res => res.data.items[0]);
}).then(res => res.data.items[0])
.catch(e => e);

0 comments on commit f15398e

Please sign in to comment.