Hi everyone,
If someone can help me to refactor the following code into an async/await? I have tried different things but it seems I am missing something?
If you ask, I am applying a TDD for an specific utility using Jest. The code below with a promise passes green:
test('**Testing data exposure should be correctly with a promise', (done) => {
function SearchFunc(prms, pgtkn) {
//https://developers.google.com/maps/documentation/javascript/examples/place-search-pagination
//A constructor would be better here
let googleMapsClient = require('@google/maps').createClient({
key: configGoogleMaps.confignode.google.browser.MapsAPIKEY,
Promise: Promise
})
prms.pagetoken = pgtkn;
let paramqs = {
query: prms.queries[0],
location: prms.location,
type: prms.type,
radius: prms.radius,
region: prms.region,
pagetoken: prms.pagetoken,
};
//expect.assertions(1);
let response = googleMapsClient.places(paramqs).asPromise();
return response
};
var db = new loki('Example');
var users = db.addCollection('users')
var sPDB = users.insert({
searchParamsDB: {
queries: ["vegan", "organic cafes"],
querycontrol: 0,
location: [52.370216, 4.895168],
type: "restaurant",
radius: 1000,
region: "Amsterdam",
pagetoken: null,
placeids: [],
stopSearch: false
}
});
users.chain()
.find()
.data();
//console.log('out promise', users.data[0]);
if (users.data[0].searchParamsDB.stopSearch) {
done();
}
let res = SearchFunc(users.data[0].searchParamsDB, null);
console.warn(res);
res
.then((d) => {
console.warn(d.status);
expect(d.json.status).toBe("OK");
if (d.json.status === "OK") {
let newplaceslist = d.json.results.map((place) => {
return {
source: "Google",
name: place.name,
id: place.place_id,
query: users.data[0].searchParamsDB.queries[users.data[0].searchParamsDB.querycontrol],
address: place.formatted_address,
status: 'OPEN'
}
})
.filter((newpl) => {
return users.data[0].searchParamsDB.placeids.indexOf(newpl.id) === -1;
});
expect(newplaceslist.length).toBeGreaterThan(0);
expect(d.json.next_page_token).not.toBeNull;
if (newplaceslist.length === 0) {
//SEE IF THERE IS ANOTHER QUERY TO EXPLORE
//UPDATE QUERYCONTROL VALUE
if (users.data[0].searchParamsDB.querycontrol > users.data[0].searchParamsDB.queries.length) {
//UPDATE stopSearch
//UPDATE RELATED VALUES
//STOP SEARCH
} else {
//CALL THE FUNCTIONS AGAIN BUT WITH THE NEW QUERY
}
} else {
//UPDATE DATABASE
};
};
done();
});
});