|
| 1 | +import { NextConfig } from "@opennextjs/aws/adapters/config/index.js"; |
| 2 | +import { localizePath } from "@opennextjs/aws/core/routing/i18n/index.js"; |
| 3 | +import { convertFromQueryString } from "@opennextjs/aws/core/routing/util.js"; |
| 4 | +import type { InternalEvent } from "@opennextjs/aws/types/open-next.js"; |
| 5 | +import { vi } from "vitest"; |
| 6 | + |
| 7 | +vi.mock("@opennextjs/aws/adapters/config/index.js", () => { |
| 8 | + return { |
| 9 | + NextConfig: { |
| 10 | + i18n: { |
| 11 | + defaultLocale: "en", |
| 12 | + locales: ["en", "fr"], |
| 13 | + }, |
| 14 | + }, |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +vi.mock("@opennextjs/aws/core/routing/i18n/accept-header.js", () => ({ |
| 19 | + acceptLanguage: (header: string, _?: string[]) => (header ? "fr" : undefined), |
| 20 | +})); |
| 21 | + |
| 22 | +type PartialEvent = Partial< |
| 23 | + Omit<InternalEvent, "body" | "rawPath" | "query"> |
| 24 | +> & { body?: string }; |
| 25 | + |
| 26 | +function createEvent(event: PartialEvent): InternalEvent { |
| 27 | + const [rawPath, qs] = (event.url ?? "/").split("?", 2); |
| 28 | + return { |
| 29 | + type: "core", |
| 30 | + method: event.method ?? "GET", |
| 31 | + rawPath, |
| 32 | + url: event.url ?? "/", |
| 33 | + body: Buffer.from(event.body ?? ""), |
| 34 | + headers: event.headers ?? {}, |
| 35 | + query: convertFromQueryString(qs ?? ""), |
| 36 | + cookies: event.cookies ?? {}, |
| 37 | + remoteAddress: event.remoteAddress ?? "::1", |
| 38 | + }; |
| 39 | +} |
| 40 | + |
| 41 | +beforeEach(() => { |
| 42 | + vi.resetAllMocks(); |
| 43 | +}); |
| 44 | + |
| 45 | +describe("localizePath", () => { |
| 46 | + it("should return raw path if no i18n config is set", () => { |
| 47 | + const i18nSpy = vi |
| 48 | + .spyOn(NextConfig, "i18n", "get") |
| 49 | + .mockReturnValue(undefined); |
| 50 | + |
| 51 | + const event = createEvent({ |
| 52 | + url: "/foo", |
| 53 | + }); |
| 54 | + |
| 55 | + const result = localizePath(event); |
| 56 | + |
| 57 | + expect(result).toEqual("/foo"); |
| 58 | + |
| 59 | + i18nSpy.mockRestore(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("should return default locale localized if localeDetection is set to false", () => { |
| 63 | + const i18nSpy = vi.spyOn(NextConfig, "i18n", "get").mockReturnValue({ |
| 64 | + defaultLocale: "en", |
| 65 | + locales: ["en", "fr"], |
| 66 | + localeDetection: false, |
| 67 | + }); |
| 68 | + |
| 69 | + const event = createEvent({ |
| 70 | + url: "/foo", |
| 71 | + headers: { |
| 72 | + "accept-language": "fr", |
| 73 | + }, |
| 74 | + cookies: { |
| 75 | + NEXT_LOCALE: "fr", |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + const result = localizePath(event); |
| 80 | + |
| 81 | + expect(result).toEqual("/en/foo"); |
| 82 | + |
| 83 | + i18nSpy.mockRestore(); |
| 84 | + }); |
| 85 | + |
| 86 | + it("should return the same path if the path is already localized", () => { |
| 87 | + const event = createEvent({ |
| 88 | + url: "/fr/foo", |
| 89 | + }); |
| 90 | + |
| 91 | + const result = localizePath(event); |
| 92 | + |
| 93 | + expect(result).toEqual("/fr/foo"); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should get locale from cookies if NEXT_LOCALE cookie is set to a valid locale", () => { |
| 97 | + const event = createEvent({ |
| 98 | + url: "/foo", |
| 99 | + cookies: { |
| 100 | + NEXT_LOCALE: "fr", |
| 101 | + }, |
| 102 | + }); |
| 103 | + |
| 104 | + const result = localizePath(event); |
| 105 | + |
| 106 | + expect(result).toEqual("/fr/foo"); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should fallback on default locale if NEXT_LOCALE cookie is set to an invalid locale", () => { |
| 110 | + const event = createEvent({ |
| 111 | + url: "/foo", |
| 112 | + cookies: { |
| 113 | + NEXT_LOCALE: "pt", |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + const result = localizePath(event); |
| 118 | + |
| 119 | + expect(result).toEqual("/en/foo"); |
| 120 | + }); |
| 121 | + |
| 122 | + it("should use accept-language header if no cookie are present", () => { |
| 123 | + const event = createEvent({ |
| 124 | + url: "/foo", |
| 125 | + headers: { |
| 126 | + "accept-language": "fr", |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + const result = localizePath(event); |
| 131 | + |
| 132 | + expect(result).toEqual("/fr/foo"); |
| 133 | + }); |
| 134 | + |
| 135 | + it("should fallback to default locale if no cookie or header are set", () => { |
| 136 | + const event = createEvent({ |
| 137 | + url: "/foo", |
| 138 | + }); |
| 139 | + |
| 140 | + const result = localizePath(event); |
| 141 | + |
| 142 | + expect(result).toEqual("/en/foo"); |
| 143 | + }); |
| 144 | +}); |
0 commit comments