|
| 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 {GenericAtRule, StringExpression, WhileRule, sass, scss} from '../..'; |
| 6 | +import * as utils from '../../../test/utils'; |
| 7 | + |
| 8 | +describe('a @while rule', () => { |
| 9 | + let node: WhileRule; |
| 10 | + describe('with empty children', () => { |
| 11 | + function describeNode(description: string, create: () => WhileRule): void { |
| 12 | + describe(description, () => { |
| 13 | + beforeEach(() => void (node = create())); |
| 14 | + |
| 15 | + it('has a name', () => expect(node.name.toString()).toBe('while')); |
| 16 | + |
| 17 | + it('has an expression', () => |
| 18 | + expect(node).toHaveStringExpression('whileCondition', 'foo')); |
| 19 | + |
| 20 | + it('has matching params', () => expect(node.params).toBe('foo')); |
| 21 | + |
| 22 | + it('has empty nodes', () => expect(node.nodes).toEqual([])); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + describeNode( |
| 27 | + 'parsed as SCSS', |
| 28 | + () => scss.parse('@while foo {}').nodes[0] as WhileRule, |
| 29 | + ); |
| 30 | + |
| 31 | + describeNode( |
| 32 | + 'parsed as Sass', |
| 33 | + () => sass.parse('@while foo').nodes[0] as WhileRule, |
| 34 | + ); |
| 35 | + |
| 36 | + describeNode( |
| 37 | + 'constructed manually', |
| 38 | + () => |
| 39 | + new WhileRule({ |
| 40 | + whileCondition: {text: 'foo'}, |
| 41 | + }), |
| 42 | + ); |
| 43 | + |
| 44 | + describeNode('constructed from ChildProps', () => |
| 45 | + utils.fromChildProps({ |
| 46 | + whileCondition: {text: 'foo'}, |
| 47 | + }), |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('with a child', () => { |
| 52 | + function describeNode(description: string, create: () => WhileRule): void { |
| 53 | + describe(description, () => { |
| 54 | + beforeEach(() => void (node = create())); |
| 55 | + |
| 56 | + it('has a name', () => expect(node.name.toString()).toBe('while')); |
| 57 | + |
| 58 | + it('has an expression', () => |
| 59 | + expect(node).toHaveStringExpression('whileCondition', 'foo')); |
| 60 | + |
| 61 | + it('has matching params', () => expect(node.params).toBe('foo')); |
| 62 | + |
| 63 | + it('has a child node', () => { |
| 64 | + expect(node.nodes).toHaveLength(1); |
| 65 | + expect(node.nodes[0]).toBeInstanceOf(GenericAtRule); |
| 66 | + expect(node.nodes[0]).toHaveProperty('name', 'child'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + describeNode( |
| 72 | + 'parsed as SCSS', |
| 73 | + () => scss.parse('@while foo {@child}').nodes[0] as WhileRule, |
| 74 | + ); |
| 75 | + |
| 76 | + describeNode( |
| 77 | + 'parsed as Sass', |
| 78 | + () => sass.parse('@while foo\n @child').nodes[0] as WhileRule, |
| 79 | + ); |
| 80 | + |
| 81 | + describeNode( |
| 82 | + 'constructed manually', |
| 83 | + () => |
| 84 | + new WhileRule({ |
| 85 | + whileCondition: {text: 'foo'}, |
| 86 | + nodes: [{name: 'child'}], |
| 87 | + }), |
| 88 | + ); |
| 89 | + |
| 90 | + describeNode('constructed from ChildProps', () => |
| 91 | + utils.fromChildProps({ |
| 92 | + whileCondition: {text: 'foo'}, |
| 93 | + nodes: [{name: 'child'}], |
| 94 | + }), |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('throws an error when assigned a new', () => { |
| 99 | + beforeEach( |
| 100 | + () => void (node = new WhileRule({whileCondition: {text: 'foo'}})), |
| 101 | + ); |
| 102 | + |
| 103 | + it('name', () => expect(() => (node.name = 'bar')).toThrow()); |
| 104 | + |
| 105 | + it('params', () => expect(() => (node.params = 'true')).toThrow()); |
| 106 | + }); |
| 107 | + |
| 108 | + describe('assigned a new expression', () => { |
| 109 | + beforeEach(() => { |
| 110 | + node = scss.parse('@while foo {}').nodes[0] as WhileRule; |
| 111 | + }); |
| 112 | + |
| 113 | + it("removes the old expression's parent", () => { |
| 114 | + const oldExpression = node.whileCondition; |
| 115 | + node.whileCondition = {text: 'bar'}; |
| 116 | + expect(oldExpression.parent).toBeUndefined(); |
| 117 | + }); |
| 118 | + |
| 119 | + it("assigns the new expression's parent", () => { |
| 120 | + const expression = new StringExpression({text: 'bar'}); |
| 121 | + node.whileCondition = expression; |
| 122 | + expect(expression.parent).toBe(node); |
| 123 | + }); |
| 124 | + |
| 125 | + it('assigns the expression explicitly', () => { |
| 126 | + const expression = new StringExpression({text: 'bar'}); |
| 127 | + node.whileCondition = expression; |
| 128 | + expect(node.whileCondition).toBe(expression); |
| 129 | + }); |
| 130 | + |
| 131 | + it('assigns the expression as ExpressionProps', () => { |
| 132 | + node.whileCondition = {text: 'bar'}; |
| 133 | + expect(node).toHaveStringExpression('whileCondition', 'bar'); |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + describe('stringifies', () => { |
| 138 | + describe('to SCSS', () => { |
| 139 | + it('with default raws', () => |
| 140 | + expect( |
| 141 | + new WhileRule({ |
| 142 | + whileCondition: {text: 'foo'}, |
| 143 | + }).toString(), |
| 144 | + ).toBe('@while foo {}')); |
| 145 | + |
| 146 | + it('with afterName', () => |
| 147 | + expect( |
| 148 | + new WhileRule({ |
| 149 | + whileCondition: {text: 'foo'}, |
| 150 | + raws: {afterName: '/**/'}, |
| 151 | + }).toString(), |
| 152 | + ).toBe('@while/**/foo {}')); |
| 153 | + |
| 154 | + it('with between', () => |
| 155 | + expect( |
| 156 | + new WhileRule({ |
| 157 | + whileCondition: {text: 'foo'}, |
| 158 | + raws: {between: '/**/'}, |
| 159 | + }).toString(), |
| 160 | + ).toBe('@while foo/**/{}')); |
| 161 | + }); |
| 162 | + }); |
| 163 | + |
| 164 | + describe('clone', () => { |
| 165 | + let original: WhileRule; |
| 166 | + beforeEach(() => { |
| 167 | + original = scss.parse('@while foo {}').nodes[0] as WhileRule; |
| 168 | + // TODO: remove this once raws are properly parsed |
| 169 | + original.raws.between = ' '; |
| 170 | + }); |
| 171 | + |
| 172 | + describe('with no overrides', () => { |
| 173 | + let clone: WhileRule; |
| 174 | + beforeEach(() => void (clone = original.clone())); |
| 175 | + |
| 176 | + describe('has the same properties:', () => { |
| 177 | + it('params', () => expect(clone.params).toBe('foo')); |
| 178 | + |
| 179 | + it('whileCondition', () => |
| 180 | + expect(clone).toHaveStringExpression('whileCondition', 'foo')); |
| 181 | + |
| 182 | + it('raws', () => expect(clone.raws).toEqual({between: ' '})); |
| 183 | + |
| 184 | + it('source', () => expect(clone.source).toBe(original.source)); |
| 185 | + }); |
| 186 | + |
| 187 | + describe('creates a new', () => { |
| 188 | + it('self', () => expect(clone).not.toBe(original)); |
| 189 | + |
| 190 | + for (const attr of ['whileCondition', 'raws'] as const) { |
| 191 | + it(attr, () => expect(clone[attr]).not.toBe(original[attr])); |
| 192 | + } |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + describe('overrides', () => { |
| 197 | + describe('raws', () => { |
| 198 | + it('defined', () => |
| 199 | + expect(original.clone({raws: {afterName: ' '}}).raws).toEqual({ |
| 200 | + afterName: ' ', |
| 201 | + })); |
| 202 | + |
| 203 | + it('undefined', () => |
| 204 | + expect(original.clone({raws: undefined}).raws).toEqual({ |
| 205 | + between: ' ', |
| 206 | + })); |
| 207 | + }); |
| 208 | + |
| 209 | + describe('whileCondition', () => { |
| 210 | + describe('defined', () => { |
| 211 | + let clone: WhileRule; |
| 212 | + beforeEach(() => { |
| 213 | + clone = original.clone({whileCondition: {text: 'bar'}}); |
| 214 | + }); |
| 215 | + |
| 216 | + it('changes params', () => expect(clone.params).toBe('bar')); |
| 217 | + |
| 218 | + it('changes whileCondition', () => |
| 219 | + expect(clone).toHaveStringExpression('whileCondition', 'bar')); |
| 220 | + }); |
| 221 | + |
| 222 | + describe('undefined', () => { |
| 223 | + let clone: WhileRule; |
| 224 | + beforeEach(() => { |
| 225 | + clone = original.clone({whileCondition: undefined}); |
| 226 | + }); |
| 227 | + |
| 228 | + it('preserves params', () => expect(clone.params).toBe('foo')); |
| 229 | + |
| 230 | + it('preserves whileCondition', () => |
| 231 | + expect(clone).toHaveStringExpression('whileCondition', 'foo')); |
| 232 | + }); |
| 233 | + }); |
| 234 | + }); |
| 235 | + }); |
| 236 | + |
| 237 | + it('toJSON', () => |
| 238 | + expect(scss.parse('@while foo {}').nodes[0]).toMatchSnapshot()); |
| 239 | +}); |
0 commit comments