|
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"); |
| 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(); |
5 | 6 |
|
| 7 | +jest.dontMock('fs'); |
6 | 8 |
|
7 |
| -jest.dontMock("fs"); |
8 | 9 |
|
9 | 10 |
|
10 | 11 |
|
11 |
| -describe("All the styles should be applied", function () { |
12 |
| - beforeEach(() => { |
13 |
| - //here I import the HTML into the document |
14 |
| - document.documentElement.innerHTML=html.toString(); |
15 |
| - |
16 |
| - |
17 |
| - }); |
18 |
| - afterEach(() => { |
19 |
| - jest.resetModules(); |
20 |
| - }); |
21 |
| - |
22 |
| - it("the padding should be '10px'", function () { |
| 12 | +describe("All the styles should be applied", ()=>{ |
| 13 | + const meta = document.querySelector("meta") |
| 14 | + const title = document.querySelector('title') |
| 15 | + const link = document.querySelector('link') |
| 16 | + test("the padding should be '10px'", ()=>{ |
23 | 17 | document.querySelector(
|
24 | 18 | "head"
|
25 | 19 |
|
26 |
| - ).innerHTML=`<style>${css.toString()}</style>`; |
27 |
| - let divTag=document.querySelector(".orange-btn"); |
28 |
| - let classTagStyles=window.getComputedStyle(divTag); |
| 20 | + ).innerHTML = `<style>${css.toString()}</style>`; |
| 21 | + let divTag = document.querySelector(".orange-btn"); |
| 22 | + let classTagStyles = window.getComputedStyle(divTag); |
29 | 23 |
|
30 | 24 | expect(classTagStyles["padding"]).toBe("10px");
|
31 | 25 | });
|
32 |
| - it("the border radius should be '4px'", function () { |
| 26 | + test("the border radius should be '4px'", ()=>{ |
33 | 27 | document.querySelector(
|
34 | 28 | "head"
|
35 | 29 |
|
36 |
| - ).innerHTML=`<style>${css.toString()}</style>`; |
37 |
| - let divTag=document.querySelector(".orange-btn"); |
38 |
| - let classTagStyles=window.getComputedStyle(divTag); |
| 30 | + ).innerHTML = `<style>${css.toString()}</style>`; |
| 31 | + let divTag = document.querySelector(".orange-btn"); |
| 32 | + let classTagStyles = window.getComputedStyle(divTag); |
39 | 33 | expect(classTagStyles["border-radius"]).toBe("4px");
|
40 | 34 | });
|
41 |
| - it("the border should be '1px solid #ffffff;'", function () { |
42 |
| - document.querySelector( |
43 |
| - "head" |
44 |
| - |
45 |
| - ).innerHTML=`<style>${css.toString()}</style>`; |
46 |
| - let divTag=document.querySelector(".orange-btn"); |
47 |
| - let classTagStyles=window.getComputedStyle(divTag); |
48 |
| - expect(classTagStyles["border"]).toBe("1px solid #ffffff"); |
49 |
| - }); |
50 |
| - it("the background should be 'rgb(255, 153, 51)'", function () { |
| 35 | + test("the background should be 'orange'", ()=>{ |
51 | 36 | document.querySelector(
|
52 | 37 | "head"
|
53 | 38 |
|
54 |
| - ).innerHTML=`<style>${css.toString()}</style>`; |
55 |
| - let divTag=document.querySelector(".orange-btn"); |
56 |
| - let classTagStyles=window.getComputedStyle(divTag); |
| 39 | + ).innerHTML = `<style>${css.toString()}</style>`; |
| 40 | + let divTag = document.querySelector(".orange-btn"); |
| 41 | + let classTagStyles = window.getComputedStyle(divTag); |
57 | 42 | expect(classTagStyles["background"]).toBe("orange");
|
58 | 43 | });
|
59 |
| - it("the underline should to be removed", function () { |
| 44 | + test("the underline should to be removed", ()=>{ |
60 | 45 | document.querySelector(
|
61 | 46 | "head"
|
62 | 47 |
|
63 |
| - ).innerHTML=`<style>${css.toString()}</style>`; |
64 |
| - let divTag=document.querySelector(".orange-btn"); |
65 |
| - let classTagStyles=window.getComputedStyle(divTag); |
| 48 | + ).innerHTML = `<style>${css.toString()}</style>`; |
| 49 | + let divTag = document.querySelector(".orange-btn"); |
| 50 | + let classTagStyles = window.getComputedStyle(divTag); |
66 | 51 | expect(classTagStyles["text-decoration"]).toBe("none");
|
67 | 52 | });
|
68 |
| - it("The mouse hover property should be #cc7a00", function () { |
| 53 | + test("The mouse hover property should be 'darkorange'", ()=>{ |
69 | 54 | // get computed styles of any element you like
|
70 | 55 | document.querySelector(
|
71 | 56 | "head"
|
72 |
| - ).innerHTML=`<style>${css.toString()}</style>`; |
73 |
| - let cssArray=document.styleSheets[0].cssRules; |
| 57 | + ).innerHTML = `<style>${css.toString()}</style>`; |
| 58 | + let cssArray = document.styleSheets[0].cssRules; |
74 | 59 |
|
75 |
| - let orangeHoverSelector=""; |
76 |
| - for (let i=0; i<cssArray.length; i++) { |
77 |
| - if (cssArray[i].selectorText===".orange-btn:hover") { |
78 |
| - orangeHoverSelector=cssArray[i].style.background; |
| 60 | + let orangeHoverSelector = ""; |
| 61 | + for (let i = 0; i < cssArray.length; i++) { |
| 62 | + if (cssArray[i].selectorText === ".orange-btn:hover") { |
| 63 | + orangeHoverSelector = cssArray[i].style.background; |
79 | 64 | }
|
80 | 65 | }
|
81 | 66 |
|
82 | 67 | expect(orangeHoverSelector).toBe("darkorange");
|
83 | 68 | });
|
84 | 69 |
|
85 |
| - it("You should be careful with the specifity", function () { |
| 70 | + test("You should be careful with the specifity", ()=>{ |
86 | 71 | document.querySelector(
|
87 | 72 | "head"
|
88 |
| - ).innerHTML=`<style>${css.toString()}</style>`; |
89 |
| - let cssArray=document.styleSheets[0].cssRules[0].selectorText; |
| 73 | + ).innerHTML = `<style>${css.toString()}</style>`; |
| 74 | + let cssArray = document.styleSheets[0].cssRules[0].selectorText; |
90 | 75 | expect(cssArray).toBe(".orange-btn");
|
91 | 76 | }
|
92 | 77 | )
|
93 |
| - it("You should not change the existing head tag elements", function () { |
| 78 | + test("You should not change the existing head tag elements", ()=>{ |
94 | 79 | let head = document.querySelector('head')
|
95 | 80 | expect(head).toBeTruthy()
|
96 |
| - |
97 |
| - let meta = head.querySelector("meta") |
98 |
| - expect(meta).not.toBe(null) |
99 |
| - |
100 |
| - const pathname = new URL(document.querySelector('link').href).pathname |
101 |
| - expect(pathname).toBe('/styles.css') |
102 |
| - |
103 |
| - let title = head.querySelector('title') |
104 |
| - expect(title).not.toBe(null) |
| 81 | + |
| 82 | + expect(meta).toBeTruthy() |
| 83 | + |
| 84 | + let href = link.getAttribute('href') |
| 85 | + expect(href).toBe('./styles.css') |
| 86 | + |
| 87 | + expect(title).toBeTruthy() |
105 | 88 | })
|
106 | 89 |
|
107 | 90 | });
|
0 commit comments