|
| 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 {BooleanExpression} from '../..'; |
| 6 | +import * as utils from '../../../test/utils'; |
| 7 | + |
| 8 | +describe('a boolean expression', () => { |
| 9 | + let node: BooleanExpression; |
| 10 | + |
| 11 | + describe('true', () => { |
| 12 | + function describeNode( |
| 13 | + description: string, |
| 14 | + create: () => BooleanExpression |
| 15 | + ): void { |
| 16 | + describe(description, () => { |
| 17 | + beforeEach(() => void (node = create())); |
| 18 | + |
| 19 | + it('has sassType boolean', () => expect(node.sassType).toBe('boolean')); |
| 20 | + |
| 21 | + it('is true', () => expect(node.value).toBe(true)); |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + describeNode('parsed', () => utils.parseExpression('true')); |
| 26 | + |
| 27 | + describeNode( |
| 28 | + 'constructed manually', |
| 29 | + () => new BooleanExpression({value: true}) |
| 30 | + ); |
| 31 | + |
| 32 | + describeNode('constructed from ExpressionProps', () => |
| 33 | + utils.fromExpressionProps({value: true}) |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('false', () => { |
| 38 | + function describeNode( |
| 39 | + description: string, |
| 40 | + create: () => BooleanExpression |
| 41 | + ): void { |
| 42 | + describe(description, () => { |
| 43 | + beforeEach(() => void (node = create())); |
| 44 | + |
| 45 | + it('has sassType boolean', () => expect(node.sassType).toBe('boolean')); |
| 46 | + |
| 47 | + it('is false', () => expect(node.value).toBe(false)); |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + describeNode('parsed', () => utils.parseExpression('false')); |
| 52 | + |
| 53 | + describeNode( |
| 54 | + 'constructed manually', |
| 55 | + () => new BooleanExpression({value: false}) |
| 56 | + ); |
| 57 | + |
| 58 | + describeNode('constructed from ExpressionProps', () => |
| 59 | + utils.fromExpressionProps({value: false}) |
| 60 | + ); |
| 61 | + }); |
| 62 | + |
| 63 | + it('assigned new value', () => { |
| 64 | + node = utils.parseExpression('true'); |
| 65 | + node.value = false; |
| 66 | + expect(node.value).toBe(false); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('stringifies', () => { |
| 70 | + it('true', () => { |
| 71 | + expect(utils.parseExpression('true').toString()).toBe('true'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('false', () => { |
| 75 | + expect(utils.parseExpression('false').toString()).toBe('false'); |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + describe('clone', () => { |
| 80 | + let original: BooleanExpression; |
| 81 | + |
| 82 | + beforeEach(() => { |
| 83 | + original = utils.parseExpression('true'); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('with no overrides', () => { |
| 87 | + let clone: BooleanExpression; |
| 88 | + |
| 89 | + beforeEach(() => void (clone = original.clone())); |
| 90 | + |
| 91 | + describe('has the same properties:', () => { |
| 92 | + it('value', () => expect(clone.value).toBe(true)); |
| 93 | + |
| 94 | + it('raws', () => expect(clone.raws).toEqual({})); |
| 95 | + |
| 96 | + it('source', () => expect(clone.source).toBe(original.source)); |
| 97 | + }); |
| 98 | + |
| 99 | + it('creates a new self', () => expect(clone).not.toBe(original)); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('overrides', () => { |
| 103 | + describe('value', () => { |
| 104 | + it('defined', () => |
| 105 | + expect(original.clone({value: false}).value).toBe(false)); |
| 106 | + |
| 107 | + it('undefined', () => |
| 108 | + expect(original.clone({value: undefined}).value).toBe(true)); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('raws', () => { |
| 112 | + it('defined', () => |
| 113 | + expect(original.clone({raws: {}}).raws).toEqual({})); |
| 114 | + |
| 115 | + it('undefined', () => |
| 116 | + expect(original.clone({raws: undefined}).raws).toEqual({})); |
| 117 | + }); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it('toJSON', () => expect(utils.parseExpression('true')).toMatchSnapshot()); |
| 122 | +}); |
0 commit comments