|
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | +import { extend_ranges } from './extend-ranges' |
| 3 | +import { generate_coverage } from './test/generate-coverage' |
| 4 | +import type { Coverage } from './parse-coverage' |
| 5 | + |
| 6 | +let html = ` |
| 7 | +<!doctype html> |
| 8 | +<html> |
| 9 | + <head> |
| 10 | + <title>test document</title> |
| 11 | + <link rel="stylesheet" href="http://localhost/style.css"> |
| 12 | + </head> |
| 13 | + <body> |
| 14 | + <h1>Hello world</h1> |
| 15 | + <p>Text</p> |
| 16 | + </body> |
| 17 | +</html>` |
| 18 | + |
| 19 | +test.describe('leaves ranges intact when nothing to change', () => { |
| 20 | + test('lonely rule', async () => { |
| 21 | + let css = `body{color:green}` |
| 22 | + let coverage = (await generate_coverage(html, { link_css: css })) as Coverage[] |
| 23 | + |
| 24 | + // Expect the incomplete coverage reported by the browser |
| 25 | + expect(coverage.at(0)!.ranges).toEqual([{ start: 0, end: 17 }]) |
| 26 | + |
| 27 | + let result = extend_ranges(coverage) |
| 28 | + expect(result.at(0)!.ranges).toEqual([{ start: 0, end: 17 }]) |
| 29 | + }) |
| 30 | +}) |
| 31 | + |
| 32 | +test.describe('@rules', () => { |
| 33 | + test('lonely @media', async () => { |
| 34 | + let css = `@media (min-width:100px){body{color:green}}` |
| 35 | + let coverage = (await generate_coverage(html, { link_css: css })) as Coverage[] |
| 36 | + |
| 37 | + // Expect the incomplete coverage reported by the browser |
| 38 | + expect(coverage.at(0)!.ranges).toEqual([{ start: 7, end: 42 }]) // (min-width:100px){body{color:green} |
| 39 | + |
| 40 | + let result = extend_ranges(coverage) |
| 41 | + expect(result.at(0)!.ranges).toEqual([{ start: 0, end: 43 }]) // @media (min-width:100px){body{color:green}} |
| 42 | + }) |
| 43 | + |
| 44 | + test.describe('adjecent to uncovered code', () => { |
| 45 | + test('@media at end', async () => { |
| 46 | + let css = `a{}@media (min-width:100px){body{color:green}}` |
| 47 | + let coverage = (await generate_coverage(html, { link_css: css })) as Coverage[] |
| 48 | + |
| 49 | + // Expect the incomplete coverage reported by the browser |
| 50 | + expect(coverage.at(0)!.ranges).toEqual([{ start: 10, end: 45 }]) // (min-width:100px){body{color:green} |
| 51 | + |
| 52 | + let result = extend_ranges(coverage) |
| 53 | + expect(result.at(0)!.ranges).toEqual([{ start: 3, end: 46 }]) // @media (min-width:100px){body{color:green}} |
| 54 | + }) |
| 55 | + |
| 56 | + test('@media at start', async () => { |
| 57 | + let css = `@media (min-width:100px){body{color:green}}a{}` |
| 58 | + let coverage = (await generate_coverage(html, { link_css: css })) as Coverage[] |
| 59 | + |
| 60 | + // Expect the incomplete coverage reported by the browser |
| 61 | + expect(coverage.at(0)!.ranges).toEqual([{ start: 7, end: 42 }]) // (min-width:100px){body{color:green} |
| 62 | + |
| 63 | + let result = extend_ranges(coverage) |
| 64 | + expect(result.at(0)!.ranges).toEqual([{ start: 0, end: 43 }]) // @media (min-width:100px){body{color:green}} |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + test.describe('adjecent to covered code', () => { |
| 69 | + test('@media at end', async () => { |
| 70 | + let css = `p{color:red}@media (min-width:100px){body{color:green}}` |
| 71 | + let coverage = (await generate_coverage(html, { link_css: css })) as Coverage[] |
| 72 | + |
| 73 | + // Expect the incomplete coverage reported by the browser |
| 74 | + expect(coverage).toEqual([ |
| 75 | + { |
| 76 | + url: 'http://localhost/style.css', |
| 77 | + text: css, |
| 78 | + ranges: [ |
| 79 | + { start: 0, end: 12 }, // p{color:red} |
| 80 | + { start: 19, end: 54 }, // (min-width:100px){body{color:green} |
| 81 | + ], |
| 82 | + }, |
| 83 | + ]) |
| 84 | + |
| 85 | + let result = extend_ranges(coverage) |
| 86 | + expect(result).toEqual([ |
| 87 | + { |
| 88 | + url: 'http://localhost/style.css', |
| 89 | + text: css, |
| 90 | + ranges: [ |
| 91 | + { start: 0, end: 12 }, // p{color:red} |
| 92 | + { start: 12, end: 55 }, // @media (min-width:100px){body{color:green}} |
| 93 | + ], |
| 94 | + }, |
| 95 | + ]) |
| 96 | + }) |
| 97 | + |
| 98 | + test('@media at start', async () => { |
| 99 | + let css = `@media (min-width:100px){body{color:green}}p{color:red}` |
| 100 | + let coverage = (await generate_coverage(html, { link_css: css })) as Coverage[] |
| 101 | + |
| 102 | + // Expect the incomplete coverage reported by the browser |
| 103 | + expect(coverage).toEqual([ |
| 104 | + { |
| 105 | + url: 'http://localhost/style.css', |
| 106 | + text: css, |
| 107 | + ranges: [ |
| 108 | + { start: 7, end: 42 }, // (min-width:100px){body{color:green} |
| 109 | + { start: 43, end: 55 }, // p{color:red} |
| 110 | + ], |
| 111 | + }, |
| 112 | + ]) |
| 113 | + |
| 114 | + let result = extend_ranges(coverage) |
| 115 | + expect(result).toEqual([ |
| 116 | + { |
| 117 | + url: 'http://localhost/style.css', |
| 118 | + text: css, |
| 119 | + ranges: [ |
| 120 | + { start: 0, end: 43 }, // @media (min-width:100px){body{color:green}} |
| 121 | + { start: 43, end: 55 }, // p{color:red} |
| 122 | + ], |
| 123 | + }, |
| 124 | + ]) |
| 125 | + }) |
| 126 | + }) |
| 127 | +}) |
0 commit comments