|
2 | 2 |
|
3 | 3 | import {render} from './helpers/test-utils'
|
4 | 4 |
|
| 5 | +const renderElementWithClasses = () => |
| 6 | + render(` |
| 7 | +<div> |
| 8 | + <button data-testid="delete-button" class="btn extra btn-danger"> |
| 9 | + Delete item |
| 10 | + </button> |
| 11 | + <button data-testid="cancel-button"> |
| 12 | + Cancel |
| 13 | + </button> |
| 14 | + <svg data-testid="svg-spinner" class="spinner clockwise"> |
| 15 | + <path /> |
| 16 | + </svg> |
| 17 | + <div data-testid="only-one-class" class="alone"></div> |
| 18 | + <div data-testid="no-classes"></div> |
| 19 | +</div> |
| 20 | +`) |
| 21 | + |
5 | 22 | test('.toHaveClass', () => {
|
6 |
| - const {queryByTestId} = render(` |
7 |
| - <div> |
8 |
| - <button data-testid="delete-button" class="btn extra btn-danger"> |
9 |
| - Delete item |
10 |
| - </button> |
11 |
| - <button data-testid="cancel-button"> |
12 |
| - Cancel |
13 |
| - </button> |
14 |
| - <svg data-testid="svg-spinner" class="spinner clockwise"> |
15 |
| - <path /> |
16 |
| - </svg> |
17 |
| - <div data-testid="no-classes"></div> |
18 |
| - </div> |
19 |
| - `) |
| 23 | + const {queryByTestId} = renderElementWithClasses() |
20 | 24 |
|
21 | 25 | expect(queryByTestId('delete-button')).toHaveClass('btn')
|
22 | 26 | expect(queryByTestId('delete-button')).toHaveClass('btn-danger')
|
@@ -91,3 +95,89 @@ test('.toHaveClass', () => {
|
91 | 95 | expect(queryByTestId('delete-button')).not.toHaveClass(' '),
|
92 | 96 | ).toThrowError(/(none)/)
|
93 | 97 | })
|
| 98 | + |
| 99 | +test('.toHaveClass with exact mode option', () => { |
| 100 | + const {queryByTestId} = renderElementWithClasses() |
| 101 | + |
| 102 | + expect(queryByTestId('delete-button')).toHaveClass('btn extra btn-danger', { |
| 103 | + exact: true, |
| 104 | + }) |
| 105 | + expect(queryByTestId('delete-button')).not.toHaveClass('btn extra', { |
| 106 | + exact: true, |
| 107 | + }) |
| 108 | + expect( |
| 109 | + queryByTestId('delete-button'), |
| 110 | + ).not.toHaveClass('btn extra btn-danger foo', {exact: true}) |
| 111 | + |
| 112 | + expect(queryByTestId('delete-button')).toHaveClass('btn extra btn-danger', { |
| 113 | + exact: false, |
| 114 | + }) |
| 115 | + expect(queryByTestId('delete-button')).toHaveClass('btn extra', { |
| 116 | + exact: false, |
| 117 | + }) |
| 118 | + expect( |
| 119 | + queryByTestId('delete-button'), |
| 120 | + ).not.toHaveClass('btn extra btn-danger foo', {exact: false}) |
| 121 | + |
| 122 | + expect(queryByTestId('delete-button')).toHaveClass( |
| 123 | + 'btn', |
| 124 | + 'extra', |
| 125 | + 'btn-danger', |
| 126 | + {exact: true}, |
| 127 | + ) |
| 128 | + expect(queryByTestId('delete-button')).not.toHaveClass('btn', 'extra', { |
| 129 | + exact: true, |
| 130 | + }) |
| 131 | + expect(queryByTestId('delete-button')).not.toHaveClass( |
| 132 | + 'btn', |
| 133 | + 'extra', |
| 134 | + 'btn-danger', |
| 135 | + 'foo', |
| 136 | + {exact: true}, |
| 137 | + ) |
| 138 | + |
| 139 | + expect(queryByTestId('delete-button')).toHaveClass( |
| 140 | + 'btn', |
| 141 | + 'extra', |
| 142 | + 'btn-danger', |
| 143 | + {exact: false}, |
| 144 | + ) |
| 145 | + expect(queryByTestId('delete-button')).toHaveClass('btn', 'extra', { |
| 146 | + exact: false, |
| 147 | + }) |
| 148 | + expect(queryByTestId('delete-button')).not.toHaveClass( |
| 149 | + 'btn', |
| 150 | + 'extra', |
| 151 | + 'btn-danger', |
| 152 | + 'foo', |
| 153 | + {exact: false}, |
| 154 | + ) |
| 155 | + |
| 156 | + expect(queryByTestId('only-one-class')).toHaveClass('alone', {exact: true}) |
| 157 | + expect(queryByTestId('only-one-class')).not.toHaveClass('alone foo', { |
| 158 | + exact: true, |
| 159 | + }) |
| 160 | + expect(queryByTestId('only-one-class')).not.toHaveClass('alone', 'foo', { |
| 161 | + exact: true, |
| 162 | + }) |
| 163 | + |
| 164 | + expect(queryByTestId('only-one-class')).toHaveClass('alone', {exact: false}) |
| 165 | + expect(queryByTestId('only-one-class')).not.toHaveClass('alone foo', { |
| 166 | + exact: false, |
| 167 | + }) |
| 168 | + expect(queryByTestId('only-one-class')).not.toHaveClass('alone', 'foo', { |
| 169 | + exact: false, |
| 170 | + }) |
| 171 | + |
| 172 | + expect(() => |
| 173 | + expect(queryByTestId('only-one-class')).not.toHaveClass('alone', { |
| 174 | + exact: true, |
| 175 | + }), |
| 176 | + ).toThrowError(/Expected the element not to have EXACTLY defined classes/) |
| 177 | + |
| 178 | + expect(() => |
| 179 | + expect(queryByTestId('only-one-class')).toHaveClass('alone', 'foo', { |
| 180 | + exact: true, |
| 181 | + }), |
| 182 | + ).toThrowError(/Expected the element to have EXACTLY defined classes/) |
| 183 | +}) |
0 commit comments