|
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(); |
7 | 6 |
|
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'); |
12 | 8 |
|
13 |
| - //apply the styles from the stylesheet if needed |
| 9 | +let cssArray = null; |
14 | 10 |
|
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", () => { |
22 | 16 | document.querySelector(
|
23 | 17 | "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; |
26 | 20 | expect(cssArray).toBe(".threeDimension");
|
27 | 21 | })
|
28 | 22 |
|
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", () => { |
30 | 24 | // we can read from the source code
|
31 | 25 | // console.log(html.toString());
|
32 |
| - expect(html.toString().indexOf(`<a`)>-1).toBeTruthy(); |
| 26 | + expect(html.toString().indexOf(`<a`) > -1).toBeTruthy(); |
33 | 27 | });
|
34 | 28 |
|
35 | 29 |
|
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", () => { |
37 | 31 | // get computed styles of any element you like
|
38 | 32 | // let cssArray=document.styleSheets[0].cssRules;
|
39 | 33 | document.querySelector(
|
40 | 34 | "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; |
43 | 37 |
|
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']; |
48 | 42 | }
|
49 | 43 | }
|
50 |
| - |
51 | 44 | 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 | + |
52 | 72 | });
|
53 | 73 |
|
54 |
| - it("You should not change the existing head tag elements", function () { |
| 74 | + test("You should not change the existing head tag elements", () => { |
55 | 75 | let head = document.querySelector('head')
|
56 | 76 | 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() |
66 | 84 | })
|
67 | 85 | });
|
68 | 86 |
|
|
0 commit comments