|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { glob } from "glob"; |
| 3 | +import findLocaleFiles from "./find-locale-paths"; |
| 4 | + |
| 5 | +vi.mock("glob", () => ({ |
| 6 | + glob: { |
| 7 | + sync: vi.fn(), |
| 8 | + }, |
| 9 | +})); |
| 10 | + |
| 11 | +describe("findLocaleFiles", () => { |
| 12 | + beforeEach(() => { |
| 13 | + vi.clearAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should find json locale files", () => { |
| 17 | + vi.mocked(glob.sync).mockReturnValue([ |
| 18 | + // valid locales |
| 19 | + "src/i18n/en.json", |
| 20 | + "src/i18n/fr.json", |
| 21 | + "src/i18n/en-US.json", |
| 22 | + "src/translations/es.json", |
| 23 | + |
| 24 | + // not a valid locale |
| 25 | + "src/xx.json", |
| 26 | + "src/settings.json", |
| 27 | + ]); |
| 28 | + |
| 29 | + const result = findLocaleFiles("json"); |
| 30 | + |
| 31 | + expect(result).toEqual({ |
| 32 | + found: true, |
| 33 | + patterns: ["src/i18n/[locale].json", "src/translations/[locale].json"], |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should find yaml locale files", () => { |
| 38 | + vi.mocked(glob.sync).mockReturnValue(["locales/en.yml", "locales/fr.yml", "translations/es.yml"]); |
| 39 | + |
| 40 | + const result = findLocaleFiles("yaml"); |
| 41 | + |
| 42 | + expect(result).toEqual({ |
| 43 | + found: true, |
| 44 | + patterns: ["locales/[locale].yml", "translations/[locale].yml"], |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should find flutter arb locale files", () => { |
| 49 | + vi.mocked(glob.sync).mockReturnValue(["lib/l10n/en.arb", "lib/l10n/es.arb", "lib/translations/fr.arb"]); |
| 50 | + |
| 51 | + const result = findLocaleFiles("flutter"); |
| 52 | + |
| 53 | + expect(result).toEqual({ |
| 54 | + found: true, |
| 55 | + patterns: ["lib/l10n/[locale].arb", "lib/translations/[locale].arb"], |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should find locale files in nested directories", () => { |
| 60 | + vi.mocked(glob.sync).mockReturnValue([ |
| 61 | + // valid locales |
| 62 | + "src/locales/en/messages.json", |
| 63 | + "src/locales/fr/messages.json", |
| 64 | + "src/i18n/es/strings.json", |
| 65 | + "src/translations/es.json", |
| 66 | + |
| 67 | + // not a valid locale |
| 68 | + "src/xx/settings.json", |
| 69 | + "src/xx.json", |
| 70 | + ]); |
| 71 | + |
| 72 | + const result = findLocaleFiles("json"); |
| 73 | + |
| 74 | + expect(result).toEqual({ |
| 75 | + found: true, |
| 76 | + patterns: [ |
| 77 | + "src/locales/[locale]/messages.json", |
| 78 | + "src/i18n/[locale]/strings.json", |
| 79 | + "src/translations/[locale].json", |
| 80 | + ], |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should return default pattern when no files found", () => { |
| 85 | + vi.mocked(glob.sync).mockReturnValue([]); |
| 86 | + |
| 87 | + const result = findLocaleFiles("json"); |
| 88 | + |
| 89 | + expect(result).toEqual({ |
| 90 | + found: false, |
| 91 | + patterns: ["i18n/[locale].json"], |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should find xcode-xcstrings locale files", () => { |
| 96 | + vi.mocked(glob.sync).mockReturnValue([ |
| 97 | + "ios/MyApp/Localizable.xcstrings", |
| 98 | + "ios/MyApp/Onboarding/Localizable.xcstrings", |
| 99 | + "ios/MyApp/Onboarding/fr.xcstrings", |
| 100 | + ]); |
| 101 | + |
| 102 | + const result = findLocaleFiles("xcode-xcstrings"); |
| 103 | + |
| 104 | + expect(result).toEqual({ |
| 105 | + found: true, |
| 106 | + patterns: ["ios/MyApp/Localizable.xcstrings", "ios/MyApp/Onboarding/Localizable.xcstrings"], |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + it("should return default pattern for xcode-xcstrings when no files found", () => { |
| 111 | + vi.mocked(glob.sync).mockReturnValue([]); |
| 112 | + |
| 113 | + const result = findLocaleFiles("xcode-xcstrings"); |
| 114 | + |
| 115 | + expect(result).toEqual({ |
| 116 | + found: false, |
| 117 | + patterns: ["Localizable.xcstrings"], |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should throw error for unsupported bucket type", () => { |
| 122 | + expect(() => findLocaleFiles("invalid")).toThrow("Unsupported bucket type: invalid"); |
| 123 | + }); |
| 124 | +}); |
0 commit comments