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