|
| 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, scss} from '../..'; |
| 6 | + |
| 7 | +describe('a @media rule', () => { |
| 8 | + let node: GenericAtRule; |
| 9 | + |
| 10 | + describe('with no interpolation', () => { |
| 11 | + beforeEach( |
| 12 | + () => |
| 13 | + void (node = scss.parse('@media screen {}').nodes[0] as GenericAtRule) |
| 14 | + ); |
| 15 | + |
| 16 | + it('has a name', () => expect(node.name).toBe('media')); |
| 17 | + |
| 18 | + it('has a paramsInterpolation', () => |
| 19 | + expect(node).toHaveInterpolation('paramsInterpolation', 'screen')); |
| 20 | + |
| 21 | + it('has matching params', () => expect(node.params).toBe('screen')); |
| 22 | + }); |
| 23 | + |
| 24 | + // TODO: test a variable used directly without interpolation |
| 25 | + |
| 26 | + describe('with interpolation', () => { |
| 27 | + beforeEach( |
| 28 | + () => |
| 29 | + void (node = scss.parse('@media (hover: #{hover}) {}') |
| 30 | + .nodes[0] as GenericAtRule) |
| 31 | + ); |
| 32 | + |
| 33 | + it('has a name', () => expect(node.name).toBe('media')); |
| 34 | + |
| 35 | + it('has a paramsInterpolation', () => { |
| 36 | + const params = node.paramsInterpolation!; |
| 37 | + expect(params.nodes[0]).toBe('('); |
| 38 | + expect(params).toHaveStringExpression(1, 'hover'); |
| 39 | + expect(params.nodes[2]).toBe(': '); |
| 40 | + expect(params.nodes[3]).toBeInstanceOf(StringExpression); |
| 41 | + expect((params.nodes[3] as StringExpression).text).toHaveStringExpression( |
| 42 | + 0, |
| 43 | + 'hover' |
| 44 | + ); |
| 45 | + expect(params.nodes[4]).toBe(')'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('has matching params', () => |
| 49 | + expect(node.params).toBe('(hover: #{hover})')); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('stringifies', () => { |
| 53 | + // TODO: Use raws technology to include the actual original text between |
| 54 | + // interpolations. |
| 55 | + it('to SCSS', () => |
| 56 | + expect( |
| 57 | + (node = scss.parse('@media #{screen} and (hover: #{hover}) {@foo}') |
| 58 | + .nodes[0] as GenericAtRule).toString() |
| 59 | + ).toBe('@media #{screen} and (hover: #{hover}) {\n @foo\n}')); |
| 60 | + }); |
| 61 | +}); |
0 commit comments