|
1 | 1 | import {render} from './helpers/test-utils'
|
2 | 2 |
|
3 |
| -test('.toHaveStyle', () => { |
4 |
| - const {container} = render(` |
| 3 | +describe('.toHaveStyle', () => { |
| 4 | + test('handles positive test cases', () => { |
| 5 | + const {container} = render(` |
| 6 | + <div class="label" style="background-color: blue; height: 100%"> |
| 7 | + Hello World |
| 8 | + </div> |
| 9 | + `) |
| 10 | + |
| 11 | + const style = document.createElement('style') |
| 12 | + style.innerHTML = ` |
| 13 | + .label { |
| 14 | + background-color: black; |
| 15 | + color: white; |
| 16 | + float: left; |
| 17 | + } |
| 18 | + ` |
| 19 | + document.body.appendChild(style) |
| 20 | + document.body.appendChild(container) |
| 21 | + |
| 22 | + expect(container.querySelector('.label')).toHaveStyle(` |
| 23 | + height: 100%; |
| 24 | + color: white; |
| 25 | + background-color: blue; |
| 26 | + `) |
| 27 | + |
| 28 | + expect(container.querySelector('.label')).toHaveStyle(` |
| 29 | + background-color: blue; |
| 30 | + color: white; |
| 31 | + `) |
| 32 | + expect(container.querySelector('.label')).toHaveStyle( |
| 33 | + 'background-color:blue;color:white', |
| 34 | + ) |
| 35 | + |
| 36 | + expect(container.querySelector('.label')).not.toHaveStyle(` |
| 37 | + color: white; |
| 38 | + font-weight: bold; |
| 39 | + `) |
| 40 | + }) |
| 41 | + |
| 42 | + test('handles negative test cases', () => { |
| 43 | + const {container} = render(` |
5 | 44 | <div class="label" style="background-color: blue; height: 100%">
|
6 | 45 | Hello World
|
7 | 46 | </div>
|
8 | 47 | `)
|
9 | 48 |
|
10 |
| - const style = document.createElement('style') |
11 |
| - style.innerHTML = ` |
| 49 | + const style = document.createElement('style') |
| 50 | + style.innerHTML = ` |
12 | 51 | .label {
|
13 | 52 | background-color: black;
|
14 | 53 | color: white;
|
15 | 54 | float: left;
|
16 | 55 | }
|
17 | 56 | `
|
18 |
| - document.body.appendChild(style) |
19 |
| - document.body.appendChild(container) |
| 57 | + document.body.appendChild(style) |
| 58 | + document.body.appendChild(container) |
20 | 59 |
|
21 |
| - expect(container.querySelector('.label')).toHaveStyle(` |
22 |
| - height: 100%; |
23 |
| - color: white; |
24 |
| - background-color: blue; |
25 |
| - `) |
| 60 | + expect(() => |
| 61 | + expect(container.querySelector('.label')).toHaveStyle( |
| 62 | + 'font-weight: bold', |
| 63 | + ), |
| 64 | + ).toThrowError() |
| 65 | + expect(() => |
| 66 | + expect(container.querySelector('.label')).not.toHaveStyle('color: white'), |
| 67 | + ).toThrowError() |
26 | 68 |
|
27 |
| - expect(container.querySelector('.label')).toHaveStyle(` |
28 |
| - background-color: blue; |
29 |
| - color: white; |
30 |
| - `) |
31 |
| - expect(container.querySelector('.label')).toHaveStyle( |
32 |
| - 'background-color:blue;color:white', |
33 |
| - ) |
| 69 | + // Make sure the test fails if the css syntax is not valid |
| 70 | + expect(() => |
| 71 | + expect(container.querySelector('.label')).not.toHaveStyle( |
| 72 | + 'font-weight bold', |
| 73 | + ), |
| 74 | + ).toThrowError() |
| 75 | + expect(() => |
| 76 | + expect(container.querySelector('.label')).toHaveStyle('color white'), |
| 77 | + ).toThrowError() |
34 | 78 |
|
35 |
| - expect(container.querySelector('.label')).not.toHaveStyle(` |
36 |
| - color: white; |
37 |
| - font-weight: bold; |
38 |
| - `) |
| 79 | + document.body.removeChild(style) |
| 80 | + document.body.removeChild(container) |
| 81 | + }) |
39 | 82 |
|
40 |
| - expect(() => |
41 |
| - expect(container.querySelector('.label')).toHaveStyle('font-weight: bold'), |
42 |
| - ).toThrowError() |
43 |
| - expect(() => |
44 |
| - expect(container.querySelector('.label')).not.toHaveStyle('color: white'), |
45 |
| - ).toThrowError() |
46 |
| - |
47 |
| - // Make sure the test fails if the css syntax is not valid |
48 |
| - expect(() => |
49 |
| - expect(container.querySelector('.label')).not.toHaveStyle( |
50 |
| - 'font-weight bold', |
51 |
| - ), |
52 |
| - ).toThrowError() |
53 |
| - expect(() => |
54 |
| - expect(container.querySelector('.label')).toHaveStyle('color white'), |
55 |
| - ).toThrowError() |
56 |
| - |
57 |
| - document.body.removeChild(style) |
58 |
| - document.body.removeChild(container) |
| 83 | + test('properly normalizes colors', () => { |
| 84 | + const {queryByTestId} = render(` |
| 85 | + <span data-testid="color-example" style="background-color: #123456">Hello World</span> |
| 86 | + `) |
| 87 | + expect(queryByTestId('color-example')).toHaveStyle( |
| 88 | + 'background-color: #123456', |
| 89 | + ) |
| 90 | + }) |
| 91 | + |
| 92 | + test('properly normalizes colors for border', () => { |
| 93 | + const {queryByTestId} = render(` |
| 94 | + <span data-testid="color-example" style="border: 1px solid #fff">Hello World</span> |
| 95 | + `) |
| 96 | + expect(queryByTestId('color-example')).toHaveStyle('border: 1px solid #fff') |
| 97 | + }) |
59 | 98 | })
|
0 commit comments