|
| 1 | +// Copyright 2025 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 {ParenthesizedExpression, StringExpression} from '../..'; |
| 6 | +import * as utils from '../../../test/utils'; |
| 7 | + |
| 8 | +describe('a parenthesized expression', () => { |
| 9 | + let node: ParenthesizedExpression; |
| 10 | + function describeNode( |
| 11 | + description: string, |
| 12 | + create: () => ParenthesizedExpression, |
| 13 | + ): void { |
| 14 | + describe(description, () => { |
| 15 | + beforeEach(() => void (node = create())); |
| 16 | + |
| 17 | + it('has sassType parenthesized', () => |
| 18 | + expect(node.sassType).toBe('parenthesized')); |
| 19 | + |
| 20 | + it('has an expression', () => |
| 21 | + expect(node).toHaveStringExpression('inParens', 'foo')); |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + describeNode('parsed', () => utils.parseExpression('(foo)')); |
| 26 | + |
| 27 | + describeNode( |
| 28 | + 'constructed manually', |
| 29 | + () => new ParenthesizedExpression({inParens: {text: 'foo'}}), |
| 30 | + ); |
| 31 | + |
| 32 | + describeNode('constructed from ExpressionProps', () => |
| 33 | + utils.fromExpressionProps({inParens: {text: 'foo'}}), |
| 34 | + ); |
| 35 | + |
| 36 | + describe('assigned new', () => { |
| 37 | + beforeEach(() => void (node = utils.parseExpression('(foo)'))); |
| 38 | + |
| 39 | + describe('expression', () => { |
| 40 | + it("removes the old expression's parent", () => { |
| 41 | + const oldInParens = node.inParens; |
| 42 | + node.inParens = {text: 'zip'}; |
| 43 | + expect(oldInParens.parent).toBeUndefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('assigns the expression explicitly', () => { |
| 47 | + const inParens = new StringExpression({text: 'zip'}); |
| 48 | + node.inParens = inParens; |
| 49 | + expect(node.inParens).toBe(inParens); |
| 50 | + expect(node).toHaveStringExpression('inParens', 'zip'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('assigns the expression as ExpressionProps', () => { |
| 54 | + node.inParens = {text: 'zip'}; |
| 55 | + expect(node).toHaveStringExpression('inParens', 'zip'); |
| 56 | + }); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('stringifies', () => { |
| 61 | + beforeEach(() => void (node = utils.parseExpression('(foo)'))); |
| 62 | + |
| 63 | + it('without raws', () => expect(node.toString()).toBe('(foo)')); |
| 64 | + |
| 65 | + it('with afterOpen', () => { |
| 66 | + node.raws.afterOpen = '/**/'; |
| 67 | + expect(node.toString()).toBe('(/**/foo)'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('with beforeClose', () => { |
| 71 | + node.raws.beforeClose = '/**/'; |
| 72 | + expect(node.toString()).toBe('(foo/**/)'); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('clone', () => { |
| 77 | + let original: ParenthesizedExpression; |
| 78 | + beforeEach(() => { |
| 79 | + original = utils.parseExpression('(foo)'); |
| 80 | + // TODO: remove this once raws are properly parsed |
| 81 | + original.raws.afterOpen = ' '; |
| 82 | + }); |
| 83 | + |
| 84 | + describe('with no overrides', () => { |
| 85 | + let clone: ParenthesizedExpression; |
| 86 | + beforeEach(() => void (clone = original.clone())); |
| 87 | + |
| 88 | + describe('has the same properties:', () => { |
| 89 | + it('inParens', () => |
| 90 | + expect(clone).toHaveStringExpression('inParens', 'foo')); |
| 91 | + |
| 92 | + it('raws', () => expect(clone.raws).toEqual({afterOpen: ' '})); |
| 93 | + |
| 94 | + it('source', () => expect(clone.source).toBe(original.source)); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('creates a new', () => { |
| 98 | + it('self', () => expect(clone).not.toBe(original)); |
| 99 | + |
| 100 | + for (const attr of ['inParens', 'raws'] as const) { |
| 101 | + it(attr, () => expect(clone[attr]).not.toBe(original[attr])); |
| 102 | + } |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + describe('overrides', () => { |
| 107 | + describe('inParens', () => { |
| 108 | + it('defined', () => |
| 109 | + expect( |
| 110 | + original.clone({inParens: {text: 'zip'}}), |
| 111 | + ).toHaveStringExpression('inParens', 'zip')); |
| 112 | + |
| 113 | + it('undefined', () => |
| 114 | + expect(original.clone({inParens: undefined})).toHaveStringExpression( |
| 115 | + 'inParens', |
| 116 | + 'foo', |
| 117 | + )); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('raws', () => { |
| 121 | + it('defined', () => |
| 122 | + expect(original.clone({raws: {beforeClose: ' '}}).raws).toEqual({ |
| 123 | + beforeClose: ' ', |
| 124 | + })); |
| 125 | + |
| 126 | + it('undefined', () => |
| 127 | + expect(original.clone({raws: undefined}).raws).toEqual({ |
| 128 | + afterOpen: ' ', |
| 129 | + })); |
| 130 | + }); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it('toJSON', () => expect(utils.parseExpression('(foo)')).toMatchSnapshot()); |
| 135 | +}); |
0 commit comments