Skip to content

Commit e5ae3f2

Browse files
committed
added test to prevent user from changing default styles plus minor improvements
1 parent 2ff5fca commit e5ae3f2

File tree

1 file changed

+58
-40
lines changed

1 file changed

+58
-40
lines changed

exercises/09-Anchor-Styles/tests.js

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,86 @@
1-
const fs=require("fs");
2-
const path=require("path");
3-
const html=fs.readFileSync(path.resolve(__dirname, "./index.html"), "utf8");
4-
const css=fs.readFileSync(path.resolve(__dirname, "./styles.css"), "utf8");
5-
let cssArray=null;
6-
jest.dontMock("fs");
1+
const fs = require('fs');
2+
const path = require('path');
3+
const html = fs.readFileSync(path.resolve(__dirname, './index.html'), 'utf8');
4+
const css = fs.readFileSync(path.resolve(__dirname, "./styles.css"), "utf8");
5+
document.documentElement.innerHTML = html.toString();
76

8-
describe("All the styles should be applied", function () {
9-
beforeEach(() => {
10-
//here I import the HTML into the document
11-
document.documentElement.innerHTML=html.toString();
7+
jest.dontMock('fs');
128

13-
//apply the styles from the stylesheet if needed
9+
let cssArray = null;
1410

15-
16-
});
17-
afterEach(() => {
18-
jest.resetModules();
19-
});
20-
21-
it("You should not add your rules above the existing code", function () {
11+
describe("All the styles should be applied", () => {
12+
const meta = document.querySelector("meta")
13+
const title = document.querySelector('title')
14+
const link = document.querySelector('link')
15+
test("You should not add your rules above the existing code", () => {
2216
document.querySelector(
2317
"head"
24-
).innerHTML=`<style>${css.toString()}</style>`;
25-
let cssArray=document.styleSheets[0].cssRules[0].selectorText;
18+
).innerHTML = `<style>${css.toString()}</style>`;
19+
let cssArray = document.styleSheets[0].cssRules[0].selectorText;
2620
expect(cssArray).toBe(".threeDimension");
2721
})
2822

29-
it("the 'a' tag in the index.html should not be deleted", function () {
23+
test("the 'a' tag in the index.html should not be deleted", () => {
3024
// we can read from the source code
3125
// console.log(html.toString());
32-
expect(html.toString().indexOf(`<a`)>-1).toBeTruthy();
26+
expect(html.toString().indexOf(`<a`) > -1).toBeTruthy();
3327
});
3428

3529

36-
it("The border-color rule for the 'threeDimension active ' property should match the instruction color", function () {
30+
test("The border-color rule for the 'threeDimension active ' property should match the instruction color", () => {
3731
// get computed styles of any element you like
3832
// let cssArray=document.styleSheets[0].cssRules;
3933
document.querySelector(
4034
"head"
41-
).innerHTML=`<style>${css.toString()}</style>`;
42-
let cssArray=document.styleSheets[0].cssRules;
35+
).innerHTML = `<style>${css.toString()}</style>`;
36+
let cssArray = document.styleSheets[0].cssRules;
4337

44-
let orangeHoverSelector="";
45-
for (let i=0; i<cssArray.length; i++) {
46-
if (cssArray[i].selectorText==="a.threeDimension:active") {
47-
orangeHoverSelector=cssArray[i].style['border-color'];
38+
let orangeHoverSelector = "";
39+
for (let i = 0; i < cssArray.length; i++) {
40+
if (cssArray[i].selectorText === "a.threeDimension:active") {
41+
orangeHoverSelector = cssArray[i].style['border-color'];
4842
}
4943
}
50-
5144
expect(orangeHoverSelector).toBe("#000 #aaa #aaa #000");
45+
46+
});
47+
test("You should not change the default styles", () => {
48+
// get computed styles of any element you like
49+
// let cssArray=document.styleSheets[0].cssRules;
50+
document.querySelector(
51+
"head"
52+
).innerHTML = `<style>${css.toString()}</style>`;
53+
let cssArray = document.styleSheets[0].cssRules;
54+
55+
let borderColor = "";
56+
let color = "";
57+
let background = "";
58+
let width = "";
59+
for (let i = 0; i < cssArray.length; i++) {
60+
if (cssArray[i].selectorText === ".threeDimension") {
61+
borderColor = cssArray[i].style["border-color"];
62+
color = cssArray[i].style['color'];
63+
background = cssArray[i].style['background'];
64+
width = cssArray[i].style['width'];
65+
expect(borderColor).toBe("#aaa #000 #000 #aaa");
66+
expect(color).toBe("black");
67+
expect(background).toBe("#fc0");
68+
expect(width).toBe("8em");
69+
}
70+
}
71+
5272
});
5373

54-
it("You should not change the existing head tag elements", function () {
74+
test("You should not change the existing head tag elements", () => {
5575
let head = document.querySelector('head')
5676
expect(head).toBeTruthy()
57-
58-
let meta = head.querySelector("meta")
59-
expect(meta).not.toBe(null)
60-
61-
const pathname = new URL(document.querySelector('link').href).pathname
62-
expect(pathname).toBe('/styles.css')
63-
64-
let title = head.querySelector('title')
65-
expect(title).not.toBe(null)
77+
78+
expect(meta).toBeTruthy()
79+
80+
let href = link.getAttribute('href')
81+
expect(href).toBe('./styles.css')
82+
83+
expect(title).toBeTruthy()
6684
})
6785
});
6886

0 commit comments

Comments
 (0)