function monteCarlo(shouldResolve) { return new Promise(function (resolve, reject) { if(!shouldResolve) reject('Oh no, the promised was rejected!'); resolve({ title: 'UOL - O Melhor conteúdo' }); }); } async function main(shouldResolve) { try { const getMonteCarlo = await monteCarlo(shouldResolve); return getMonteCarlo.title; } catch(e) { return e; } }
function monteCarlo() {- function monteCarlo(shouldResolve) {
- return new Promise(function (resolve, reject) {
resolve([{titulo: 'UOL - O Melhor conteúdo'}]);- if(!shouldResolve) reject('Oh no, the promised was rejected!');
- resolve({ title: 'UOL - O Melhor conteúdo' });
- });
- }
async function main() {// converter para ES6monteCarlo().then(function(response) {console.log(response)});return true;- async function main(shouldResolve) {
- try {
- const getMonteCarlo = await monteCarlo(shouldResolve);
- return getMonteCarlo.title;
- }
- catch(e) {
- return e;
- }
- }
describe("monteCarlo", function(){ it("returns the title if the promise resolves", async function(){ Test.assertEquals(await main(true), "UOL - O Melhor conteúdo"); }); it("returns the error message if the promise rejects", async function(){ Test.assertEquals(await main(false), "Oh no, the promised was rejected!"); Test.assertEquals(await main(), "Oh no, the promised was rejected!"); }); });
describe("Solution", function(){it("should test for something", async function(){Test.assertEquals(await main(), true, "This is just an example of how you can write your own TDD tests");- describe("monteCarlo", function(){
- it("returns the title if the promise resolves", async function(){
- Test.assertEquals(await main(true), "UOL - O Melhor conteúdo");
- });
- it("returns the error message if the promise rejects", async function(){
- Test.assertEquals(await main(false), "Oh no, the promised was rejected!");
- Test.assertEquals(await main(), "Oh no, the promised was rejected!");
- });
- });
const main = () => { const a = [1, 2, 3]; const b = [4, 5, 6]; const c = [7, 8, 9]; return [...a, ...b, ...c].join(','); }
- const main = () => {
let a = [1, 2, 3];- const a = [1, 2, 3];
- const b = [4, 5, 6];
- const c = [7, 8, 9];
let b = [4, 5, 6];let c = [7, 8, 9];// converter para ES6var d = a.concat(b).concat(c);return d.join(',');- return [...a, ...b, ...c].join(',');
- }