|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2015 - present Instructure, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +import { describe, it, expect, afterEach, vi } from 'vitest' |
| 26 | +import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'fs' |
| 27 | +import { tmpdir } from 'os' |
| 28 | +import { join } from 'path' |
| 29 | +import generateLegacyIconsData from '../icons/generate-legacy-icons-data.ts' |
| 30 | + |
| 31 | +describe('generateLegacyIconsData', () => { |
| 32 | + let dir: string |
| 33 | + |
| 34 | + afterEach(() => { |
| 35 | + if (dir) rmSync(dir, { recursive: true, force: true }) |
| 36 | + vi.restoreAllMocks() |
| 37 | + }) |
| 38 | + |
| 39 | + function setupFixtures() { |
| 40 | + dir = mkdtempSync(join(tmpdir(), 'gen-legacy-')) + '/' |
| 41 | + // The function derives its source dir from process.cwd() + '/svg/'. |
| 42 | + // Build that structure in our temp dir and redirect cwd to it. |
| 43 | + mkdirSync(dir + 'svg/Solid', { recursive: true }) |
| 44 | + mkdirSync(dir + 'svg/Line', { recursive: true }) |
| 45 | + writeFileSync( |
| 46 | + dir + 'svg/Solid/check.svg', |
| 47 | + '<svg><path d="solid-check"/></svg>' |
| 48 | + ) |
| 49 | + writeFileSync( |
| 50 | + dir + 'svg/Line/check.svg', |
| 51 | + '<svg><path d="line-check"/></svg>' |
| 52 | + ) |
| 53 | + writeFileSync(dir + 'svg/Solid/add.svg', '<svg><path d="solid-add"/></svg>') |
| 54 | + writeFileSync(dir + 'svg/Line/add.svg', '<svg><path d="line-add"/></svg>') |
| 55 | + vi.spyOn(process, 'cwd').mockReturnValue(dir.slice(0, -1)) |
| 56 | + } |
| 57 | + |
| 58 | + it('returns one merged entry per glyphName, not one per variant', () => { |
| 59 | + setupFixtures() |
| 60 | + const result = generateLegacyIconsData() |
| 61 | + expect(result).toHaveLength(2) |
| 62 | + }) |
| 63 | + |
| 64 | + it('merges Line and Solid variants into lineSrc and solidSrc', () => { |
| 65 | + setupFixtures() |
| 66 | + const result = generateLegacyIconsData() as Array<{ |
| 67 | + name: string |
| 68 | + glyphName: string |
| 69 | + bidirectional: boolean |
| 70 | + lineSrc?: string |
| 71 | + solidSrc?: string |
| 72 | + }> |
| 73 | + |
| 74 | + const check = result.find((g) => g.glyphName === 'check') |
| 75 | + expect(check).toBeDefined() |
| 76 | + expect(check?.name).toEqual('IconCheck') |
| 77 | + expect(check?.lineSrc).toEqual('<svg><path d="line-check"/></svg>') |
| 78 | + expect(check?.solidSrc).toEqual('<svg><path d="solid-check"/></svg>') |
| 79 | + }) |
| 80 | + |
| 81 | + it('keeps bidirectional false (the function hardcodes an empty list)', () => { |
| 82 | + setupFixtures() |
| 83 | + const result = generateLegacyIconsData() as Array<{ |
| 84 | + bidirectional: boolean |
| 85 | + }> |
| 86 | + for (const entry of result) { |
| 87 | + expect(entry.bidirectional).toBe(false) |
| 88 | + } |
| 89 | + }) |
| 90 | +}) |
0 commit comments