|
| 1 | +// Copyright 2024 Google Inc. Use of this source code is governed by an |
| 2 | +// MIT-style license that can be found in the LICENSE file or at |
| 3 | +// https://opensource.org/licenses/MIT. |
| 4 | + |
| 5 | +import {CssComment, Interpolation, Root, css, sass, scss} from '../..'; |
| 6 | +import * as utils from '../../../test/utils'; |
| 7 | + |
| 8 | +describe('a CSS-style comment', () => { |
| 9 | + let node: CssComment; |
| 10 | + function describeNode(description: string, create: () => CssComment): void { |
| 11 | + describe(description, () => { |
| 12 | + beforeEach(() => void (node = create())); |
| 13 | + |
| 14 | + it('has type comment', () => expect(node.type).toBe('comment')); |
| 15 | + |
| 16 | + it('has sassType comment', () => expect(node.sassType).toBe('comment')); |
| 17 | + |
| 18 | + it('has matching textInterpolation', () => |
| 19 | + expect(node).toHaveInterpolation('textInterpolation', 'foo')); |
| 20 | + |
| 21 | + it('has matching text', () => expect(node.text).toBe('foo')); |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + describeNode( |
| 26 | + 'parsed as SCSS', |
| 27 | + () => scss.parse('/* foo */').nodes[0] as CssComment |
| 28 | + ); |
| 29 | + |
| 30 | + describeNode( |
| 31 | + 'parsed as CSS', |
| 32 | + () => css.parse('/* foo */').nodes[0] as CssComment |
| 33 | + ); |
| 34 | + |
| 35 | + describeNode( |
| 36 | + 'parsed as Sass', |
| 37 | + () => sass.parse('/* foo').nodes[0] as CssComment |
| 38 | + ); |
| 39 | + |
| 40 | + describe('constructed manually', () => { |
| 41 | + describeNode( |
| 42 | + 'with an interpolation', |
| 43 | + () => |
| 44 | + new CssComment({ |
| 45 | + textInterpolation: new Interpolation({nodes: ['foo']}), |
| 46 | + }) |
| 47 | + ); |
| 48 | + |
| 49 | + describeNode('with a text string', () => new CssComment({text: 'foo'})); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('constructed from ChildProps', () => { |
| 53 | + describeNode('with an interpolation', () => |
| 54 | + utils.fromChildProps({ |
| 55 | + textInterpolation: new Interpolation({nodes: ['foo']}), |
| 56 | + }) |
| 57 | + ); |
| 58 | + |
| 59 | + describeNode('with a text string', () => |
| 60 | + utils.fromChildProps({text: 'foo'}) |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('parses raws', () => { |
| 65 | + describe('in SCSS', () => { |
| 66 | + it('with whitespace before and after text', () => |
| 67 | + expect((scss.parse('/* foo */').nodes[0] as CssComment).raws).toEqual({ |
| 68 | + left: ' ', |
| 69 | + right: ' ', |
| 70 | + closed: true, |
| 71 | + })); |
| 72 | + |
| 73 | + it('with whitespace before and after interpolation', () => |
| 74 | + expect( |
| 75 | + (scss.parse('/* #{foo} */').nodes[0] as CssComment).raws |
| 76 | + ).toEqual({left: ' ', right: ' ', closed: true})); |
| 77 | + |
| 78 | + it('without whitespace before and after text', () => |
| 79 | + expect((scss.parse('/*foo*/').nodes[0] as CssComment).raws).toEqual({ |
| 80 | + left: '', |
| 81 | + right: '', |
| 82 | + closed: true, |
| 83 | + })); |
| 84 | + |
| 85 | + it('without whitespace before and after interpolation', () => |
| 86 | + expect((scss.parse('/*#{foo}*/').nodes[0] as CssComment).raws).toEqual({ |
| 87 | + left: '', |
| 88 | + right: '', |
| 89 | + closed: true, |
| 90 | + })); |
| 91 | + |
| 92 | + it('with whitespace and no text', () => |
| 93 | + expect((scss.parse('/* */').nodes[0] as CssComment).raws).toEqual({ |
| 94 | + left: ' ', |
| 95 | + right: '', |
| 96 | + closed: true, |
| 97 | + })); |
| 98 | + |
| 99 | + it('with no whitespace and no text', () => |
| 100 | + expect((scss.parse('/**/').nodes[0] as CssComment).raws).toEqual({ |
| 101 | + left: '', |
| 102 | + right: '', |
| 103 | + closed: true, |
| 104 | + })); |
| 105 | + }); |
| 106 | + |
| 107 | + describe('in Sass', () => { |
| 108 | + // TODO: Test explicit whitespace after text and interpolation once we |
| 109 | + // properly parse raws from somewhere other than the original text. |
| 110 | + |
| 111 | + it('with whitespace before text', () => |
| 112 | + expect((sass.parse('/* foo').nodes[0] as CssComment).raws).toEqual({ |
| 113 | + left: ' ', |
| 114 | + right: '', |
| 115 | + closed: false, |
| 116 | + })); |
| 117 | + |
| 118 | + it('with whitespace before interpolation', () => |
| 119 | + expect((sass.parse('/* #{foo}').nodes[0] as CssComment).raws).toEqual({ |
| 120 | + left: ' ', |
| 121 | + right: '', |
| 122 | + closed: false, |
| 123 | + })); |
| 124 | + |
| 125 | + it('without whitespace before and after text', () => |
| 126 | + expect((sass.parse('/*foo').nodes[0] as CssComment).raws).toEqual({ |
| 127 | + left: '', |
| 128 | + right: '', |
| 129 | + closed: false, |
| 130 | + })); |
| 131 | + |
| 132 | + it('without whitespace before and after interpolation', () => |
| 133 | + expect((sass.parse('/*#{foo}').nodes[0] as CssComment).raws).toEqual({ |
| 134 | + left: '', |
| 135 | + right: '', |
| 136 | + closed: false, |
| 137 | + })); |
| 138 | + |
| 139 | + it('with no whitespace and no text', () => |
| 140 | + expect((sass.parse('/*').nodes[0] as CssComment).raws).toEqual({ |
| 141 | + left: '', |
| 142 | + right: '', |
| 143 | + closed: false, |
| 144 | + })); |
| 145 | + |
| 146 | + it('with a trailing */', () => |
| 147 | + expect((sass.parse('/* foo */').nodes[0] as CssComment).raws).toEqual({ |
| 148 | + left: ' ', |
| 149 | + right: ' ', |
| 150 | + closed: true, |
| 151 | + })); |
| 152 | + }); |
| 153 | + }); |
| 154 | + |
| 155 | + describe('stringifies', () => { |
| 156 | + describe('to SCSS', () => { |
| 157 | + it('with default raws', () => |
| 158 | + expect(new CssComment({text: 'foo'}).toString()).toBe('/* foo */')); |
| 159 | + |
| 160 | + it('with left', () => |
| 161 | + expect( |
| 162 | + new CssComment({ |
| 163 | + text: 'foo', |
| 164 | + raws: {left: '\n'}, |
| 165 | + }).toString() |
| 166 | + ).toBe('/*\nfoo */')); |
| 167 | + |
| 168 | + it('with right', () => |
| 169 | + expect( |
| 170 | + new CssComment({ |
| 171 | + text: 'foo', |
| 172 | + raws: {right: '\n'}, |
| 173 | + }).toString() |
| 174 | + ).toBe('/* foo\n*/')); |
| 175 | + |
| 176 | + it('with before', () => |
| 177 | + expect( |
| 178 | + new Root({ |
| 179 | + nodes: [new CssComment({text: 'foo', raws: {before: '/**/'}})], |
| 180 | + }).toString() |
| 181 | + ).toBe('/**//* foo */')); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + describe('assigned new text', () => { |
| 186 | + beforeEach(() => { |
| 187 | + node = scss.parse('/* foo */').nodes[0] as CssComment; |
| 188 | + }); |
| 189 | + |
| 190 | + it("removes the old text's parent", () => { |
| 191 | + const oldText = node.textInterpolation!; |
| 192 | + node.textInterpolation = 'bar'; |
| 193 | + expect(oldText.parent).toBeUndefined(); |
| 194 | + }); |
| 195 | + |
| 196 | + it("assigns the new interpolation's parent", () => { |
| 197 | + const interpolation = new Interpolation({nodes: ['bar']}); |
| 198 | + node.textInterpolation = interpolation; |
| 199 | + expect(interpolation.parent).toBe(node); |
| 200 | + }); |
| 201 | + |
| 202 | + it('assigns the interpolation explicitly', () => { |
| 203 | + const interpolation = new Interpolation({nodes: ['bar']}); |
| 204 | + node.textInterpolation = interpolation; |
| 205 | + expect(node.textInterpolation).toBe(interpolation); |
| 206 | + }); |
| 207 | + |
| 208 | + it('assigns the interpolation as a string', () => { |
| 209 | + node.textInterpolation = 'bar'; |
| 210 | + expect(node).toHaveInterpolation('textInterpolation', 'bar'); |
| 211 | + }); |
| 212 | + |
| 213 | + it('assigns the interpolation as text', () => { |
| 214 | + node.text = 'bar'; |
| 215 | + expect(node).toHaveInterpolation('textInterpolation', 'bar'); |
| 216 | + }); |
| 217 | + }); |
| 218 | + |
| 219 | + describe('clone', () => { |
| 220 | + let original: CssComment; |
| 221 | + beforeEach( |
| 222 | + () => void (original = scss.parse('/* foo */').nodes[0] as CssComment) |
| 223 | + ); |
| 224 | + |
| 225 | + describe('with no overrides', () => { |
| 226 | + let clone: CssComment; |
| 227 | + beforeEach(() => { |
| 228 | + clone = original.clone(); |
| 229 | + }); |
| 230 | + |
| 231 | + describe('has the same properties:', () => { |
| 232 | + it('textInterpolation', () => |
| 233 | + expect(clone).toHaveInterpolation('textInterpolation', 'foo')); |
| 234 | + |
| 235 | + it('text', () => expect(clone.text).toBe('foo')); |
| 236 | + |
| 237 | + it('raws', () => |
| 238 | + expect(clone.raws).toEqual({left: ' ', right: ' ', closed: true})); |
| 239 | + |
| 240 | + it('source', () => expect(clone.source).toBe(original.source)); |
| 241 | + }); |
| 242 | + |
| 243 | + describe('creates a new', () => { |
| 244 | + it('self', () => expect(clone).not.toBe(original)); |
| 245 | + |
| 246 | + for (const attr of ['textInterpolation', 'raws'] as const) { |
| 247 | + it(attr, () => expect(clone[attr]).not.toBe(original[attr])); |
| 248 | + } |
| 249 | + }); |
| 250 | + }); |
| 251 | + |
| 252 | + describe('overrides', () => { |
| 253 | + describe('text', () => { |
| 254 | + describe('defined', () => { |
| 255 | + let clone: CssComment; |
| 256 | + beforeEach(() => { |
| 257 | + clone = original.clone({text: 'bar'}); |
| 258 | + }); |
| 259 | + |
| 260 | + it('changes text', () => expect(clone.text).toBe('bar')); |
| 261 | + |
| 262 | + it('changes textInterpolation', () => |
| 263 | + expect(clone).toHaveInterpolation('textInterpolation', 'bar')); |
| 264 | + }); |
| 265 | + |
| 266 | + describe('undefined', () => { |
| 267 | + let clone: CssComment; |
| 268 | + beforeEach(() => { |
| 269 | + clone = original.clone({text: undefined}); |
| 270 | + }); |
| 271 | + |
| 272 | + it('preserves text', () => expect(clone.text).toBe('foo')); |
| 273 | + |
| 274 | + it('preserves textInterpolation', () => |
| 275 | + expect(clone).toHaveInterpolation('textInterpolation', 'foo')); |
| 276 | + }); |
| 277 | + }); |
| 278 | + |
| 279 | + describe('textInterpolation', () => { |
| 280 | + describe('defined', () => { |
| 281 | + let clone: CssComment; |
| 282 | + beforeEach(() => { |
| 283 | + clone = original.clone({ |
| 284 | + textInterpolation: new Interpolation({nodes: ['baz']}), |
| 285 | + }); |
| 286 | + }); |
| 287 | + |
| 288 | + it('changes text', () => expect(clone.text).toBe('baz')); |
| 289 | + |
| 290 | + it('changes textInterpolation', () => |
| 291 | + expect(clone).toHaveInterpolation('textInterpolation', 'baz')); |
| 292 | + }); |
| 293 | + |
| 294 | + describe('undefined', () => { |
| 295 | + let clone: CssComment; |
| 296 | + beforeEach(() => { |
| 297 | + clone = original.clone({textInterpolation: undefined}); |
| 298 | + }); |
| 299 | + |
| 300 | + it('preserves text', () => expect(clone.text).toBe('foo')); |
| 301 | + |
| 302 | + it('preserves textInterpolation', () => |
| 303 | + expect(clone).toHaveInterpolation('textInterpolation', 'foo')); |
| 304 | + }); |
| 305 | + }); |
| 306 | + |
| 307 | + describe('raws', () => { |
| 308 | + it('defined', () => |
| 309 | + expect(original.clone({raws: {right: ' '}}).raws).toEqual({ |
| 310 | + right: ' ', |
| 311 | + })); |
| 312 | + |
| 313 | + it('undefined', () => |
| 314 | + expect(original.clone({raws: undefined}).raws).toEqual({ |
| 315 | + left: ' ', |
| 316 | + right: ' ', |
| 317 | + closed: true, |
| 318 | + })); |
| 319 | + }); |
| 320 | + }); |
| 321 | + }); |
| 322 | + |
| 323 | + it('toJSON', () => |
| 324 | + expect(scss.parse('/* foo */').nodes[0]).toMatchSnapshot()); |
| 325 | +}); |
0 commit comments