|
| 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 {NumberExpression} from '../..'; |
| 6 | +import * as utils from '../../../test/utils'; |
| 7 | + |
| 8 | +describe('a number expression', () => { |
| 9 | + let node: NumberExpression; |
| 10 | + |
| 11 | + describe('unitless', () => { |
| 12 | + function describeNode( |
| 13 | + description: string, |
| 14 | + create: () => NumberExpression |
| 15 | + ): void { |
| 16 | + describe(description, () => { |
| 17 | + beforeEach(() => void (node = create())); |
| 18 | + |
| 19 | + it('has sassType number', () => expect(node.sassType).toBe('number')); |
| 20 | + |
| 21 | + it('is a number', () => expect(node.value).toBe(123)); |
| 22 | + |
| 23 | + it('has no unit', () => expect(node.unit).toBeNull()); |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + describeNode('parsed', () => utils.parseExpression('123')); |
| 28 | + |
| 29 | + describeNode( |
| 30 | + 'constructed manually', |
| 31 | + () => |
| 32 | + new NumberExpression({ |
| 33 | + value: 123, |
| 34 | + }) |
| 35 | + ); |
| 36 | + |
| 37 | + describeNode('constructed from ExpressionProps', () => |
| 38 | + utils.fromExpressionProps({ |
| 39 | + value: 123, |
| 40 | + }) |
| 41 | + ); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('with a unit', () => { |
| 45 | + function describeNode( |
| 46 | + description: string, |
| 47 | + create: () => NumberExpression |
| 48 | + ): void { |
| 49 | + describe(description, () => { |
| 50 | + beforeEach(() => void (node = create())); |
| 51 | + |
| 52 | + it('has sassType number', () => expect(node.sassType).toBe('number')); |
| 53 | + |
| 54 | + it('is a number', () => expect(node.value).toBe(123)); |
| 55 | + |
| 56 | + it('has a unit', () => expect(node.unit).toBe('px')); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + describeNode('parsed', () => utils.parseExpression('123px')); |
| 61 | + |
| 62 | + describeNode( |
| 63 | + 'constructed manually', |
| 64 | + () => |
| 65 | + new NumberExpression({ |
| 66 | + value: 123, |
| 67 | + unit: 'px', |
| 68 | + }) |
| 69 | + ); |
| 70 | + |
| 71 | + describeNode('constructed from ExpressionProps', () => |
| 72 | + utils.fromExpressionProps({ |
| 73 | + value: 123, |
| 74 | + unit: 'px', |
| 75 | + }) |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('floating-point number', () => { |
| 80 | + describe('unitless', () => { |
| 81 | + beforeEach(() => void (node = utils.parseExpression('3.14'))); |
| 82 | + |
| 83 | + it('value', () => expect(node.value).toBe(3.14)); |
| 84 | + |
| 85 | + it('unit', () => expect(node.unit).toBeNull()); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('with a unit', () => { |
| 89 | + beforeEach(() => void (node = utils.parseExpression('1.618px'))); |
| 90 | + |
| 91 | + it('value', () => expect(node.value).toBe(1.618)); |
| 92 | + |
| 93 | + it('unit', () => expect(node.unit).toBe('px')); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('assigned new', () => { |
| 98 | + beforeEach(() => void (node = utils.parseExpression('123'))); |
| 99 | + |
| 100 | + it('value', () => { |
| 101 | + node.value = 456; |
| 102 | + expect(node.value).toBe(456); |
| 103 | + }); |
| 104 | + |
| 105 | + it('unit', () => { |
| 106 | + node.unit = 'px'; |
| 107 | + expect(node.unit).toBe('px'); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('stringifies', () => { |
| 112 | + it('unitless', () => { |
| 113 | + expect(utils.parseExpression('123').toString()).toBe('123'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('with a unit', () => { |
| 117 | + expect(utils.parseExpression('123px').toString()).toBe('123px'); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('clone', () => { |
| 122 | + let original: NumberExpression; |
| 123 | + |
| 124 | + beforeEach(() => { |
| 125 | + original = utils.parseExpression('123'); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('with no overrides', () => { |
| 129 | + let clone: NumberExpression; |
| 130 | + |
| 131 | + beforeEach(() => void (clone = original.clone())); |
| 132 | + |
| 133 | + describe('has the same properties:', () => { |
| 134 | + it('value', () => expect(clone.value).toBe(123)); |
| 135 | + |
| 136 | + it('unit', () => expect(clone.unit).toBeNull()); |
| 137 | + |
| 138 | + it('raws', () => expect(clone.raws).toEqual({})); |
| 139 | + |
| 140 | + it('source', () => expect(clone.source).toBe(original.source)); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('creates a new', () => { |
| 144 | + it('self', () => expect(clone).not.toBe(original)); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + describe('overrides', () => { |
| 149 | + describe('value', () => { |
| 150 | + it('defined', () => |
| 151 | + expect(original.clone({value: 123}).value).toBe(123)); |
| 152 | + |
| 153 | + it('undefined', () => |
| 154 | + expect(original.clone({value: undefined}).value).toBe(123)); |
| 155 | + }); |
| 156 | + |
| 157 | + describe('unit', () => { |
| 158 | + it('defined', () => |
| 159 | + expect(original.clone({unit: 'px'}).unit).toBe('px')); |
| 160 | + |
| 161 | + it('undefined', () => |
| 162 | + expect(original.clone({unit: undefined}).unit).toBeNull()); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('raws', () => { |
| 166 | + it('defined', () => |
| 167 | + expect(original.clone({raws: {}}).raws).toEqual({})); |
| 168 | + |
| 169 | + it('undefined', () => |
| 170 | + expect(original.clone({raws: undefined}).raws).toEqual({})); |
| 171 | + }); |
| 172 | + }); |
| 173 | + }); |
| 174 | + |
| 175 | + it('toJSON', () => expect(utils.parseExpression('123%')).toMatchSnapshot()); |
| 176 | +}); |
0 commit comments