-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
175 additions
and
2 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 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,31 @@ | ||
|
||
describe('DAY 9: Async/await', () => { | ||
/** | ||
* @param {string} outcome resolve|reject | ||
* @returns {Promise} | ||
*/ | ||
function fetchData (outcome) { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => { | ||
switch (outcome) { | ||
case 'resolve': | ||
resolve('data retrieved'); | ||
break; | ||
default: | ||
reject(new Error('no data')); | ||
break; | ||
} | ||
}, 50); | ||
}); | ||
} | ||
|
||
// @see https://jestjs.io/docs/en/asynchronous for a hint | ||
it(`make an async test for fetchData to verify resolution`, () => { | ||
throw new Error('replace the test body'); | ||
}); | ||
|
||
// @see https://jestjs.io/docs/en/asynchronous for a hint | ||
it(`make an async test for fetchData to verify rejection`, () => { | ||
throw new Error('replace the test body'); | ||
}); | ||
}); |
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,38 @@ | ||
|
||
describe('DAY 9: Callback', () => { | ||
|
||
it(`(Synchronous callback) | ||
create a function named caller | ||
deduce the rest of the implementation reading the 2 expects`, () => { | ||
let callback = jest.fn(); | ||
let callbackArgument = Symbol('callbackArgument'); | ||
|
||
/** | ||
* | ||
* @param {function} callback | ||
* @returns {undefined} | ||
*/ | ||
let caller = () => {}; | ||
|
||
let result = caller(callback); | ||
|
||
expect(callback).toBeCalledWith(callbackArgument); | ||
expect(result).toBe(callback); | ||
}); | ||
|
||
it(`(Asynchronous callback) | ||
change the function "caller" | ||
deduce the implementation reading the 2 expects`, done => { | ||
|
||
let callback = jest.fn(); | ||
let callbackArgument = Symbol('callbackArgument'); | ||
|
||
// @see https://jestjs.io/docs/en/asynchronous | ||
let caller = () => {}; | ||
|
||
setTimeout(() => expect(callback).toBeCalledWith(callbackArgument), 1000); | ||
|
||
setTimeout(() => caller(callback), 50); | ||
|
||
}, 2000); | ||
}); |
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,24 @@ | ||
|
||
describe('DAY 9: Event Loop', () => { | ||
|
||
it(`modify the initial expression of the for loop to pass the test`, done => { | ||
|
||
var i = 0; | ||
/** | ||
* | ||
* @param {number} i | ||
* @returns {undefined} | ||
*/ | ||
function counter (i) { | ||
if (i === 3) { | ||
done(); | ||
} | ||
} | ||
|
||
for (var i = 0; i < 4; i++) { | ||
setTimeout(() => counter(i), 0); | ||
} | ||
|
||
expect(i).toBe(0); | ||
}); | ||
}); |
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,80 @@ | ||
|
||
describe('DAY 9: Promises', () => { | ||
|
||
it(`validString function should return a Promise | ||
the promise should: | ||
resolve with the input value if it's a string | ||
reject with an instance of MySampleTypeError if it's not | ||
the error message should be a "Template literal" (see the catch block for a hint)`, () => { | ||
|
||
/** | ||
* | ||
* @param {*} input | ||
* @returns {Promise} | ||
*/ | ||
function validateString (input) { | ||
return new Promise((resolve, reject) => { | ||
|
||
}); | ||
} | ||
|
||
let input1 = `I'm a a string`; | ||
let input2 = 5; | ||
|
||
/** | ||
* this test is not quite good | ||
* we might have false positives | ||
* ? can you tell why? | ||
*/ | ||
return validateString(input1).then(resolution => { | ||
expect(resolution).toBe(`I'm a a string`); | ||
}).then(() => { | ||
return validateString(input2); | ||
}).catch((error) => { | ||
expect(error).toBeInstanceOf(TypeError); | ||
expect(error.message).toBe(`${input2} is not a string`); | ||
}); | ||
}); | ||
|
||
it(`validString function should exactly as the previous test | ||
stringToUpper function should return a Promise | ||
and will try to resolve the string to uppercase | ||
or catch the error and reject it | ||
`, () => { | ||
|
||
/** | ||
* | ||
* @param {*} input | ||
* @returns {Promise} | ||
*/ | ||
function validateString (input) { | ||
return new Promise((resolve, reject) => { | ||
resolve(input); | ||
}); | ||
} | ||
|
||
/** | ||
* | ||
* @param {*} input | ||
* @returns {Promise} | ||
*/ | ||
function stringToUpper (input) { | ||
return new Promise((resolve, reject) => { | ||
resolve('change me'); | ||
}); | ||
} | ||
|
||
let input = 'oo'; | ||
|
||
/** | ||
* this tests has a redundant validation | ||
* ? can you tell why is that? | ||
*/ | ||
return validateString(input).then(resolution => { | ||
return stringToUpper(resolution); | ||
}).then(resolution => { | ||
expect(resolution).toBe(input.toUpperCase()); | ||
}); | ||
}); | ||
|
||
}); |