|
| 1 | +import { describe, it, expect, vi } from "vitest"; |
| 2 | +import { getDictionary } from "./get-dictionary"; |
| 3 | + |
| 4 | +describe("get-dictionary", () => { |
| 5 | + const mockLoaderEn = vi.fn().mockResolvedValue( |
| 6 | + Promise.resolve({ |
| 7 | + default: { hello: "Hello", goodbye: "Goodbye" }, |
| 8 | + otherExport: "ignored", |
| 9 | + }), |
| 10 | + ); |
| 11 | + const mockLoaderEs = vi.fn().mockResolvedValue( |
| 12 | + Promise.resolve({ |
| 13 | + default: { hello: "Hola", goodbye: "Adiós" }, |
| 14 | + }), |
| 15 | + ); |
| 16 | + const loaders = { |
| 17 | + en: mockLoaderEn, |
| 18 | + es: mockLoaderEs, |
| 19 | + }; |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + vi.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe("getDictionary", () => { |
| 26 | + it("should load dictionary for specific locale using correct async loader", async () => { |
| 27 | + const result = await getDictionary("es", loaders); |
| 28 | + expect(mockLoaderEs).toHaveBeenCalledTimes(1); |
| 29 | + expect(result).toEqual({ hello: "Hola", goodbye: "Adiós" }); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should fallback to first available loader when specific locale not found", async () => { |
| 33 | + const result = await getDictionary("fr", loaders); |
| 34 | + |
| 35 | + expect(mockLoaderEn).toHaveBeenCalledTimes(1); |
| 36 | + expect(result).toEqual({ hello: "Hello", goodbye: "Goodbye" }); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should throw error when no loaders are provided", async () => { |
| 40 | + expect(() => getDictionary("en", {})).toThrow( |
| 41 | + "No available dictionary loaders found", |
| 42 | + ); |
| 43 | + expect(() => getDictionary("en")).toThrow( |
| 44 | + "No available dictionary loaders found", |
| 45 | + ); |
| 46 | + }); |
| 47 | + }); |
| 48 | +}); |
0 commit comments