|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import { |
| 9 | + generateLayersDeclaration, |
| 10 | + findLayer, |
| 11 | + isValidLayerName, |
| 12 | +} from '../layers'; |
| 13 | +import type {PluginOptions} from '../options'; |
| 14 | + |
| 15 | +describe('isValidLayerName', () => { |
| 16 | + it('accepts valid names', () => { |
| 17 | + expect(isValidLayerName('layer1')).toBe(true); |
| 18 | + expect(isValidLayerName('layer1.layer2')).toBe(true); |
| 19 | + expect(isValidLayerName('layer-1.layer_2.layer3')).toBe(true); |
| 20 | + }); |
| 21 | + |
| 22 | + it('rejects layer with coma', () => { |
| 23 | + expect(isValidLayerName('lay,er1')).toBe(false); |
| 24 | + }); |
| 25 | + it('rejects layer with space', () => { |
| 26 | + expect(isValidLayerName('lay er1')).toBe(false); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe('generateLayersDeclaration', () => { |
| 31 | + it('for list of layers', () => { |
| 32 | + expect(generateLayersDeclaration(['layer1', 'layer2'])).toBe( |
| 33 | + '@layer layer1, layer2;', |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it('for empty list of layers', () => { |
| 38 | + // Not useful to generate it, but still valid CSS anyway |
| 39 | + expect(generateLayersDeclaration([])).toBe('@layer ;'); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('findLayer', () => { |
| 44 | + const inputFilePath = 'filePath'; |
| 45 | + |
| 46 | + function testFor(layers: PluginOptions['layers']) { |
| 47 | + return findLayer(inputFilePath, Object.entries(layers)); |
| 48 | + } |
| 49 | + |
| 50 | + it('for empty layers', () => { |
| 51 | + expect(testFor({})).toBeUndefined(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('for single matching layer', () => { |
| 55 | + expect(testFor({layer: (filePath) => filePath === inputFilePath})).toBe( |
| 56 | + 'layer', |
| 57 | + ); |
| 58 | + }); |
| 59 | + |
| 60 | + it('for single non-matching layer', () => { |
| 61 | + expect( |
| 62 | + testFor({layer: (filePath) => filePath !== inputFilePath}), |
| 63 | + ).toBeUndefined(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('for multiple matching layers', () => { |
| 67 | + expect( |
| 68 | + testFor({ |
| 69 | + layer1: (filePath) => filePath === inputFilePath, |
| 70 | + layer2: (filePath) => filePath === inputFilePath, |
| 71 | + layer3: (filePath) => filePath === inputFilePath, |
| 72 | + }), |
| 73 | + ).toBe('layer1'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('for multiple non-matching layers', () => { |
| 77 | + expect( |
| 78 | + testFor({ |
| 79 | + layer1: (filePath) => filePath !== inputFilePath, |
| 80 | + layer2: (filePath) => filePath !== inputFilePath, |
| 81 | + layer3: (filePath) => filePath !== inputFilePath, |
| 82 | + }), |
| 83 | + ).toBeUndefined(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('for multiple mixed matching layers', () => { |
| 87 | + expect( |
| 88 | + testFor({ |
| 89 | + layer1: (filePath) => filePath !== inputFilePath, |
| 90 | + layer2: (filePath) => filePath === inputFilePath, |
| 91 | + layer3: (filePath) => filePath !== inputFilePath, |
| 92 | + layer4: (filePath) => filePath === inputFilePath, |
| 93 | + }), |
| 94 | + ).toBe('layer2'); |
| 95 | + }); |
| 96 | +}); |
0 commit comments