|
| 1 | +import { Translator, getAvailableLanguages, LanguageCode } from "./language"; |
| 2 | + |
| 3 | +// Store original navigator |
| 4 | +const originalNavigator = global.navigator; |
| 5 | + |
| 6 | +// Mock navigator object |
| 7 | +const createMockNavigator = (language?: string, userLanguage?: string) => { |
| 8 | + const mockNavigator: any = {}; |
| 9 | + |
| 10 | + if (language !== undefined) mockNavigator.language = language; |
| 11 | + if (userLanguage !== undefined) mockNavigator.userLanguage = userLanguage; |
| 12 | + |
| 13 | + if (language === undefined && userLanguage === undefined) { |
| 14 | + mockNavigator.language = "en-US"; |
| 15 | + } |
| 16 | + |
| 17 | + Object.defineProperty(global, "navigator", { |
| 18 | + value: mockNavigator, |
| 19 | + writable: true, |
| 20 | + configurable: true, |
| 21 | + }); |
| 22 | + |
| 23 | + return mockNavigator; |
| 24 | +}; |
| 25 | + |
| 26 | +describe("Translator", () => { |
| 27 | + beforeEach(() => createMockNavigator()); |
| 28 | + afterAll(() => { |
| 29 | + Object.defineProperty(global, "navigator", { |
| 30 | + value: originalNavigator, |
| 31 | + writable: true, |
| 32 | + configurable: true, |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe("Constructor", () => { |
| 37 | + it("should use provided language code", () => { |
| 38 | + const translator = new Translator("es_ES"); |
| 39 | + expect(translator.translate("buttons.next")).toBe("Siguiente"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should detect browser language and match locale", () => { |
| 43 | + createMockNavigator("fr-FR"); |
| 44 | + const translator = new Translator(); |
| 45 | + expect(translator.translate("buttons.next")).toBe("Suivant"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should fallback to en_US for unsupported languages", () => { |
| 49 | + createMockNavigator("zh-CN"); |
| 50 | + const translator = new Translator(); |
| 51 | + expect(translator.translate("buttons.next")).toBe("Next"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should handle underscores and dashes in language codes", () => { |
| 55 | + createMockNavigator("es_ES"); |
| 56 | + const translator1 = new Translator(); |
| 57 | + expect(translator1.translate("buttons.next")).toBe("Siguiente"); |
| 58 | + |
| 59 | + createMockNavigator("fr-FR"); |
| 60 | + const translator2 = new Translator(); |
| 61 | + expect(translator2.translate("buttons.next")).toBe("Suivant"); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe("setLanguage", () => { |
| 66 | + it("should change active language correctly", () => { |
| 67 | + const translator = new Translator("en_US"); |
| 68 | + translator.setLanguage("fr_FR"); |
| 69 | + expect(translator.translate("buttons.done")).toBe("Terminé"); |
| 70 | + |
| 71 | + translator.setLanguage("fa_IR"); |
| 72 | + expect(translator.translate("buttons.done")).toBe("پایان"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should work with all supported languages", () => { |
| 76 | + const translator = new Translator(); |
| 77 | + (["en_US", "es_ES", "fr_FR", "de_DE", "fa_IR"] as LanguageCode[]).forEach( |
| 78 | + (code) => { |
| 79 | + translator.setLanguage(code); |
| 80 | + expect(typeof translator.translate("buttons.next")).toBe("string"); |
| 81 | + } |
| 82 | + ); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe("translate", () => { |
| 87 | + let translator: Translator; |
| 88 | + |
| 89 | + beforeEach(() => (translator = new Translator("en_US"))); |
| 90 | + |
| 91 | + it("should translate standard keys", () => { |
| 92 | + expect(translator.translate("buttons.next")).toBe("Next"); |
| 93 | + expect(translator.translate("buttons.prev")).toBe("Back"); |
| 94 | + expect(translator.translate("buttons.done")).toBe("Done"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("should return key if not found", () => { |
| 98 | + expect(translator.translate("nonexistent.key")).toBe("nonexistent.key"); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe("getAvailableLanguages", () => { |
| 103 | + it("should return all language codes", () => { |
| 104 | + const codes = getAvailableLanguages(); |
| 105 | + expect(codes).toEqual( |
| 106 | + expect.arrayContaining(["en_US", "es_ES", "fr_FR", "de_DE", "fa_IR"]) |
| 107 | + ); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe("Integration with Tour options", () => { |
| 112 | + class MockTour { |
| 113 | + private _options: any = {}; |
| 114 | + setOptions(options: any) { |
| 115 | + const processed = { ...options }; |
| 116 | + if (processed.language) { |
| 117 | + const translator = new Translator(processed.language); |
| 118 | + processed.nextLabel = |
| 119 | + processed.nextLabel ?? translator.translate("buttons.next"); |
| 120 | + processed.prevLabel = |
| 121 | + processed.prevLabel ?? translator.translate("buttons.prev"); |
| 122 | + processed.doneLabel = |
| 123 | + processed.doneLabel ?? translator.translate("buttons.done"); |
| 124 | + } |
| 125 | + this._options = { ...this._options, ...processed }; |
| 126 | + } |
| 127 | + getOption(key: string) { |
| 128 | + return this._options[key]; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + it("should translate labels based on language code", () => { |
| 133 | + const tour = new MockTour(); |
| 134 | + tour.setOptions({ language: "es_ES" }); |
| 135 | + expect(tour.getOption("nextLabel")).toBe("Siguiente"); |
| 136 | + expect(tour.getOption("prevLabel")).toBe("Atrás"); |
| 137 | + expect(tour.getOption("doneLabel")).toBe("Hecho"); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should not override custom labels", () => { |
| 141 | + const tour = new MockTour(); |
| 142 | + tour.setOptions({ language: "fr_FR", nextLabel: "Custom Next" }); |
| 143 | + expect(tour.getOption("nextLabel")).toBe("Custom Next"); |
| 144 | + expect(tour.getOption("doneLabel")).toBe("Terminé"); |
| 145 | + }); |
| 146 | + |
| 147 | + it("should handle multiple languages sequentially", () => { |
| 148 | + const tour = new MockTour(); |
| 149 | + tour.setOptions({ language: "de_DE" }); |
| 150 | + expect(tour.getOption("nextLabel")).toBe("Weiter"); |
| 151 | + |
| 152 | + tour.setOptions({ language: "fa_IR" }); |
| 153 | + expect(tour.getOption("nextLabel")).toBe("بعدی"); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments