Skip to content

Commit e8fe23c

Browse files
committed
Add first test with jest-puppeteer
1 parent 6026563 commit e8fe23c

44 files changed

Lines changed: 3875 additions & 1865 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.eslintrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
"browser": true,
66
"jest": true
77
},
8+
"globals": {
9+
"page": true,
10+
"browser": true,
11+
"context": true,
12+
"jestPuppeteer": true
13+
},
814
"rules": {
915
"prettier/prettier": ["error"],
1016
"strict": "off",

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ typings/
5959

6060
.netlify
6161
dist/
62+
temp/

Week1/js-exercises/ex1-bookList.html

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Book List</title>
7+
</head>
8+
9+
<body>
10+
<h1>My Book List</h1>
11+
<div id="bookList"><!-- put the ul element here --></div>
12+
</body>
13+
<script src="./index.js"></script>
14+
</html>
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/**
1+
/*
22
33
** Exercise 1: The book list **
44
5-
I 'd like to display my three favorite books inside a nice webpage!
5+
I'd like to display my three favorite books inside a nice webpage!
66
77
1. Iterate through the array of books.
88
2. For each book, create a `<p>`
@@ -14,29 +14,30 @@
1414
The end result should look something like this:
1515
https: //hyf-js2-week1-makeme-ex1-demo.herokuapp.com/
1616
17-
*/
17+
*/
1818

1919
function createBookList(books) {
2020
// your code goes in here, return the ul element
2121
}
2222

23-
const books = [{
23+
const myBooks = [
24+
{
2425
title: 'The Design of Everyday Things',
2526
author: 'Don Norman',
26-
alreadyRead: false
27+
alreadyRead: false,
2728
},
2829
{
2930
title: 'The Most Human Human',
3031
author: 'Brian Christian',
31-
alreadyRead: true
32+
alreadyRead: true,
3233
},
3334
{
3435
title: 'The Pragmatic Programmer',
3536
author: 'Andrew Hunt',
36-
alreadyRead: true
37-
}
37+
alreadyRead: true,
38+
},
3839
];
3940

40-
let ulElement = createBookList(books);
41+
const ulElement = createBookList(myBooks);
4142

42-
document.querySelector("#bookList").appendChild(ulElement);
43+
// document.querySelector('#bookList').appendChild(ulElement);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const fs = require('fs').promises;
2+
const path = require('path');
3+
const { HtmlValidate } = require('html-validate');
4+
const { getFormatter } = require('html-validate/dist/cli/formatter');
5+
const { setUp } = require('./helpers');
6+
7+
const stylish = getFormatter('stylish');
8+
9+
const exercisesDir = path.join(__dirname, '../js-exercises/ex1-bookList');
10+
11+
const htmlValidate = new HtmlValidate({
12+
extends: ['html-validate:recommended'],
13+
rules: { 'no-trailing-whitespace': 'off' },
14+
});
15+
16+
describe('Generated HTML', () => {
17+
beforeAll(async () => {
18+
await fs.copyFile(
19+
path.join(exercisesDir, 'index.html'),
20+
'./temp/index.html'
21+
);
22+
await fs.copyFile(path.join(exercisesDir, 'index.js'), './temp/index.js');
23+
24+
await setUp(page);
25+
});
26+
27+
test('should be syntactically valid', async () => {
28+
const outerHTML = await page.evaluate(
29+
() => document.documentElement.outerHTML
30+
);
31+
const htmlText = `<!DOCTYPE html>\n${outerHTML}`;
32+
const report = htmlValidate.validateString(htmlText);
33+
const validationReport = stylish(report);
34+
expect(validationReport).toBe('');
35+
});
36+
37+
test('should contain an unordered list', async () => {
38+
const ul = await page.evaluate(
39+
() => document.querySelector('ul > li').outerHTML
40+
);
41+
console.log(ul);
42+
});
43+
});

Week1/js-tests/helpers.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const config = {
2+
baseUrl: 'http://localhost:5000/',
3+
blockedResourceTypes: ['image', 'stylesheet', 'font'],
4+
};
5+
6+
async function setUp(page) {
7+
await page.setRequestInterception(true);
8+
page.on('request', req => {
9+
if (config.blockedResourceTypes.includes(req.resourceType())) {
10+
req.abort();
11+
} else {
12+
req.continue();
13+
}
14+
});
15+
return page.goto(config.baseUrl, { waitUntil: 'networkidle0' });
16+
}
17+
18+
module.exports = {
19+
setUp,
20+
};

Week2/homework/maartjes-work.js

Lines changed: 0 additions & 65 deletions
This file was deleted.

Week2/homework/map-filter.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

Week2/js-exercises/ex1-oddOnesOut.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
/**
1+
/*
22
33
** Exercise 1: The odd ones out **
44
55
Rewrite the following function using map and filter.
66
Avoid using for loop or forEach.
77
The function should still behave the same.
88
9-
*/
9+
*/
1010
function doubleEvenNumbers(numbers) {
1111
const newNumbers = [];
1212
for (let i = 0; i < numbers.length; i++) {
@@ -18,4 +18,4 @@ function doubleEvenNumbers(numbers) {
1818
}
1919

2020
const myNumbers = [1, 2, 3, 4];
21-
console.log(doubleEvenNumbers(myNumbers)); // Logs "[4, 8]" to the console
21+
console.log(doubleEvenNumbers(myNumbers)); // Logs "[4, 8]" to the console

0 commit comments

Comments
 (0)