|
| 1 | +import lessGen from '../less'; |
| 2 | +import { renderSrcAttribute } from '../../../utils/css'; |
| 3 | +import { resolve } from 'path'; |
| 4 | + |
| 5 | +const renderSrcMock = renderSrcAttribute as any as jest.Mock; |
| 6 | + |
| 7 | +const mockOptions = { |
| 8 | + name: 'test', |
| 9 | + prefix: 'tf', |
| 10 | + tag: 'b', |
| 11 | + codepoints: { 'my-icon': 0xf101 }, |
| 12 | + assets: { 'my-icon': null }, |
| 13 | + templates: { |
| 14 | + less: resolve(__dirname, '../../../../templates/less.hbs') |
| 15 | + } |
| 16 | +} as any; |
| 17 | + |
| 18 | +jest.mock('../../../utils/css', () => ({ |
| 19 | + renderSrcAttribute: jest.fn(() => '"::src-attr::"') |
| 20 | +})); |
| 21 | + |
| 22 | +describe('`LESS` asset generator', () => { |
| 23 | + beforeEach(() => { |
| 24 | + renderSrcMock.mockClear(); |
| 25 | + }); |
| 26 | + |
| 27 | + test('renders LESS correctly with prefix and tag name options', async () => { |
| 28 | + expect( |
| 29 | + await lessGen.generate(mockOptions, Buffer.from('')) |
| 30 | + ).toMatchSnapshot(); |
| 31 | + }); |
| 32 | + |
| 33 | + test('renders LESS correctly with `selector` option', async () => { |
| 34 | + expect( |
| 35 | + await lessGen.generate( |
| 36 | + { ...mockOptions, selector: '.my-selector' }, |
| 37 | + Buffer.from('') |
| 38 | + ) |
| 39 | + ).toMatchSnapshot(); |
| 40 | + }); |
| 41 | + |
| 42 | + test('calls renderSrcAttribute correctly and includes its return value in the rendered template', async () => { |
| 43 | + const fontBuffer = Buffer.from('::svg-content::'); |
| 44 | + |
| 45 | + const result = await lessGen.generate(mockOptions, fontBuffer); |
| 46 | + |
| 47 | + expect(renderSrcMock).toHaveBeenCalledTimes(1); |
| 48 | + expect(renderSrcMock).toHaveBeenCalledWith(mockOptions, fontBuffer); |
| 49 | + expect(result).toContain('::src-attr::'); |
| 50 | + }); |
| 51 | + |
| 52 | + test('renders expected selector blocks', async () => { |
| 53 | + const less = await lessGen.generate(mockOptions, Buffer.from('')); |
| 54 | + |
| 55 | + expect(less).toContain( |
| 56 | + 'b[class^="tf-"]::before, b[class*=" tf-"]::before {' |
| 57 | + ); |
| 58 | + expect(less).toContain('.tf-my-icon::before {'); |
| 59 | + }); |
| 60 | + |
| 61 | + test('renders expected variables', async () => { |
| 62 | + const less = await lessGen.generate(mockOptions, Buffer.from('')); |
| 63 | + |
| 64 | + expect(less).toContain('@test-font:'); |
| 65 | + expect(less).toContain('@test-map:'); |
| 66 | + }); |
| 67 | + |
| 68 | + test('renders expected selector blocks with `selector` option', async () => { |
| 69 | + const less = await lessGen.generate( |
| 70 | + { ...mockOptions, selector: '.my-selector' }, |
| 71 | + Buffer.from('') |
| 72 | + ); |
| 73 | + |
| 74 | + expect(less).toContain('.my-selector::before {'); |
| 75 | + expect(less).toContain('.my-selector.tf-my-icon::before {'); |
| 76 | + }); |
| 77 | +}); |
0 commit comments