Skip to content

Commit

Permalink
tests for day 9 completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaxolotl committed Oct 14, 2019
1 parent dc37d33 commit 91d4b00
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "a-walk-in-javascript",
"version": "1.9.0",
"version": "1.12.0",
"description": "A day by day walk through the basics of javascript assisted with notes, exercises and references to the most significant resources online",
"main": "''",
"scripts": {
Expand Down
31 changes: 31 additions & 0 deletions src/day_09/asyncAwait.test.js
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');
});
});
38 changes: 38 additions & 0 deletions src/day_09/callback.test.js
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);
});
24 changes: 24 additions & 0 deletions src/day_09/eventLoop.test.js
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);
});
});
80 changes: 80 additions & 0 deletions src/day_09/promise.test.js
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());
});
});

});

0 comments on commit 91d4b00

Please sign in to comment.